Skip to content

Commit

Permalink
Merge pull request #1030 from opentensor/feat/admin-set-evm-chain-id
Browse files Browse the repository at this point in the history
Feat/admin set evm chain
  • Loading branch information
sam0x17 authored Dec 6, 2024
2 parents 01ff37b + eb154a8 commit f87d97d
Show file tree
Hide file tree
Showing 9 changed files with 105,446 additions and 144 deletions.
276 changes: 141 additions & 135 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion pallets/admin-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ log = { workspace = true }
pallet-subtensor = { version = "4.0.0-dev", default-features = false, path = "../subtensor" }
sp-weights = { workspace = true }
substrate-fixed = { workspace = true }
pallet-evm-chain-id = { workspace = true }
pallet-drand = { workspace = true, default-features = false }


[dev-dependencies]
sp-core = { workspace = true }
sp-io = { workspace = true }
Expand All @@ -52,6 +52,7 @@ std = [
"pallet-subtensor/std",
"sp-consensus-aura/std",
"pallet-balances/std",
"pallet-evm-chain-id/std",
"pallet-scheduler/std",
"sp-runtime/std",
"sp-tracing/std",
Expand All @@ -77,6 +78,7 @@ try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-balances/try-runtime",
"pallet-evm-chain-id/try-runtime",
"pallet-scheduler/try-runtime",
"sp-runtime/try-runtime",
"pallet-subtensor/try-runtime",
Expand Down
28 changes: 27 additions & 1 deletion pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod pallet {
use frame_support::pallet_prelude::*;
use frame_support::traits::tokens::Balance;
use frame_system::pallet_prelude::*;
use pallet_evm_chain_id::{self, ChainId};
use sp_runtime::BoundedVec;

/// The main data structure of the module.
Expand All @@ -29,7 +30,11 @@ pub mod pallet {

/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config: frame_system::Config + pallet_subtensor::pallet::Config {
pub trait Config:
frame_system::Config
+ pallet_subtensor::pallet::Config
+ pallet_evm_chain_id::pallet::Config
{
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

Expand Down Expand Up @@ -1212,6 +1217,27 @@ pub mod pallet {
);
Ok(())
}

/// Sets the EVM ChainID.
///
/// # Arguments
/// * `origin` - The origin of the call, which must be the subnet owner or the root account.
/// * `chainId` - The u64 chain ID
///
/// # Errors
/// * `BadOrigin` - If the caller is neither the subnet owner nor the root account.
///
/// # Weight
/// Weight is handled by the `#[pallet::weight]` attribute.
#[pallet::call_index(58)]
#[pallet::weight(<T as Config>::WeightInfo::sudo_set_evm_chain_id())]
pub fn sudo_set_evm_chain_id(origin: OriginFor<T>, chain_id: u64) -> DispatchResult {
// Ensure the call is made by the root account
ensure_root(origin)?;

ChainId::<T>::set(chain_id);
Ok(())
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions pallets/admin-utils/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ frame_support::construct_runtime!(
SubtensorModule: pallet_subtensor::{Pallet, Call, Storage, Event<T>, Error<T>} = 4,
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>} = 5,
Drand: pallet_drand::{Pallet, Call, Storage, Event<T>} = 6,
EVMChainId: pallet_evm_chain_id = 7,
}
);

Expand Down Expand Up @@ -277,6 +278,7 @@ impl pallet_scheduler::Config for Test {
type Preimages = ();
}

impl pallet_evm_chain_id::Config for Test {}
impl pallet_drand::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = pallet_drand::weights::SubstrateWeight<Test>;
Expand Down
33 changes: 33 additions & 0 deletions pallets/admin-utils/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,3 +1433,36 @@ fn sudo_set_commit_reveal_weights_interval() {
assert_eq!(SubtensorModule::get_reveal_period(netuid), to_be_set);
});
}

#[test]
fn test_sudo_root_sets_evm_chain_id() {
new_test_ext().execute_with(|| {
let chain_id: u64 = 945;
assert_eq!(pallet_evm_chain_id::ChainId::<Test>::get(), 0);

assert_ok!(AdminUtils::sudo_set_evm_chain_id(
<<Test as Config>::RuntimeOrigin>::root(),
chain_id
));

assert_eq!(pallet_evm_chain_id::ChainId::<Test>::get(), chain_id);
});
}

#[test]
fn test_sudo_non_root_cannot_set_evm_chain_id() {
new_test_ext().execute_with(|| {
let chain_id: u64 = 945;
assert_eq!(pallet_evm_chain_id::ChainId::<Test>::get(), 0);

assert_eq!(
AdminUtils::sudo_set_evm_chain_id(
<<Test as Config>::RuntimeOrigin>::signed(U256::from(0)),
chain_id
),
Err(DispatchError::BadOrigin)
);

assert_eq!(pallet_evm_chain_id::ChainId::<Test>::get(), 0);
});
}
9 changes: 9 additions & 0 deletions pallets/admin-utils/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub trait WeightInfo {
fn sudo_set_tempo() -> Weight;
fn sudo_set_commit_reveal_weights_interval() -> Weight;
fn sudo_set_commit_reveal_weights_enabled() -> Weight;
fn sudo_set_evm_chain_id() -> Weight;
}

/// Weights for `pallet_admin_utils` using the Substrate node and recommended hardware.
Expand Down Expand Up @@ -431,6 +432,10 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
fn sudo_set_evm_chain_id() -> Weight {
Weight::from_parts(20_200_000, 0)
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
}

// For backwards compatibility and tests.
Expand Down Expand Up @@ -805,4 +810,8 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
fn sudo_set_evm_chain_id() -> Weight {
Weight::from_parts(20_200_000, 0)
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
}
105,144 changes: 105,144 additions & 0 deletions plain_spec_finney.json

Large diffs are not rendered by default.

74 changes: 74 additions & 0 deletions plain_spec_testfinney.json

Large diffs are not rendered by default.

20 changes: 13 additions & 7 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,14 +1130,20 @@ impl pallet_admin_utils::Config for Runtime {
type WeightInfo = pallet_admin_utils::weights::SubstrateWeight<Runtime>;
}

// Define the ChainId
parameter_types! {
pub const SubtensorChainId: u64 = 0x03B1; // Unicode for lowercase alpha
// pub const SubtensorChainId: u64 = 0x03C4; // Unicode for lowercase tau
}

/// Define the ChainId
/// EVM Chain ID will be set by sudo transaction for each chain
/// Mainnet Finney: 0x03C4 - Unicode for lowercase tau
/// TestNet Finney: 0x03B1 - Unicode for lowercase alpha
impl pallet_evm_chain_id::Config for Runtime {}

pub struct ConfigurableChainId;

impl Get<u64> for ConfigurableChainId {
fn get() -> u64 {
pallet_evm_chain_id::ChainId::<Runtime>::get()
}
}

pub struct FindAuthorTruncated<F>(PhantomData<F>);
impl<F: FindAuthor<u32>> FindAuthor<H160> for FindAuthorTruncated<F> {
fn find_author<'a, I>(digests: I) -> Option<H160>
Expand Down Expand Up @@ -1222,7 +1228,7 @@ impl pallet_evm::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type PrecompilesType = FrontierPrecompiles<Self>;
type PrecompilesValue = PrecompilesValue;
type ChainId = SubtensorChainId;
type ChainId = ConfigurableChainId;
type BlockGasLimit = BlockGasLimit;
type Runner = pallet_evm::runner::stack::Runner<Self>;
type OnChargeTransaction = ();
Expand Down

0 comments on commit f87d97d

Please sign in to comment.