Skip to content

Commit

Permalink
feat: add new field hex to RPC getbestchainlock
Browse files Browse the repository at this point in the history
It adds ability to receive raw chainlock in hex format (compatible with ZMQ subscription)
  • Loading branch information
knst committed Jan 14, 2025
1 parent 3edc738 commit 53a5707
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,29 +241,38 @@ static RPCHelpMan getbestchainlock()
{RPCResult::Type::NUM, "height", "The block height or index"},
{RPCResult::Type::STR_HEX, "signature", "The ChainLock's BLS signature"},
{RPCResult::Type::BOOL, "known_block", "True if the block is known by our node"},
{RPCResult::Type::STR_HEX, "hex", "The serialized, hex-encoded data for best ChainLock"},
}},
RPCExamples{
HelpExampleCli("getbestchainlock", "")
+ HelpExampleRpc("getbestchainlock", "")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
UniValue result(UniValue::VOBJ);

const NodeContext& node = EnsureAnyNodeContext(request.context);

const LLMQContext& llmq_ctx = EnsureLLMQContext(node);
const llmq::CChainLockSig clsig = llmq_ctx.clhandler->GetBestChainLock();
if (clsig.IsNull()) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Unable to find any ChainLock");
}

UniValue result(UniValue::VOBJ);

result.pushKV("blockhash", clsig.getBlockHash().GetHex());
result.pushKV("height", clsig.getHeight());
result.pushKV("signature", clsig.getSig().ToString());

const ChainstateManager& chainman = EnsureChainman(node);
LOCK(cs_main);
result.pushKV("known_block", chainman.m_blockman.LookupBlockIndex(clsig.getBlockHash()) != nullptr);
{
const ChainstateManager& chainman = EnsureChainman(node);
LOCK(cs_main);
result.pushKV("known_block", chainman.m_blockman.LookupBlockIndex(clsig.getBlockHash()) != nullptr);
}

CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
ssTx << clsig;
result.pushKV("hex", HexStr(ssTx));

return result;
},
};
Expand Down
3 changes: 3 additions & 0 deletions test/functional/interface_zmq_dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 hex getbestchainlock before any CL appeared")
assert_raises_rpc_error(-32603, "Unable to find any ChainLock", self.nodes[0].getbestchainlock)
self.wait_for_sporks_same()
self.activate_v19(expected_activation_height=900)
self.log.info("Activated v19 at height:" + str(self.nodes[0].getblockcount()))
Expand Down Expand Up @@ -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].getbestchainlock()['hex'])
# Unsubscribe from ChainLock messages
self.unsubscribe(chain_lock_publishers)

Expand Down

0 comments on commit 53a5707

Please sign in to comment.