Skip to content

Commit

Permalink
Merge pull request DigiByte-Core#122 from JaredTate/feature/getblockr…
Browse files Browse the repository at this point in the history
…eward

Add new RPC Command "getblockreward"
  • Loading branch information
JaredTate authored Apr 13, 2023
2 parents ca3034c + 875d26d commit 4003458
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
42 changes: 38 additions & 4 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@
#include <validationinterface.h>
#include <versionbits.h>
#include <warnings.h>

#include <stdint.h>

#include <univalue.h>

#include <condition_variable>
#include <memory>
#include <mutex>
#include <validation.h>

struct CUpdatedBlock
{
Expand All @@ -62,6 +60,8 @@ struct CUpdatedBlock
static Mutex cs_blockchange;
static std::condition_variable cond_blockchange;
static CUpdatedBlock latestblock GUARDED_BY(cs_blockchange);
static UniValue getblockreward(const JSONRPCRequest& request);


NodeContext& EnsureAnyNodeContext(const std::any& context)
{
Expand Down Expand Up @@ -2684,6 +2684,40 @@ UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFil
return result;
}

static RPCHelpMan getblockreward()
{
return RPCHelpMan{"getblockreward",
"\nReturns the current DGB block reward.\n",
{},
RPCResult{
RPCResult::Type::OBJ, "", "Information about the current block reward",
{
{"blockreward", RPCResult::Type::NUM, "n", "The current block reward in DGB"},
}},
RPCExamples{
HelpExampleCli("getblockreward", "")
+ HelpExampleRpc("getblockreward", "")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
ChainstateManager& chainman = EnsureAnyChainman(request.context);
LOCK(cs_main);
CChain& active_chain = chainman.ActiveChain();
CBlockIndex* pindex = active_chain.Tip();

if (!pindex)
throw JSONRPCError(RPC_INTERNAL_ERROR, "Error: Couldn't find the current block");

int64_t nReward = GetBlockSubsidy(pindex->nHeight, Params().GetConsensus());
UniValue result(UniValue::VOBJ);
result.pushKV("blockreward", ValueFromAmount(nReward));

return result;
}};
}



void RegisterBlockchainRPCCommands(CRPCTable &t)
{
// clang-format off
Expand All @@ -2710,10 +2744,10 @@ static const CRPCCommand commands[] =
{ "blockchain", &pruneblockchain, },
{ "blockchain", &savemempool, },
{ "blockchain", &verifychain, },

{ "blockchain", &preciousblock, },
{ "blockchain", &scantxoutset, },
{ "blockchain", &getblockfilter, },
{ "blockchain", &getblockreward, },\

/* Not shown in help */
{ "hidden", &invalidateblock, },
Expand Down
30 changes: 30 additions & 0 deletions test/functional/rpc_getblockreward.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python3
# Copyright (c) 2021 The DigiByte Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

from test_framework.test_framework import DigiByteTestFramework
from test_framework.util import assert_equal

class GetBlockRewardTest(DigiByteTestFramework):
def set_test_params(self):
self.num_nodes = 1
self.setup_clean_chain = True

def run_test(self):
node = self.nodes[0]

# Mine some blocks
num_blocks = 101
node.generatetoaddress(num_blocks, node.get_deterministic_priv_key().address, invalid_call=False)

# Test getblockreward RPC
block_reward = node.getblockreward()
self.log.info("Current block reward: {}".format(block_reward["blockreward"]))

# Replace 12.5 with the expected block reward value for your test
expected_block_reward = 72000
assert_equal(block_reward["blockreward"], expected_block_reward)

if __name__ == '__main__':
GetBlockRewardTest().main()
1 change: 1 addition & 0 deletions test/functional/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
'rpc_signmessage.py',
'rpc_generateblock.py',
'rpc_generate.py',
'rpc_getblockreward.py',
'wallet_balance.py --legacy-wallet',
'wallet_balance.py --descriptors',
'feature_nulldummy.py --legacy-wallet',
Expand Down

0 comments on commit 4003458

Please sign in to comment.