Skip to content

Commit

Permalink
bitcoin-tx: Accept input via stdin. Add input handling to tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Garzik committed Aug 19, 2014
1 parent d789386 commit fb14452
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
28 changes: 26 additions & 2 deletions src/bitcoin-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <stdio.h>
#include <boost/assign/list_of.hpp>
#include <boost/algorithm/string.hpp>

using namespace std;
using namespace boost::assign;
Expand Down Expand Up @@ -501,13 +502,34 @@ static void OutputTx(const CTransaction& tx)
OutputTxHex(tx);
}

static string readStdin()
{
char buf[4096];
string ret;

while (!feof(stdin)) {
size_t bread = fread(buf, 1, sizeof(buf), stdin);
ret.append(buf, bread);
if (bread < sizeof(buf))
break;
}

if (ferror(stdin))
throw runtime_error("error reading stdin");

boost::algorithm::trim_right(ret);

return ret;
}

static int CommandLineRawTx(int argc, char* argv[])
{
string strPrint;
int nRet = 0;
try {
// Skip switches
while (argc > 1 && IsSwitchChar(argv[1][0])) {
// Skip switches; Permit common stdin convention "-"
while (argc > 1 && IsSwitchChar(argv[1][0]) &&
(argv[1][1] != 0)) {
argc--;
argv++;
}
Expand All @@ -522,6 +544,8 @@ static int CommandLineRawTx(int argc, char* argv[])

// param: hex-encoded bitcoin transaction
string strHexTx(argv[1]);
if (strHexTx == "-") // "-" implies standard input
strHexTx = readStdin();

if (!DecodeHexTx(txDecodeTmp, strHexTx))
throw runtime_error("invalid transaction encoding");
Expand Down
12 changes: 10 additions & 2 deletions src/test/bctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@

def bctest(testDir, testObj):
execargs = testObj['exec']

stdinCfg = None
inputData = None
if "input" in testObj:
filename = testDir + "/" + testObj['input']
inputData = open(filename).read()
stdinCfg = subprocess.PIPE

outputFn = testObj['output_cmp']
outputData = open(testDir + "/" + outputFn).read()

proc = subprocess.Popen(execargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proc = subprocess.Popen(execargs, stdin=stdinCfg, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
try:
outs = proc.communicate()
outs = proc.communicate(input=inputData)
except OSError:
print("OSError, Failed to execute " + execargs[0])
sys.exit(1)
Expand Down
4 changes: 4 additions & 0 deletions src/test/data/bitcoin-util-test.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
[
{ "exec": ["./bitcoin-tx", "-create"],
"output_cmp": "blanktx.hex"
},
{ "exec": ["./bitcoin-tx", "-"],
"input": "blanktx.hex",
"output_cmp": "blanktx.hex"
}
]

0 comments on commit fb14452

Please sign in to comment.