Skip to content

Commit f87d97d

Browse files
authored
Merge pull request #1030 from opentensor/feat/admin-set-evm-chain-id
Feat/admin set evm chain
2 parents 01ff37b + eb154a8 commit f87d97d

File tree

9 files changed

+105446
-144
lines changed

9 files changed

+105446
-144
lines changed

Diff for: Cargo.lock

+141-135
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: pallets/admin-utils/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ log = { workspace = true }
2929
pallet-subtensor = { version = "4.0.0-dev", default-features = false, path = "../subtensor" }
3030
sp-weights = { workspace = true }
3131
substrate-fixed = { workspace = true }
32+
pallet-evm-chain-id = { workspace = true }
3233
pallet-drand = { workspace = true, default-features = false }
3334

34-
3535
[dev-dependencies]
3636
sp-core = { workspace = true }
3737
sp-io = { workspace = true }
@@ -52,6 +52,7 @@ std = [
5252
"pallet-subtensor/std",
5353
"sp-consensus-aura/std",
5454
"pallet-balances/std",
55+
"pallet-evm-chain-id/std",
5556
"pallet-scheduler/std",
5657
"sp-runtime/std",
5758
"sp-tracing/std",
@@ -77,6 +78,7 @@ try-runtime = [
7778
"frame-support/try-runtime",
7879
"frame-system/try-runtime",
7980
"pallet-balances/try-runtime",
81+
"pallet-evm-chain-id/try-runtime",
8082
"pallet-scheduler/try-runtime",
8183
"sp-runtime/try-runtime",
8284
"pallet-subtensor/try-runtime",

Diff for: pallets/admin-utils/src/lib.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub mod pallet {
2020
use frame_support::pallet_prelude::*;
2121
use frame_support::traits::tokens::Balance;
2222
use frame_system::pallet_prelude::*;
23+
use pallet_evm_chain_id::{self, ChainId};
2324
use sp_runtime::BoundedVec;
2425

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

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

@@ -1212,6 +1217,27 @@ pub mod pallet {
12121217
);
12131218
Ok(())
12141219
}
1220+
1221+
/// Sets the EVM ChainID.
1222+
///
1223+
/// # Arguments
1224+
/// * `origin` - The origin of the call, which must be the subnet owner or the root account.
1225+
/// * `chainId` - The u64 chain ID
1226+
///
1227+
/// # Errors
1228+
/// * `BadOrigin` - If the caller is neither the subnet owner nor the root account.
1229+
///
1230+
/// # Weight
1231+
/// Weight is handled by the `#[pallet::weight]` attribute.
1232+
#[pallet::call_index(58)]
1233+
#[pallet::weight(<T as Config>::WeightInfo::sudo_set_evm_chain_id())]
1234+
pub fn sudo_set_evm_chain_id(origin: OriginFor<T>, chain_id: u64) -> DispatchResult {
1235+
// Ensure the call is made by the root account
1236+
ensure_root(origin)?;
1237+
1238+
ChainId::<T>::set(chain_id);
1239+
Ok(())
1240+
}
12151241
}
12161242
}
12171243

Diff for: pallets/admin-utils/src/tests/mock.rs

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ frame_support::construct_runtime!(
2929
SubtensorModule: pallet_subtensor::{Pallet, Call, Storage, Event<T>, Error<T>} = 4,
3030
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>} = 5,
3131
Drand: pallet_drand::{Pallet, Call, Storage, Event<T>} = 6,
32+
EVMChainId: pallet_evm_chain_id = 7,
3233
}
3334
);
3435

@@ -277,6 +278,7 @@ impl pallet_scheduler::Config for Test {
277278
type Preimages = ();
278279
}
279280

