This C++ tool parses a Bitcoin block in JSON format (from Blockstream Esplora API), calculates the total transaction fees and checks if the coinbase output is valid or not.
- Reads a JSON array of transactions from
stdin. - Calculates total input and output amounts for all transactions (except of course the coinbase).
- Calculates the total transaction fees with this simple formula:
fee = sum(inputs) - sum(outputs) - Ignore the coinbase transaction
- Parses the
coinbase_outputvalue (first transaction) - Computes the maximum allowed reward :
max_reward = block_reward + total_fees - Verifies that the coinbase output does not exceed the sum of
(block reward + total fees), - so basically it checks if the checkd block is valid or not.
- Reward is hardcoded, supposed to be at july 2025 at 6.25 BTC (625000000 satoshi), (corresponding to
halving #3(starting from block height 630000)). - JSON is in the format returned by Blockstream Esplora API.
```bash mkdir build && cd build cmake .. make
curl https://blockstream.info/api/block/000000007bc154e0fa7ea32218a72fe2c1bb9f86cf8c9ebf9a715ed27fdb229a/txs/0 | ./txfees Expected output: 0
The program prints the total transaction fees (in satoshi) If the coinbase output exceeds the maximum allowed value, it prints a warning and exits with code 1
Exit Codes
| Code | Meaning |
|---|---|
0 |
Block is valid |
1 |
Coinbase output exceeds allowed reward |
The tool uses nlohmann/json for parsing, and is fully self-contained. json.hpp is downloaded from here: https://github.com/nlohmann/json/releases/download/v3.11.3/json.hpp and save it in: external/json.hpp
No external runtime dependencies.
Compatible with Linux and Windows.