// Copyright 2024 Moonbeam foundation // This file is part of Moonbeam. // Moonbeam is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // Moonbeam is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with Moonbeam. If not, see . use sc_client_api::execution_extensions::ExecutionExtensions; use sc_executor::{RuntimeVersion, RuntimeVersionOf}; use sp_api::ProofRecorder; use sp_core::traits::{CallContext, Externalities}; use sp_runtime::traits::{Block as BlockT, HashingFor}; use sp_state_machine::{OverlayedChanges, StorageProof}; use std::cell::RefCell; use std::marker::PhantomData; /// Call executor that executes methods locally, querying all required /// data from local backend. #[derive(Clone)] pub struct LazyLoadingCallExecutor { inner: Executor, _phantom_data: PhantomData, } impl LazyLoadingCallExecutor where Executor: sc_client_api::CallExecutor + Clone + 'static, { /// Creates new instance of local call executor. pub fn new(executor: Executor) -> sp_blockchain::Result { Ok(LazyLoadingCallExecutor { inner: executor, _phantom_data: Default::default(), }) } } impl sc_client_api::CallExecutor for LazyLoadingCallExecutor where Executor: sc_client_api::CallExecutor, Block: BlockT, { type Error = Executor::Error; type Backend = Executor::Backend; fn execution_extensions(&self) -> &ExecutionExtensions { &self.inner.execution_extensions() } fn call( &self, at_hash: Block::Hash, method: &str, call_data: &[u8], context: CallContext, ) -> sp_blockchain::Result> { self.inner.call(at_hash, method, call_data, context) } fn contextual_call( &self, at_hash: Block::Hash, method: &str, call_data: &[u8], changes: &RefCell>>, // not used in lazy loading _recorder: &Option>, call_context: CallContext, extensions: &RefCell, ) -> Result, sp_blockchain::Error> { self.inner.contextual_call( at_hash, method, call_data, changes, &None, call_context, extensions, ) } fn runtime_version( &self, at_hash: Block::Hash, call_context: CallContext, ) -> sp_blockchain::Result { sc_client_api::CallExecutor::runtime_version(&self.inner, at_hash, call_context) } fn prove_execution( &self, at_hash: Block::Hash, method: &str, call_data: &[u8], ) -> sp_blockchain::Result<(Vec, StorageProof)> { self.inner.prove_execution(at_hash, method, call_data) } } impl RuntimeVersionOf for LazyLoadingCallExecutor where Executor: RuntimeVersionOf, Block: BlockT, { fn runtime_version( &self, ext: &mut dyn Externalities, runtime_code: &sp_core::traits::RuntimeCode, ) -> Result { self.inner.runtime_version(ext, runtime_code) } }