cli, rpc: add -rpcid option for custom request IDs

Add a -rpcid CLI argument to bitcoin-cli that allows setting a custom
string as the JSON-RPC request ID instead of the hardcoded default of 1.
This enables correlating requests and responses when debugging or when
multiple clients are making concurrent calls.

On the server side, include the request ID in the RPC debug log line
when it differs from the default value of 1, so that custom IDs are
visible in the debug.log output.
This commit is contained in:
Torkel Rogstad
2026-04-05 15:03:57 +02:00
parent a7c30da1f6
commit c54f37c1ba
3 changed files with 21 additions and 4 deletions

View File

@@ -421,6 +421,18 @@ class TestBitcoinCli(BitcoinTestFramework):
self.test_netinfo()
self.log.info("Test -rpcid option sets custom JSON-RPC request ID")
with self.nodes[0].assert_debug_log(expected_msgs=['id=myrpcid']):
self.nodes[0].cli('-rpcid=myrpcid').getblockcount()
self.log.info("Test default request logs default id=1")
with self.nodes[0].assert_debug_log(expected_msgs=["ThreadRPCServer method=getblockcount", "id=1"]):
self.nodes[0].cli.getblockcount()
self.log.info("Test that request ids with unsafe characters are sanitized in the log")
with self.nodes[0].assert_debug_log(expected_msgs=["ThreadRPCServer method=getblockcount", "id=abcdef"]):
self.nodes[0].cli('-rpcid=abc<\n>def').getblockcount()
self.log.info("Test -version with node stopped")
self.stop_node(0)
cli_response = self.nodes[0].cli('-version').send_cli()