Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

pallet-mmr: improve offchain storage, relax LeafData requirements #11799

Closed
acatangiu opened this issue Jul 7, 2022 · 6 comments
Closed

pallet-mmr: improve offchain storage, relax LeafData requirements #11799

acatangiu opened this issue Jul 7, 2022 · 6 comments
Assignees
Labels
J0-enhancement An additional feature request. U2-some_time_soon Issue is worth doing soon. Z3-substantial Can be fixed by an experienced coder with a working knowledge of the codebase.

Comments

@acatangiu
Copy link
Contributor

Issue

PR #11594 introduced fork-resistant offchain storage for MMR leaves.

Leaves are now saved offchain using (parent_hash, pos)-based key to be fork-resistant and later canonicalized to pos-based key.

Relying on (parent_hash, pos)-based keys means that conflicts could now only happen on 1-block deep forks, where two forks with identical line of ancestors compete to write at the same offchain key. This introduced a requirement on the LeafData to only reference data coming from ancestor blocks, so in the corner case above, the conflicting writes end up writing same values anyway.

While the current behavior works for polkadot/kusama BEEFY+MMR deployment, I think we can improve and eliminate the LeafData requirement shown above.

Proposed solution

Leaves are added to the MMR offchain storage by runtime during pallet_mmr::<Pallet as Hooks>::on_initialize(BlockNumber) here using indexing API. At that time block_hash is unavailable (block is still under construction) so we're using parent_hash-based key instead to uniquely identify this leaf in offchain db (only it's not unique for 1-block deep forks, ergo the LeafData limitation).

Idea is to drop using the indexing API and instead store the new leaf in a placeholder Pallet::NewLeaf runtime storage item, and have the offchain worker function move/copy the leaf to offchain db after the block is built and truly unique (block_hash, pos) is available to be used as offchain key.

The same Pallet::NewLeaf storage is reused for temporarily holding the new leaf at every block.

@acatangiu acatangiu added J0-enhancement An additional feature request. U4-some_day_maybe Issue might be worth doing eventually. Z6-mentor An easy task where a mentor is available. Please indicate in the issue who the mentor could be. Z1-easy Can be fixed primarily by duplicating and adapting code by an intermediate coder labels Jul 7, 2022
@acatangiu acatangiu added Z2-medium Can be fixed by a coder with good Rust knowledge but little knowledge of the codebase. and removed Z1-easy Can be fixed primarily by duplicating and adapting code by an intermediate coder labels Jul 14, 2022
@acatangiu acatangiu added U2-some_time_soon Issue is worth doing soon. and removed U4-some_day_maybe Issue might be worth doing eventually. labels Sep 23, 2022
@seunlanlege
Copy link
Contributor

Will pick this up on monday

@acatangiu
Copy link
Contributor Author

Doing this will normally require an offchain database migration for any runtime deploying #11594 (Rococo runtime >= 9260).

Right now though, pallet-mmr is only deployed on Rococo which is still running runtime version 9250; if we get this change in soon, then we might get away upgrading directly from 9250 without needing database migration.

@iTranscend
Copy link

Hi, I've gone ahead to make the changes suggested in the Issue spec.

I haven't updated the tests yet, but I'll do that over the next few days.

I've opened a draft PR here.

@acatangiu
Copy link
Contributor Author

Hi, I've gone ahead to make the changes suggested in the Issue spec.

I haven't updated the tests yet, but I'll do that over the next few days.

I've opened a draft PR here.

Do you need any help here?

@iTranscend
Copy link

Hi @acatangiu.
I've started working on the changes you suggested but I don't think I'll be able to finish them up as quick as they are needed.
I'd love it if someone else can pick up from where I stopped so we can get away with upgrading directly from 9250 without needing a database migration.

@acatangiu acatangiu self-assigned this Oct 6, 2022
@acatangiu acatangiu added Z3-substantial Can be fixed by an experienced coder with a working knowledge of the codebase. and removed Z6-mentor An easy task where a mentor is available. Please indicate in the issue who the mentor could be. Z2-medium Can be fixed by a coder with good Rust knowledge but little knowledge of the codebase. labels Oct 6, 2022
@acatangiu
Copy link
Contributor Author

Unfortunately offchain_worker is not reliable enough to allow for this.. see #12446 (comment)

Closing the issue as "Not possible".

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
J0-enhancement An additional feature request. U2-some_time_soon Issue is worth doing soon. Z3-substantial Can be fixed by an experienced coder with a working knowledge of the codebase.
Projects
No open projects
Status: Done
3 participants