Beacon REST API - Fetching Blocks on a Fork
By Adrian Sutton
When debugging issues on the beacon chain, it can be useful to download all blocks on a particularly, potentially non-canonical fork. This script will do just that.
The script should work with any client that supports the standard REST API. Execute it with fetch.sh <BLOCK_ROOT> <NUMBER_OF_BLOCKS_TO_DOWNLOAD>
#!/bin/bash
set -euo pipefail
ROOT=${1:?Must specify a starting block root}
COUNT=${2:?Must specify number of blocks to fetch}
for i in $(seq 1 $COUNT)
do
curl -s http://localhost:5051/eth/v2/beacon/blocks/${ROOT} | jq . > tmp.json
SLOT=$(cat tmp.json | jq -r .data.message.slot)
PARENT=$(cat tmp.json | jq -r .data.message.parent_root)
mv tmp.json ${SLOT}.json
curl -s -H 'Accept: application/octet-stream' http://localhost:5051/eth/v2/beacon/blocks/${ROOT} > ${SLOT}.ssz
echo "$SLOT ($ROOT)"
ROOT=$PARENT
done
Blocks are downloaded in both JSON and SSZ format. As it downloads it prints the slot and block root for each block it downloads.
This is particularly useful when combined with Teku’s data-storage-non-canonical-blocks-enabled
option which makes it store all blocks it receives even if they don’t wind up on the finalized chain.