Protect against uncle bandits

In your docs it is stated that in order the get protection against uncled blocks, the following check should be included in the smart contratc function or transaction:

require(blockhash(block.number - 1) == expectedParentHash, "block was uncled");

My question is: where do I source the expectedParentHash? How can I implement this correctly? As far as I know, expectedParentHash is not a global variable. How can I make my contract find the right one?

Thanks

2 Likes

can anybody please provide some support???

expectedParentHash is the current head of the chain, the block which you are analyzing, and building upon.

In the case of your tx landing in an uncle block (or a reorg), this check would prevent the tx from being grabbed and then included in the canonical chain.

Uncle blocks aren’t a thing in PoS Ethereum thought, as each slot in an epoch already has a predetermined block proposer. Reorgs however can technically still happen.

cc @brock: does it makes sense to still include this check? :eyes:

I am sorry. I am afraid I did not convey the doubt correctly.

I know how to search for the last block hash (for instance in the block explorer). My question is how to implement that variable in the contract I want to make the frontrunning-safe call to. Obviously, it cannot be hard coded in it, since it first has to be deployed and AFTERWARDS, in a later block, called upon, so the expectedParentHash is unknown at the time of deployment.

It can be an input of the called function, together with all the other parameters. But this method would require to keep an eye on the block explorer, grab the hash as soon as a new block is produced and run to input it in my transaction hoping I still make it on time before a newer block is produced. Is this the case or am I missing something? Can you please provide a sample / guideline of how this has been implemented?

Thank you very much for your replies.

Since the timestamp is now deterministic (a 12-second multiple of the slot), could checking that help?