Skip to content

Commit

Permalink
Refactor precompile set (#1613)
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier authored Oct 22, 2024
1 parent ab61be5 commit 92e74df
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 265 deletions.
8 changes: 4 additions & 4 deletions node/src/chain_spec/crab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ pub fn development_config() -> ChainSpec {
// EVM stuff.
"evm": {
"accounts": BTreeMap::from_iter(
<CrabPrecompiles<Runtime>>::used_addresses()
.iter()
Precompiles::set()
.into_iter()
.map(|p| {
(
p.to_owned(),
H160(p),
GenesisAccount {
nonce: Default::default(),
balance: Default::default(),
Expand Down Expand Up @@ -236,7 +236,7 @@ pub fn genesis_config() -> ChainSpec {
// EVM stuff.
"evm": {
"accounts": BTreeMap::from_iter(
<CrabPrecompiles<Runtime>>::used_addresses().iter().map(|p| {
Precompiles::set().iter().map(|p| {
(
p,
GenesisAccount {
Expand Down
10 changes: 5 additions & 5 deletions node/src/chain_spec/darwinia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ pub fn development_config() -> ChainSpec {
// EVM stuff.
"evm": {
"accounts": BTreeMap::from_iter(
<DarwiniaPrecompiles<Runtime>>::used_addresses()
.iter()
Precompiles::set()
.into_iter()
.map(|p| {
(
p.to_owned(),
H160(p),
GenesisAccount {
nonce: Default::default(),
balance: Default::default(),
Expand Down Expand Up @@ -225,9 +225,9 @@ pub fn genesis_config() -> ChainSpec {
// EVM stuff.
"evm": {
"accounts": BTreeMap::from_iter(
<DarwiniaPrecompiles<Runtime>>::used_addresses().iter().map(|p| {
Precompiles::set().into_iter().map(|p| {
(
p,
H160(p),
GenesisAccount {
nonce: Default::default(),
balance: Default::default(),
Expand Down
8 changes: 4 additions & 4 deletions node/src/chain_spec/koi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ pub fn development_config() -> ChainSpec {
// EVM stuff.
"evm": {
"accounts": BTreeMap::from_iter(
<KoiPrecompiles<Runtime>>::used_addresses()
.iter()
Precompiles::set()
.into_iter()
.map(|p| {
(
p.to_owned(),
H160(p),
GenesisAccount {
nonce: Default::default(),
balance: Default::default(),
Expand Down Expand Up @@ -183,7 +183,7 @@ pub fn genesis_config() -> ChainSpec {
// EVM stuff.
"evm": {
"accounts": BTreeMap::from_iter(
<KoiPrecompiles<Runtime>>::used_addresses().iter().map(|p| {
Precompiles::set().iter().map(|p| {
(
p,
GenesisAccount {
Expand Down
5 changes: 3 additions & 2 deletions precompile/assets/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ where
Self(Default::default())
}

pub fn used_addresses() -> [H160; 1] {
pub fn set() -> [H160; 1] {
[addr_of(TEST_ID)]
}
}
Expand All @@ -110,7 +110,7 @@ where

fn is_precompile(&self, address: H160, _gas: u64) -> fp_evm::IsPrecompileResult {
fp_evm::IsPrecompileResult::Answer {
is_precompile: Self::used_addresses().contains(&address),
is_precompile: Self::set().contains(&address),
extra_cost: 0,
}
}
Expand Down Expand Up @@ -214,6 +214,7 @@ impl ExtBuilder {
}
}

// TODO: unify with the one in `darwinia-common-runtime`.
fn addr_of<V>(v: V) -> H160
where
V: Into<u64>,
Expand Down
4 changes: 2 additions & 2 deletions precompile/deposit/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ where
Self(Default::default())
}

pub fn used_addresses() -> [H160; 1] {
pub fn set() -> [H160; 1] {
[addr(1)]
}
}
Expand All @@ -121,7 +121,7 @@ where

fn is_precompile(&self, address: H160, _gas: u64) -> fp_evm::IsPrecompileResult {
fp_evm::IsPrecompileResult::Answer {
is_precompile: Self::used_addresses().contains(&address),
is_precompile: Self::set().contains(&address),
extra_cost: 0,
}
}
Expand Down
4 changes: 2 additions & 2 deletions precompile/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ where
Self(Default::default())
}

pub fn used_addresses() -> [H160; 1] {
pub fn set() -> [H160; 1] {
[addr(1)]
}
}
Expand All @@ -120,7 +120,7 @@ where

fn is_precompile(&self, address: H160, _gas: u64) -> fp_evm::IsPrecompileResult {
fp_evm::IsPrecompileResult::Answer {
is_precompile: Self::used_addresses().contains(&address),
is_precompile: Self::set().contains(&address),
extra_cost: 0,
}
}
Expand Down
4 changes: 2 additions & 2 deletions precompile/state-storage/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ where
Self(Default::default())
}

pub fn used_addresses() -> [H160; 1] {
pub fn set() -> [H160; 1] {
[addr(1)]
}
}
Expand All @@ -101,7 +101,7 @@ where

fn is_precompile(&self, address: H160, _gas: u64) -> fp_evm::IsPrecompileResult {
fp_evm::IsPrecompileResult::Answer {
is_precompile: Self::used_addresses().contains(&address),
is_precompile: Self::set().contains(&address),
extra_cost: 0,
}
}
Expand Down
3 changes: 1 addition & 2 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ impl pallet_evm::FeeCalculator for FixedGasPrice {
pub struct AssetIdConverter;
impl darwinia_precompile_assets::AccountToAssetId<AccountId, AssetId> for AssetIdConverter {
fn account_to_asset_id(account_id: AccountId) -> AssetId {
let addr: H160 = account_id.into();
addr.to_low_u64_be()
H160::from(account_id).to_low_u64_be()
}
}
77 changes: 75 additions & 2 deletions runtime/common/src/pallet_config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,76 @@
pub mod precompiles {
pub const ADDR_EC_RECOVER: [u8; 20] = address_of(0x01);
pub const ADDR_SHA256: [u8; 20] = address_of(0x02);
pub const ADDR_RIPEMD160: [u8; 20] = address_of(0x03);
pub const ADDR_IDENTITY: [u8; 20] = address_of(0x04);
pub const ADDR_MODEXP: [u8; 20] = address_of(0x05);
pub const ADDR_BN128_ADD: [u8; 20] = address_of(0x06);
pub const ADDR_BN128_MUL: [u8; 20] = address_of(0x07);
pub const ADDR_BN128_PAIRING: [u8; 20] = address_of(0x08);
pub const ADDR_BLAKE2F: [u8; 20] = address_of(0x09);
pub const ADDR_BLS12381_G1_ADD: [u8; 20] = address_of(0x0c);
pub const ADDR_BLS12381_G1_MUL: [u8; 20] = address_of(0x0d);
pub const ADDR_BLS12381_G1_MULTI_EXP: [u8; 20] = address_of(0x0e);
pub const ADDR_BLS12381_G2_ADD: [u8; 20] = address_of(0x0f);
pub const ADDR_BLS12381_G2_MUL: [u8; 20] = address_of(0x10);
pub const ADDR_BLS12381_G2_MULTI_EXP: [u8; 20] = address_of(0x11);
pub const ADDR_BLS12381_PAIRING: [u8; 20] = address_of(0x12);
pub const ADDR_BLS12381_MAP_G1: [u8; 20] = address_of(0x13);
pub const ADDR_BLS12381_MAP_G2: [u8; 20] = address_of(0x14);
// [0x400, 0x800) for stable precompiles.
pub const ADDR_STATE_STORAGE: [u8; 20] = address_of(0x400);
pub const ADDR_DISPATCH: [u8; 20] = address_of(0x401);
// [0x402, 0x600) for assets precompiles.
pub const ADDR_KTON: [u8; 20] = address_of(0x402);
pub const ADDR_USDT: [u8; 20] = address_of(0x403);
pub const ADDR_PINK: [u8; 20] = address_of(0x404);
pub const ADDR_DOT: [u8; 20] = address_of(0x405);
pub const ADDR_DEPOSIT_DEPRECATED: [u8; 20] = address_of(0x600);
pub const ADDR_STAKING_DEPRECATED: [u8; 20] = address_of(0x601);
pub const ADDR_CONVICTION_VOTING: [u8; 20] = address_of(0x602);
// [0x800..) for the experimental precompiles.
pub const ADDR_EXPERIMENTAL: [u8; 20] = address_of(0x800);

pub const fn address_of(v: u64) -> [u8; 20] {
[
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
((v >> 56) & 0xff) as u8,
((v >> 48) & 0xff) as u8,
((v >> 40) & 0xff) as u8,
((v >> 32) & 0xff) as u8,
((v >> 24) & 0xff) as u8,
((v >> 16) & 0xff) as u8,
((v >> 8) & 0xff) as u8,
(v & 0xff) as u8,
]
}

#[test]
fn address_of_should_work() {
// polkadot-sdk
use sp_core::H160;

fn non_const_address_of(v: u64) -> H160 {
H160::from_low_u64_be(v)
}

for code in 0x01..=0x800 {
assert_eq!(address_of(code), non_const_address_of(code).0);
}
}
}

// darwinia
use dc_primitives::*;
// polkadot-sdk
Expand All @@ -18,13 +91,13 @@ pub const MAXIMUM_BLOCK_WEIGHT: frame_support::weights::Weight =
cumulus_primitives_core::relay_chain::MAX_POV_SIZE as u64,
);

const BLOCK_GAS_LIMIT: u64 = 20_000_000;

#[cfg(not(feature = "runtime-benchmarks"))]
const EXISTENTIAL_DEPOSIT: Balance = 0;
#[cfg(feature = "runtime-benchmarks")]
const EXISTENTIAL_DEPOSIT: Balance = 1;

const BLOCK_GAS_LIMIT: u64 = 20_000_000;

frame_support::parameter_types! {
pub const ExistentialDeposit: Balance = EXISTENTIAL_DEPOSIT;
pub const MaxBalance: Balance = Balance::max_value();
Expand Down
Loading

0 comments on commit 92e74df

Please sign in to comment.