<p>Ok, so the code is just fine as Andrew assumed. <br/>
It was odd to me that first several blocks are the same. They all have one transaction and the same output value of 50BTC, but according to site <a href="https://blockstream.info/testnet/" rel="nofollow noreferrer">https://blockstream.info/testnet/</a>, where you just type a block height in search bar and you can see all transactions inside of resulting block, this is correct.</p>

CC BY-SA 4.0
2020-07-12T08:57:58.947Z
2020-07-12T08:57:58.947Z
ddavi031
<p>currently I'm exploring Bitcoin source code and I tried to add custom function <code>getblockvalue</code> inside of blockchain.cpp. <br/>
Purpose of function is to return sum of all transaction outputs inside of a block based on given height as an argument.<br/></p>
<pre><code>static UniValue getblockvalue(const JSONRPCRequest&amp; request)
{
    int blockHeight = request.params[0].get_int();
    if (blockHeight &lt; 0 || blockHeight &gt; ::ChainActive().Height())
    {
        throw JSONRPCError(RPC_INVALID_PARAMETER, &quot;Block height out of range&quot;);
    }
        
    CBlockIndex* pBlockIndex = ::ChainActive()[blockHeight];
    if(pBlockIndex == nullptr)
    {
        throw JSONRPCError(RPC_INVALID_PARAMETER, &quot;Block index is NULL&quot;);
    
    }
    const CBlock block = GetBlockChecked(pBlockIndex);
    UniValue resultObject(UniValue::VOBJ);
    double blockValue = 0;
    int numberOfTransactions = 0;
    
    for(const auto &amp;transaction : block.vtx)
    {
        for (unsigned int i = 0; i &lt; transaction-&gt;vout.size(); ++i)
        {
            blockValue+= transaction-&gt;vout[i].nValue;
        }
        ++numberOfTransactions;
    }
    
    resultObject.pushKV(&quot;blockHeight&quot;,          (int)blockHeight);
    resultObject.pushKV(&quot;numberOfTransactions&quot;, (int)numberOfTransactions);
    resultObject.pushKV(&quot;blockValue&quot;,           ValueFromAmount(blockValue));
    
    return resultObject;
}
</code></pre>
<hr />
<p>I did all necessary changes: I added argument to <code>vRPCConvertParams[]</code> and extended <code>CRPCCommand commands[]</code>.<br/>
So, I tried calling bitcoin-cli -testnet getblockvalue 25; result is: <br/></p>
<pre><code>{
&quot;blockHeight&quot; : 25,
&quot;numberOfTransactions&quot; : 1,
&quot;blockValue&quot; : 50.00000000
}
</code></pre>
<p>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?</p>

CC BY-SA 4.0
2020-07-11T15:01:23.637Z
What makes you think it is always returning the genesis block? Did you try a recent block?
Ava Chow
CC BY-SA 4.0
2020-07-11T15:11:25.253Z
I tried a lot of random numbers. I'm not sure it does, but its odd to me that every block has the same result of a function? To be honest I tried number "999999" as block height and it gave me result: `numberOfTransactions 2`, and `blockValue: 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?
ddavi031
CC BY-SA 4.0
2020-07-11T15:30:35.533Z
@AndrewChow you were right, code is ok. Thank  you a lot. Since you are moderator, should i just delete my question since has no point?
ddavi031
CC BY-SA 4.0
2020-07-12T07:02:03.717Z
Misconceptions should be addressed in the answer to a question, and self-answers are welcome. You could therefore submit an answer to explain what you found out, if you think it would be interesting to future readers.
Murch
<bitcoin-core><bitcoin-cli><genesis-block>
GetBlockChecked always returning genesis block?
ddavi031
# GetBlockChecked always returning genesis block?

(Asked by: username_0 on 2020-07-11)

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?

**Tags:** bitcoin-core, bitcoin-cli, genesis-block

> **Comments:**
> **username_1**: What makes you think it is always returning the genesis block? Did you try a recent block?  
> **username_0**: I tried a lot of random numbers. I'm not sure it does, but its odd to me that every block has the same result of a function? To be honest I tried number "999999" as block height and it gave me result: `numberOfTransactions 2`, and `blockValue: 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?  
> **username_0**: @AndrewChow you were right, code is ok. Thank  you a lot. Since you are moderator, should i just delete my question since has no point?  
> **username_2**: Misconceptions should be addressed in the answer to a question, and self-answers are welcome. You could therefore submit an answer to explain what you found out, if you think it would be interesting to future readers.  

---

## 1 answers

### Answer by username_0 on 2020-07-12. Score: 1 | Accepted answer

Ok, so the code is just fine as Andrew assumed.   
It was odd to me that first several blocks are the same. They all have one transaction and the same output value of 50BTC, but according to site <https://blockstream.info/testnet/>, where you just type a block height in search bar and you can see all transactions inside of resulting block, this is correct.


---

