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

Set StateBackend::Transaction to PrefixedMemoryDB #14612

Merged
merged 16 commits into from
Aug 17, 2023

Conversation

bkchr
Copy link
Member

@bkchr bkchr commented Jul 21, 2023

The main change this pull request doing is the removal of Transaction associated type from sp_state_machine::Backend. The transaction is then fixed to PrefixedMemoryDB. Basically PrefixedMemoryDB was always the transaction type being used in Substrate. This removes a lot of generics which simplifies the code in a lot of places. Another important change this pull request is doing is to move the so called "storage transaction cache" into the OverlayedChanges. There is no real need to have this cache out of the OverlayedChanges and thus, we can also simplify some things here and there.

This is works towards: paritytech/polkadot-sdk#27

Downstream Code changes

  • Everywhere where things like TransactionFor or StateBackend::Transaction was used in where bounds or similar can just be removed to make the code compile again.
  • BlockImportParams was also changed to only take one generic argument, the block type and not anymore the transaction type as the second generic parameter.
  • DefaultImportQueue is also only taking one generic argument after this pr, the block type.

polkadot companion: paritytech/polkadot#7536
cumulus companion: paritytech/cumulus#2923

@bkchr bkchr added A0-please_review Pull request needs code review. C1-low PR touches the given topic and has a low impact on builders. B1-note_worthy Changes should be noted in the release notes T0-node This PR/Issue is related to the topic “node”. labels Jul 21, 2023
@bkchr bkchr requested a review from a team July 21, 2023 21:46
bkchr added a commit to paritytech/cumulus that referenced this pull request Jul 21, 2023
}
}

impl<H: Hasher> Clone for OverlayedChanges<H> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't this be derived?

Copy link
Member

@davxy davxy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome cleanup

@@ -142,7 +188,7 @@ pub struct StorageChanges<Transaction, H: Hasher> {
/// [`main_storage_changes`](StorageChanges::main_storage_changes) and from
/// [`child_storage_changes`](StorageChanges::child_storage_changes).
/// [`offchain_storage_changes`](StorageChanges::offchain_storage_changes).
pub transaction: Transaction,
pub transaction: PrefixedMemoryDB<H>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit. Replace PrefixedMemoryDB<H> in the overall file with the alias BackendTransaction<H> (import crate::backend::BackendTransaction).

I know it's just an alias, but for sure is more explicit about the usage intent

/// Storage transactions are calculated as part of the `storage_root`.
/// These transactions can be reused for importing the block into the
/// storage. So, we cache them to not require a recomputation of those transactions.
pub struct StorageTransactionCache<Transaction, H: Hasher> {
pub struct StorageTransactionCache<H: Hasher> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub struct StorageTransactionCache<H: Hasher> {
struct StorageTransactionCache<H: Hasher> {

If only used here maybe we can keep it private?

primitives/state-machine/src/overlayed_changes/mod.rs Outdated Show resolved Hide resolved
primitives/state-machine/src/overlayed_changes/mod.rs Outdated Show resolved Hide resolved
@@ -176,7 +177,7 @@ mod execution {
pub type DefaultHandler<E> = fn(CallResult<E>, CallResult<E>) -> CallResult<E>;

/// Trie backend with in-memory storage.
pub type InMemoryBackend<H> = TrieBackend<MemoryDB<H>, H>;
pub type InMemoryBackend<H> = TrieBackend<PrefixedMemoryDB<H>, H>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really related to this PR, but since there is some overhaul in progress...
Can't we define the InMemoryBackend in in_memory_backend.rs (maybe as a newtype) and replace the function new_in_mem with a new constructor in the impl block for that newtype?

@davxy davxy requested a review from a team August 4, 2023 12:35
@bkchr bkchr added the D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit label Aug 16, 2023
@bkchr bkchr requested review from a team August 16, 2023 13:04
@bkchr
Copy link
Member Author

bkchr commented Aug 17, 2023

bot merge

@paritytech-processbot paritytech-processbot bot merged commit ecd503d into master Aug 17, 2023
4 checks passed
@paritytech-processbot paritytech-processbot bot deleted the bkchr-rework-storage-transaction-cache branch August 17, 2023 10:49
paritytech-processbot bot pushed a commit to paritytech/cumulus that referenced this pull request Aug 17, 2023
* Companion for Substrate#14612

paritytech/substrate#14612

* Remove patch

* Cargo.lock

* Fix

* Fix compilation

* Fix Fix

* ...

* :face_palm:

* .................

* update lockfile for {"polkadot", "substrate"}

* ".git/.scripts/commands/fmt/fmt.sh"

---------

Co-authored-by: parity-processbot <>
Ank4n pushed a commit that referenced this pull request Aug 20, 2023
* Yep

* Try to get it working everywhere

* Make `from_raw_storage` start with an empty db

* More fixes!

* Make everything compile

* Fix `child_storage_root`

* Fix after merge

* Cleanups

* Update primitives/state-machine/src/overlayed_changes/mod.rs

Co-authored-by: Davide Galassi <davxy@datawok.net>

* Review comments

* Fix issues

* Silence warning

* FMT

* Clippy

---------

Co-authored-by: Davide Galassi <davxy@datawok.net>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B1-note_worthy Changes should be noted in the release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit T0-node This PR/Issue is related to the topic “node”.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants