Skip to content

Commit

Permalink
rpc: add GetTarget helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjors committed Jan 22, 2025
1 parent d20d96f commit 341f932
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/rpc/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

#include <chain.h>
#include <clientversion.h>
#include <common/args.h>
#include <common/messages.h>
Expand All @@ -13,6 +14,7 @@
#include <key_io.h>
#include <node/types.h>
#include <outputtype.h>
#include <pow.h>
#include <rpc/util.h>
#include <script/descriptor.h>
#include <script/interpreter.h>
Expand Down Expand Up @@ -1418,3 +1420,9 @@ std::vector<RPCResult> ScriptPubKeyDoc() {
{RPCResult::Type::STR, "type", "The type (one of: " + GetAllOutputTypes() + ")"},
};
}

uint256 GetTarget(const CBlockIndex& blockindex, const uint256 pow_limit)
{
arith_uint256 target{*CHECK_NONFATAL(DeriveTarget(blockindex.nBits, pow_limit))};
return ArithToUint256(target);
}
10 changes: 10 additions & 0 deletions src/rpc/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,14 @@ void PushWarnings(const std::vector<bilingual_str>& warnings, UniValue& obj);

std::vector<RPCResult> ScriptPubKeyDoc();

/***
* Get the target for a given block index.
*
* @param[in] blockindex the block
* @param[in] pow_limit PoW limit (consensus parameter)
*
* @return the target
*/
uint256 GetTarget(const CBlockIndex& blockindex, const uint256 pow_limit);

#endif // BITCOIN_RPC_UTIL_H

0 comments on commit 341f932

Please sign in to comment.