Skip to content

Commit 341f932

Browse files
committed
rpc: add GetTarget helper
1 parent d20d96f commit 341f932

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Diff for: src/rpc/util.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <bitcoin-build-config.h> // IWYU pragma: keep
66

7+
#include <chain.h>
78
#include <clientversion.h>
89
#include <common/args.h>
910
#include <common/messages.h>
@@ -13,6 +14,7 @@
1314
#include <key_io.h>
1415
#include <node/types.h>
1516
#include <outputtype.h>
17+
#include <pow.h>
1618
#include <rpc/util.h>
1719
#include <script/descriptor.h>
1820
#include <script/interpreter.h>
@@ -1418,3 +1420,9 @@ std::vector<RPCResult> ScriptPubKeyDoc() {
14181420
{RPCResult::Type::STR, "type", "The type (one of: " + GetAllOutputTypes() + ")"},
14191421
};
14201422
}
1423+
1424+
uint256 GetTarget(const CBlockIndex& blockindex, const uint256 pow_limit)
1425+
{
1426+
arith_uint256 target{*CHECK_NONFATAL(DeriveTarget(blockindex.nBits, pow_limit))};
1427+
return ArithToUint256(target);
1428+
}

Diff for: src/rpc/util.h

+10
Original file line numberDiff line numberDiff line change
@@ -516,4 +516,14 @@ void PushWarnings(const std::vector<bilingual_str>& warnings, UniValue& obj);
516516

517517
std::vector<RPCResult> ScriptPubKeyDoc();
518518

519+
/***
520+
* Get the target for a given block index.
521+
*
522+
* @param[in] blockindex the block
523+
* @param[in] pow_limit PoW limit (consensus parameter)
524+
*
525+
* @return the target
526+
*/
527+
uint256 GetTarget(const CBlockIndex& blockindex, const uint256 pow_limit);
528+
519529
#endif // BITCOIN_RPC_UTIL_H

0 commit comments

Comments
 (0)