From 756b77228a9c3523639bf59f12cba10aa7090fdb Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Fri, 6 Dec 2024 16:50:24 +0700 Subject: [PATCH] feat: implement new RPC getrawbestchainlock --- src/rpc/blockchain.cpp | 32 +++++++++++++++++++++++++++ test/functional/interface_zmq_dash.py | 3 +++ 2 files changed, 35 insertions(+) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 871ef727b15abf..1a11da86c84d4c 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -269,6 +269,37 @@ static RPCHelpMan getbestchainlock() }; } +static RPCHelpMan getrawbestchainlock() +{ + return RPCHelpMan{"getrawbestchainlock", + "\nReturns the raw best ChainLock. Throws an error if there is no known ChainLock yet.", + {}, + RPCResult{ + RPCResult::Type::STR, "data", "The serialized, hex-encoded data for best ChainLock" + }, + RPCExamples{ + HelpExampleCli("getrawbestchainlock", "") + + HelpExampleRpc("getrawbestchainlock", "") + }, + [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue +{ + UniValue result(UniValue::VOBJ); + + const NodeContext& node = EnsureAnyNodeContext(request.context); + + LLMQContext& llmq_ctx = EnsureLLMQContext(node); + llmq::CChainLockSig clsig = llmq_ctx.clhandler->GetBestChainLock(); + if (clsig.IsNull()) { + throw JSONRPCError(RPC_MISC_ERROR, "Unable to find any ChainLock"); + } + CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); + ssTx << clsig; + return HexStr(ssTx); + +}, + }; +} + void RPCNotifyBlockChange(const CBlockIndex* pindex) { if(pindex) { @@ -2671,6 +2702,7 @@ static const CRPCCommand commands[] = { "blockchain", &getblockstats, }, { "blockchain", &getbestblockhash, }, { "blockchain", &getbestchainlock, }, + { "blockchain", &getrawbestchainlock, }, { "blockchain", &getblockcount, }, { "blockchain", &getblock, }, { "blockchain", &getblockfrompeer, }, diff --git a/test/functional/interface_zmq_dash.py b/test/functional/interface_zmq_dash.py index c5da66d510c890..956abaa5a56a4b 100755 --- a/test/functional/interface_zmq_dash.py +++ b/test/functional/interface_zmq_dash.py @@ -136,6 +136,8 @@ def run_test(self): self.zmq_context = zmq.Context() # Initialize the network self.nodes[0].sporkupdate("SPORK_17_QUORUM_DKG_ENABLED", 0) + self.log.info("Test RPC getrawbestchainlock before any CL appeared") + assert_raises_rpc_error(-1, "Unable to find any ChainLock", self.nodes[0].getrawbestchainlock) self.wait_for_sporks_same() self.activate_v19(expected_activation_height=900) self.log.info("Activated v19 at height:" + str(self.nodes[0].getblockcount())) @@ -263,6 +265,7 @@ def test_chainlock_publishers(self): assert_equal(uint256_to_string(zmq_chain_lock.blockHash), rpc_chain_lock_hash) assert_equal(zmq_chain_locked_block.hash, rpc_chain_lock_hash) assert_equal(zmq_chain_lock.sig.hex(), rpc_best_chain_lock_sig) + assert_equal(zmq_chain_lock.serialize().hex(), self.nodes[0].getrawbestchainlock()) # Unsubscribe from ChainLock messages self.unsubscribe(chain_lock_publishers)