From bb54879029c9fce39ebbdaa5e8c2bd75185af44a Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Tue, 13 Aug 2024 16:27:40 +0700 Subject: [PATCH] chore: rebase daan/api --- runtime/devnet/src/config/api.rs | 6 +- runtime/devnet/src/config/extension.rs | 142 ------------------------- runtime/devnet/src/config/mod.rs | 1 - 3 files changed, 1 insertion(+), 148 deletions(-) delete mode 100644 runtime/devnet/src/config/extension.rs diff --git a/runtime/devnet/src/config/api.rs b/runtime/devnet/src/config/api.rs index b1deaf80..5f234cd4 100644 --- a/runtime/devnet/src/config/api.rs +++ b/runtime/devnet/src/config/api.rs @@ -1,8 +1,4 @@ -use crate::{ - config::assets::TrustBackedAssetsInstance, - fungibles::{self}, - Runtime, -}; +use crate::{config::assets::TrustBackedAssetsInstance, fungibles, Runtime, RuntimeCall}; use codec::{Decode, Encode, MaxEncodedLen}; use pop_chain_extension::{CallFilter, ReadState}; use sp_std::vec::Vec; diff --git a/runtime/devnet/src/config/extension.rs b/runtime/devnet/src/config/extension.rs deleted file mode 100644 index 8c32772a..00000000 --- a/runtime/devnet/src/config/extension.rs +++ /dev/null @@ -1,142 +0,0 @@ -use crate::{ - config::assets::TrustBackedAssetsInstance, - fungibles::{self}, - Runtime, RuntimeCall, -}; -use codec::{Decode, Encode, MaxEncodedLen}; -use frame_support::traits::OriginTrait; -use frame_support::{ensure, traits::Contains}; -use frame_system::RawOrigin; -use pallet_contracts::chain_extension::{BufInBufOutState, Environment, Ext}; -use pop_runtime_extensions::{ - constants::{DECODING_FAILED_ERROR, LOG_TARGET, UNKNOWN_CALL_ERROR}, - dispatch_call, DispatchCallParamsHandler, PopApiExtensionConfig, ReadStateParamsHandler, -}; -use sp_core::Get; -use sp_runtime::DispatchError; - -/// A query of runtime state. -#[derive(Encode, Decode, Debug, MaxEncodedLen)] -#[repr(u8)] -pub enum RuntimeRead { - /// Fungible token queries. - #[codec(index = 150)] - Fungibles(fungibles::Read), -} - -/// A type to identify allowed calls to the Runtime from the API. -pub struct AllowedApiCalls; - -impl Contains for AllowedApiCalls { - /// Allowed runtime calls from the API. - fn contains(c: &RuntimeCall) -> bool { - use fungibles::Call::*; - matches!( - c, - RuntimeCall::Fungibles( - transfer { .. } - | transfer_from { .. } - | approve { .. } | increase_allowance { .. } - | decrease_allowance { .. } - ) - ) - } -} - -impl Contains> for AllowedApiCalls { - /// Allowed state queries from the API. - fn contains(c: &RuntimeRead) -> bool { - use fungibles::Read::*; - matches!( - c, - RuntimeRead::Fungibles( - TotalSupply(..) - | BalanceOf { .. } | Allowance { .. } - | TokenName(..) | TokenSymbol(..) - | TokenDecimals(..) - ) - ) - } -} - -/// Wrapper to enable versioning of runtime state reads. -#[derive(Decode, Debug)] -enum VersionedStateRead { - /// Version zero of state reads. - #[codec(index = 0)] - V0(RuntimeRead), -} - -/// Wrapper to enable versioning of runtime calls. -#[derive(Decode, Debug)] -enum VersionedDispatch { - /// Version zero of dispatch calls. - #[codec(index = 0)] - V0(T::RuntimeCall), -} - -pub struct ChainExtensionEnvironment; - -impl DispatchCallParamsHandler for ChainExtensionEnvironment { - fn handle_params( - env: &mut Environment, - params: Vec, - ) -> Result<(), DispatchError> - where - E: Ext, - T: PopApiExtensionConfig, - { - const LOG_PREFIX: &str = " dispatch |"; - - let call = - >::decode(&mut ¶ms[..]).map_err(|_| DECODING_FAILED_ERROR)?; - - // Contract is the origin by default. - let mut origin: T::RuntimeOrigin = RawOrigin::Signed(env.ext().address().clone()).into(); - match call { - VersionedDispatch::V0(call) => { - origin.add_filter(T::AllowedDispatchCalls::contains); - dispatch_call::(env, call, origin, LOG_PREFIX) - }, - } - } -} - -impl ReadStateParamsHandler for ChainExtensionEnvironment { - fn handle_params( - env: &mut Environment, - params: Vec, - ) -> Result<(), DispatchError> - where - E: Ext, - T: PopApiExtensionConfig, - { - const LOG_PREFIX: &str = " read_state |"; - - let read = - >::decode(&mut ¶ms[..]).map_err(|_| DECODING_FAILED_ERROR)?; - - // Charge weight for doing one storage read. - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - let result = match read { - VersionedStateRead::V0(read) => { - ensure!(AllowedApiCalls::contains(&read), UNKNOWN_CALL_ERROR); - match read { - RuntimeRead::Fungibles(key) => fungibles::Pallet::::read_state(key), - } - }, - }; - log::trace!( - target:LOG_TARGET, - "{} result: {:?}.", LOG_PREFIX, result - ); - env.write(&result, false, None) - } -} - -impl PopApiExtensionConfig for Runtime { - type AssetInstance = TrustBackedAssetsInstance; - type ReadStateParamsHandler = ChainExtensionEnvironment; - type DispatchCallParamsHandler = ChainExtensionEnvironment; - type AllowedDispatchCalls = AllowedApiCalls; -} diff --git a/runtime/devnet/src/config/mod.rs b/runtime/devnet/src/config/mod.rs index 1cf09430..f62ffa76 100644 --- a/runtime/devnet/src/config/mod.rs +++ b/runtime/devnet/src/config/mod.rs @@ -1,7 +1,6 @@ pub(crate) mod api; pub mod assets; mod contracts; -pub(crate) mod extension; mod proxy; // Public due to integration tests crate. pub mod xcm;