Skip to content

Commit

Permalink
move delta backend from execution prover to custom api
Browse files Browse the repository at this point in the history
  • Loading branch information
vedhavyas committed Jan 6, 2025
1 parent 7ffa30b commit 9c454dc
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 57 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions crates/sp-domains-fraud-proof/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ include = [

[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.12", default-features = false, features = ["derive"] }
domain-block-builder = { version = "0.1.0", path = "../../domains/client/block-builder", optional = true, default-features = false }
domain-block-preprocessor = { version = "0.1.0", default-features = false, path = "../../domains/client/block-preprocessor", optional = true }
domain-runtime-primitives = { version = "0.1.0", default-features = false, path = "../../domains/primitives/runtime" }
frame-support = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" }
Expand Down Expand Up @@ -41,7 +42,6 @@ trie-db = { version = "0.29.1", default-features = false }
thiserror = { version = "2.0.0", default-features = false }

[dev-dependencies]
domain-block-builder = { version = "0.1.0", path = "../../domains/client/block-builder" }
domain-block-preprocessor = { version = "0.1.0", path = "../../domains/client/block-preprocessor" }
domain-test-service = { version = "0.1.0", path = "../../domains/test/service" }
ethereum = "0.15.0"
Expand All @@ -54,13 +54,11 @@ libsecp256k1 = { version = "0.7.1", features = ["static-context", "hmac"] }
pallet-balances = { git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" }
pallet-ethereum = { git = "https://github.com/autonomys/frontier", rev = "f80f9e2bad338f3bf3854b256b3c4edea23e5968", features = ['default'] }
pallet-evm = { version = "6.0.0-dev", git = "https://github.com/autonomys/frontier", rev = "f80f9e2bad338f3bf3854b256b3c4edea23e5968", default-features = false }
parking_lot = "0.12.2"
rand = { version = "0.8.5", features = ["min_const_gen"] }
rlp = "0.5.2"
sp-core = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" }
sc-cli = { git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305", default-features = false }
sc-service = { git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305", default-features = false }
subspace-test-client = { version = "0.1.0", path = "../../test/subspace-test-client" }
subspace-test-service = { version = "0.1.0", path = "../../test/subspace-test-service" }
subspace-runtime-primitives = { version = "0.1.0", path = "../../crates/subspace-runtime-primitives" }
tempfile = "3.13.0"
Expand All @@ -70,6 +68,7 @@ tokio = "1.40.0"
default = ["std"]
std = [
"codec/std",
"domain-block-builder",
"domain-block-preprocessor",
"domain-runtime-primitives/std",
"frame-support/std",
Expand Down
54 changes: 2 additions & 52 deletions crates/sp-domains-fraud-proof/src/execution_prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
//! (`initialize_block` and `finalize_block`) and any specific extrinsic execution are supported.
use crate::fraud_proof::ExecutionPhase;
use codec::Codec;
use hash_db::{HashDB, Hasher, Prefix};
use domain_block_builder::create_delta_backend;
use hash_db::HashDB;
use sc_client_api::backend::Backend;
use sp_api::StorageProof;
use sp_core::traits::CodeExecutor;
use sp_runtime::traits::{Block as BlockT, HashingFor};
use sp_state_machine::backend::AsTrieBackend;
use sp_state_machine::{TrieBackend, TrieBackendBuilder, TrieBackendStorage};
use sp_trie::DBValue;
use std::marker::PhantomData;
use std::sync::Arc;
Expand Down Expand Up @@ -87,52 +86,3 @@ where
}
}
}

/// Create a new trie backend with memory DB delta changes.
///
/// This can be used to verify any extrinsic-specific execution on the combined state of `backend`
/// and `delta`.
fn create_delta_backend<'a, S, H, DB>(
backend: &'a TrieBackend<S, H>,
delta: DB,
post_delta_root: H::Out,
) -> TrieBackend<DeltaBackend<'a, S, H, DB>, H>
where
S: 'a + TrieBackendStorage<H>,
H: 'a + Hasher,
H::Out: Codec,
DB: HashDB<H, DBValue>,
{
let essence = backend.essence();
let delta_backend = DeltaBackend {
backend: essence.backend_storage(),
delta,
_phantom: PhantomData::<H>,
};
TrieBackendBuilder::new(delta_backend, post_delta_root).build()
}

struct DeltaBackend<'a, S, H, DB>
where
S: 'a + TrieBackendStorage<H>,
H: 'a + Hasher,
DB: HashDB<H, DBValue>,
{
backend: &'a S,
delta: DB,
_phantom: PhantomData<H>,
}

impl<'a, S, H, DB> TrieBackendStorage<H> for DeltaBackend<'a, S, H, DB>
where
S: 'a + TrieBackendStorage<H>,
H: 'a + Hasher,
DB: HashDB<H, DBValue>,
{
fn get(&self, key: &H::Out, prefix: Prefix) -> Result<Option<DBValue>, String> {
match HashDB::get(&self.delta, key, prefix) {
Some(v) => Ok(Some(v)),
None => Ok(self.backend.get(key, prefix)?),
}
}
}
1 change: 1 addition & 0 deletions domains/client/block-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.12", features = ["derive"] }
hash-db = { version = "0.16.0", default-features = false }
sc-client-api = { git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" }
sp-api = { git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" }
sp-blockchain = { git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" }
Expand Down
57 changes: 57 additions & 0 deletions domains/client/block-builder/src/custom_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! Custom API that is efficient to collect storage roots.
use codec::Codec;
use hash_db::{HashDB, Hasher, Prefix};
use sp_state_machine::{DBValue, TrieBackend, TrieBackendBuilder, TrieBackendStorage};
use std::marker::PhantomData;

/// Create a new trie backend with memory DB delta changes.
///
/// This can be used to verify any extrinsic-specific execution on the combined state of `backend`
/// and `delta`.
pub fn create_delta_backend<'a, S, H, DB>(
backend: &'a TrieBackend<S, H>,
delta: DB,
post_delta_root: H::Out,
) -> TrieBackend<DeltaBackend<'a, S, H, DB>, H>
where
S: 'a + TrieBackendStorage<H>,
H: 'a + Hasher,
H::Out: Codec,
DB: HashDB<H, DBValue>,
{
let essence = backend.essence();
let delta_backend = DeltaBackend {
backend: essence.backend_storage(),
delta,
_phantom: PhantomData::<H>,
};
TrieBackendBuilder::new(delta_backend, post_delta_root).build()
}

/// DeltaBackend provides the TrieBackend using main backend and some delta changes
/// that are not part of the main backend.
pub struct DeltaBackend<'a, S, H, DB>
where
S: 'a + TrieBackendStorage<H>,
H: 'a + Hasher,
DB: HashDB<H, DBValue>,
{
backend: &'a S,
delta: DB,
_phantom: PhantomData<H>,
}

impl<'a, S, H, DB> TrieBackendStorage<H> for DeltaBackend<'a, S, H, DB>
where
S: 'a + TrieBackendStorage<H>,
H: 'a + Hasher,
DB: HashDB<H, DBValue>,
{
fn get(&self, key: &H::Out, prefix: Prefix) -> Result<Option<DBValue>, String> {
match HashDB::get(&self.delta, key, prefix) {
Some(v) => Ok(Some(v)),
None => Ok(self.backend.get(key, prefix)?),
}
}
}
3 changes: 3 additions & 0 deletions domains/client/block-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
#![warn(missing_docs)]

mod custom_api;

use codec::Encode;
pub use custom_api::{create_delta_backend, DeltaBackend};
use sc_client_api::backend;
use sp_api::{
ApiExt, ApiRef, Core, ProvideRuntimeApi, StorageChanges, StorageProof, TransactionOutcome,
Expand Down

0 comments on commit 9c454dc

Please sign in to comment.