From ef9e86039aee0a43ea4a64eaaaf759b370f6a51e Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Wed, 8 Jan 2025 13:03:08 +0100 Subject: [PATCH] rpc: gettarget --- src/rpc/blockchain.cpp | 21 ++++++++++++++++++++ src/rpc/util.cpp | 8 ++++++++ src/rpc/util.h | 10 ++++++++++ src/test/fuzz/rpc.cpp | 1 + test/functional/rpc_blockchain.py | 9 +++++++++ test/functional/test_framework/blocktools.py | 6 ++++++ 6 files changed, 55 insertions(+) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 3caf4bf4ebe..6756900cb2e 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -447,6 +447,26 @@ static RPCHelpMan getdifficulty() }; } +static RPCHelpMan gettarget() +{ + return RPCHelpMan{"gettarget", + "\nReturns the proof-of-work target.\n", + {}, + RPCResult{ + RPCResult::Type::STR_HEX, "", "the proof-of-work target."}, + RPCExamples{ + HelpExampleCli("gettarget", "") + + HelpExampleRpc("gettarget", "") + }, + [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue +{ + ChainstateManager& chainman = EnsureAnyChainman(request.context); + CBlockIndex& tip{*CHECK_NONFATAL(WITH_LOCK(chainman.GetMutex(), return chainman.ActiveChain().Tip()))}; + return GetTarget(tip, chainman.GetParams().GetConsensus().powLimit).GetHex(); +}, + }; +} + static RPCHelpMan getblockfrompeer() { return RPCHelpMan{ @@ -3382,6 +3402,7 @@ void RegisterBlockchainRPCCommands(CRPCTable& t) {"blockchain", &getblockheader}, {"blockchain", &getchaintips}, {"blockchain", &getdifficulty}, + {"blockchain", &gettarget}, {"blockchain", &getdeploymentinfo}, {"blockchain", &gettxout}, {"blockchain", &gettxoutsetinfo}, diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp index b1fbc256414..2941dda8c0a 100644 --- a/src/rpc/util.cpp +++ b/src/rpc/util.cpp @@ -4,6 +4,7 @@ #include // IWYU pragma: keep +#include #include #include #include @@ -13,6 +14,7 @@ #include #include #include +#include #include #include