Skip to content

Commit 2346aa7

Browse files
committed
feat: implement new rpc getislock
It return InstantSend Lock as JSON or as hex-string depends on verbose=0|1 Hex format is compatible with zmq-subscription
1 parent 1ecb6b5 commit 2346aa7

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

src/rpc/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
116116
{ "gettransaction", 1, "include_watchonly" },
117117
{ "gettransaction", 2, "verbose" },
118118
{ "getrawtransaction", 1, "verbose" },
119+
{ "getislocks", 1, "txids" },
119120
{ "getrawtransactionmulti", 0, "transactions" },
120121
{ "getrawtransactionmulti", 1, "verbose" },
121122
{ "gettxchainlocks", 0, "txids" },

src/rpc/rawtransaction.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ static RPCHelpMan getrawtransactionmulti() {
300300
{"verbose", RPCArg::Type::BOOL, RPCArg::Default{false},
301301
"If false, return a string, otherwise return a json object"},
302302
},
303+
// TODO: replace RPCResults to proper annotation
303304
RPCResults{},
304305
RPCExamples{
305306
HelpExampleCli("getrawtransactionmulti",
@@ -366,6 +367,100 @@ static RPCHelpMan getrawtransactionmulti() {
366367
};
367368
}
368369

370+
static RPCHelpMan getislocks()
371+
{
372+
return RPCHelpMan{"getislocks",
373+
"\nReturns the raw InstantSend lock data for each txids. Returns Null if there is no known IS yet.",
374+
{
375+
{"txids", RPCArg::Type::ARR, RPCArg::Optional::NO, "The transaction ids (no more than 100)",
376+
{
377+
{"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "A transaction hash"},
378+
},
379+
},
380+
{"verbose", RPCArg::Type::NUM, RPCArg::Default{1}, "0 for hex-encoded data, 1 for a json object"},
381+
},
382+
RPCResult{
383+
RPCResult::Type::ARR, "", "Response is an array with the same size as the input txids",
384+
{
385+
RPCResult{"for verbose = 0 if InstantSend Lock is known for specified txid",
386+
RPCResult::Type::STR, "data", "The serialized, hex-encoded data for 'txid'"
387+
},
388+
RPCResult{"for verbose = 1 if InstantSend Lock is known for specified txid",
389+
RPCResult::Type::OBJ, "", "",
390+
{
391+
{RPCResult::Type::STR_HEX, "txid", "The transaction id"},
392+
{RPCResult::Type::ARR, "inputs", "The inputs",
393+
{
394+
{RPCResult::Type::OBJ, "", "",
395+
{
396+
{RPCResult::Type::STR_HEX, "txid", "The transaction id"},
397+
{RPCResult::Type::NUM, "vout", "The output number"},
398+
},
399+
},
400+
}},
401+
{RPCResult::Type::STR_HEX, "cycleHash", "The Cycle Hash"},
402+
{RPCResult::Type::STR_HEX, "signature", "The InstantSend's BLS signature"},
403+
}},
404+
RPCResult{"if no InstantSend Lock is known for specified txid",
405+
RPCResult::Type::STR, "data", "Just 'None' string"
406+
},
407+
}},
408+
RPCExamples{
409+
HelpExampleCli("getislocks", "'[\"txid\",...]'")
410+
+ HelpExampleRpc("getislocks", "'[\"txid\",...]'")
411+
},
412+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
413+
{
414+
const NodeContext& node = EnsureAnyNodeContext(request.context);
415+
416+
UniValue result_arr(UniValue::VARR);
417+
UniValue txids = request.params[0].get_array();
418+
if (txids.size() > 100) {
419+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Up to 100 txids only");
420+
}
421+
422+
int verbose = 1;
423+
if (!request.params[1].isNull()) {
424+
if (request.params[1].isBool()) {
425+
verbose = request.params[1].get_bool() ? 1 : 0;
426+
} else {
427+
verbose = request.params[1].get_int();
428+
}
429+
}
430+
431+
const LLMQContext& llmq_ctx = EnsureLLMQContext(node);
432+
for (const auto idx : irange::range(txids.size())) {
433+
const uint256 txid(ParseHashV(txids[idx], "txid"));
434+
435+
if (const llmq::CInstantSendLockPtr islock = llmq_ctx.isman->GetInstantSendLockByTxid(txid); islock != nullptr) {
436+
if (verbose == 1) {
437+
UniValue objIS(UniValue::VOBJ);
438+
objIS.pushKV("txid", islock->txid.ToString());
439+
UniValue outputs(UniValue::VARR);
440+
for (const auto out : islock->inputs) {
441+
UniValue outpoint(UniValue::VOBJ);
442+
outpoint.pushKV("txid", out.hash.ToString());
443+
outpoint.pushKV("vout", static_cast<int64_t>(out.n));
444+
outputs.push_back(outpoint);
445+
}
446+
objIS.pushKV("cycleHash", islock->cycleHash.ToString());
447+
objIS.pushKV("sig", islock->sig.ToString());
448+
result_arr.push_back(objIS);
449+
} else {
450+
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
451+
ssTx << *islock;
452+
result_arr.push_back(HexStr(ssTx));
453+
}
454+
} else {
455+
result_arr.push_back("None");
456+
}
457+
}
458+
return result_arr;
459+
460+
},
461+
};
462+
}
463+
369464
static RPCHelpMan gettxchainlocks()
370465
{
371466
return RPCHelpMan{
@@ -2088,6 +2183,7 @@ static const CRPCCommand commands[] =
20882183
{ "rawtransactions", &getassetunlockstatuses, },
20892184
{ "rawtransactions", &getrawtransaction, },
20902185
{ "rawtransactions", &getrawtransactionmulti, },
2186+
{ "rawtransactions", &getislocks, },
20912187
{ "rawtransactions", &gettxchainlocks, },
20922188
{ "rawtransactions", &createrawtransaction, },
20932189
{ "rawtransactions", &decoderawtransaction, },

test/functional/interface_zmq_dash.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ def test_instantsend_publishers(self):
288288
# Create two raw TXs, they will conflict with each other
289289
rpc_raw_tx_1 = self.create_raw_tx(self.nodes[0], self.nodes[0], 1, 1, 100)
290290
rpc_raw_tx_2 = self.create_raw_tx(self.nodes[0], self.nodes[0], 1, 1, 100)
291+
assert_equal(['None'], self.nodes[0].getislocks([rpc_raw_tx_1['txid']], 0))
291292
# Send the first transaction and wait for the InstantLock
292293
rpc_raw_tx_1_hash = self.nodes[0].sendrawtransaction(rpc_raw_tx_1['hex'])
293294
self.wait_for_instantlock(rpc_raw_tx_1_hash, self.nodes[0])
@@ -307,6 +308,8 @@ def test_instantsend_publishers(self):
307308
assert_equal(zmq_tx_lock_tx.hash, rpc_raw_tx_1['txid'])
308309
zmq_tx_lock = msg_isdlock()
309310
zmq_tx_lock.deserialize(zmq_tx_lock_sig_stream)
311+
assert_equal(rpc_raw_tx_1['txid'], self.nodes[0].getislocks([rpc_raw_tx_1['txid']], 1)[0]['txid'])
312+
assert_equal([zmq_tx_lock.serialize().hex()], self.nodes[0].getislocks([rpc_raw_tx_1['txid']], 0))
310313
assert_equal(uint256_to_string(zmq_tx_lock.txid), rpc_raw_tx_1['txid'])
311314
# Try to send the second transaction. This must throw an RPC error because it conflicts with rpc_raw_tx_1
312315
# which already got the InstantSend lock.

0 commit comments

Comments
 (0)