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

Support updating the trie changes directly into the database #5862

Open
liuchengxu opened this issue Sep 29, 2024 · 0 comments
Open

Support updating the trie changes directly into the database #5862

liuchengxu opened this issue Sep 29, 2024 · 0 comments

Comments

@liuchengxu
Copy link
Contributor

Part of #5053 (comment)

The idea is to support updating the trie with given storage directly into the underlying database instead of collecting the db_updates via PrefixedMemoryDB and then applied into the database. Such that we can avoid the potential OOM issue when dealing with a large state import.

Once this feature is implemented, #4 can be resolved easily by forwarding the storage key/values to the db whenever a piece of the downloaded state is received from the peer instead of keeping them in the memory until the completion of entire state download.

Implementation plan

  1. Create TrieCommitter by adapting Ephermal, specifically, replace overlay which is PrefixedMemoryDB with the underlying database. And then the new keys will be directly written to the DB.

pub(crate) struct Ephemeral<'a, S: 'a + TrieBackendStorage<H>, H: 'a + Hasher> {
storage: &'a S,
overlay: &'a mut PrefixedMemoryDB<H>,
}

  1. Add new API commit_trie_changes() in Backend trait

  2. Replace reset_storage with the new API commit_trie_changes().

    let state_root = operation.op.reset_storage(storage, state_version)?;

I have made a PoC based on the above flow, you can find it here. I added a basic test for the new API commit_trie_changes() https://github.com/liuchengxu/polkadot-sdk/blob/09c2b4d6f35ebd9f42917813357d57075123f222/substrate/client/db/src/lib.rs#L4768, it passes, which means applying the state changes using new API seems to work, but simplying replacing reset_storage with commit_trie_changes() runs into the error IncompleteDatabase when conducting a fast sync test locally.

@bkchr Please validate the implementation plan above and I'd appreciate if some expert could help look into the IncompleteDatabase error, I have no idea why merely inserting new keys would result in this error. Just pull https://github.com/liuchengxu/polkadot-sdk/tree/trie-committer and starting a local fast sync test should reproduce it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant