Skip to content

kenhero/bitcoin_tx_fees

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bitcoin Transaction Fee Checker

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.

What it does

  • 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_output value (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.

Assumptions

  • 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.

Build (Cross Platform Linux/Windows)

```bash
mkdir build && cd build
cmake ..
make

How to run:

curl https://blockstream.info/api/block/000000007bc154e0fa7ea32218a72fe2c1bb9f86cf8c9ebf9a715ed27fdb229a/txs/0 | ./txfees Expected output: 0

Output

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

Notes

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published