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 all commits
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
4 changes: 3 additions & 1 deletion 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: 1 addition & 1 deletion runtime/altair/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -413,5 +413,5 @@ on-chain-release-build = [
# Set timing constants (e.g. session period) to faster versions to speed up testing.
fast-runtime = []

# Allow general users to create pools and smart contracts, instead of requiring governance
# Allow to handle Algol and Altair differently
testnet-runtime = []
10 changes: 9 additions & 1 deletion runtime/altair/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,21 @@ pub type UpgradeAltair1034 = (
pallet_xcm::migration::v1::MigrateToV1<crate::Runtime>,
// Sets currently unset safe XCM version to v2
xcm_v2_to_v3::SetSafeXcmVersion,
// Sets account codes for all precompiles
runtime_common::migrations::precompile_account_codes::Migration<
crate::Runtime,
{ crate::VERSION.spec_version },
>,
);

/// The Upgrade set for Algol - it excludes the migrations already executed in
/// the side releases that only landed on Algol (1028 to 1031) but not yet on
/// Altair.
#[cfg(feature = "testnet-runtime")]
pub type UpgradeAltair1034 = ();
pub type UpgradeAltair1034 = (runtime_common::migrations::precompile_account_codes::Migration<
crate::Runtime,
{ crate::VERSION.spec_version },
>);

mod asset_registry {
use cfg_primitives::Balance;
Expand Down
2 changes: 1 addition & 1 deletion runtime/centrifuge/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "centrifuge-runtime"
version = "0.10.21"
version = "0.10.22"
authors = ["Centrifuge <[email protected]>"]
edition = "2021"
build = "build.rs"
Expand Down
4 changes: 2 additions & 2 deletions runtime/centrifuge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("centrifuge"),
impl_name: create_runtime_str!("centrifuge"),
authoring_version: 1,
spec_version: 1021,
spec_version: 1022,
impl_version: 1,
#[cfg(not(feature = "disable-runtime-api"))]
apis: RUNTIME_API_VERSIONS,
Expand Down Expand Up @@ -1952,7 +1952,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
migrations::UpgradeCentrifuge1021,
migrations::UpgradeCentrifuge1022,
>;

pub struct TransactionConverter;
Expand Down
43 changes: 29 additions & 14 deletions runtime/centrifuge/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
// GNU General Public License for more details.
use crate::{Runtime, Weight};

pub type UpgradeCentrifuge1021 = (anemoy_pool::Migration, add_wrapped_usdc_variants::Migration);
pub type UpgradeCentrifuge1022 = (
anemoy_pool::Migration<crate::Runtime, 1021>,
add_wrapped_usdc_variants::Migration<crate::Runtime, 1021>,
// Sets account codes for all precompiles
runtime_common::migrations::precompile_account_codes::Migration<crate::Runtime, 1022>,
);

/// Migrate the Anemoy Pool's currency from LpEthUSC to Circle's USDC,
/// native on Polkadot's AssetHub.
Expand All @@ -35,9 +40,11 @@ mod anemoy_pool {

const ANEMOY_POOL_ID: PoolId = 4_139_607_887;

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

impl OnRuntimeUpgrade for Migration {
impl<T: frame_system::Config, const BEFORE_VERSION: u32> OnRuntimeUpgrade
for Migration<T, BEFORE_VERSION>
{
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
let pool_details: PoolDetailsOf<Runtime> =
Expand All @@ -52,10 +59,13 @@ mod anemoy_pool {
}

fn on_runtime_upgrade() -> Weight {
// To be executed at 1021, reject higher spec_versions
if crate::VERSION.spec_version >= 1022 {
log::error!(
"anemoy_pool::Migration: NOT execution since VERSION.spec_version >= 1022"
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 >= BEFORE_VERSION {
log::warn!(
"anemoy_pool::Migration: NOT execution since current version higher than BEFORE_VERSION"
);
return Weight::zero();
}
Expand Down Expand Up @@ -155,14 +165,19 @@ pub mod add_wrapped_usdc_variants {
use crate::OrderBook;
use crate::{liquidity_pools::LiquidityPoolsPalletIndex, Balance, OrmlAssetRegistry, Runtime};

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

impl OnRuntimeUpgrade for Migration {
impl<T: frame_system::Config, const BEFORE_VERSION: u32> OnRuntimeUpgrade
for Migration<T, BEFORE_VERSION>
{
fn on_runtime_upgrade() -> Weight {
// To be executed at 1021, reject higher spec_versions
if crate::VERSION.spec_version >= 1022 {
log::error!(
"add_wrapped_usdc_variants::Migration: NOT executing since VERSION.spec_version >= 1022"
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 >= BEFORE_VERSION {
log::warn!(
"add_wrapped_usdc_variants::Migration: NOT execution since current version higher than BEFORE_VERSION"
);
return Weight::zero();
}
Expand Down Expand Up @@ -262,7 +277,7 @@ pub mod add_wrapped_usdc_variants {
}
}

impl Migration {
impl<T, const BEFORE_VERSION: u32> Migration<T, BEFORE_VERSION> {
fn get_unregistered_ids() -> Vec<CurrencyId> {
vec![CURRENCY_ID_LP_BASE, CURRENCY_ID_LP_ARB, CURRENCY_ID_LP_CELO]
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repository = "https://github.com/centrifuge/centrifuge-chain"
[dependencies]
serde = { version = "1.0.119" }
smallvec = "1.6.1"
hex-literal = { version = "0.3.4", default-features = false }

# Substrate dependencies
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
Expand Down Expand Up @@ -76,7 +77,6 @@ sp-io = { git = "https://github.com/paritytech/substrate", default-features = fa

[dev-dependencies]
cfg-mocks = { path = "../../libs/mocks", features = ["runtime-benchmarks", "std"] }
hex-literal = "0.3.4"
pallet-collective = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
sp-io = { git = "https://github.com/paritytech/substrate", default-features = true, branch = "polkadot-v0.9.38" }

Expand Down
66 changes: 22 additions & 44 deletions runtime/common/src/evm/precompile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use core::marker::PhantomData;

use codec::Decode;
use frame_support::dispatch::{Dispatchable, GetDispatchInfo, PostDispatchInfo};
use hex_literal::hex;
use pallet_evm::{Precompile, PrecompileHandle, PrecompileResult, PrecompileSet};
use pallet_evm_precompile_blake2::Blake2F;
use pallet_evm_precompile_bn128::{Bn128Add, Bn128Mul, Bn128Pairing};
Expand All @@ -23,22 +24,29 @@ use pallet_evm_precompile_sha3fips::Sha3FIPS256;
use pallet_evm_precompile_simple::{ECRecover, ECRecoverPublicKey, Identity, Ripemd160, Sha256};
use sp_core::H160;

/// `pallet_evm::AccountCodes` must be populated for precompiles as
/// otherwise `OPCODE::EXTCODESIZE` will make the EVM error upon calling an
/// precompile.
///
/// The following bytes represent: `PUSH1 00`, `PUSH1 00`, `REVERT`.
pub const PRECOMPILE_CODE_STORAGE: [u8; 5] = hex!("60006000fd");

// 0000->1023: Standard Ethereum precompiles
const ECRECOVER_ADDR: Addr = addr(1);
const SHA256_ADDR: Addr = addr(2);
const RIPEMD160_ADDR: Addr = addr(3);
const IDENTITY_ADDR: Addr = addr(4);
const MODEXP_ADDR: Addr = addr(5);
const BN128ADD_ADDR: Addr = addr(6);
const BN128MUL_ADDR: Addr = addr(7);
const BN128PAIRING_ADDR: Addr = addr(8);
const BLAKE2F_ADDR: Addr = addr(9);
pub const ECRECOVER_ADDR: Addr = addr(1);
pub const SHA256_ADDR: Addr = addr(2);
pub const RIPEMD160_ADDR: Addr = addr(3);
pub const IDENTITY_ADDR: Addr = addr(4);
pub const MODEXP_ADDR: Addr = addr(5);
pub const BN128ADD_ADDR: Addr = addr(6);
pub const BN128MUL_ADDR: Addr = addr(7);
pub const BN128PAIRING_ADDR: Addr = addr(8);
pub const BLAKE2F_ADDR: Addr = addr(9);
// 1024->2047: Nonstandard precompiles shared with other chains (such
// as Moonbeam). See
// https://docs.moonbeam.network/builders/pallets-precompiles/precompiles/overview/#precompiled-contract-addresses
const SHA3FIPS256_ADDR: Addr = addr(1024);
const DISPATCH_ADDR: Addr = addr(1025);
const ECRECOVERPUBLICKEY_ADDR: Addr = addr(1026);
pub const SHA3FIPS256_ADDR: Addr = addr(1024);
pub const DISPATCH_ADDR: Addr = addr(1025);
pub const ECRECOVERPUBLICKEY_ADDR: Addr = addr(1026);
// 2048-XXXX: Nonstandard precompiles that are specific to our chain.

/// The address of our local Axelar gateway. This is the address that
Expand Down Expand Up @@ -102,43 +110,13 @@ where

/// Altair's precompiles
/// For now, Altair uses the exact same set of precompiles used in Development.
pub type Altair<R> = Development<R>;
pub type Altair<R> = CentrifugePrecompiles<R>;

/// A set of precompiles. This set might contain
/// not yet mainnet ready precompiles in order to test
/// those in development or staging environment without touching
/// the mainnet set.
pub struct Development<R>(CentrifugePrecompiles<R>);

impl<R> Development<R> {
#[allow(clippy::new_without_default)] // We'll never use Default and can't derive it.
pub fn new() -> Self {
Self(CentrifugePrecompiles::new())
}
}

impl<R> PrecompileSet for Development<R>
where
R: pallet_evm::Config + axelar_gateway_precompile::Config + frame_system::Config,
R::RuntimeCall: Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo + Decode,
<R::RuntimeCall as Dispatchable>::RuntimeOrigin: From<Option<R::AccountId>>,
axelar_gateway_precompile::Pallet<R>: Precompile,
{
fn execute(&self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {
self.0
.execute(handle)
.or_else(|| match handle.code_address().0 {
LP_AXELAR_GATEWAY => {
Some(<axelar_gateway_precompile::Pallet<R> as Precompile>::execute(handle))
}
_ => None,
})
}

fn is_precompile(&self, address: H160) -> bool {
self.0.is_precompile(address) | matches!(address.0, LP_AXELAR_GATEWAY)
}
}
pub type Development<R> = CentrifugePrecompiles<R>;

// H160 cannot be used in a match statement due to its hand-rolled
// PartialEq implementation. This just gives a nice name to the
Expand Down
1 change: 1 addition & 0 deletions runtime/common/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@

pub mod asset_registry_xcmv3;
pub mod nuke;
pub mod precompile_account_codes;
Loading
Loading