Skip to content

Instantly share code, notes, and snippets.

@potuz
Last active November 26, 2020 12:37
Show Gist options
  • Save potuz/79b6ddceb13501b2a1d0af81d1a1a4c5 to your computer and use it in GitHub Desktop.
Save potuz/79b6ddceb13501b2a1d0af81d1a1a4c5 to your computer and use it in GitHub Desktop.
Show latests blocks in eth 2.0
#!/usr/bin/env python
from eth2spec.phase0 import spec
import requests
import json
#import sys, getopt
import base64
#The base address of the beacon-node
base_url = 'http://localhost:3501/eth/v1alpha1/'
#Prysm's API calls
blocks_endpoint = "beacon/blocks"
chainhead_endpoint = "beacon/chainhead"
def print_epoch(epoch):
#Get this epoch's blocks
query = "?epoch=%d" % (epoch)
headResp = requests.get(base_url+blocks_endpoint+query)
data = headResp.content.decode()
resp_json = json.loads(data)
containers = resp_json["blockContainers"]
containers.reverse()
for c in containers :
block = c["block"]["block"]
slot = int(block["slot"])
proposer = int(block["proposerIndex"])
body = block["body"]
graffiti_temp = base64.b64decode(body["graffiti"])
graffiti = graffiti_temp.decode('utf8')
attestations = len(body["attestations"])
deposits = len(body["deposits"])
exits = len(body["voluntaryExits"])
proposerSlashings = len(body["proposerSlashings"])
attesterSlashings = len(body["attesterSlashings"])
print("%8d%10d%10s%8d%5d%3d/%-3d%5d%20s" % (epoch, slot, proposer,
attestations, deposits,
proposerSlashings,
attesterSlashings,
exits, graffiti))
def main():
#Get chainhead
response = requests.get(base_url+chainhead_endpoint)
data = response.content.decode()
chainhead = json.loads(data)
epoch = int(chainhead['headEpoch'])
print(" Epoch Slot Proposer Att Dep Slsh Graffiti")
print_epoch(epoch)
print_epoch(epoch-1)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment