Skip to content

Commit

Permalink
Add conviction voting precompile for Darwinia (#1375)
Browse files Browse the repository at this point in the history
* Add conviction voting evm support for the darwinia network

* Add migration
  • Loading branch information
boundless-forest authored Jan 3, 2024
1 parent 453b195 commit d12b775
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 42 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions runtime/darwinia/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ pallet-evm-precompile-modexp = { workspace = true }
pallet-evm-precompile-simple = { workspace = true }

# moonbeam
moonbeam-evm-tracer = { workspace = true, optional = true }
moonbeam-rpc-primitives-debug = { workspace = true }
pallet-asset-manager = { workspace = true }
pallet-ethereum-xcm = { workspace = true }
precompile-utils = { workspace = true }
xcm-primitives = { workspace = true }
moonbeam-evm-tracer = { workspace = true, optional = true }
moonbeam-rpc-primitives-debug = { workspace = true }
pallet-asset-manager = { workspace = true }
pallet-evm-precompile-conviction-voting = { workspace = true }
pallet-ethereum-xcm = { workspace = true }
precompile-utils = { workspace = true }
xcm-primitives = { workspace = true }

# open-web3-stack
orml-xcm-support = { workspace = true }
Expand Down Expand Up @@ -212,6 +213,7 @@ std = [
"moonbeam-evm-tracer/std",
"moonbeam-rpc-primitives-debug/std",
"pallet-asset-manager/std",
"pallet-evm-precompile-conviction-voting/std",
"pallet-ethereum-xcm/std",
"precompile-utils/std",
"xcm-primitives/std",
Expand Down
12 changes: 12 additions & 0 deletions runtime/darwinia/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
}

fn migrate() -> frame_support::weights::Weight {
// polkadot-sdk
use sp_core::H160;
use sp_std::str::FromStr;

const REVERT_BYTECODE: [u8; 5] = [0x60, 0x00, 0x60, 0x00, 0xFD];
// CONVICTION_VOTING_ADDRESS equals to the addr(1538) in the pallet-evm runtime.
const CONVICTION_VOTING_ADDRESS: &str = "0x0000000000000000000000000000000000000602";
if let Ok(addr) = H160::from_str(CONVICTION_VOTING_ADDRESS) {
EVM::create_account(addr, REVERT_BYTECODE.to_vec());
return RuntimeBlockWeights::get().max_block;
}

frame_support::weights::Weight::zero()
// RuntimeBlockWeights::get().max_block
// <Runtime as frame_system::Config>::DbWeight::get().reads_writes(0, 1)
Expand Down
75 changes: 39 additions & 36 deletions runtime/darwinia/src/pallets/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,25 @@ where
Self(Default::default())
}

pub fn used_addresses() -> [sp_core::H160; 16] {
pub fn used_addresses() -> [sp_core::H160; 17] {
[
addr(1),
addr(2),
addr(3),
addr(4),
addr(5),
addr(6),
addr(7),
addr(8),
addr(9),
addr(1024),
addr(1025),
addr(1026), // For KTON asset.
addr(1027), // For Tether USDT.
addr(1536),
addr(1537),
addr(2048),
addr(0x01),
addr(0x02),
addr(0x03),
addr(0x04),
addr(0x05),
addr(0x06),
addr(0x07),
addr(0x08),
addr(0x09),
addr(0x400),
addr(0x401),
addr(0x402), // For KTON asset.
addr(0x403), // For Tether USDT.
addr(0x600),
addr(0x601),
addr(0x602),
addr(0x800),
]
}
}
Expand Down Expand Up @@ -93,36 +94,38 @@ where

match code_addr {
// Ethereum precompiles:
a if a == addr(1) => Some(pallet_evm_precompile_simple::ECRecover::execute(handle)),
a if a == addr(2) => Some(pallet_evm_precompile_simple::Sha256::execute(handle)),
a if a == addr(3) => Some(pallet_evm_precompile_simple::Ripemd160::execute(handle)),
a if a == addr(4) => Some(pallet_evm_precompile_simple::Identity::execute(handle)),
a if a == addr(5) => Some(pallet_evm_precompile_modexp::Modexp::execute(handle)),
a if a == addr(6) => Some(pallet_evm_precompile_bn128::Bn128Add::execute(handle)),
a if a == addr(7) => Some(pallet_evm_precompile_bn128::Bn128Mul::execute(handle)),
a if a == addr(8) => Some(pallet_evm_precompile_bn128::Bn128Pairing::execute(handle)),
a if a == addr(9) => Some(pallet_evm_precompile_blake2::Blake2F::execute(handle)),
// Darwinia precompiles: [1024, 2048) for stable precompiles.
a if a == addr(1024) => Some(<darwinia_precompile_state_storage::StateStorage<
a if a == addr(0x01) => Some(pallet_evm_precompile_simple::ECRecover::execute(handle)),
a if a == addr(0x02) => Some(pallet_evm_precompile_simple::Sha256::execute(handle)),
a if a == addr(0x03) => Some(pallet_evm_precompile_simple::Ripemd160::execute(handle)),
a if a == addr(0x04) => Some(pallet_evm_precompile_simple::Identity::execute(handle)),
a if a == addr(0x05) => Some(pallet_evm_precompile_modexp::Modexp::execute(handle)),
a if a == addr(0x06) => Some(pallet_evm_precompile_bn128::Bn128Add::execute(handle)),
a if a == addr(0x07) => Some(pallet_evm_precompile_bn128::Bn128Mul::execute(handle)),
a if a == addr(0x08) => Some(pallet_evm_precompile_bn128::Bn128Pairing::execute(handle)),
a if a == addr(0x09) => Some(pallet_evm_precompile_blake2::Blake2F::execute(handle)),
// Darwinia precompiles: [0x400, 0x800) for stable precompiles.
a if a == addr(0x400) => Some(<darwinia_precompile_state_storage::StateStorage<
Runtime,
darwinia_precompile_state_storage::StateStorageFilter,
>>::execute(handle)),
a if a == addr(1025) => Some(<pallet_evm_precompile_dispatch::Dispatch<
a if a == addr(0x401) => Some(<pallet_evm_precompile_dispatch::Dispatch<
Runtime,
DarwiniaDispatchValidator,
>>::execute(handle)),
// [1026, 1536) reserved for assets precompiles.
a if (1026..1536).contains(&AssetIdConverter::account_to_asset_id(a.into())) =>
// [0x402, 0x600) reserved for assets precompiles.
a if (0x402..0x600).contains(&AssetIdConverter::account_to_asset_id(a.into())) =>
Some(<darwinia_precompile_assets::ERC20Assets<Runtime, AssetIdConverter>>::execute(
handle,
)),
// [1536, 2048) reserved for other stable precompiles.
a if a == addr(1536) =>
// [0x600, 0x800) reserved for other stable precompiles.
a if a == addr(0x600) =>
Some(<darwinia_precompile_deposit::Deposit<Runtime>>::execute(handle)),
a if a == addr(1537) =>
a if a == addr(0x601) =>
Some(<darwinia_precompile_staking::Staking<Runtime>>::execute(handle)),
// [2048..) reserved for the experimental precompiles.
a if a == addr(2048) =>
a if a == addr(0x602) =>
Some(<pallet_evm_precompile_conviction_voting::ConvictionVotingPrecompile<Runtime>>::execute(handle)),
// [0x800..) reserved for the experimental precompiles.
a if a == addr(0x800) =>
Some(<darwinia_precompile_bls12_381::BLS12381<Runtime>>::execute(handle)),
_ => None,
}
Expand Down

0 comments on commit d12b775

Please sign in to comment.