Fetching slot and Difficulty information from the terminal

Sometimes, we need to fetch the information at a slot in order to debug a
testnet. The consensus clients make this easy by following the standard beacon
APIs [https://ethereum.github.io/beacon-APIs] (most of them atleast), we just
need to query them in order.

Information that is usually at hand for debugging: Slot number. So we can just
use the endpoint /eth/v2/beacon/blocks/{block_id}, where block_id can be either
“finalized”,”genesis”,”slot” or a “hex encoded root”.

Since we would want to do this often, let’s abstract it into a cli function. I
will assume from here on that your system uses zsh and has jq installed, it
should however work similarly for bash.

  1. Open your ~/.zshrc file
  2. Go to the bottom and add a comment # fetch the entire block given a CL slot
    number or something similar
  3. Below the comment, enter: fetch_block_at_slot(){curl -sS
    “$1”/eth/v1/beacon/blocks/$2 | jq }
  4. Reload the config with source ~/.zshrc

This will add the function fetch_block_at_slot and it accepts 2 arguments: The
first argument being the endpoint to which the data is feched from. The second
is the slot number.

An example of the usage is:

1
2
3
4
5
6
7
8
9
10
11
fetch_block_at_slot localhost:4000 1100
> {
"data": {
"message": {
"slot": "1100",
"proposer_index": "12798",
"parent_root": "0xf130e3162dc304d05d397c8d79f81b38e7d7a1b92a7ca87b65f5c2f81f03a10a",
"state_root": "0xea580fc86377c3db5301acd224204603394f607e98c9a63813d6e2ef68cd605c",
"body": {
....
}

Supposing we need to fetch the execution payload very often, well, we can just
add a new function to handle that as well! Similar to before, add a function
called: fetch_execution_payload(){curl -sS "$1"/eth/v1/beacon/blocks/$2 | jq -r '.data.message.body.execution_payload' }.

An example of this function is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fetch_execution_payload localhost:4000 1100
> {
"parent_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"state_root": "0x0000000000000000000000000000000000000000000000000000000000000000",
"receipt_root": "0x0000000000000000000000000000000000000000000000000000000000000000",
"logs_bloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"random": "0x0000000000000000000000000000000000000000000000000000000000000000",
"block_number": "0",
"gas_limit": "0",
"gas_used": "0",
"timestamp": "0",
"extra_data": "0x",
"base_fee_per_gas": "0",
"block_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"transactions": []
}

TTD stands for total terminal difficulty, it defines when the Ethereum PoW is
ready for the merge. Difficulty goes up with each block till TTD is hit, if
things go correctly, we then switch to PoS mode. Total Difficulty can be
obtained from the block information, similar to the earlier examples.

  1. Open your ~/.zshrc file
  2. Go to the bottom and add a comment # calculate percentage of TTD doneor
    something similar
  3. Below the comment, enter: fetch_ttd_status(){echo “Total Difficulty
    completed till TTD: $(($(curl -s --request POST "$1" --header 'Content-Type: application/json' --data-raw '{ "jsonrpc":"2.0", "method":"eth_getBlockByNumber", "params":[ "latest", false ], "id":1 }' | jq -r '.result.totalDifficulty')))/"$2" * 100" | bc) % }
  4. Reload the config with source ~/.zshrc
  5. You need to pass the RPC URL as well as the Total Terminal Difficulty while
    calling this function

An example of this function is:

1
2
fetch_ttd_status https://rpc.devnet3.themerge.dev 5000000000
> Total Difficulty completed till TTD: 8.1700 %