0

You can get consensus related values such prevrandao and validator details with eth_getBlockByNumber or eth_getBlockByHash, but how can you get this data for many blocks without making thousands of RPC calls?

For instance eth_getLogs lets you specify a filter, allowing it to return logs for many transactions, but doesn't have the type of data I'm looking for.

I understand there's things like The Graph, but I'm trying to first find a way to do this with a JSON RPC call (assuming the former even has this sort of data).

1 Answer 1

0

The Ethereum JSON RPC API does not provide a native method/s to retrieve consensus layer data for multiple blocks in a single call. However, there are a few strategies you can employ to mitigate the problem:

  1. Batch Requests: Many Ethereum clients support batched JSON RPC requests e.g. Infura and Alchemy. This means you can send multiple RPC calls as a single HTTP request. Even though this doesn't reduce the number of RPC calls, it can reduce the HTTP overhead and speed up the data fetching process.

    Example using JSON:

    [
       {"jsonrpc":"2.0", "id":1, "method":"eth_getBlockByNumber", "params":["0x1", true]},
       {"jsonrpc":"2.0", "id":2, "method":"eth_getBlockByNumber", "params":["0x2", true]}
    ]
    

    While the above example only fetches two blocks, you can extend it to retrieve as many blocks as needed. Also, you can use ethers to make batch requests as explained here

  2. The Graph: While you mentioned wanting a direct JSON RPC solution, it's worth noting that third-party services like The Graph can be incredibly helpful for this sort of task. They provide flexible querying capabilities using GraphQL and often store data in more query-optimized ways. For your specific data requirements you may want to take a look at the Ethereum Blocks Subgraph by Blocklytics

3
  • There seems to be some limitations with batch requests, so still experimenting with it to see if it can do what I need. As for The Graph, I can't seem to find a way to get a block's prevrandao (usually under the alias mixHash) value with it. I've tried mixHash, prevrandao, randao and variations on these, but none are working. Nothing's coming up when looking through the docs or googling either. Do you know if this value is available via The Graph?
    – user72364
    Sep 18 at 10:33
  • @user72364 you can specify the difficulty field in your gql query to the subgraph which should correspond with the prevrandao
    – MShakeG
    Sep 18 at 11:29
  • Unfortunately since the merge, difficulty returns 0. This can be confirmed by running the query you linked, which already contains difficulty. I've seen dev comments about it being an alias for prevrandao, but most implements don't actually map to it.
    – user72364
    Sep 18 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.