Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/precompile account codes #1580

Merged
merged 9 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ fc-rpc = { git = "https://github.com/PureStake/frontier", default-features = fal
fc-rpc-core = { git = "https://github.com/PureStake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.38" }
fp-consensus = { git = "https://github.com/PureStake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.38" }
fp-rpc = { git = "https://github.com/PureStake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.38" }
fp-evm = { git = "https://github.com/PureStake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.38" }
fp-storage = { git = "https://github.com/PureStake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.38" }
pallet-evm = { git = "https://github.com/PureStake/frontier", default-features = false, branch = "moonbeam-polkadot-v0.9.38" }

[build-dependencies]
substrate-build-script-utils = { optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
Expand Down
2 changes: 0 additions & 2 deletions runtime/altair/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ pub type UpgradeAltair1034 = (
runtime_common::migrations::precompile_account_codes::Migration<
crate::Runtime,
{ crate::VERSION.spec_version },
{ crate::VERSION.spec_version },
>,
);

Expand All @@ -66,7 +65,6 @@ pub type UpgradeAltair1034 = (
pub type UpgradeAltair1034 = (runtime_common::migrations::precompile_account_codes::Migration<
crate::Runtime,
{ crate::VERSION.spec_version },
{ crate::VERSION.spec_version },
>);

mod asset_registry {
Expand Down
6 changes: 1 addition & 5 deletions runtime/centrifuge/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ pub type UpgradeCentrifuge1022 = (
anemoy_pool::Migration,
add_wrapped_usdc_variants::Migration,
// Sets account codes for all precompiles
runtime_common::migrations::precompile_account_codes::Migration<
crate::Runtime,
{ crate::VERSION.spec_version },
1020,
>,
runtime_common::migrations::precompile_account_codes::Migration<crate::Runtime, 1020>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will exclude the migration from Catalyst as the last_version is 1021 here. I'd rename WITH_VERSION to MAX_VERSION and set it to 1021 for Centrifuge runtime.

);

/// Migrate the Anemoy Pool's currency from LpEthUSC to Circle's USDC,
Expand Down
23 changes: 9 additions & 14 deletions runtime/common/src/migrations/precompile_account_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@
traits::{Get, OnRuntimeUpgrade},
weights::Weight,
};
use sp_arithmetic::traits::{EnsureAdd, EnsureMul};

Check warning on line 18 in runtime/common/src/migrations/precompile_account_codes.rs

View workflow job for this annotation

GitHub Actions / docs

unused imports: `EnsureAdd`, `EnsureMul`
use sp_core::H160;

use crate::evm::precompile::PRECOMPILE_CODE_STORAGE;

pub struct Migration<T, const CURR_VERSION: u32, const FROM_VERSION: u32>(
sp_std::marker::PhantomData<T>,
);
pub struct Migration<T, const WITH_VERSION: u32>(sp_std::marker::PhantomData<T>);

impl<T: pallet_evm::Config, const CURR_VERSION: u32, const FROM_VERSION: u32> OnRuntimeUpgrade
for Migration<T, CURR_VERSION, FROM_VERSION>
impl<T: pallet_evm::Config, const WITH_VERSION: u32> OnRuntimeUpgrade
for Migration<T, WITH_VERSION>
{
fn on_runtime_upgrade() -> Weight {
log::info!("precompile::AccountCodes: Inserting precompile account codes: on_runtime_upgrade: started");

if CURR_VERSION > FROM_VERSION {
let last_version = frame_system::LastRuntimeUpgrade::<T>::get()
.map(|v| v.spec_version.0)
.unwrap_or(<T::Version as frame_support::traits::Get<_>>::get().spec_version);

if last_version > WITH_VERSION {
log::warn!("[precompile::AccountCodes: Current runtime version too high. Skipping migration. Migration can probably be removed.");
return Weight::zero();
}
Expand Down Expand Up @@ -203,14 +205,7 @@

// NOTE: This is a worst case weight and we do not care to adjust it correctly
// depending on skipped read/writes.
Weight::from_ref_time(
T::DbWeight::get()
.read
.ensure_mul(13)
.unwrap_or(u64::MAX)
.ensure_add(T::DbWeight::get().write.ensure_mul(13).unwrap_or(u64::MAX))
.unwrap_or(u64::MAX),
)
T::DbWeight::get().reads_writes(13, 13)
}

#[cfg(feature = "try-runtime")]
Expand Down
98 changes: 94 additions & 4 deletions src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
// module level.
#![allow(clippy::derive_partial_eq_without_eq)]

use std::collections::BTreeMap;

use altair_runtime::constants::currency::{AIR, MILLI_AIR};
use cfg_primitives::{
currency_decimals, parachains, Balance, BlockNumber, CFG, MILLI_CFG, SAFE_XCM_VERSION,
Expand All @@ -43,7 +45,7 @@ use runtime_common::account_conversion::AccountConverter;
use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup};
use sc_service::{ChainType, Properties};
use serde::{Deserialize, Serialize};
use sp_core::{crypto::UncheckedInto, sr25519, Encode, Pair, Public};
use sp_core::{crypto::UncheckedInto, sr25519, Encode, Pair, Public, H160, U256};
use sp_runtime::traits::{IdentifyAccount, Verify};
use xcm::{
latest::MultiLocation,
Expand Down Expand Up @@ -667,7 +669,9 @@ fn centrifuge_genesis(
chain_id: chain_id.into(),
},
ethereum: Default::default(),
evm: Default::default(),
evm: centrifuge_runtime::EVMConfig {
accounts: precompile_account_genesis(),
},
liquidity_rewards_base: Default::default(),
polkadot_xcm: centrifuge_runtime::PolkadotXcmConfig {
safe_xcm_version: Some(SAFE_XCM_VERSION),
Expand Down Expand Up @@ -779,7 +783,9 @@ fn altair_genesis(
chain_id: chain_id.into(),
},
ethereum: Default::default(),
evm: Default::default(),
evm: centrifuge_runtime::EVMConfig {
accounts: precompile_account_genesis(),
},
liquidity_rewards_base: Default::default(),
polkadot_xcm: altair_runtime::PolkadotXcmConfig {
safe_xcm_version: Some(SAFE_XCM_VERSION),
Expand Down Expand Up @@ -940,7 +946,9 @@ fn development_genesis(
chain_id: chain_id.into(),
},
ethereum: Default::default(),
evm: Default::default(),
evm: centrifuge_runtime::EVMConfig {
accounts: precompile_account_genesis(),
},
block_rewards_base: Default::default(),
liquidity_rewards_base: Default::default(),
polkadot_xcm: development_runtime::PolkadotXcmConfig {
Expand Down Expand Up @@ -1015,3 +1023,85 @@ 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
}
Loading