Skip to content

Commit

Permalink
Precompile for balances ERC-20 (#1731)
Browse files Browse the repository at this point in the history
* Use last features from precompile_utils & add balances erc20

* taplo fmt

* fix integration tests

* add forbid-evm-reentrancy

* extra docs

* add AcceptDelegateCall to balance-erc20 precompile

* remove delegate call

* apply review CR

* add h160 utility and remove to_evm_address

* add migration

* fix node

* fix features

* correct taplo installation
  • Loading branch information
lemunozm authored Feb 15, 2024
1 parent 45c6f38 commit db2209f
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 523 deletions.
56 changes: 45 additions & 11 deletions Cargo.lock

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

10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ rand = { version = "0.8.5", default-features = false }
rev_slice = { version = "0.1.5", default-features = false }
impl-trait-for-tuples = "0.2.1"
num-traits = { version = "0.2", default-features = false }
num_enum = { version = "0.5.3", default-features = false }

# Cumulus
cumulus-pallet-aura-ext = { git = "https://github.com/paritytech/cumulus", default-features = false, branch = "polkadot-v0.9.43" }
Expand Down Expand Up @@ -218,8 +219,12 @@ orml-xtokens = { git = "https://github.com/open-web3-stack/open-runtime-module-l
fp-rpc = { git = "https://github.com/moonbeam-foundation/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.43" }
fp-self-contained = { git = "https://github.com/moonbeam-foundation/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.43" }
pallet-base-fee = { git = "https://github.com/moonbeam-foundation/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.43" }
pallet-ethereum = { git = "https://github.com/moonbeam-foundation/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.43" }
pallet-evm = { git = "https://github.com/moonbeam-foundation/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.43" }
pallet-ethereum = { git = "https://github.com/moonbeam-foundation/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.43", features = [
"forbid-evm-reentrancy",
] }
pallet-evm = { git = "https://github.com/moonbeam-foundation/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.43", features = [
"forbid-evm-reentrancy",
] }
pallet-evm-chain-id = { git = "https://github.com/moonbeam-foundation/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.43" }
pallet-evm-precompile-blake2 = { git = "https://github.com/moonbeam-foundation/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.43" }
pallet-evm-precompile-bn128 = { git = "https://github.com/moonbeam-foundation/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.43" }
Expand All @@ -241,6 +246,7 @@ moonbeam-relay-encoder = { git = "https://github.com/moonbeam-foundation/moonbea
xcm-primitives = { git = "https://github.com/moonbeam-foundation/moonbeam", default-features = false, rev = "96ac7576f93bb6828415bf3edeef9e8c4b5b4adf" }
pallet-xcm-transactor = { git = "https://github.com/moonbeam-foundation/moonbeam", default-features = false, rev = "96ac7576f93bb6828415bf3edeef9e8c4b5b4adf" }
precompile-utils = { git = "https://github.com/moonbeam-foundation/moonbeam", default-features = false, rev = "96ac7576f93bb6828415bf3edeef9e8c4b5b4adf" }
pallet-evm-precompile-balances-erc20 = { git = "https://github.com/moonbeam-foundation/moonbeam", default-features = false, rev = "96ac7576f93bb6828415bf3edeef9e8c4b5b4adf" }

# Centrifuge organization
fudge = { git = "https://github.com/centrifuge/fudge", branch = "polkadot-v0.9.43" }
Expand Down
7 changes: 1 addition & 6 deletions ci/run-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ case $TARGET in
;;

lint-taplo)
# The recomended installation fails because an issue in taplo, issue:
# https://github.com/tamasfe/taplo/issues/507
# Should be fixed in the next taplo release.
# Recomended command:
# cargo install taplo-cli --locked
cargo install --git=https://github.com/tamasfe/taplo.git taplo-cli
cargo install taplo-cli --locked
taplo fmt --check
;;

Expand Down
109 changes: 24 additions & 85 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ use cfg_types::{
use cfg_utils::vec_to_fixed_array;
use cumulus_primitives_core::ParaId;
use hex_literal::hex;
use runtime_common::account_conversion::AccountConverter;
use runtime_common::{account_conversion::AccountConverter, evm::precompile::H160Addresses};
use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup};
use sc_service::{ChainType, Properties};
use serde::{Deserialize, Serialize};
use sp_core::{crypto::UncheckedInto, sr25519, Encode, Pair, Public, H160, U256};
use sp_core::{crypto::UncheckedInto, sr25519, Encode, Pair, Public, H160};
use sp_runtime::traits::{IdentifyAccount, Verify};
use xcm::{
latest::MultiLocation,
Expand All @@ -59,6 +59,10 @@ pub type CentrifugeChainSpec =
pub type DevelopmentChainSpec =
sc_service::GenericChainSpec<development_runtime::GenesisConfig, Extensions>;

use altair_runtime::evm::AltairPrecompiles;
use centrifuge_runtime::evm::CentrifugePrecompiles;
use development_runtime::evm::DevelopmentPrecompiles;

/// Helper function to generate a crypto pair from seed
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
TPublic::Pair::from_string(&format!("//{seed}"), None)
Expand Down Expand Up @@ -666,7 +670,7 @@ fn centrifuge_genesis(
},
ethereum: Default::default(),
evm: centrifuge_runtime::EVMConfig {
accounts: precompile_account_genesis(),
accounts: precompile_account_genesis::<CentrifugePrecompiles>(),
},
liquidity_rewards_base: Default::default(),
polkadot_xcm: centrifuge_runtime::PolkadotXcmConfig {
Expand Down Expand Up @@ -780,7 +784,7 @@ fn altair_genesis(
},
ethereum: Default::default(),
evm: centrifuge_runtime::EVMConfig {
accounts: precompile_account_genesis(),
accounts: precompile_account_genesis::<AltairPrecompiles>(),
},
liquidity_rewards_base: Default::default(),
polkadot_xcm: altair_runtime::PolkadotXcmConfig {
Expand Down Expand Up @@ -943,7 +947,7 @@ fn development_genesis(
},
ethereum: Default::default(),
evm: centrifuge_runtime::EVMConfig {
accounts: precompile_account_genesis(),
accounts: precompile_account_genesis::<DevelopmentPrecompiles>(),
},
block_rewards_base: Default::default(),
liquidity_rewards_base: Default::default(),
Expand Down Expand Up @@ -1020,84 +1024,19 @@ fn asset_registry_assets() -> Vec<(CurrencyId, Vec<u8>)> {
]
}

fn precompile_account_genesis() -> BTreeMap<H160, fp_evm::GenesisAccount> {
use fp_evm::GenesisAccount;
use runtime_common::evm::precompile::*;

let mut map = BTreeMap::new();
/*
pub struct GenesisAccount {
/// Account nonce.
pub nonce: U256,
/// Account balance.
pub balance: U256,
/// Full account storage.
pub storage: std::collections::BTreeMap<sp_core::H256, sp_core::H256>,
/// Account code.
pub code: Vec<u8>,
}
*/
let to_genesis_account = |code: [u8; 5]| -> GenesisAccount {
GenesisAccount {
nonce: U256::zero(),
balance: U256::zero(),
storage: BTreeMap::new(),
code: code.to_vec(),
}
};

map.insert(
H160::from(ECRECOVER_ADDR),
to_genesis_account(PRECOMPILE_CODE_STORAGE),
);
map.insert(
H160::from(SHA256_ADDR),
to_genesis_account(PRECOMPILE_CODE_STORAGE),
);
map.insert(
H160::from(RIPEMD160_ADDR),
to_genesis_account(PRECOMPILE_CODE_STORAGE),
);
map.insert(
H160::from(IDENTITY_ADDR),
to_genesis_account(PRECOMPILE_CODE_STORAGE),
);
map.insert(
H160::from(MODEXP_ADDR),
to_genesis_account(PRECOMPILE_CODE_STORAGE),
);
map.insert(
H160::from(BN128ADD_ADDR),
to_genesis_account(PRECOMPILE_CODE_STORAGE),
);
map.insert(
H160::from(BN128MUL_ADDR),
to_genesis_account(PRECOMPILE_CODE_STORAGE),
);
map.insert(
H160::from(BN128PAIRING_ADDR),
to_genesis_account(PRECOMPILE_CODE_STORAGE),
);
map.insert(
H160::from(BLAKE2F_ADDR),
to_genesis_account(PRECOMPILE_CODE_STORAGE),
);
map.insert(
H160::from(SHA3FIPS256_ADDR),
to_genesis_account(PRECOMPILE_CODE_STORAGE),
);
map.insert(
H160::from(DISPATCH_ADDR),
to_genesis_account(PRECOMPILE_CODE_STORAGE),
);
map.insert(
H160::from(ECRECOVERPUBLICKEY_ADDR),
to_genesis_account(PRECOMPILE_CODE_STORAGE),
);
map.insert(
H160::from(LP_AXELAR_GATEWAY),
to_genesis_account(PRECOMPILE_CODE_STORAGE),
);

map
fn precompile_account_genesis<PrecompileSet: H160Addresses>(
) -> BTreeMap<H160, fp_evm::GenesisAccount> {
PrecompileSet::h160_addresses()
.map(|addr| {
(
addr,
fp_evm::GenesisAccount {
nonce: Default::default(),
balance: Default::default(),
storage: Default::default(),
code: runtime_common::evm::precompile::utils::REVERT_BYTECODE.to_vec(),
},
)
})
.collect()
}
9 changes: 6 additions & 3 deletions runtime/altair/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use pallet_ethereum::PostLogContent;
use pallet_evm::{EnsureAddressRoot, EnsureAddressTruncated};
use runtime_common::{
account_conversion::AccountConverter,
evm::{precompile::Altair, BaseFeeThreshold, WEIGHT_PER_GAS},
evm::{precompile::Precompiles, BaseFeeThreshold, WEIGHT_PER_GAS},
};
use sp_core::{crypto::ByteArray, H160, U256};
use sp_runtime::Permill;
Expand All @@ -41,9 +41,11 @@ impl<F: FindAuthor<u32>> FindAuthor<H160> for FindAuthorTruncated<F> {
}
}

pub type AltairPrecompiles = Precompiles<crate::Runtime, TokenSymbol>;

parameter_types! {
pub BlockGasLimit: U256 = U256::from(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT.ref_time() / WEIGHT_PER_GAS);
pub PrecompilesValue: Altair<Runtime> = Altair::<_>::new();
pub PrecompilesValue: AltairPrecompiles = Precompiles::<_, _>::new();
pub WeightPerGas: Weight = Weight::from_parts(WEIGHT_PER_GAS, 0);
//
// pub GasLimitPovSizeRatio: u64 = {
Expand All @@ -65,6 +67,7 @@ parameter_types! {
// in their staging environment. As we can not constantly assert this value we hardcode
// it for now.
pub const GasLimitStorageGrowthRatio: u64 = 366;
pub const TokenSymbol: &'static str = "AIR";
}

impl pallet_evm::Config for Runtime {
Expand All @@ -81,7 +84,7 @@ impl pallet_evm::Config for Runtime {
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type OnChargeTransaction = ();
type OnCreate = ();
type PrecompilesType = Altair<Self>;
type PrecompilesType = AltairPrecompiles;
type PrecompilesValue = PrecompilesValue;
type Runner = pallet_evm::runner::stack::Runner<Self>;
type RuntimeEvent = RuntimeEvent;
Expand Down
9 changes: 6 additions & 3 deletions runtime/centrifuge/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use pallet_ethereum::PostLogContent;
use pallet_evm::{EnsureAddressRoot, EnsureAddressTruncated};
use runtime_common::{
account_conversion::AccountConverter,
evm::{precompile::CentrifugePrecompiles, BaseFeeThreshold, WEIGHT_PER_GAS},
evm::{precompile::Precompiles, BaseFeeThreshold, WEIGHT_PER_GAS},
origin::EnsureAccountOrRootOr,
};
use sp_core::{crypto::ByteArray, H160, U256};
Expand All @@ -42,9 +42,11 @@ impl<F: FindAuthor<u32>> FindAuthor<H160> for FindAuthorTruncated<F> {
}
}

pub type CentrifugePrecompiles = Precompiles<crate::Runtime, TokenSymbol>;

parameter_types! {
pub BlockGasLimit: U256 = U256::from(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT.ref_time() / WEIGHT_PER_GAS);
pub PrecompilesValue: CentrifugePrecompiles<crate::Runtime> = CentrifugePrecompiles::<_>::new();
pub PrecompilesValue: CentrifugePrecompiles = Precompiles::<_, _>::new();
pub WeightPerGas: Weight = Weight::from_parts(WEIGHT_PER_GAS, 0);
//
// pub GasLimitPovSizeRatio: u64 = {
Expand All @@ -66,6 +68,7 @@ parameter_types! {
// in their staging environment. As we can not constantly assert this value we hardcode
// it for now.
pub const GasLimitStorageGrowthRatio: u64 = 366;
pub const TokenSymbol: &'static str = "CFG";
}

impl pallet_evm::Config for crate::Runtime {
Expand All @@ -82,7 +85,7 @@ impl pallet_evm::Config for crate::Runtime {
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type OnChargeTransaction = ();
type OnCreate = ();
type PrecompilesType = CentrifugePrecompiles<Self>;
type PrecompilesType = CentrifugePrecompiles;
type PrecompilesValue = PrecompilesValue;
type Runner = pallet_evm::runner::stack::Runner<Self>;
type RuntimeEvent = crate::RuntimeEvent;
Expand Down
Loading

0 comments on commit db2209f

Please sign in to comment.