Skip to main content

domain_block_builder/
custom_api.rs

1//! Custom API that is efficient to collect storage roots.
2
3use hash_db::{HashDB, Hasher, Prefix};
4use parity_scale_codec::{Codec, Decode, Encode};
5use sc_client_api::{ExecutorProvider, StateBackend, backend};
6use sp_core::offchain::OffchainOverlayedChange;
7use sp_core::traits::{CallContext, CodeExecutor, FetchRuntimeCode};
8use sp_inherents::InherentData;
9use sp_runtime::traits::{Block as BlockT, HashingFor, NumberFor};
10use sp_runtime::{ApplyExtrinsicResult, ExtrinsicInclusionMode, TransactionOutcome};
11use sp_state_machine::backend::AsTrieBackend;
12use sp_state_machine::{
13    BackendTransaction, DBValue, IndexOperation, OverlayedChanges, StateMachine, StorageChanges,
14    StorageKey, StorageValue, TrieBackend, TrieBackendBuilder, TrieBackendStorage,
15};
16use std::borrow::Cow;
17use std::collections::HashMap;
18use std::marker::PhantomData;
19use std::mem;
20use std::sync::Arc;
21use subspace_runtime_primitives::ExtrinsicFor;
22
23type TrieBackendStorageFor<State, Block> =
24    <State as StateBackend<HashingFor<Block>>>::TrieBackendStorage;
25
26pub(crate) type TrieDeltaBackendFor<'a, State, Block> = TrieBackend<
27    DeltaBackend<'a, TrieBackendStorageFor<State, Block>, HashingFor<Block>>,
28    HashingFor<Block>,
29>;
30
31struct MappedStorageChanges<H: Hasher> {
32    /// All changes to the main storage.
33    ///
34    /// A value of `None` means that it was deleted.
35    pub main_storage_changes: HashMap<StorageKey, Option<StorageValue>>,
36    /// All changes to the child storages.
37    pub child_storage_changes: HashMap<StorageKey, HashMap<StorageKey, Option<StorageValue>>>,
38    /// Offchain state changes to write to the offchain database.
39    pub offchain_storage_changes: HashMap<(Vec<u8>, Vec<u8>), OffchainOverlayedChange>,
40    /// A transaction for the backend that contains all changes from
41    /// [`main_storage_changes`](StorageChanges::main_storage_changes) and from
42    /// [`child_storage_changes`](StorageChanges::child_storage_changes) but excluding
43    /// [`offchain_storage_changes`](StorageChanges::offchain_storage_changes).
44    pub transaction: BackendTransaction<H>,
45    /// The storage root after applying the transaction.
46    pub transaction_storage_root: H::Out,
47    /// Changes to the transaction index,
48    pub transaction_index_changes: Vec<IndexOperation>,
49}
50
51#[derive(Clone)]
52struct RuntimeCode {
53    code: Vec<u8>,
54    heap_pages: Option<u64>,
55    hash: Vec<u8>,
56}
57
58impl<H: Hasher> From<MappedStorageChanges<H>> for StorageChanges<H> {
59    fn from(value: MappedStorageChanges<H>) -> Self {
60        let MappedStorageChanges {
61            main_storage_changes,
62            child_storage_changes,
63            offchain_storage_changes,
64            transaction,
65            transaction_storage_root,
66            transaction_index_changes,
67        } = value;
68        StorageChanges {
69            main_storage_changes: main_storage_changes.into_iter().collect(),
70            child_storage_changes: child_storage_changes
71                .into_iter()
72                .map(|(k, v)| (k, v.into_iter().collect()))
73                .collect(),
74            offchain_storage_changes: offchain_storage_changes.into_iter().collect(),
75            transaction,
76            transaction_storage_root,
77            transaction_index_changes,
78        }
79    }
80}
81
82impl<H: Hasher> From<StorageChanges<H>> for MappedStorageChanges<H> {
83    fn from(value: StorageChanges<H>) -> Self {
84        let StorageChanges {
85            main_storage_changes,
86            child_storage_changes,
87            offchain_storage_changes,
88            transaction,
89            transaction_storage_root,
90            transaction_index_changes,
91        } = value;
92        MappedStorageChanges {
93            main_storage_changes: main_storage_changes.into_iter().collect(),
94            child_storage_changes: child_storage_changes
95                .into_iter()
96                .map(|(k, v)| (k, v.into_iter().collect()))
97                .collect(),
98            offchain_storage_changes: offchain_storage_changes.into_iter().collect(),
99            transaction,
100            transaction_storage_root,
101            transaction_index_changes,
102        }
103    }
104}
105
106impl<H: Hasher> MappedStorageChanges<H> {
107    fn consolidate_storage_changes(&mut self, changes: StorageChanges<H>) -> H::Out {
108        let StorageChanges {
109            main_storage_changes,
110            child_storage_changes,
111            offchain_storage_changes,
112            transaction,
113            transaction_storage_root,
114            mut transaction_index_changes,
115        } = changes;
116        self.main_storage_changes.extend(main_storage_changes);
117        child_storage_changes
118            .into_iter()
119            .for_each(|(k, v)| self.child_storage_changes.entry(k).or_default().extend(v));
120        self.offchain_storage_changes
121            .extend(offchain_storage_changes);
122        self.transaction.consolidate(transaction);
123        self.transaction.purge();
124        self.transaction_index_changes
125            .append(&mut transaction_index_changes);
126        let previous_storage_root = self.transaction_storage_root;
127        self.transaction_storage_root = transaction_storage_root;
128        previous_storage_root
129    }
130}
131
132/// Storage changes are the collected throughout the execution.
133pub struct CollectedStorageChanges<H: Hasher> {
134    /// Storage changes that are captured during the execution.
135    /// Can be used for block import.
136    pub storage_changes: StorageChanges<H>,
137    /// Intermediate storage roots in between the execution.
138    pub intermediate_roots: Vec<H::Out>,
139}
140
141/// Create a new trie backend with memory DB delta changes.
142///
143/// This can be used to verify any extrinsic-specific execution on the combined state of `backend`
144/// and `delta`.
145pub fn create_delta_backend<'a, S, H>(
146    backend: &'a TrieBackend<S, H>,
147    delta: &'a BackendTransaction<H>,
148    post_delta_root: H::Out,
149) -> TrieBackend<DeltaBackend<'a, S, H>, H>
150where
151    S: 'a + TrieBackendStorage<H>,
152    H: 'a + Hasher,
153    H::Out: Codec,
154{
155    create_delta_backend_with_maybe_delta(backend, Some(delta), post_delta_root)
156}
157
158fn create_delta_backend_with_maybe_delta<'a, S, H>(
159    backend: &'a TrieBackend<S, H>,
160    maybe_delta: Option<&'a BackendTransaction<H>>,
161    post_delta_root: H::Out,
162) -> TrieBackend<DeltaBackend<'a, S, H>, H>
163where
164    S: 'a + TrieBackendStorage<H>,
165    H: 'a + Hasher,
166    H::Out: Codec,
167{
168    let essence = backend.essence();
169    let delta_backend = DeltaBackend {
170        backend: essence.backend_storage(),
171        delta: maybe_delta,
172        _phantom: PhantomData::<H>,
173    };
174    TrieBackendBuilder::new(delta_backend, post_delta_root).build()
175}
176
177/// DeltaBackend provides the TrieBackend using main backend and some delta changes
178/// that are not part of the main backend.
179pub struct DeltaBackend<'a, S, H>
180where
181    S: 'a + TrieBackendStorage<H>,
182    H: 'a + Hasher,
183{
184    backend: &'a S,
185    delta: Option<&'a BackendTransaction<H>>,
186    _phantom: PhantomData<H>,
187}
188
189impl<'a, S, H> TrieBackendStorage<H> for DeltaBackend<'a, S, H>
190where
191    S: 'a + TrieBackendStorage<H>,
192    H: 'a + Hasher,
193{
194    fn get(&self, key: &H::Out, prefix: Prefix) -> Result<Option<DBValue>, String> {
195        if let Some(db) = &self.delta
196            && let Some(v) = HashDB::get(*db, key, prefix)
197        {
198            Ok(Some(v))
199        } else {
200            Ok(self.backend.get(key, prefix)?)
201        }
202    }
203}
204
205pub(crate) struct TrieBackendApi<Client, Block: BlockT, Backend: backend::Backend<Block>, Exec> {
206    parent_hash: Block::Hash,
207    parent_number: NumberFor<Block>,
208    client: Arc<Client>,
209    state: Backend::State,
210    executor: Arc<Exec>,
211    maybe_storage_changes: Option<MappedStorageChanges<HashingFor<Block>>>,
212    intermediate_roots: Vec<Block::Hash>,
213    runtime_code: RuntimeCode,
214}
215
216impl<Client, Block, Backend, Exec> FetchRuntimeCode for TrieBackendApi<Client, Block, Backend, Exec>
217where
218    Block: BlockT,
219    Backend: backend::Backend<Block>,
220{
221    fn fetch_runtime_code(&self) -> Option<Cow<'_, [u8]>> {
222        Some(Cow::from(&self.runtime_code.code))
223    }
224}
225
226impl<Client, Block, Backend, Exec> TrieBackendApi<Client, Block, Backend, Exec>
227where
228    Block: BlockT,
229    Backend: backend::Backend<Block>,
230    Client: ExecutorProvider<Block>,
231    Exec: CodeExecutor,
232{
233    pub(crate) fn new(
234        parent_hash: Block::Hash,
235        parent_number: NumberFor<Block>,
236        client: Arc<Client>,
237        backend: Arc<Backend>,
238        executor: Arc<Exec>,
239    ) -> Result<Self, sp_blockchain::Error> {
240        // Trusted: bounded block-building context
241        let state = backend.state_at(parent_hash, sc_client_api::TrieCacheContext::Trusted)?;
242        let trie_backend = state.as_trie_backend();
243        let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(
244            trie_backend,
245            sp_state_machine::backend::TryPendingCode::Yes,
246        );
247        let sp_core::traits::RuntimeCode {
248            code_fetcher,
249            heap_pages,
250            hash,
251        } = state_runtime_code
252            .runtime_code()
253            .map_err(sp_blockchain::Error::RuntimeCode)?;
254        let runtime_code = RuntimeCode {
255            code: code_fetcher
256                .fetch_runtime_code()
257                .map(|c| c.to_vec())
258                .ok_or(sp_blockchain::Error::RuntimeCode("missing runtime code"))?,
259            heap_pages,
260            hash,
261        };
262        Ok(Self {
263            parent_hash,
264            parent_number,
265            client,
266            state,
267            executor,
268            maybe_storage_changes: None,
269            intermediate_roots: vec![],
270            runtime_code,
271        })
272    }
273
274    fn runtime_code(&self) -> sp_core::traits::RuntimeCode<'_> {
275        sp_core::traits::RuntimeCode {
276            code_fetcher: self,
277            heap_pages: self.runtime_code.heap_pages,
278            hash: self.runtime_code.hash.clone(),
279        }
280    }
281
282    fn call_function<R: Decode>(
283        &self,
284        method: &str,
285        call_data: Vec<u8>,
286        trie_backend: &TrieDeltaBackendFor<Backend::State, Block>,
287        overlayed_changes: &mut OverlayedChanges<HashingFor<Block>>,
288    ) -> Result<R, sp_blockchain::Error> {
289        let mut extensions = self
290            .client
291            .execution_extensions()
292            .extensions(self.parent_hash, self.parent_number);
293
294        let runtime_code = self.runtime_code();
295
296        let result = StateMachine::<_, _, _>::new(
297            trie_backend,
298            overlayed_changes,
299            &*self.executor,
300            method,
301            call_data.as_slice(),
302            &mut extensions,
303            &runtime_code,
304            CallContext::Onchain { import: false },
305        )
306        .set_parent_hash(self.parent_hash)
307        .execute()?;
308
309        R::decode(&mut result.as_slice())
310            .map_err(|err| sp_blockchain::Error::CallResultDecode("failed to decode Result", err))
311    }
312
313    fn consolidate_storage_changes(&mut self, changes: StorageChanges<HashingFor<Block>>) {
314        let changes = if let Some(mut mapped_changes) = self.maybe_storage_changes.take() {
315            let previous_root = mapped_changes.consolidate_storage_changes(changes);
316            self.intermediate_roots.push(previous_root);
317            mapped_changes
318        } else {
319            changes.into()
320        };
321        self.maybe_storage_changes = Some(changes)
322    }
323
324    pub(crate) fn initialize_block(
325        &self,
326        header: Block::Header,
327        backend: &TrieDeltaBackendFor<Backend::State, Block>,
328        overlayed_changes: &mut OverlayedChanges<HashingFor<Block>>,
329    ) -> Result<ExtrinsicInclusionMode, sp_blockchain::Error> {
330        let call_data = header.encode();
331        self.call_function(
332            "Core_initialize_block",
333            call_data,
334            backend,
335            overlayed_changes,
336        )
337    }
338
339    pub(crate) fn apply_extrinsic(
340        &self,
341        extrinsic: ExtrinsicFor<Block>,
342        backend: &TrieDeltaBackendFor<Backend::State, Block>,
343        overlayed_changes: &mut OverlayedChanges<HashingFor<Block>>,
344    ) -> Result<ApplyExtrinsicResult, sp_blockchain::Error> {
345        let call_data = extrinsic.encode();
346        self.call_function(
347            "BlockBuilder_apply_extrinsic",
348            call_data,
349            backend,
350            overlayed_changes,
351        )
352    }
353
354    pub(crate) fn finalize_block(
355        &self,
356        backend: &TrieDeltaBackendFor<Backend::State, Block>,
357        overlayed_changes: &mut OverlayedChanges<HashingFor<Block>>,
358    ) -> Result<Block::Header, sp_blockchain::Error> {
359        self.call_function(
360            "BlockBuilder_finalize_block",
361            vec![],
362            backend,
363            overlayed_changes,
364        )
365    }
366
367    pub(crate) fn inherent_extrinsics(
368        &self,
369        inherent: InherentData,
370        backend: &TrieDeltaBackendFor<Backend::State, Block>,
371        overlayed_changes: &mut OverlayedChanges<HashingFor<Block>>,
372    ) -> Result<Vec<ExtrinsicFor<Block>>, sp_blockchain::Error> {
373        let call_data = inherent.encode();
374        self.call_function(
375            "BlockBuilder_inherent_extrinsics",
376            call_data,
377            backend,
378            overlayed_changes,
379        )
380    }
381
382    /// Collect storage changes returns the storage changes and intermediate roots collected so far.
383    /// The changes are reset after this call.
384    /// Could return None if there were no execution done.
385    pub(crate) fn collect_storage_changes(
386        &mut self,
387    ) -> Option<CollectedStorageChanges<HashingFor<Block>>> {
388        let mut intermediate_roots = mem::take(&mut self.intermediate_roots);
389        // we did not add the last transaction storage root.
390        // include that and then return
391        let maybe_storage_changes = self.maybe_storage_changes.take();
392        if let Some(storage_changes) = maybe_storage_changes {
393            intermediate_roots.push(storage_changes.transaction_storage_root);
394            Some(CollectedStorageChanges {
395                storage_changes: storage_changes.into(),
396                intermediate_roots,
397            })
398        } else {
399            None
400        }
401    }
402
403    pub(crate) fn execute_in_transaction<F, R>(
404        &mut self,
405        call: F,
406    ) -> Result<R, sp_blockchain::Error>
407    where
408        F: FnOnce(
409            &Self,
410            &TrieDeltaBackendFor<Backend::State, Block>,
411            &mut OverlayedChanges<HashingFor<Block>>,
412        ) -> TransactionOutcome<Result<R, sp_blockchain::Error>>,
413        R: Decode,
414    {
415        let trie_backend = self.state.as_trie_backend();
416        let mut overlayed_changes = OverlayedChanges::default();
417        let (state_root, delta) = if let Some(changes) = &self.maybe_storage_changes {
418            (changes.transaction_storage_root, Some(&changes.transaction))
419        } else {
420            (*trie_backend.root(), None)
421        };
422        let trie_delta_backend =
423            create_delta_backend_with_maybe_delta(trie_backend, delta, state_root);
424
425        let outcome = call(self, &trie_delta_backend, &mut overlayed_changes);
426        match outcome {
427            TransactionOutcome::Commit(result) => {
428                let state_version = sp_core::storage::StateVersion::V1;
429                let storage_changes = overlayed_changes
430                    .drain_storage_changes(&trie_delta_backend, state_version)
431                    .map_err(sp_blockchain::Error::StorageChanges)?;
432                self.consolidate_storage_changes(storage_changes);
433                result
434            }
435            TransactionOutcome::Rollback(result) => result,
436        }
437    }
438}