Skip to content

Commit

Permalink
test: Fix vendored precompile tests
Browse files Browse the repository at this point in the history
  • Loading branch information
conr2d committed Oct 7, 2024
1 parent eed75d6 commit 8bf66f0
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 34 deletions.
2 changes: 1 addition & 1 deletion vendor/cosmwasm/std/src/testing/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ mod tests {
use crate::{coin, coins, instantiate2_address, ContractInfoResponse, HexBinary, Response};
#[cfg(feature = "staking")]
use crate::{Decimal, Delegation};
use base64::{engine::general_purpose, Engine};
//use base64::{engine::general_purpose, Engine};
use cosmwasm_core::BLS12_381_G1_GENERATOR;
use hex_literal::hex;
use serde::Deserialize;
Expand Down
1 change: 0 additions & 1 deletion vendor/moonbeam/precompiles/assets-erc20/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ precompile-utils = { git = "https://github.com/polkadot-evm/frontier", branch =
#moonkit-xcm-primitives = { workspace = true }

[dev-dependencies]
derive_more = "1.0"
hex-literal = "0.4"
libsecp256k1 = "0.7"
serde = "1.0"
Expand Down
6 changes: 5 additions & 1 deletion vendor/moonbeam/precompiles/assets-erc20/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ use frame_support::{
//use moonkit_xcm_primitives::AccountIdAssetIdConversion;
use pallet_evm::AddressMapping;
use precompile_utils::prelude::*;
use sp_runtime::traits::{Bounded, Dispatchable, Zero};
use sp_runtime::traits::{Bounded, Dispatchable};
#[cfg(not(test))]
use sp_runtime::traits::Zero;

use sp_core::{MaxEncodedLen, H160, H256, U256};

Expand Down Expand Up @@ -246,6 +248,7 @@ where
let owner = Runtime::AddressMapping::into_account_id(owner);
let spender: Runtime::AccountId = Runtime::AddressMapping::into_account_id(spender);
// XXX: Need a general way to handle non-unified account
#[cfg(not(test))]
if frame_system::Account::<Runtime>::get(&spender).nonce.is_zero() {
return Err(revert("spender not unified"));
}
Expand Down Expand Up @@ -301,6 +304,7 @@ where
let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);
let to = Runtime::AddressMapping::into_account_id(to);
// XXX: Need a general way to handle non-unified account
#[cfg(not(test))]
if frame_system::Account::<Runtime>::get(&to).nonce.is_zero() {
return Err(revert("to not unified"));
}
Expand Down
22 changes: 6 additions & 16 deletions vendor/moonbeam/precompiles/assets-erc20/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,18 @@ mock_account!(ForeignAssetId(AssetId), |value: ForeignAssetId| {
});

// Implement the trait, where we convert AccountId to AssetID
impl AccountIdAssetIdConversion<AccountId, AssetId> for Runtime {
impl AddressToAssetId<AssetId> for Runtime {
/// The way to convert an account to assetId is by ensuring that the prefix is 0XFFFFFFFF
/// and by taking the lowest 128 bits as the assetId
fn account_to_asset_id(account: AccountId) -> Option<(Vec<u8>, AssetId)> {
if account.has_prefix_u32(FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX) {
return Some((
FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX.to_be_bytes().to_vec(),
account.without_prefix(),
));
fn address_to_asset_id(address: H160) -> Option<AssetId> {
if &address[..4] == &FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX.to_be_bytes()[..] {
return Some(AssetId::from_be_bytes(address[4..].try_into().unwrap()));
}
None
}

// Not used for now
fn asset_id_to_account(_prefix: &[u8], asset_id: AssetId) -> AccountId {
fn asset_id_to_address(asset_id: AssetId) -> H160 {
ForeignAssetId(asset_id).into()
}
}
Expand Down Expand Up @@ -123,7 +120,7 @@ impl pallet_timestamp::Config for Runtime {
}

parameter_types! {
pub const ExistentialDeposit: u128 = 0;
pub const ExistentialDeposit: u128 = 1;
}

impl pallet_balances::Config for Runtime {
Expand Down Expand Up @@ -155,8 +152,6 @@ pub type Precompiles<R> = PrecompileSetBuilder<
pub type ForeignPCall = Erc20AssetsPrecompileSetCall<Runtime, pallet_assets::Instance1>;

const MAX_POV_SIZE: u64 = 5 * 1024 * 1024;
/// Block Storage Limit in bytes. Set to 40KB.
const BLOCK_STORAGE_LIMIT: u64 = 40 * 1024;

parameter_types! {
pub BlockGasLimit: U256 = U256::from(u64::MAX);
Expand All @@ -166,10 +161,6 @@ parameter_types! {
let block_gas_limit = BlockGasLimit::get().min(u64::MAX.into()).low_u64();
block_gas_limit.saturating_div(MAX_POV_SIZE)
};
pub GasLimitStorageGrowthRatio: u64 = {
let block_gas_limit = BlockGasLimit::get().min(u64::MAX.into()).low_u64();
block_gas_limit.saturating_div(BLOCK_STORAGE_LIMIT)
};
}

impl pallet_evm::Config for Runtime {
Expand All @@ -192,7 +183,6 @@ impl pallet_evm::Config for Runtime {
type OnCreate = ();
type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
type SuicideQuickClearLimit = ConstU32<0>;
type GasLimitStorageGrowthRatio = GasLimitStorageGrowthRatio;
type Timestamp = Timestamp;
type WeightInfo = pallet_evm::weights::SubstrateWeight<Runtime>;
}
Expand Down
1 change: 0 additions & 1 deletion vendor/moonbeam/precompiles/balances-erc20/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pallet-evm = { git = "https://github.com/polkadot-evm/frontier", branch = "stabl
precompile-utils = { git = "https://github.com/polkadot-evm/frontier", branch = "stable2407", default-features = false }

[dev-dependencies]
derive_more = "1.0"
hex-literal = "0.4"
libsecp256k1 = "0.7"
serde = "1.0"
Expand Down
9 changes: 1 addition & 8 deletions vendor/moonbeam/precompiles/balances-erc20/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl pallet_timestamp::Config for Runtime {
}

parameter_types! {
pub const ExistentialDeposit: u128 = 0;
pub const ExistentialDeposit: u128 = 1;
}

impl pallet_balances::Config for Runtime {
Expand All @@ -105,8 +105,6 @@ pub type Precompiles<R> =
pub type PCall = Erc20BalancesPrecompileCall<Runtime, ()>;

const MAX_POV_SIZE: u64 = 5 * 1024 * 1024;
/// Block storage limit in bytes. Set to 40 KB.
const BLOCK_STORAGE_LIMIT: u64 = 40 * 1024;

parameter_types! {
pub BlockGasLimit: U256 = U256::from(u64::MAX);
Expand All @@ -116,10 +114,6 @@ parameter_types! {
let block_gas_limit = BlockGasLimit::get().min(u64::MAX.into()).low_u64();
block_gas_limit.saturating_div(MAX_POV_SIZE)
};
pub GasLimitStorageGrowthRatio: u64 = {
let block_gas_limit = BlockGasLimit::get().min(u64::MAX.into()).low_u64();
block_gas_limit.saturating_div(BLOCK_STORAGE_LIMIT)
};
}

impl pallet_evm::Config for Runtime {
Expand All @@ -142,7 +136,6 @@ impl pallet_evm::Config for Runtime {
type OnCreate = ();
type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
type SuicideQuickClearLimit = ConstU32<0>;
type GasLimitStorageGrowthRatio = GasLimitStorageGrowthRatio;
type Timestamp = Timestamp;
type WeightInfo = pallet_evm::weights::SubstrateWeight<Runtime>;
}
Expand Down
15 changes: 9 additions & 6 deletions vendor/moonbeam/precompiles/balances-erc20/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ fn transfer() {
Precompile1,
PCall::transfer { to: Address(Bob.into()), value: 400.into() },
)
.expect_cost(176215756) // 1 weight => 1 gas in mock
.expect_cost(173364756) // 1 weight => 1 gas in mock
.expect_log(log3(
Precompile1,
SELECTOR_LOG_TRANSFER,
Expand Down Expand Up @@ -336,7 +336,7 @@ fn transfer_from() {
value: 400.into(),
},
)
.expect_cost(176215756) // 1 weight => 1 gas in mock
.expect_cost(173364756) // 1 weight => 1 gas in mock
.expect_log(log3(
Precompile1,
SELECTOR_LOG_TRANSFER,
Expand Down Expand Up @@ -426,7 +426,7 @@ fn transfer_from_self() {
value: 400.into(),
},
)
.expect_cost(176215756) // 1 weight => 1 gas in mock
.expect_cost(173364756) // 1 weight => 1 gas in mock
.expect_log(log3(
Precompile1,
SELECTOR_LOG_TRANSFER,
Expand Down Expand Up @@ -549,6 +549,9 @@ fn deposit(data: Vec<u8>) {
to: Precompile1.into(),
amount: 500
}),
RuntimeEvent::System(frame_system::Event::KilledAccount {
account: Precompile1.into()
}),
// Precompile1 send it back since deposit should be a no-op.
RuntimeEvent::Balances(pallet_balances::Event::Transfer {
from: Precompile1.into(),
Expand Down Expand Up @@ -760,7 +763,7 @@ fn permit_valid() {
let value: U256 = 500u16.into();
let deadline: U256 = 0u8.into(); // todo: proper timestamp

let permit = Eip2612::<Runtime, NativeErc20Metadata>::generate_permit(
let permit = Eip2612::<Runtime>::generate_permit(
Precompile1.into(),
owner,
spender,
Expand Down Expand Up @@ -843,7 +846,7 @@ fn permit_invalid_nonce() {
let value: U256 = 500u16.into();
let deadline: U256 = 0u8.into();

let permit = Eip2612::<Runtime, NativeErc20Metadata>::generate_permit(
let permit = Eip2612::<Runtime>::generate_permit(
Precompile1.into(),
owner,
spender,
Expand Down Expand Up @@ -982,7 +985,7 @@ fn permit_invalid_deadline() {
let value: U256 = 500u16.into();
let deadline: U256 = 5u8.into(); // deadline < timestamp => expired

let permit = Eip2612::<Runtime, NativeErc20Metadata>::generate_permit(
let permit = Eip2612::<Runtime>::generate_permit(
Precompile1.into(),
owner,
spender,
Expand Down

0 comments on commit 8bf66f0

Please sign in to comment.