Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pallet-MMR should use custom block_hash getter instead of using frame_system::block_hash for non-canonicalised blocks #4062

Closed
2 tasks done
vedhavyas opened this issue Apr 10, 2024 · 4 comments · Fixed by #4080
Labels
I5-enhancement An additional feature request. I10-unconfirmed Issue might be valid, but it's not yet known.

Comments

@vedhavyas
Copy link
Contributor

Is there an existing issue?

  • I have searched the existing issues

Experiencing problems? Have you tried our Stack Exchange first?

  • This is not a support question.

Motivation

Currently pallet_mmr uses frame_system::block_hash to get the hash of a given block number while constructing the non-canonicalised keys. This works fine with Polkadot and other blockchains where finality is quicker. Currently, polkadot stores 4096 number of block hashes on the runtime and before the block_hashes are pruned, the blocks are already finalized.

At subspace, we do not have a quick finality and we cannot put a static number at which we can gaurantee block finalization happens. With this context, when an MMR proof is being verified, if the particular block is not canonicalised yet, it will look at frame_system::block_hashes but by then our runtime already prunes the block hash due to frame_system::Config::BlockHashCount and the storage returns an Default value for the Hash since the frame_system::block_hash is a ValueQuery. With the default value given, the MMR proof verification fails on our end.

Request

pallet_mmr should ideally define a custom getter to fetch the block_hash instead of directly using the frame_system. This will provide the projects using the pallet_mmr can impl the custom getter to fetch the said block_hash.

For polkadot and likes, the impl will just wrap frame_system::block_hashes function and should not change anything while Subspace and other project can impl custom logic to fetch the correct block_hash for a given non-canonicalised block number.

Solution

Solution would be introduce a new type for pallet_mmr::Config that can fetch block_hash for a given block_number

Are you willing to help with this request?

Yes!

@vedhavyas vedhavyas added the I5-enhancement An additional feature request. label Apr 10, 2024
@github-actions github-actions bot added the I10-unconfirmed Issue might be valid, but it's not yet known. label Apr 10, 2024
@acatangiu
Copy link
Contributor

IIUC this

let ancestor_parent_hash = <frame_system::Pallet<T>>::block_hash(ancestor_parent_block_num);
is what is causing problems for you.

So instead of <frame_system::Pallet<T>>::block_hash (mapping block-number -> block-hash on current fork), I am guessing you will use some other on-chain map block_number -> some-unique-identifier that runs deeper than 4096, right?

Solution would be introduce a new type for pallet_mmr::Config that can fetch block_hash for a given block_number

Sounds ok to me. I would not call it block_hash anymore, maybe block_uid or something to avoid confusions since it can be something else than the actual "block hash".

@vedhavyas
Copy link
Contributor Author

So instead of <frame_system::Pallet>::block_hash (mapping block-number -> block-hash on current fork), I am guessing you will use some other on-chain map block_number -> some-unique-identifier that runs deeper than 4096, right?

Yes, this is causing the issue.
But instead of block_number -> some-unique-identifier we would still be using block-number -> block-hash but instead of using frame_system::block_hash we would like to provide a custom trait to config that fetches this map so that we can plug in different way of fetching block_hash instead of directly from frame_system. For polkadot or other chains, this trait will still use frame_system underneath.

I hope this makes sense

@acatangiu
Copy link
Contributor

acatangiu commented Apr 10, 2024

Ok, sounds good to me. I can help review the PR and push it through once you post it.

@vedhavyas
Copy link
Contributor Author

@acatangiu PR for the above proposed solution path is here - #4080

github-merge-queue bot pushed a commit that referenced this issue Apr 12, 2024
This PR introduces `BlockHashProvider` into `pallet_mmr::Config`
This type is used to get `block_hash` for a given `block_number` rather
than directly using `frame_system::Pallet::block_hash`

The `DefaultBlockHashProvider` uses `frame_system::Pallet::block_hash`
to get the `block_hash`

Closes: #4062
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I5-enhancement An additional feature request. I10-unconfirmed Issue might be valid, but it's not yet known.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants