6

Is there a simple way to find out if a given block is finalized in PoS Ethereum?

Currently, I'm calling the endpoint ../eth/v1/beacon/states/finalized/finality_checkpoints of the Beacon Node and it delivers me the number of latest finalized Epoch.

After that, I'm using the returned Epoch number to get all blocks using an endpoint provided by beaconcha.in https://beaconcha.in/api/v1/epoch/<epoch-number>/blocks which lists all the Blocks in that Epoch

And the last step I do is check if the number of my block is smaller than the lowest blocknumber in the Epoch.

I'm looking for a simpler way to get that information using a library or the Beacon Node or execution Node or some API providers like Etherscan, Infura, beaconcha.in, etc...

Thanks

3 Answers 3

4

I have found another way using the latest version of Web3js 1.8.0. They added a new BlockTag that is called finalized.

var Web3 = require('web3');

async function main() {
    const blockNumberToCheck = 15625164;
    const web3 = new Web3("https://mainnet.infura.io/v3/<api-key>");
    const numberOfLatestFinalizedBlock = await web3.eth.getBlock("finalized");
    // console.log(numberOfLatestFinalizedBlock);
    if (blockNumberToCheck <= numberOfLatestFinalizedBlock.number) {
      console.log(`Block ${blockNumberToCheck} is Finalized`)
      return true
    } else {
      console.log(`Block ${blockNumberToCheck} is NOT Finalized`)
      return false
    }
  
  }
  
  // We recommend this pattern to be able to use async/await everywhere
  // and properly handle errors.
  main().catch((error) => {
    console.error(error);
    process.exitCode = 1;
  });
  
1
  • If there are other ways then please add them in new answer
    – Majd TL
    Commented Sep 27, 2022 at 13:57
3

Just for reference to other people, if they are looking for cuRL requests, they can do it like so using Alchemy endpoints:

curl https://eth-mainnet.g.alchemy.com/v2/<apiKey> -k \
-X POST \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method": "eth_getBlockByNumber", "params": ["finalized", false], "id": 0}' 
1
  • Also through Erigon and thus Geth as well (almost certainly) Commented May 6 at 13:48
0

what about geth clique? i see "finalized" just on proof of stake butt on proof of authority is not found or im wrong configuration?

curl http://IP_ADDR:8545 \
  -X POST \
  -H "Content-Type: application/json" \
  --data '{"method":"eth_getBlockByNumber","params":["finalized",false],"id":1,"jsonrpc":"2.0"}' | jq

Output:

{
  "jsonrpc": "2.0",
  "id": 1,
  "error": {
    "code": -32000,
    "message": "finalized block not found"
  }
}
New contributor
Joy Wibowo is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • Which version of Geth Clique? You might ask this as a new question. I'm not sure about the rules in PoA, but I think finality there is instant. Finality differs in PoS, PoW, and PoA. In PoS, votes are counted and a predefined percentage in the protocol decides the finality of a block. In PoW, each person decides finality based on the block confirmations they prefer. In PoA, if you trust the authority, then a transaction is instantly final once added. What I mean is: there is no hard concept of finality in PoW and PoA implemented in the protocol, and therefore probably no method or need for it.
    – Majd TL
    Commented 2 days ago

Your Answer

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

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