Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gear-builtin): Separate actor id declaration #4051

Merged
merged 5 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions pallets/gear-builtin/src/bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ const IS_VALIDATED: Validate = ark_scale::is_validated(HOST_CALL);
pub struct Actor<T: Config>(PhantomData<T>);

impl<T: Config> BuiltinActor for Actor<T> {
const ID: u64 = 1;

type Error = BuiltinActorError;

fn handle(dispatch: &StoredDispatch, gas_limit: u64) -> (Result<Payload, Self::Error>, u64) {
Expand Down
26 changes: 21 additions & 5 deletions pallets/gear-builtin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ use alloc::{
collections::{btree_map::Entry, BTreeMap},
string::ToString,
};
use core::marker::PhantomData;
use core_processor::{
common::{ActorExecutionErrorReplyReason, DispatchResult, JournalNote, TrapExplanation},
process_execution_error, process_success, SuccessfulDispatchResultKind,
Expand Down Expand Up @@ -109,13 +110,28 @@ impl From<BuiltinActorError> for ActorExecutionErrorReplyReason {
pub trait BuiltinActor {
type Error;

/// The global unique ID of the trait implementer type.
const ID: u64;

/// Handles a message and returns a result and the actual gas spent.
fn handle(dispatch: &StoredDispatch, gas_limit: u64) -> (Result<Payload, Self::Error>, u64);
}

/// A marker struct to associate a builtin actor with its unique ID.
pub struct ActorWithId<const ID: u64, A: BuiltinActor>(PhantomData<A>);

/// Glue trait to implement `BuiltinCollection` for a tuple of `ActorWithId`.
trait BuiltinActorWithId {
const ID: u64;

type Error;
type Actor: BuiltinActor<Error = Self::Error>;
}

impl<const ID: u64, A: BuiltinActor> BuiltinActorWithId for ActorWithId<ID, A> {
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<E> {
Expand All @@ -127,7 +143,7 @@ pub trait BuiltinCollection<E> {

// Assuming as many as 16 builtin actors for the meantime
#[impl_for_tuples(16)]
#[tuple_types_custom_trait_bound(BuiltinActor<Error = E> + 'static)]
#[tuple_types_custom_trait_bound(BuiltinActorWithId<Error = E> + 'static)]
impl<E> BuiltinCollection<E> for Tuple {
fn collect(
registry: &mut BTreeMap<ProgramId, Box<HandleFn<E>>>,
Expand All @@ -137,7 +153,7 @@ impl<E> BuiltinCollection<E> for Tuple {
#(
let actor_id = id_converter(Tuple::ID);
if let Entry::Vacant(e) = registry.entry(actor_id) {
e.insert(Box::new(Tuple::handle));
e.insert(Box::new(Tuple::Actor::handle));
} else {
unreachable!("Duplicate builtin ids");
}
Expand Down
26 changes: 12 additions & 14 deletions pallets/gear-builtin/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{self as pallet_gear_builtin, bls12_381, BuiltinActor, BuiltinActorError};
use crate::{self as pallet_gear_builtin, bls12_381, ActorWithId, BuiltinActor, BuiltinActorError};
use common::{GasProvider, GasTree};
use core::cell::RefCell;
use frame_support::{
Expand Down Expand Up @@ -131,16 +131,14 @@ pub struct SuccessBuiltinActor {}
impl BuiltinActor for SuccessBuiltinActor {
type Error = BuiltinActorError;

const ID: u64 = u64::from_le_bytes(*b"bltn/suc");

fn handle(
dispatch: &StoredDispatch,
_gas_limit: u64,
) -> (Result<Payload, BuiltinActorError>, u64) {
if !in_transaction() {
DEBUG_EXECUTION_TRACE.with(|d| {
d.borrow_mut().push(ExecutionTraceFrame {
destination: <Self as BuiltinActor>::ID,
destination: SUCCESS_ACTOR_ID,
source: dispatch.source(),
input: dispatch.payload_bytes().to_vec(),
is_success: true,
Expand All @@ -160,16 +158,14 @@ pub struct ErrorBuiltinActor {}
impl BuiltinActor for ErrorBuiltinActor {
type Error = BuiltinActorError;

const ID: u64 = u64::from_le_bytes(*b"bltn/err");

fn handle(
dispatch: &StoredDispatch,
_gas_limit: u64,
) -> (Result<Payload, BuiltinActorError>, u64) {
if !in_transaction() {
DEBUG_EXECUTION_TRACE.with(|d| {
d.borrow_mut().push(ExecutionTraceFrame {
destination: <Self as BuiltinActor>::ID,
destination: ERROR_ACTOR_ID,
source: dispatch.source(),
input: dispatch.payload_bytes().to_vec(),
is_success: false,
Expand All @@ -185,8 +181,6 @@ pub struct HonestBuiltinActor {}
impl BuiltinActor for HonestBuiltinActor {
type Error = BuiltinActorError;

const ID: u64 = u64::from_le_bytes(*b"bltn/hon");

fn handle(
dispatch: &StoredDispatch,
gas_limit: u64,
Expand All @@ -196,7 +190,7 @@ impl BuiltinActor for HonestBuiltinActor {
if !in_transaction() {
DEBUG_EXECUTION_TRACE.with(|d| {
d.borrow_mut().push(ExecutionTraceFrame {
destination: <Self as BuiltinActor>::ID,
destination: HONEST_ACTOR_ID,
source: dispatch.source(),
input: dispatch.payload_bytes().to_vec(),
is_success: !is_error,
Expand All @@ -215,13 +209,17 @@ impl BuiltinActor for HonestBuiltinActor {
}
}

const HONEST_ACTOR_ID: u64 = u64::from_le_bytes(*b"bltn/hon");
const SUCCESS_ACTOR_ID: u64 = u64::from_le_bytes(*b"bltn/suc");
const ERROR_ACTOR_ID: u64 = u64::from_le_bytes(*b"bltn/err");

impl pallet_gear_builtin::Config for Test {
type RuntimeCall = RuntimeCall;
type Builtins = (
SuccessBuiltinActor,
ErrorBuiltinActor,
HonestBuiltinActor,
bls12_381::Actor<Self>,
ActorWithId<SUCCESS_ACTOR_ID, SuccessBuiltinActor>,
ActorWithId<ERROR_ACTOR_ID, ErrorBuiltinActor>,
ActorWithId<HONEST_ACTOR_ID, HonestBuiltinActor>,
ActorWithId<1, bls12_381::Actor<Self>>,
);
type WeightInfo = ();
}
Expand Down
2 changes: 0 additions & 2 deletions pallets/gear-builtin/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ where
T::AccountId: Origin,
CallOf<T>: From<pallet_staking::Call<T>>,
{
const ID: u64 = 2;

type Error = BuiltinActorError;

fn handle(dispatch: &StoredDispatch, gas_limit: u64) -> (Result<Payload, Self::Error>, u64) {
Expand Down
65 changes: 7 additions & 58 deletions pallets/gear-builtin/src/tests/bad_builtin_ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{self as pallet_gear_builtin, BuiltinActor, BuiltinActorError};
use crate::{self as pallet_gear_builtin, ActorWithId, BuiltinActor, BuiltinActorError};
use frame_support::{
construct_runtime, parameter_types,
traits::{ConstBool, ConstU32, ConstU64, FindAuthor, OnFinalize, OnInitialize},
Expand Down Expand Up @@ -95,61 +95,10 @@ pallet_gear::impl_config!(
BuiltinDispatcherFactory = GearBuiltin,
);

pub struct FirstBuiltinActor {}
impl BuiltinActor for FirstBuiltinActor {
pub struct SomeBuiltinActor {}
impl BuiltinActor for SomeBuiltinActor {
type Error = BuiltinActorError;

const ID: u64 = 1_u64;

fn handle(
_dispatch: &StoredDispatch,
_gas_limit: u64,
) -> (Result<Payload, BuiltinActorError>, u64) {
let payload = b"Success".to_vec().try_into().expect("Small vector");

(Ok(payload), 1_000_u64)
}
}

pub struct SecondBuiltinActor {}
impl BuiltinActor for SecondBuiltinActor {
type Error = BuiltinActorError;

const ID: u64 = 2_u64;

fn handle(
_dispatch: &StoredDispatch,
_gas_limit: u64,
) -> (Result<Payload, BuiltinActorError>, u64) {
let payload = b"Success".to_vec().try_into().expect("Small vector");

(Ok(payload), 1_000_u64)
}
}

pub struct ThirdBuiltinActor {}
impl BuiltinActor for ThirdBuiltinActor {
type Error = BuiltinActorError;

const ID: u64 = 3_u64;

fn handle(
_dispatch: &StoredDispatch,
_gas_limit: u64,
) -> (Result<Payload, BuiltinActorError>, u64) {
let payload = b"Success".to_vec().try_into().expect("Small vector");

(Ok(payload), 1_000_u64)
}
}

// Duplicate builtin id: `BuiltinId(2)` already exists.
pub struct DuplicateBuiltinActor {}
impl BuiltinActor for DuplicateBuiltinActor {
type Error = BuiltinActorError;

const ID: u64 = 2_u64;

fn handle(
_dispatch: &StoredDispatch,
_gas_limit: u64,
Expand All @@ -163,10 +112,10 @@ impl BuiltinActor for DuplicateBuiltinActor {
impl pallet_gear_builtin::Config for Test {
type RuntimeCall = RuntimeCall;
type Builtins = (
FirstBuiltinActor,
SecondBuiltinActor,
ThirdBuiltinActor,
DuplicateBuiltinActor,
ActorWithId<1, SomeBuiltinActor>,
ActorWithId<2, SomeBuiltinActor>,
ActorWithId<3, SomeBuiltinActor>,
ActorWithId<2, SomeBuiltinActor>, // 2 already exists
);
type WeightInfo = ();
}
Expand Down
4 changes: 2 additions & 2 deletions pallets/gear-builtin/src/tests/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ mod util {
BLOCK_AUTHOR, ENDOWMENT, EXISTENTIAL_DEPOSIT, MILLISECS_PER_BLOCK, SIGNER, UNITS,
VAL_1_STASH, VAL_2_STASH, VAL_3_STASH,
};
use crate::{self as pallet_gear_builtin, staking::Actor as StakingBuiltin};
use crate::{self as pallet_gear_builtin, staking::Actor as StakingBuiltin, ActorWithId};
pub(super) use common::Origin;
pub(super) use demo_staking_broker::WASM_BINARY;
use frame_election_provider_support::{
Expand Down Expand Up @@ -757,7 +757,7 @@ mod util {

impl pallet_gear_builtin::Config for Test {
type RuntimeCall = RuntimeCall;
type Builtins = (StakingBuiltin<Self>,);
type Builtins = (ActorWithId<2, StakingBuiltin<Self>>,);
type WeightInfo = ();
}

Expand Down
2 changes: 0 additions & 2 deletions pallets/gear-eth-bridge/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ impl<T: Config> BuiltinActor for Actor<T>
where
T::AccountId: Origin,
{
const ID: u64 = 3;

type Error = BuiltinActorError;

fn handle(dispatch: &StoredDispatch, gas_limit: u64) -> (Result<Payload, Self::Error>, u64) {
Expand Down
5 changes: 4 additions & 1 deletion pallets/gear-eth-bridge/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use frame_support::{
use frame_support_test::TestRandomness;
use frame_system::{self as system, pallet_prelude::BlockNumberFor};
use gprimitives::ActorId;
use pallet_gear_builtin::ActorWithId;
use pallet_session::{SessionManager, ShouldEndSession};
use sp_core::{ed25519::Public, H256};
use sp_runtime::{
Expand Down Expand Up @@ -172,9 +173,11 @@ pallet_gear::impl_config!(
BuiltinDispatcherFactory = GearBuiltin,
);

pub const BUILTIN_ID: u64 = 1;

impl pallet_gear_builtin::Config for Test {
type RuntimeCall = RuntimeCall;
type Builtins = (crate::builtin::Actor<Test>,);
type Builtins = (ActorWithId<BUILTIN_ID, crate::builtin::Actor<Test>>,);
type WeightInfo = ();
}

Expand Down
3 changes: 1 addition & 2 deletions pallets/gear-eth-bridge/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,10 +539,9 @@ mod utils {
use crate::builtin;
use gear_core::message::UserMessage;
use gprimitives::{ActorId, MessageId};
use pallet_gear_builtin::BuiltinActor;

pub(crate) fn builtin_id() -> ActorId {
GearBuiltin::generate_actor_id(builtin::Actor::<Test>::ID)
GearBuiltin::generate_actor_id(BUILTIN_ID)
}

#[track_caller]
Expand Down
11 changes: 6 additions & 5 deletions runtime/vara/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ use frame_system::{
};
use pallet_election_provider_multi_phase::{GeometricDepositBase, SolutionAccuracyOf};
pub use pallet_gear::manager::{ExtManager, HandleKind};
use pallet_gear_builtin::ActorWithId;
pub use pallet_gear_payment::CustomChargeTransactionPayment;
pub use pallet_gear_staking_rewards::StakingBlackList;
use pallet_grandpa::{
Expand Down Expand Up @@ -1172,16 +1173,16 @@ impl pallet_gear_messenger::Config for Runtime {
/// Builtin actors arranged in a tuple.
#[cfg(not(feature = "dev"))]
pub type BuiltinActors = (
pallet_gear_builtin::bls12_381::Actor<Runtime>,
pallet_gear_builtin::staking::Actor<Runtime>,
ActorWithId<1, pallet_gear_builtin::bls12_381::Actor<Runtime>>,
ActorWithId<2, pallet_gear_builtin::staking::Actor<Runtime>>,
);

/// Builtin actors arranged in a tuple.
#[cfg(feature = "dev")]
pub type BuiltinActors = (
pallet_gear_builtin::bls12_381::Actor<Runtime>,
pallet_gear_builtin::staking::Actor<Runtime>,
pallet_gear_eth_bridge::Actor<Runtime>,
ActorWithId<1, pallet_gear_builtin::bls12_381::Actor<Runtime>>,
ActorWithId<2, pallet_gear_builtin::staking::Actor<Runtime>>,
ActorWithId<3, pallet_gear_eth_bridge::Actor<Runtime>>,
);

impl pallet_gear_builtin::Config for Runtime {
Expand Down
Loading