Skip to content

Instantly share code, notes, and snippets.

@garethtdavies
Last active July 19, 2021 23:04
Show Gist options
  • Save garethtdavies/18ee98214bb3511bc2d59a9b8998ea17 to your computer and use it in GitHub Desktop.
Save garethtdavies/18ee98214bb3511bc2d59a9b8998ea17 to your computer and use it in GitHub Desktop.
Evaluate block producer performance
# This script checks the performance of a block producer given the provable slots they won
# Producing a block is not the only metric, we also want to know they produced at the canonical height for this slot
from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport
from tabulate import tabulate
# Select your transport with a defined url endpoint
transport = AIOHTTPTransport(url="https://graphql.minaexplorer.com")
# Create a GraphQL client using the defined transport
client = Client(transport=transport, fetch_schema_from_transport=True)
# This is the block producer we are evaluating
creator = "B62qpge4uMq4Vv5Rvc8Gw9qSquUYd6xoW1pz7HQkMSHm6h1o7pvLPAN"
output = []
# Get the slots won from the check output of VRF https://github.com/zkvalidator/mina-vrf-rs
slots = [
14304, 14311, 14352, 14359, 14385, 14421, 14506, 14519, 14529, 14562,
14578, 14672, 14938, 15030, 15084, 15134, 15137, 15208, 15216, 15304,
15390, 15409, 15431, 15471, 15716, 15860, 15904, 15928, 15931, 15964,
16058, 16264, 16305, 16312, 16387, 16427, 16446, 16456, 16606, 16624,
16661, 16669, 16767, 16808, 16838, 16863, 16907, 16928, 16978, 16992,
17031, 17048, 17063, 17102, 17125, 17128, 17141, 17175, 17216, 17235,
17286, 17290, 17313, 17466, 17542, 17602, 17683, 17691, 17818, 17869,
17910, 17924, 17945, 18224, 18230, 18253, 18310, 18445, 18459, 18512,
18517, 18518, 18768, 18778, 18802, 18899, 18921, 18935, 18967, 19016,
19021, 19098, 19132, 19190, 19254, 19268, 19269, 19288, 19379, 19461,
19491, 19547, 19599, 19686, 19690, 19706, 19802, 19845, 19858, 19873,
19897, 19906, 19976, 20012, 20077, 20093, 20129, 20159, 20178, 20185,
20209, 20231, 20323, 20345, 20363, 20371, 20372, 20377, 20398, 20451,
20462, 20524, 20545, 20636, 20771, 20829, 20900, 20925, 20972, 20987,
20999, 21068, 21069, 21105, 21236, 21281, 21314, 21342, 21378
]
for s in slots:
# GraphQL query to get the blocks produced for the current slot
query = gql("""
query blocksForSlot {{
blocks(query: {{protocolState: {{consensusState: {{slotSinceGenesis: {slot} }}}}}}) {{
blockHeight
canonical
creator
dateTime
stateHash
}}
}}
""".format(slot=s))
result = client.execute(query)
# What is the canonical height for this slot?
if result["blocks"]:
canonical_block = list(
filter(lambda d: d['canonical'] == True, result["blocks"]))
if canonical_block:
canonical_height = canonical_block[0]["blockHeight"]
# Did our creator produce at the right height - we might not have the canonical block
producer_blocks = list(
filter(
lambda d: d['creator'] == creator and d['blockHeight'] ==
canonical_height, result["blocks"]))
if producer_blocks:
output.append(
[s, True, canonical_height,
len(result["blocks"])])
else:
output.append(
[s, False, canonical_height,
len(result["blocks"])])
else:
# No canonical block for this height - possible a later block reorged this
# Is this block produced at a height greater than the maximum we have seen?
# GraphQL query to get the canonical height at the time of the slot
query2 = gql("""
query heightAtSlot {{
blocks(limit: 1, query: {{canonical: true, protocolState: {{consensusState: {{slotSinceGenesis_lt: {slot} }}}}}}, sortBy: BLOCKHEIGHT_DESC) {{
blockHeight
}}
}}
""".format(slot=s))
result2 = client.execute(query2)
prior_height = result2["blocks"][0]["blockHeight"]
# Did we produce a block greater than the current height at this slot
producer_blocks = list(
filter(
lambda d: d['creator'] == creator and d['blockHeight'] >
prior_height, result["blocks"]))
# We produced a block(s) at greater than the current canonical tip and it was valid
if producer_blocks:
output.append(
[s, True, prior_height + 1,
len(result["blocks"])])
# It was at the wrong height
else:
output.append(
[s, False, prior_height + 1,
len(result["blocks"])])
else:
# No block for this slot so cannot have produced
output.append([s, False, None, len(result["blocks"])])
produced_correct_height = list(filter(lambda d: d[1] == True, output))
# Table headers
headers = [
"Global Slot", "Canonical Height", "Block Height", "# Blocks at Height"
]
# Output a summary table for each slot
print(tabulate(output, headers=headers, tablefmt="pretty"))
performance_score = "{:.2%}".format(len(produced_correct_height) / len(output))
print(
f"There were {len(output)} slots and the producer produced at the canonical height for {len(produced_correct_height)} slots and has a performance score of {performance_score}."
)
@garethtdavies
Copy link
Author

