Skip to content

Commit

Permalink
Port XT PR BitcoinUnlimited#403: [RPC][Bitcoin-TX] Add support for se…
Browse files Browse the repository at this point in the history
…quence number

bitcoin/bitcoin#7957 - [RPC][Bitcoin-TX] Add support for sequence number
bitcoin/bitcoin#8164 - [Bitcoin-Tx] fix missing test fixtures, fix 32bit atoi issue
bitcoin/bitcoin#8171 - [RPC] Fix createrawtx sequence number unsigned int parsing

[RPC][Bitcoin-TX] Add support for sequence number
  • Loading branch information
dgenr8 authored and sickpig committed May 4, 2018
1 parent ff1de38 commit 6c5d05a
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 6 deletions.
21 changes: 21 additions & 0 deletions qa/rpc-tests/rawtransactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,27 @@ def run_test(self):
txid = self.nodes[0].sendrawtransaction(signedtxn["hex"], False, "STANDARD")
assert(len(txid) == 64)

inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 1000}]
outputs = { self.nodes[0].getnewaddress() : 1 }
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
decrawtx= self.nodes[0].decoderawtransaction(rawtx)
assert_equal(decrawtx['vin'][0]['sequence'], 1000)

inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : -1}]
outputs = { self.nodes[0].getnewaddress() : 1 }
assert_raises(JSONRPCException, self.nodes[0].createrawtransaction, inputs, outputs)

inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 4294967296}]
outputs = { self.nodes[0].getnewaddress() : 1 }
assert_raises(JSONRPCException, self.nodes[0].createrawtransaction, inputs, outputs)

inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 4294967294}]
outputs = { self.nodes[0].getnewaddress() : 1 }
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
decrawtx= self.nodes[0].decoderawtransaction(rawtx)
assert_equal(decrawtx['vin'][0]['sequence'], 4294967294)


if __name__ == '__main__':
RawTransactionsTest().main()

Expand Down
4 changes: 3 additions & 1 deletion src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ EXTRA_DIST += \
test/data/txcreate2.hex \
test/data/txcreatedata1.hex \
test/data/txcreatedata2.hex \
test/data/txcreatesign.hex
test/data/txcreatesign.hex \
test/data/txcreatedata_seq0.hex \
test/data/txcreatedata_seq1.hex

JSON_TEST_FILES = \
test/data/script_tests.json \
Expand Down
17 changes: 12 additions & 5 deletions src/bitcoin-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,15 @@ static void MutateTxLocktime(CMutableTransaction &tx, const string &cmdVal)

static void MutateTxAddInput(CMutableTransaction &tx, const string &strInput)
{
std::vector<std::string> vStrInputParts;
boost::split(vStrInputParts, strInput, boost::is_any_of(":"));

// separate TXID:VOUT in string
size_t pos = strInput.find(':');
if ((pos == string::npos) || (pos == 0) || (pos == (strInput.size() - 1)))
if (vStrInputParts.size() < 2)
throw runtime_error("TX input missing separator");

// extract and validate TXID
string strTxid = strInput.substr(0, pos);
string strTxid = vStrInputParts[0];
if ((strTxid.size() != 64) || !IsHex(strTxid))
throw runtime_error("invalid TX input txid");
uint256 txid(uint256S(strTxid));
Expand All @@ -237,15 +239,20 @@ static void MutateTxAddInput(CMutableTransaction &tx, const string &strInput)
static const unsigned int maxVout = BLOCKSTREAM_CORE_MAX_BLOCK_SIZE / minTxOutSz;

// extract and validate vout
string strVout = strInput.substr(pos + 1, string::npos);
string strVout = vStrInputParts[1];
int vout = atoi(strVout);
// BU: be strict about what is generated. TODO: BLOCKSTREAM_CORE_MAX_BLOCK_SIZE should be converted to a cmd line
// parameter
if ((vout < 0) || (vout > (int)maxVout))
throw runtime_error("invalid TX input vout");

// extract the optional sequence number
uint32_t nSequenceIn = std::numeric_limits<unsigned int>::max();
if (vStrInputParts.size() > 2)
nSequenceIn = std::stoul(vStrInputParts[2]);

// append to transaction input list
CTxIn txin(txid, vout);
CTxIn txin(txid, vout, CScript(), nSequenceIn);
tx.vin.push_back(txin);
}

