From fa1640617e061431059908fbf496dccca6b4e112 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Fri, 21 Jul 2023 14:42:49 +0200 Subject: [PATCH 1/2] test: Add SyncWithValidationInterfaceQueue to mockscheduler RPC This makes existing tests less brittle, see https://github.com/bitcoin/bitcoin/pull/28108/files#r1268966663 --- src/rpc/node.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rpc/node.cpp b/src/rpc/node.cpp index 38284016424..f614253195b 100644 --- a/src/rpc/node.cpp +++ b/src/rpc/node.cpp @@ -93,6 +93,7 @@ static RPCHelpMan mockscheduler() // protect against null pointer dereference CHECK_NONFATAL(node_context->scheduler); node_context->scheduler->MockForward(std::chrono::seconds(delta_seconds)); + SyncWithValidationInterfaceQueue(); return UniValue::VNULL; }, From fabef121b0cdfac6ec1985f6c08c5685a886ba5a Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Fri, 21 Jul 2023 15:05:44 +0200 Subject: [PATCH 2/2] refactor: Use EnsureAnyNodeContext node_context is never null, but if it was, it would lead to a nullptr dereference in node_context->scheduler. Just use EnsureAnyNodeContext everywhere for more robust, consistent, and correct code. --- src/rpc/node.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/rpc/node.cpp b/src/rpc/node.cpp index f614253195b..dc5ecb9cbc1 100644 --- a/src/rpc/node.cpp +++ b/src/rpc/node.cpp @@ -57,11 +57,9 @@ static RPCHelpMan setmocktime() throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Mocktime cannot be negative: %s.", time)); } SetMockTime(time); - auto node_context = util::AnyPtr(request.context); - if (node_context) { - for (const auto& chain_client : node_context->chain_clients) { - chain_client->setMockTime(time); - } + const NodeContext& node_context{EnsureAnyNodeContext(request.context)}; + for (const auto& chain_client : node_context.chain_clients) { + chain_client->setMockTime(time); } return UniValue::VNULL; @@ -89,10 +87,8 @@ static RPCHelpMan mockscheduler() throw std::runtime_error("delta_time must be between 1 and 3600 seconds (1 hr)"); } - auto node_context = CHECK_NONFATAL(util::AnyPtr(request.context)); - // protect against null pointer dereference - CHECK_NONFATAL(node_context->scheduler); - node_context->scheduler->MockForward(std::chrono::seconds(delta_seconds)); + const NodeContext& node_context{EnsureAnyNodeContext(request.context)}; + CHECK_NONFATAL(node_context.scheduler)->MockForward(std::chrono::seconds{delta_seconds}); SyncWithValidationInterfaceQueue(); return UniValue::VNULL;