Sample output:

+-------------+------------------+--------------+--------------------+
| Global Slot | Canonical Height | Block Height | # Blocks at Height |
+-------------+------------------+--------------+--------------------+
|    42879    |       True       |    30496     |         5          |
|    42916    |       True       |    30526     |         6          |
|    42947    |       True       |    30550     |         1          |
|    42961    |       True       |    30557     |         3          |
|    43046    |       True       |    30620     |         2          |
|    43102    |       True       |    30663     |         3          |
|    43134    |       True       |    30682     |         2          |
|    43158    |       True       |    30699     |         1          |
|    43199    |       True       |    30731     |         2          |
|    43301    |       True       |    30808     |         2          |
|    43307    |       True       |    30813     |         2          |
|    43404    |       True       |    30883     |         1          |
|    43439    |       True       |    30912     |         3          |
|    43458    |       True       |    30928     |         1          |
|    43463    |       True       |    30931     |         2          |
|    43477    |       True       |    30943     |         4          |
|    43496    |       True       |    30958     |         2          |
|    43645    |       True       |    31069     |         3          |
|    43655    |       True       |    31076     |         3          |
|    43671    |       True       |    31089     |         2          |
|    43676    |       True       |    31093     |         5          |
|    43700    |       True       |    31108     |         1          |
|    43741    |       True       |    31134     |         3          |
|    43757    |       True       |    31144     |         5          |
|    43797    |       True       |    31174     |         2          |
|    43814    |       True       |    31189     |         2          |
|    43847    |       True       |    31214     |         2          |
|    43882    |       True       |    31238     |         5          |
|    43962    |       True       |    31293     |         2          |
|    43980    |       True       |    31303     |         3          |
|    44007    |       True       |    31322     |         1          |
|    44020    |       True       |    31332     |         4          |
|    44121    |       True       |    31409     |         3          |
|    44199    |       True       |    31468     |         2          |
|    44234    |       True       |    31491     |         3          |
|    44348    |       True       |    31568     |         1          |
|    44354    |       True       |    31571     |         4          |
|    44358    |       True       |    31574     |         6          |
|    44364    |       True       |    31580     |         4          |
|    44384    |       True       |    31595     |         3          |
|    44469    |       True       |    31654     |         2          |
|    44476    |       True       |    31659     |         5          |
|    44548    |       True       |    31718     |         3          |
|    44708    |       True       |    31836     |         2          |
|    44730    |       True       |    31853     |         2          |
|    44809    |       True       |    31913     |         1          |
|    44822    |       True       |    31920     |         1          |
|    44880    |       True       |    31958     |         4          |
|    44900    |       True       |    31976     |         5          |
|    44966    |       True       |    32025     |         3          |
|    44979    |       True       |    32034     |         3          |
|    45017    |       True       |    32067     |         5          |
|    45103    |       True       |    32135     |         1          |
|    45128    |       True       |    32153     |         4          |
|    45156    |       True       |    32170     |         2          |
|    45171    |       True       |    32182     |         5          |
|    45190    |       True       |    32197     |         2          |
|    45256    |       True       |    32246     |         2          |
|    45494    |       True       |    32433     |         4          |
|    45549    |       True       |    32469     |         3          |
|    45564    |       True       |    32479     |         1          |
|    45568    |       True       |    32482     |         6          |
|    45587    |       True       |    32496     |         3          |
|    45704    |       True       |    32591     |         2          |
|    45725    |       True       |    32603     |         1          |
|    45825    |       True       |    32673     |         1          |
|    45944    |       True       |    32762     |         2          |
|    45974    |       True       |    32784     |         5          |
|    45979    |       True       |    32787     |         3          |
|    45989    |       True       |    32795     |         2          |
|    46064    |       True       |    32850     |         6          |
|    46178    |       True       |    32931     |         3          |
|    46211    |       True       |    32955     |         3          |
|    46254    |       True       |    32991     |         5          |
|    46281    |       True       |    33006     |         3          |
|    46405    |       True       |    33096     |         1          |
|    46508    |       True       |    33170     |         1          |
|    46547    |       True       |    33202     |         1          |
|    46577    |       True       |    33224     |         3          |
|    46625    |       True       |    33260     |         1          |
|    46652    |       True       |    33281     |         1          |
|    46665    |       True       |    33292     |         4          |
|    46827    |       True       |    33404     |         1          |
|    46850    |       True       |    33417     |         3          |
|    46851    |       True       |    33418     |         1          |
|    46985    |       True       |    33508     |         2          |
|    47030    |       True       |    33541     |         3          |
|    47168    |       True       |    33638     |         2          |
|    47222    |       True       |    33676     |         3          |
|    47232    |       True       |    33683     |         4          |
|    47295    |       True       |    33723     |         3          |
|    47296    |       True       |    33724     |         1          |
|    47341    |       True       |    33753     |         3          |
|    47426    |       True       |    33813     |         5          |
|    47450    |       True       |    33829     |         1          |
|    47482    |       True       |    33854     |         3          |
|    47504    |       True       |    33872     |         2          |
|    47505    |       True       |    33873     |         2          |
|    47582    |       True       |    33936     |         4          |
|    47650    |       True       |    33981     |         2          |
|    47676    |       True       |    34001     |         2          |
|    47853    |       True       |    34128     |         3          |
|    47920    |       True       |    34176     |         1          |
|    47951    |       True       |    34201     |         3          |
|    47990    |       True       |    34231     |         2          |
|    48017    |       True       |    34251     |         2          |
|    48104    |       True       |    34311     |         4          |
|    48164    |       True       |    34357     |         1          |
|    48206    |       True       |    34384     |         3          |
|    48210    |       True       |    34386     |         2          |
|    48257    |       True       |    34423     |         2          |
|    48258    |       True       |    34424     |         1          |
|    48300    |       True       |    34456     |         6          |
|    48335    |       True       |    34480     |         3          |
|    48352    |       True       |    34495     |         1          |
|    48377    |       True       |    34512     |         2          |
|    48392    |       True       |    34521     |         3          |
|    48423    |       True       |    34546     |         1          |
|    48499    |       True       |    34592     |         2          |
|    48506    |       True       |    34597     |         2          |
|    48520    |       True       |    34607     |         2          |
|    48561    |       True       |    34637     |         2          |
|    48568    |       True       |    34642     |         3          |
|    48572    |       True       |    34645     |         3          |
|    48635    |       True       |    34693     |         2          |
|    48652    |       True       |    34703     |         3          |
|    48735    |       True       |    34761     |         1          |
|    48766    |       True       |    34785     |         5          |
|    48789    |       True       |    34800     |         2          |
|    48850    |       True       |    34845     |         2          |
|    48867    |       True       |    34859     |         2          |
|    48929    |       True       |    34900     |         2          |
|    48981    |       True       |    34938     |         2          |
|    49000    |       True       |    34953     |         4          |
|    49037    |       True       |    34980     |         2          |
|    49074    |       True       |    35008     |         4          |
|    49088    |       True       |    35019     |         4          |
|    49133    |       True       |    35052     |         3          |
|    49266    |       True       |    35152     |         1          |
|    49334    |       True       |    35201     |         5          |
|    49358    |       True       |    35215     |         1          |
|    49568    |       True       |    35360     |         6          |
|    49647    |       True       |    35410     |         2          |
|    49652    |       True       |    35414     |         3          |
|    49685    |       True       |    35437     |         6          |
|    49749    |       True       |    35491     |         1          |
|    49786    |       True       |    35516     |         6          |
|    49838    |       True       |    35560     |         3          |
|    49954    |       True       |    35640     |         4          |
+-------------+------------------+--------------+--------------------+
There were 149 slots and the producer produced at the canonical height for 149 slots and has a performance score of 100.00%.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment