Skip to content

Commit

Permalink
Add precompiles and tests to evm template
Browse files Browse the repository at this point in the history
  • Loading branch information
fgamundi committed Nov 10, 2023
1 parent 9c9df19 commit c51e03b
Show file tree
Hide file tree
Showing 22 changed files with 2,859 additions and 930 deletions.
367 changes: 311 additions & 56 deletions Cargo.lock

Large diffs are not rendered by default.

60 changes: 33 additions & 27 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@ tp-core = { path = "primitives/core", default-features = false }
tp-traits = { path = "primitives/traits", default-features = false }

# Moonkit (wasm)
nimbus-consensus = { git = "https://github.com/moondance-labs/moonkit", branch = "tanssi-polkadot-v1.1.0" }
nimbus-primitives = { git = "https://github.com/moondance-labs/moonkit", branch = "tanssi-polkadot-v1.1.0", default-features = false }
pallet-author-inherent = { git = "https://github.com/moondance-labs/moonkit", branch = "tanssi-polkadot-v1.1.0", default-features = false }
pallet-maintenance-mode = { git = "https://github.com/moondance-labs/moonkit", branch = "tanssi-polkadot-v1.1.0", default-features = false }
pallet-migrations = { git = "https://github.com/moondance-labs/moonkit", branch = "tanssi-polkadot-v1.1.0", default-features = false }
xcm-primitives = { git = "https://github.com/moondance-labs/moonkit", branch = "tanssi-polkadot-v1.1.0", default-features = false }
nimbus-consensus = { git = "https://github.com/fgamundi/moonkit", branch = "tanssi-polkadot-v1.1.0" }
nimbus-primitives = { git = "https://github.com/fgamundi/moonkit", branch = "tanssi-polkadot-v1.1.0", default-features = false }
pallet-author-inherent = { git = "https://github.com/fgamundi/moonkit", branch = "tanssi-polkadot-v1.1.0", default-features = false }
pallet-maintenance-mode = { git = "https://github.com/fgamundi/moonkit", branch = "tanssi-polkadot-v1.1.0", default-features = false }
pallet-migrations = { git = "https://github.com/fgamundi/moonkit", branch = "tanssi-polkadot-v1.1.0", default-features = false }
xcm-primitives = { git = "https://github.com/fgamundi/moonkit", branch = "tanssi-polkadot-v1.1.0", default-features = false }
pallet-evm-precompile-balances-erc20 = { git = "https://github.com/fgamundi/moonkit", branch = "tanssi-polkadot-v1.1.0", default-features = false }
pallet-evm-precompile-batch = { git = "https://github.com/fgamundi/moonkit", branch = "tanssi-polkadot-v1.1.0", default-features = false }
pallet-evm-precompile-call-permit = { git = "https://github.com/fgamundi/moonkit", branch = "tanssi-polkadot-v1.1.0", default-features = false }
pallet-evm-precompile-xcm-utils = { git = "https://github.com/fgamundi/moonkit", branch = "tanssi-polkadot-v1.1.0", default-features = false }

# Substrate (wasm)
sp-consensus-beefy = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.1.0", default-features = false }
Expand Down Expand Up @@ -190,38 +194,40 @@ cumulus-test-relay-sproof-builder = { git = "https://github.com/moondance-labs/p
xcm-emulator = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.1.0", default-features = false }

# Frontier (wasm)
fp-account = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
fp-evm = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
fp-rpc = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
fp-self-contained = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
pallet-base-fee = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
pallet-dynamic-fee = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
pallet-ethereum = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
pallet-evm = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
pallet-evm-chain-id = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
pallet-evm-precompile-modexp = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
pallet-evm-precompile-sha3fips = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
pallet-evm-precompile-simple = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
pallet-hotfix-sufficients = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
fp-account = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }
fp-evm = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }
fp-rpc = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }
fp-self-contained = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }
pallet-base-fee = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }
pallet-dynamic-fee = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }
pallet-ethereum = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }
pallet-evm = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }
pallet-evm-chain-id = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }
pallet-evm-precompile-modexp = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }
pallet-evm-precompile-sha3fips = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }
pallet-evm-precompile-simple = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }
pallet-hotfix-sufficients = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }
precompile-utils = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }

# Frontier (client)
fc-api = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
fc-cli = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
fc-consensus = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
fc-db = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
fc-mapping-sync = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
fc-rpc = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", features = [
fc-api = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }
fc-cli = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }
fc-consensus = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }
fc-db = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }
fc-mapping-sync = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }
fc-rpc = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", features = [
"rpc-binary-search-estimate",
] }
fc-rpc-core = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
fc-storage = { git = "https://github.com/moondance-labs/frontier", branch = "tanssi-polkadot-v1.1.0", default-features = false }
fc-rpc-core = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }
fc-storage = { git = "https://github.com/fgamundi/frontier", branch = "precompile-xcm-util", default-features = false }

# General (wasm)
hex-literal = { version = "0.3.4" }
log = { version = "0.4.17", default-features = false }
serde = { version = "1.0.152", default-features = false }
smallvec = "1.10.0"
rand_chacha = { version = "0.3.1", default-features = false }
num_enum = { version = "0.7.1", default-features = false }

# General (client)
async-io = "1.3"
Expand Down
69 changes: 23 additions & 46 deletions container-chains/templates/frontier/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@
use {
container_chain_template_frontier_runtime::{
AccountId, EVMChainIdConfig, EVMConfig, MaintenanceModeConfig, MigrationsConfig,
PolkadotXcmConfig,
PolkadotXcmConfig, Precompiles,
},
cumulus_primitives_core::ParaId,
fp_evm::GenesisAccount,
hex_literal::hex,
sc_chain_spec::{ChainSpecExtension, ChainSpecGroup},
sc_network::config::MultiaddrWithPeerId,
sc_service::ChainType,
serde::{Deserialize, Serialize},
sp_core::{H160, U256},
std::{collections::BTreeMap, str::FromStr},
};

/// Specialized `ChainSpec` for the normal parachain runtime.
Expand Down Expand Up @@ -155,6 +154,12 @@ fn testnet_genesis(
id: ParaId,
root_key: AccountId,
) -> container_chain_template_frontier_runtime::RuntimeGenesisConfig {
// This is the simplest bytecode to revert without returning any data.
// We will pre-deploy it under all of our precompiles to ensure they can be called from
// within contracts.
// (PUSH1 0x00 PUSH1 0x00 REVERT)
let revert_bytecode = vec![0x60, 0x00, 0x60, 0x00, 0xFD];

container_chain_template_frontier_runtime::RuntimeGenesisConfig {
system: container_chain_template_frontier_runtime::SystemConfig {
code: container_chain_template_frontier_runtime::WASM_BINARY
Expand Down Expand Up @@ -182,49 +187,21 @@ fn testnet_genesis(
..Default::default()
},
evm: EVMConfig {
accounts: {
let mut map = BTreeMap::new();
map.insert(
// H160 address of Alice dev account
// Derived from SS58 (42 prefix) address
// SS58: 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
// hex: 0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d
// Using the full hex key, truncating to the first 20 bytes (the first 40 hex chars)
H160::from_str("d43593c715fdd31c61141abd04a99fd6822c8558")
.expect("internal H160 is valid; qed"),
fp_evm::GenesisAccount {
balance: U256::from_str("0xffffffffffffffffffffffffffffffff")
.expect("internal U256 is valid; qed"),
code: Default::default(),
nonce: Default::default(),
storage: Default::default(),
},
);
map.insert(
// H160 address of CI test runner account
H160::from_str("6be02d1d3665660d22ff9624b7be0551ee1ac91b")
.expect("internal H160 is valid; qed"),
fp_evm::GenesisAccount {
balance: U256::from_str("0xffffffffffffffffffffffffffffffff")
.expect("internal U256 is valid; qed"),
code: Default::default(),
nonce: Default::default(),
storage: Default::default(),
},
);
map.insert(
// H160 address for benchmark usage
H160::from_str("1000000000000000000000000000000000000001")
.expect("internal H160 is valid; qed"),
fp_evm::GenesisAccount {
nonce: U256::from(1),
balance: U256::from(1_000_000_000_000_000_000_000_000u128),
storage: Default::default(),
code: vec![0x00],
},
);
map
},
// We need _some_ code inserted at the precompile address so that
// the evm will actually call the address.
accounts: Precompiles::used_addresses()
.map(|addr| {
(
addr.into(),
GenesisAccount {
nonce: Default::default(),
balance: Default::default(),
storage: Default::default(),
code: revert_bytecode.clone(),
},
)
})
.collect(),
..Default::default()
},
ethereum: Default::default(),
Expand Down
12 changes: 12 additions & 0 deletions container-chains/templates/frontier/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ parity-scale-codec = { workspace = true, features = ["derive"] }
scale-info = { workspace = true, features = ["derive"] }
serde = { workspace = true, optional = true, features = ["derive"] }
smallvec = { workspace = true }
num_enum = { workspace = true }

# Local
ccp-xcm = { workspace = true }
Expand All @@ -28,6 +29,10 @@ pallet-author-inherent = { workspace = true }
pallet-maintenance-mode = { workspace = true, features = ["xcm-support"] }
pallet-migrations = { workspace = true }
xcm-primitives = { workspace = true }
pallet-evm-precompile-balances-erc20 = { workspace = true }
pallet-evm-precompile-batch = { workspace = true }
pallet-evm-precompile-call-permit = { workspace = true }
pallet-evm-precompile-xcm-utils = { workspace = true }

# Substrate
frame-executive = { workspace = true }
Expand Down Expand Up @@ -91,6 +96,8 @@ pallet-evm-precompile-modexp = { workspace = true }
pallet-evm-precompile-sha3fips = { workspace = true }
pallet-evm-precompile-simple = { workspace = true }
pallet-hotfix-sufficients = { workspace = true }
precompile-utils = { workspace = true }

[build-dependencies]
substrate-wasm-builder = { workspace = true }

Expand Down Expand Up @@ -125,6 +132,11 @@ std = [
"pallet-evm-precompile-modexp/std",
"pallet-evm-precompile-sha3fips/std",
"pallet-evm-precompile-simple/std",
"pallet-evm-precompile-balances-erc20/std",
"pallet-evm-precompile-batch/std",
"pallet-evm-precompile-call-permit/std",
"pallet-evm-precompile-xcm-utils/std",
"precompile-utils/std",
"pallet-evm/std",
"pallet-hotfix-sufficients/std",
"pallet-maintenance-mode/std",
Expand Down
8 changes: 5 additions & 3 deletions container-chains/templates/frontier/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mod precompiles;
pub mod xcm_config;

use {
crate::precompiles::FrontierPrecompiles,
crate::precompiles::TemplatePrecompiles,
cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases,
cumulus_primitives_core::{relay_chain::BlockNumber as RelayBlockNumber, DmpMessageHandler},
fp_account::EthereumSignature,
Expand Down Expand Up @@ -96,6 +96,8 @@ pub use {
// Polkadot imports
use polkadot_runtime_common::BlockHashCount;

pub type Precompiles = TemplatePrecompiles<Runtime>;

/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
pub type Signature = EthereumSignature;

Expand Down Expand Up @@ -781,7 +783,7 @@ impl FindAuthor<H160> for FindAuthorAdapter {

parameter_types! {
pub BlockGasLimit: U256 = U256::from(BLOCK_GAS_LIMIT);
pub PrecompilesValue: FrontierPrecompiles<Runtime> = FrontierPrecompiles::<_>::new();
pub PrecompilesValue: TemplatePrecompiles<Runtime> = TemplatePrecompiles::<_>::new();
pub WeightPerGas: Weight = Weight::from_parts(weight_per_gas(BLOCK_GAS_LIMIT, NORMAL_DISPATCH_RATIO, WEIGHT_MILLISECS_PER_BLOCK), 0);
}

Expand All @@ -796,7 +798,7 @@ impl pallet_evm::Config for Runtime {
type AddressMapping = IdentityAddressMapping;
type Currency = Balances;
type RuntimeEvent = RuntimeEvent;
type PrecompilesType = FrontierPrecompiles<Self>;
type PrecompilesType = TemplatePrecompiles<Self>;
type PrecompilesValue = PrecompilesValue;
type ChainId = EVMChainId;
type BlockGasLimit = BlockGasLimit;
Expand Down
Loading

0 comments on commit c51e03b

Please sign in to comment.