From 25660908f81c7dcfd7eff02d1da4068bf59ef955 Mon Sep 17 00:00:00 2001 From: Andrey Lazarko Date: Mon, 21 Oct 2024 15:46:08 +0200 Subject: [PATCH] concrete error type for builtin actors --- pallets/gear-builtin/src/bls12_381.rs | 7 ++-- pallets/gear-builtin/src/lib.rs | 35 +++++++++---------- pallets/gear-builtin/src/mock.rs | 6 ---- pallets/gear-builtin/src/staking.rs | 7 ++-- .../gear-builtin/src/tests/bad_builtin_ids.rs | 2 -- pallets/gear-eth-bridge/src/builtin.rs | 7 ++-- 6 files changed, 29 insertions(+), 35 deletions(-) diff --git a/pallets/gear-builtin/src/bls12_381.rs b/pallets/gear-builtin/src/bls12_381.rs index c1ed6aa4638..2d11c18f46a 100644 --- a/pallets/gear-builtin/src/bls12_381.rs +++ b/pallets/gear-builtin/src/bls12_381.rs @@ -30,9 +30,10 @@ const IS_VALIDATED: Validate = ark_scale::is_validated(HOST_CALL); pub struct Actor(PhantomData); impl BuiltinActor for Actor { - type Error = BuiltinActorError; - - fn handle(dispatch: &StoredDispatch, gas_limit: u64) -> (Result, u64) { + fn handle( + dispatch: &StoredDispatch, + gas_limit: u64, + ) -> (Result, u64) { let message = dispatch.message(); let payload = message.payload_bytes(); let (result, gas_spent) = match payload.first().copied() { diff --git a/pallets/gear-builtin/src/lib.rs b/pallets/gear-builtin/src/lib.rs index e31069a873e..ba3e7aeac15 100644 --- a/pallets/gear-builtin/src/lib.rs +++ b/pallets/gear-builtin/src/lib.rs @@ -75,6 +75,8 @@ pub use pallet::*; const LOG_TARGET: &str = "gear::builtin"; +pub type ActorErrorHandleFn = HandleFn; + /// Built-in actor error type #[derive(Encode, Decode, Clone, Debug, PartialEq, Eq, derive_more::Display)] pub enum BuiltinActorError { @@ -109,10 +111,11 @@ impl From for ActorExecutionErrorReplyReason { /// A trait representing an interface of a builtin actor that can handle a message /// from message queue (a `StoredDispatch`) to produce an outcome and gas spent. pub trait BuiltinActor { - type Error; - /// Handles a message and returns a result and the actual gas spent. - fn handle(dispatch: &StoredDispatch, gas_limit: u64) -> (Result, u64); + fn handle( + dispatch: &StoredDispatch, + gas_limit: u64, + ) -> (Result, u64); } /// A marker struct to associate a builtin actor with its unique ID. @@ -121,33 +124,29 @@ pub struct ActorWithId(PhantomData); /// Glue trait to implement `BuiltinCollection` for a tuple of `ActorWithId`. trait BuiltinActorWithId { const ID: u64; - - type Error; - type Actor: BuiltinActor; + type Actor: BuiltinActor; } impl BuiltinActorWithId for ActorWithId { const ID: u64 = ID; - - type Error = A::Error; type Actor = A; } /// A trait defining a method to convert a tuple of `BuiltinActor` types into /// a in-memory collection of builtin actors. -pub trait BuiltinCollection { +pub trait BuiltinCollection { fn collect( - registry: &mut BTreeMap>>, + registry: &mut BTreeMap>, id_converter: &dyn Fn(u64) -> ProgramId, ); } // Assuming as many as 16 builtin actors for the meantime #[impl_for_tuples(16)] -#[tuple_types_custom_trait_bound(BuiltinActorWithId + 'static)] -impl BuiltinCollection for Tuple { +#[tuple_types_custom_trait_bound(BuiltinActorWithId + 'static)] +impl BuiltinCollection for Tuple { fn collect( - registry: &mut BTreeMap>>, + registry: &mut BTreeMap>, id_converter: &dyn Fn(u64) -> ProgramId, ) { for_tuples!( @@ -194,7 +193,7 @@ pub mod pallet { + GetDispatchInfo; /// The builtin actor type. - type Builtins: BuiltinCollection; + type Builtins: BuiltinCollection; /// Weight cost incurred by builtin actors calls. type WeightInfo: WeightInfo; @@ -217,6 +216,7 @@ pub mod pallet { impl BuiltinDispatcherFactory for Pallet { type Error = BuiltinActorError; + type Output = BuiltinRegistry; fn create() -> (BuiltinRegistry, u64) { @@ -228,7 +228,7 @@ impl BuiltinDispatcherFactory for Pallet { } pub struct BuiltinRegistry { - pub registry: BTreeMap>>, + pub registry: BTreeMap>, pub _phantom: sp_std::marker::PhantomData, } impl BuiltinRegistry { @@ -245,14 +245,13 @@ impl BuiltinRegistry { impl BuiltinDispatcher for BuiltinRegistry { type Error = BuiltinActorError; - - fn lookup<'a>(&'a self, id: &ProgramId) -> Option<&'a HandleFn> { + fn lookup<'a>(&'a self, id: &ProgramId) -> Option<&'a ActorErrorHandleFn> { self.registry.get(id).map(|f| &**f) } fn run( &self, - f: &HandleFn, + f: &ActorErrorHandleFn, dispatch: StoredDispatch, gas_limit: u64, ) -> Vec { diff --git a/pallets/gear-builtin/src/mock.rs b/pallets/gear-builtin/src/mock.rs index 98ce908b8b7..de8b0113a18 100644 --- a/pallets/gear-builtin/src/mock.rs +++ b/pallets/gear-builtin/src/mock.rs @@ -131,8 +131,6 @@ pallet_gear::impl_config!( // A builtin actor who always returns success (even if not enough gas is provided). pub struct SuccessBuiltinActor {} impl BuiltinActor for SuccessBuiltinActor { - type Error = BuiltinActorError; - fn handle( dispatch: &StoredDispatch, _gas_limit: u64, @@ -158,8 +156,6 @@ impl BuiltinActor for SuccessBuiltinActor { // A builtin actor that always returns an error. pub struct ErrorBuiltinActor {} impl BuiltinActor for ErrorBuiltinActor { - type Error = BuiltinActorError; - fn handle( dispatch: &StoredDispatch, _gas_limit: u64, @@ -181,8 +177,6 @@ impl BuiltinActor for ErrorBuiltinActor { // An honest bulitin actor that actually checks whether the gas is sufficient. pub struct HonestBuiltinActor {} impl BuiltinActor for HonestBuiltinActor { - type Error = BuiltinActorError; - fn handle( dispatch: &StoredDispatch, gas_limit: u64, diff --git a/pallets/gear-builtin/src/staking.rs b/pallets/gear-builtin/src/staking.rs index d6900bb5f68..a14577a0a78 100644 --- a/pallets/gear-builtin/src/staking.rs +++ b/pallets/gear-builtin/src/staking.rs @@ -142,9 +142,10 @@ where T::AccountId: Origin, CallOf: From>, { - type Error = BuiltinActorError; - - fn handle(dispatch: &StoredDispatch, gas_limit: u64) -> (Result, u64) { + fn handle( + dispatch: &StoredDispatch, + gas_limit: u64, + ) -> (Result, u64) { let message = dispatch.message(); let origin: T::AccountId = dispatch.source().cast(); let mut payload = message.payload_bytes(); diff --git a/pallets/gear-builtin/src/tests/bad_builtin_ids.rs b/pallets/gear-builtin/src/tests/bad_builtin_ids.rs index bef2567d7d2..37dd5e42d69 100644 --- a/pallets/gear-builtin/src/tests/bad_builtin_ids.rs +++ b/pallets/gear-builtin/src/tests/bad_builtin_ids.rs @@ -99,8 +99,6 @@ pallet_gear::impl_config!( pub struct SomeBuiltinActor {} impl BuiltinActor for SomeBuiltinActor { - type Error = BuiltinActorError; - fn handle( _dispatch: &StoredDispatch, _gas_limit: u64, diff --git a/pallets/gear-eth-bridge/src/builtin.rs b/pallets/gear-eth-bridge/src/builtin.rs index 68223d976d0..32c887fe407 100644 --- a/pallets/gear-eth-bridge/src/builtin.rs +++ b/pallets/gear-eth-bridge/src/builtin.rs @@ -39,9 +39,10 @@ impl BuiltinActor for Actor where T::AccountId: Origin, { - type Error = BuiltinActorError; - - fn handle(dispatch: &StoredDispatch, gas_limit: u64) -> (Result, u64) { + fn handle( + dispatch: &StoredDispatch, + gas_limit: u64, + ) -> (Result, u64) { if !dispatch.value().is_zero() { return ( Err(BuiltinActorError::Custom(LimitedStr::from_small_str(