281+
impl pallet_evm_chain_id::Config for Test {}
280282
impl pallet_drand::Config for Test {
281283
type RuntimeEvent = RuntimeEvent;
282284
type WeightInfo = pallet_drand::weights::SubstrateWeight<Test>;

Diff for: pallets/admin-utils/src/tests/mod.rs

+33
Original file line numberDiff line numberDiff line change
@@ -1433,3 +1433,36 @@ fn sudo_set_commit_reveal_weights_interval() {
14331433
assert_eq!(SubtensorModule::get_reveal_period(netuid), to_be_set);
14341434
});
14351435
}
1436+
1437+
#[test]
1438+
fn test_sudo_root_sets_evm_chain_id() {
1439+
new_test_ext().execute_with(|| {
1440+
let chain_id: u64 = 945;
1441+
assert_eq!(pallet_evm_chain_id::ChainId::<Test>::get(), 0);
1442+
1443+
assert_ok!(AdminUtils::sudo_set_evm_chain_id(
1444+
<<Test as Config>::RuntimeOrigin>::root(),
1445+
chain_id
1446+
));
1447+
1448+
assert_eq!(pallet_evm_chain_id::ChainId::<Test>::get(), chain_id);
1449+
});
1450+
}
1451+
1452+
#[test]
1453+
fn test_sudo_non_root_cannot_set_evm_chain_id() {
1454+
new_test_ext().execute_with(|| {
1455+
let chain_id: u64 = 945;
1456+
assert_eq!(pallet_evm_chain_id::ChainId::<Test>::get(), 0);
1457+
1458+
assert_eq!(
1459+
AdminUtils::sudo_set_evm_chain_id(
1460+
<<Test as Config>::RuntimeOrigin>::signed(U256::from(0)),
1461+
chain_id
1462+
),
1463+
Err(DispatchError::BadOrigin)
1464+
);
1465+
1466+
assert_eq!(pallet_evm_chain_id::ChainId::<Test>::get(), 0);
1467+
});
1468+
}

Diff for: pallets/admin-utils/src/weights.rs

+9
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub trait WeightInfo {
6262
fn sudo_set_tempo() -> Weight;
6363
fn sudo_set_commit_reveal_weights_interval() -> Weight;
6464
fn sudo_set_commit_reveal_weights_enabled() -> Weight;
65+
fn sudo_set_evm_chain_id() -> Weight;
6566
}
6667

6768
/// Weights for `pallet_admin_utils` using the Substrate node and recommended hardware.
@@ -431,6 +432,10 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
431432
.saturating_add(T::DbWeight::get().reads(1_u64))
432433
.saturating_add(T::DbWeight::get().writes(1_u64))
433434
}
435+
fn sudo_set_evm_chain_id() -> Weight {
436+
Weight::from_parts(20_200_000, 0)
437+
.saturating_add(RocksDbWeight::get().writes(1_u64))
438+
}
434439
}
435440

436441
// For backwards compatibility and tests.
@@ -805,4 +810,8 @@ impl WeightInfo for () {
805810
.saturating_add(RocksDbWeight::get().reads(1_u64))
806811
.saturating_add(RocksDbWeight::get().writes(1_u64))
807812
}
813+
fn sudo_set_evm_chain_id() -> Weight {
814+
Weight::from_parts(20_200_000, 0)
815+
.saturating_add(RocksDbWeight::get().writes(1_u64))
816+
}
808817
}

Diff for: plain_spec_finney.json

+105,144
Large diffs are not rendered by default.

Diff for: plain_spec_testfinney.json

+74
Large diffs are not rendered by default.

Diff for: runtime/src/lib.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -1130,14 +1130,20 @@ impl pallet_admin_utils::Config for Runtime {
11301130
type WeightInfo = pallet_admin_utils::weights::SubstrateWeight<Runtime>;
11311131
}
11321132

1133-
// Define the ChainId
1134-
parameter_types! {
1135-
pub const SubtensorChainId: u64 = 0x03B1; // Unicode for lowercase alpha
1136-
// pub const SubtensorChainId: u64 = 0x03C4; // Unicode for lowercase tau
1137-
}
1138-
1133+
/// Define the ChainId
1134+
/// EVM Chain ID will be set by sudo transaction for each chain
1135+
/// Mainnet Finney: 0x03C4 - Unicode for lowercase tau
1136+
/// TestNet Finney: 0x03B1 - Unicode for lowercase alpha
11391137
impl pallet_evm_chain_id::Config for Runtime {}
11401138

1139+
pub struct ConfigurableChainId;
1140+
1141+
impl Get<u64> for ConfigurableChainId {
1142+
fn get() -> u64 {
1143+
pallet_evm_chain_id::ChainId::<Runtime>::get()
1144+
}
1145+
}
1146+
11411147
pub struct FindAuthorTruncated<F>(PhantomData<F>);
11421148
impl<F: FindAuthor<u32>> FindAuthor<H160> for FindAuthorTruncated<F> {
11431149
fn find_author<'a, I>(digests: I) -> Option<H160>
@@ -1222,7 +1228,7 @@ impl pallet_evm::Config for Runtime {
12221228
type RuntimeEvent = RuntimeEvent;
12231229
type PrecompilesType = FrontierPrecompiles<Self>;
12241230
type PrecompilesValue = PrecompilesValue;
1225-
type ChainId = SubtensorChainId;
1231+
type ChainId = ConfigurableChainId;
12261232
type BlockGasLimit = BlockGasLimit;
12271233
type Runner = pallet_evm::runner::stack::Runner<Self>;
12281234
type OnChargeTransaction = ();

0 commit comments

Comments
 (0)