Skip to content

Commit 79004d4

Browse files
committed
Merge #7957: [RPC][Bitcoin-TX] Add support for sequence number
ae357d5 [Bitcoin-Tx] Add tests for sequence number support (Jonas Schnelli) e59336f [bitcoin-tx] allow to set nSequence number over the in= command (Jonas Schnelli) a946bb6 [RPC] createrawtransaction: add option to set the sequence number per input (Jonas Schnelli)
2 parents 22e0b35 + ae357d5 commit 79004d4

File tree

6 files changed

+41
-8
lines changed

6 files changed

+41
-8
lines changed

qa/rpc-tests/rawtransactions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,11 @@ def run_test(self):
138138
self.sync_all()
139139
assert_equal(self.nodes[0].getbalance(), bal+Decimal('50.00000000')+Decimal('2.19000000')) #block reward + tx
140140

141+
inputs = [ {'txid' : "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000", 'vout' : 1, 'sequence' : 1000}]
142+
outputs = { self.nodes[0].getnewaddress() : 1 }
143+
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
144+
decrawtx= self.nodes[0].decoderawtransaction(rawtx)
145+
assert_equal(decrawtx['vin'][0]['sequence'], 1000)
146+
141147
if __name__ == '__main__':
142148
RawTransactionsTest().main()

src/bitcoin-tx.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static bool AppInitRawTx(int argc, char* argv[])
7171
strUsage = HelpMessageGroup(_("Commands:"));
7272
strUsage += HelpMessageOpt("delin=N", _("Delete input N from TX"));
7373
strUsage += HelpMessageOpt("delout=N", _("Delete output N from TX"));
74-
strUsage += HelpMessageOpt("in=TXID:VOUT", _("Add input to TX"));
74+
strUsage += HelpMessageOpt("in=TXID:VOUT(:SEQUENCE_NUMBER)", _("Add input to TX"));
7575
strUsage += HelpMessageOpt("locktime=N", _("Set TX lock time to N"));
7676
strUsage += HelpMessageOpt("nversion=N", _("Set TX version to N"));
7777
strUsage += HelpMessageOpt("outaddr=VALUE:ADDRESS", _("Add address-based output to TX"));
@@ -181,15 +181,15 @@ static void MutateTxLocktime(CMutableTransaction& tx, const string& cmdVal)
181181

182182
static void MutateTxAddInput(CMutableTransaction& tx, const string& strInput)
183183
{
184+
std::vector<std::string> vStrInputParts;
185+
boost::split(vStrInputParts, strInput, boost::is_any_of(":"));
186+
184187
// separate TXID:VOUT in string
185-
size_t pos = strInput.find(':');
186-
if ((pos == string::npos) ||
187-
(pos == 0) ||
188-
(pos == (strInput.size() - 1)))
188+
if (vStrInputParts.size()<2)
189189
throw runtime_error("TX input missing separator");
190190

191191
// extract and validate TXID
192-
string strTxid = strInput.substr(0, pos);
192+
string strTxid = vStrInputParts[0];
193193
if ((strTxid.size() != 64) || !IsHex(strTxid))
194194
throw runtime_error("invalid TX input txid");
195195
uint256 txid(uint256S(strTxid));
@@ -198,13 +198,18 @@ static void MutateTxAddInput(CMutableTransaction& tx, const string& strInput)
198198
static const unsigned int maxVout = MAX_BLOCK_SIZE / minTxOutSz;
199199

200200
// extract and validate vout
201-
string strVout = strInput.substr(pos + 1, string::npos);
201+
string strVout = vStrInputParts[1];
202202
int vout = atoi(strVout);
203203
if ((vout < 0) || (vout > (int)maxVout))
204204
throw runtime_error("invalid TX input vout");
205205

206+
// extract the optional sequence number
207+
uint32_t nSequenceIn=std::numeric_limits<unsigned int>::max();
208+
if (vStrInputParts.size() > 2)
209+
nSequenceIn = atoi(vStrInputParts[2]);
210+
206211
// append to transaction input list
207-
CTxIn txin(txid, vout);
212+
CTxIn txin(txid, vout, CScript(), nSequenceIn);
208213
tx.vin.push_back(txin);
209214
}
210215

src/rpc/rawtransaction.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ UniValue createrawtransaction(const UniValue& params, bool fHelp)
334334
" {\n"
335335
" \"txid\":\"id\", (string, required) The transaction id\n"
336336
" \"vout\":n (numeric, required) The output number\n"
337+
" \"sequence\":n (numeric, optional) The sequence number\n"
337338
" }\n"
338339
" ,...\n"
339340
" ]\n"
@@ -384,6 +385,12 @@ UniValue createrawtransaction(const UniValue& params, bool fHelp)
384385
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout must be positive");
385386

386387
uint32_t nSequence = (rawTx.nLockTime ? std::numeric_limits<uint32_t>::max() - 1 : std::numeric_limits<uint32_t>::max());
388+
389+
// set the sequence number if passed in the parameters object
390+
const UniValue& sequenceObj = find_value(o, "sequence");
391+
if (sequenceObj.isNum())
392+
nSequence = sequenceObj.get_int();
393+
387394
CTxIn in(COutPoint(txid, nOutput), CScript(), nSequence);
388395

389396
rawTx.vin.push_back(in);

src/test/data/bitcoin-util-test.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,18 @@
8686
"outaddr=0.18:13tuJJDR2RgArmgfv6JScSdreahzgc4T6o",
8787
"outdata=54686973204f505f52455455524e207472616e73616374696f6e206f7574707574207761732063726561746564206279206d6f646966696564206372656174657261777472616e73616374696f6e2e"],
8888
"output_cmp": "txcreatedata2.hex"
89+
},
90+
{ "exec": "./bitcoin-tx",
91+
"args":
92+
["-create",
93+
"in=5897de6bd6027a475eadd57019d4e6872c396d0716c4875a5f1a6fcfdf385c1f:0:4294967293",
94+
"outaddr=0.18:13tuJJDR2RgArmgfv6JScSdreahzgc4T6o"],
95+
"output_cmp": "txcreatedata_seq0.hex"
96+
},
97+
{ "exec": "./bitcoin-tx",
98+
"args":
99+
["01000000011f5c38dfcf6f1a5f5a87c416076d392c87e6d41970d5ad5e477a02d66bde97580000000000fdffffff0180a81201000000001976a9141fc11f39be1729bf973a7ab6a615ca4729d6457488ac00000000",
100+
"in=5897de6bd6027a475eadd57019d4e6872c396d0716c4875a5f1a6fcfdf385c1f:0:1"],
101+
"output_cmp": "txcreatedata_seq1.hex"
89102
}
90103
]

src/test/data/txcreatedata_seq0.hex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
01000000011f5c38dfcf6f1a5f5a87c416076d392c87e6d41970d5ad5e477a02d66bde97580000000000fdffffff0180a81201000000001976a9141fc11f39be1729bf973a7ab6a615ca4729d6457488ac00000000

src/test/data/txcreatedata_seq1.hex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
01000000021f5c38dfcf6f1a5f5a87c416076d392c87e6d41970d5ad5e477a02d66bde97580000000000fdffffff1f5c38dfcf6f1a5f5a87c416076d392c87e6d41970d5ad5e477a02d66bde97580000000000010000000180a81201000000001976a9141fc11f39be1729bf973a7ab6a615ca4729d6457488ac00000000

0 commit comments

Comments
 (0)