currently I'm exploring Bitcoin source code and I tried to add custom function getblockvalue
inside of blockchain.cpp.
Purpose of function is to return sum of all transaction outputs inside of a block based on given height as an argument.
static UniValue getblockvalue(const JSONRPCRequest& request)
{
int blockHeight = request.params[0].get_int();
if (blockHeight < 0 || blockHeight > ::ChainActive().Height())
{
throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range");
}
CBlockIndex* pBlockIndex = ::ChainActive()[blockHeight];
if(pBlockIndex == nullptr)
{
throw JSONRPCError(RPC_INVALID_PARAMETER, "Block index is NULL");
}
const CBlock block = GetBlockChecked(pBlockIndex);
UniValue resultObject(UniValue::VOBJ);
double blockValue = 0;
int numberOfTransactions = 0;
for(const auto &transaction : block.vtx)
{
for (unsigned int i = 0; i < transaction->vout.size(); ++i)
{
blockValue+= transaction->vout[i].nValue;
}
++numberOfTransactions;
}
resultObject.pushKV("blockHeight", (int)blockHeight);
resultObject.pushKV("numberOfTransactions", (int)numberOfTransactions);
resultObject.pushKV("blockValue", ValueFromAmount(blockValue));
return resultObject;
}
I did all necessary changes: I added argument to vRPCConvertParams[]
and extended CRPCCommand commands[]
.
So, I tried calling bitcoin-cli -testnet getblockvalue 25; result is:
{
"blockHeight" : 25,
"numberOfTransactions" : 1,
"blockValue" : 50.00000000
}
The result is the same for every given block height. It always gives me information about genesis block. Can you help me what did I do wrong here?
numberOfTransactions 2
, andblockValue: 3.1264
. And i thought that is a part of an error, because seeing only 2 transactions inside of a block, is to small... is it?