ethglobal / nfthack-support

Issue tracker for NFTHack support
5 stars 2 forks source link

How to prove a given blockhash is valid #4

Open auryn-macmillan opened 3 years ago

auryn-macmillan commented 3 years ago

I have a public function with a parameter bytes32 _blockhash.

How can I prove that the given blockhash is a valid part of the chain that lead to the current blockhash? (I've been told that this should be possible via a merkle proof, but I'm not familiar with how to do this)

jzaki commented 3 years ago

@auryn-macmillan Are you still looking into this? Please ping me in the #hack-support discord channel :)

MariusVanDerWijden commented 3 years ago

@auryn-macmillan @jzaki Have you worked this out? You can check the last 256 blockhashes in the EVM with the BLOCKHASH opcode. Longer than 256 blocks into the past is not possible. You can only be certain that a blockhash is part of the valid chain if you submit all blockheaders between the blockhash to prove and the current head, which is extremely costly. If your issue is solved, please close it :)

jzaki commented 3 years ago

In a search at the time, I came across EIPs 210 and 1218, but did not get to verify implementation, or find an existing library that makes use of them.

EIP210 "This removes the need for implementations to have an explicit way to look into historical block hashes, simplifying the protocol definition and removing a large component of the “implied state” (information that is technically state but is not part of the state tree) and thereby making the protocol more “pure”. Additionally, it allows blocks to directly point to blocks far behind them, which enables extremely efficient and secure light client protocols."

EIP1218 "This EIP ensures that it's possible to prove the hash of block N to the chain at height N2 with ~log2(N2 - N) - 8 Merkle branches. It should not be too hard to use existing libraries to write a utility contract and library that produces and verifies these proofs."

tim-becker commented 1 year ago

I want to share one scalable solution to accessing historical block hashes on-chain that we’re using in Relic Protocol. We store Merkle roots of chunks of historical block hashes in storage, and use zk-SNARKs to prove their validity.

For reference, see https://github.com/Relic-Protocol/relic-contracts/blob/2ecb2ffdd3a450a8eb7c352628c2ef51ed038c42/contracts/BlockHistory.sol

This is already deployed on mainnet, and we'll be releasing a developer SDK for integration shortly.