Expand Down
14 changes: 14 additions & 0 deletions src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ UniValue createrawtransaction(const UniValue &params, bool fHelp)
" {\n"
" \"txid\":\"id\", (string, required) The transaction id\n"
" \"vout\":n (numeric, required) The output number\n"
" \"vout\":n, (numeric, required) The output number\n"
" \"sequence\":n (numeric, optional) The sequence number\n"
" }\n"
" ,...\n"
" ]\n"
Expand Down Expand Up @@ -415,6 +417,18 @@ UniValue createrawtransaction(const UniValue &params, bool fHelp)

uint32_t nSequence =
(rawTx.nLockTime ? std::numeric_limits<uint32_t>::max() - 1 : std::numeric_limits<uint32_t>::max());

// set the sequence number if passed in the parameters object
const UniValue &sequenceObj = find_value(o, "sequence");
if (sequenceObj.isNum())
{
int64_t seqNr64 = sequenceObj.get_int64();
if (seqNr64 < 0 || seqNr64 > std::numeric_limits<uint32_t>::max())
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, sequence number is out of range");
else
nSequence = (uint32_t)seqNr64;
}

CTxIn in(COutPoint(txid, nOutput), CScript(), nSequence);

rawTx.vin.push_back(in);
Expand Down
13 changes: 13 additions & 0 deletions src/test/data/bitcoin-util-test.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,18 @@
"outaddr=0.18:13tuJJDR2RgArmgfv6JScSdreahzgc4T6o",
"outdata=54686973204f505f52455455524e207472616e73616374696f6e206f7574707574207761732063726561746564206279206d6f646966696564206372656174657261777472616e73616374696f6e2e"],
"output_cmp": "txcreatedata2.hex"
},
{ "exec": "./bitcoin-tx",
"args":
["-create",
"in=5897de6bd6027a475eadd57019d4e6872c396d0716c4875a5f1a6fcfdf385c1f:0:4294967293",
"outaddr=0.18:13tuJJDR2RgArmgfv6JScSdreahzgc4T6o"],
"output_cmp": "txcreatedata_seq0.hex"
},
{ "exec": "./bitcoin-tx",
"args":
["01000000011f5c38dfcf6f1a5f5a87c416076d392c87e6d41970d5ad5e477a02d66bde97580000000000fdffffff0180a81201000000001976a9141fc11f39be1729bf973a7ab6a615ca4729d6457488ac00000000",
"in=5897de6bd6027a475eadd57019d4e6872c396d0716c4875a5f1a6fcfdf385c1f:0:1"],
"output_cmp": "txcreatedata_seq1.hex"
}
]
1 change: 1 addition & 0 deletions src/test/data/txcreatedata_seq0.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
01000000011f5c38dfcf6f1a5f5a87c416076d392c87e6d41970d5ad5e477a02d66bde97580000000000fdffffff0180a81201000000001976a9141fc11f39be1729bf973a7ab6a615ca4729d6457488ac00000000
1 change: 1 addition & 0 deletions src/test/data/txcreatedata_seq1.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
01000000021f5c38dfcf6f1a5f5a87c416076d392c87e6d41970d5ad5e477a02d66bde97580000000000fdffffff1f5c38dfcf6f1a5f5a87c416076d392c87e6d41970d5ad5e477a02d66bde97580000000000010000000180a81201000000001976a9141fc11f39be1729bf973a7ab6a615ca4729d6457488ac00000000

0 comments on commit 6c5d05a

Please sign in to comment.