Skip to content

Commit 756b772

Browse files
committed
feat: implement new RPC getrawbestchainlock
1 parent c791ce6 commit 756b772

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/rpc/blockchain.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,37 @@ static RPCHelpMan getbestchainlock()
269269
};
270270
}
271271

272+
static RPCHelpMan getrawbestchainlock()
273+
{
274+
return RPCHelpMan{"getrawbestchainlock",
275+
"\nReturns the raw best ChainLock. Throws an error if there is no known ChainLock yet.",
276+
{},
277+
RPCResult{
278+
RPCResult::Type::STR, "data", "The serialized, hex-encoded data for best ChainLock"
279+
},
280+
RPCExamples{
281+
HelpExampleCli("getrawbestchainlock", "")
282+
+ HelpExampleRpc("getrawbestchainlock", "")
283+
},
284+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
285+
{
286+
UniValue result(UniValue::VOBJ);
287+
288+
const NodeContext& node = EnsureAnyNodeContext(request.context);
289+
290+
LLMQContext& llmq_ctx = EnsureLLMQContext(node);
291+
llmq::CChainLockSig clsig = llmq_ctx.clhandler->GetBestChainLock();
292+
if (clsig.IsNull()) {
293+
throw JSONRPCError(RPC_MISC_ERROR, "Unable to find any ChainLock");
294+
}
295+
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
296+
ssTx << clsig;
297+
return HexStr(ssTx);
298+
299+
},
300+
};
301+
}
302+
272303
void RPCNotifyBlockChange(const CBlockIndex* pindex)
273304
{
274305
if(pindex) {
@@ -2671,6 +2702,7 @@ static const CRPCCommand commands[] =
26712702
{ "blockchain", &getblockstats, },
26722703
{ "blockchain", &getbestblockhash, },
26732704
{ "blockchain", &getbestchainlock, },
2705+
{ "blockchain", &getrawbestchainlock, },
26742706
{ "blockchain", &getblockcount, },
26752707
{ "blockchain", &getblock, },
26762708
{ "blockchain", &getblockfrompeer, },

test/functional/interface_zmq_dash.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ def run_test(self):
136136
self.zmq_context = zmq.Context()
137137
# Initialize the network
138138
self.nodes[0].sporkupdate("SPORK_17_QUORUM_DKG_ENABLED", 0)
139+
self.log.info("Test RPC getrawbestchainlock before any CL appeared")
140+
assert_raises_rpc_error(-1, "Unable to find any ChainLock", self.nodes[0].getrawbestchainlock)
139141
self.wait_for_sporks_same()
140142
self.activate_v19(expected_activation_height=900)
141143
self.log.info("Activated v19 at height:" + str(self.nodes[0].getblockcount()))
@@ -263,6 +265,7 @@ def test_chainlock_publishers(self):
263265
assert_equal(uint256_to_string(zmq_chain_lock.blockHash), rpc_chain_lock_hash)
264266
assert_equal(zmq_chain_locked_block.hash, rpc_chain_lock_hash)
265267
assert_equal(zmq_chain_lock.sig.hex(), rpc_best_chain_lock_sig)
268+
assert_equal(zmq_chain_lock.serialize().hex(), self.nodes[0].getrawbestchainlock())
266269
# Unsubscribe from ChainLock messages
267270
self.unsubscribe(chain_lock_publishers)
268271

0 commit comments

Comments
 (0)