Skip to content

Commit

Permalink
Modify eth verify (#1644)
Browse files Browse the repository at this point in the history
* modify eth verify

* add tests

* Apply suggestions from code review

Co-authored-by: Xiliang Chen <[email protected]>

* fix srtool build

* checked_div instead of saturating_div

Co-authored-by: Xiliang Chen <[email protected]>
  • Loading branch information
zjb0807 and xlc authored Nov 28, 2021
1 parent 85e6eee commit 82d54ff
Show file tree
Hide file tree
Showing 13 changed files with 259 additions and 95 deletions.
2 changes: 2 additions & 0 deletions modules/asset-registry/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ ord_parameter_types! {
pub const TreasuryAccount: AccountId = AccountId::from([2u8; 32]);
pub const NetworkContractAccount: AccountId = AccountId::from([0u8; 32]);
pub const StorageDepositPerByte: u128 = 10;
pub const TxFeePerGas: u128 = 10;
pub const DeveloperDeposit: u64 = 1000;
pub const DeploymentFee: u64 = 200;
}
Expand All @@ -108,6 +109,7 @@ impl module_evm::Config for Runtime {
type TransferAll = ();
type NewContractExtraBytes = NewContractExtraBytes;
type StorageDepositPerByte = StorageDepositPerByte;
type TxFeePerGas = TxFeePerGas;
type Event = Event;
type Precompiles = ();
type ChainId = ();
Expand Down
2 changes: 2 additions & 0 deletions modules/currencies/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ ord_parameter_types! {
pub const TreasuryAccount: AccountId32 = AccountId32::from([2u8; 32]);
pub const NetworkContractAccount: AccountId32 = AccountId32::from([0u8; 32]);
pub const StorageDepositPerByte: u128 = 10;
pub const TxFeePerGas: u128 = 10;
pub const DeveloperDeposit: u64 = 1000;
pub const DeploymentFee: u64 = 200;
}
Expand All @@ -159,6 +160,7 @@ impl module_evm::Config for Runtime {
type TransferAll = ();
type NewContractExtraBytes = NewContractExtraBytes;
type StorageDepositPerByte = StorageDepositPerByte;
type TxFeePerGas = TxFeePerGas;
type Event = Event;
type Precompiles = ();
type ChainId = ();
Expand Down
2 changes: 2 additions & 0 deletions modules/evm-bridge/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ ord_parameter_types! {
pub const TreasuryAccount: AccountId32 = AccountId32::from([2u8; 32]);
pub const NetworkContractAccount: AccountId32 = AccountId32::from([0u8; 32]);
pub const StorageDepositPerByte: u128 = 10;
pub const TxFeePerGas: u128 = 10;
pub const DeveloperDeposit: u64 = 1000;
pub const DeploymentFee: u64 = 200;
}
Expand All @@ -115,6 +116,7 @@ impl module_evm::Config for Runtime {
type TransferAll = ();
type NewContractExtraBytes = NewContractExtraBytes;
type StorageDepositPerByte = StorageDepositPerByte;
type TxFeePerGas = TxFeePerGas;
type Event = Event;
type Precompiles = ();
type ChainId = ();
Expand Down
33 changes: 29 additions & 4 deletions modules/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ use sha3::{Digest, Keccak256};
use sp_io::KillStorageResult::{AllRemoved, SomeRemaining};
use sp_runtime::{
traits::{
Convert, DispatchInfoOf, One, PostDispatchInfoOf, Saturating, SignedExtension, UniqueSaturatedInto, Zero,
Convert, DispatchInfoOf, One, PostDispatchInfoOf, Saturating, SignedExtension, UniqueSaturatedFrom,
UniqueSaturatedInto, Zero,
},
transaction_validity::TransactionValidityError,
Either, TransactionOutcome,
Expand Down Expand Up @@ -185,6 +186,11 @@ pub mod module {
#[pallet::constant]
type StorageDepositPerByte: Get<BalanceOf<Self>>;

/// Tx fee required for per gas.
/// Provide to the client
#[pallet::constant]
type TxFeePerGas: Get<BalanceOf<Self>>;

/// The overarching event type.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

Expand Down Expand Up @@ -860,6 +866,25 @@ pub mod module {
}

impl<T: Config> Pallet<T> {
pub fn pad_zero(b: BalanceOf<T>, num: u32) -> BalanceOf<T> {
BalanceOf::<T>::unique_saturated_from(
UniqueSaturatedInto::<u128>::unique_saturated_into(b).saturating_mul(10u128.saturating_pow(num)),
)
}

pub fn truncate_zero(b: BalanceOf<T>, num: u32) -> BalanceOf<T> {
BalanceOf::<T>::unique_saturated_from(
UniqueSaturatedInto::<u128>::unique_saturated_into(b)
.checked_div(10u128.saturating_pow(num))
.expect("divisor is non-zero; qed"),
)
}

/// Get StorageDepositPerByte of actual decimals
pub fn get_storage_deposit_per_byte() -> BalanceOf<T> {
Self::truncate_zero(T::StorageDepositPerByte::get(), 6)
}

/// Check whether an account is empty.
pub fn is_account_empty(address: &H160) -> bool {
let account_id = T::AddressMapping::get_account_id(address);
Expand Down Expand Up @@ -1244,7 +1269,7 @@ impl<T: Config> Pallet<T> {
}

let user = T::AddressMapping::get_account_id(caller);
let amount = T::StorageDepositPerByte::get().saturating_mul(limit.into());
let amount = Self::get_storage_deposit_per_byte().saturating_mul(limit.into());

log::debug!(
target: "evm",
Expand All @@ -1263,7 +1288,7 @@ impl<T: Config> Pallet<T> {
}

let user = T::AddressMapping::get_account_id(caller);
let amount = T::StorageDepositPerByte::get().saturating_mul(unused.into());
let amount = Self::get_storage_deposit_per_byte().saturating_mul(unused.into());

log::debug!(
target: "evm",
Expand All @@ -1285,7 +1310,7 @@ impl<T: Config> Pallet<T> {

let user = T::AddressMapping::get_account_id(caller);
let contract_acc = T::AddressMapping::get_account_id(contract);
let amount = T::StorageDepositPerByte::get().saturating_mul((storage.abs() as u32).into());
let amount = Self::get_storage_deposit_per_byte().saturating_mul((storage.abs() as u32).into());

log::debug!(
target: "evm",
Expand Down
4 changes: 3 additions & 1 deletion modules/evm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ ord_parameter_types! {
pub const TreasuryAccount: AccountId32 = AccountId32::from([2u8; 32]);
pub const NetworkContractAccount: AccountId32 = AccountId32::from([0u8; 32]);
pub const NewContractExtraBytes: u32 = 100;
pub const StorageDepositPerByte: u64 = 10;
pub const StorageDepositPerByte: u64 = 10_000_000;
pub const TxFeePerGas: u64 = 20_000_000;
pub const DeveloperDeposit: u64 = 1000;
pub const DeploymentFee: u64 = 200;
pub const ChainId: u64 = 1;
Expand All @@ -185,6 +186,7 @@ impl Config for Runtime {
type TransferAll = Currencies;
type NewContractExtraBytes = NewContractExtraBytes;
type StorageDepositPerByte = StorageDepositPerByte;
type TxFeePerGas = TxFeePerGas;

type Event = Event;
type Precompiles = ();
Expand Down
44 changes: 22 additions & 22 deletions modules/evm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ fn call_reverts_with_message() {

assert_eq!(result.exit_reason, ExitReason::Succeed(ExitSucceed::Returned));

let alice_balance = INITIAL_BALANCE - 323 * <Runtime as Config>::StorageDepositPerByte::get();
let alice_balance = INITIAL_BALANCE - 323 * EVM::get_storage_deposit_per_byte();

assert_eq!(balance(alice()), alice_balance);

Expand Down Expand Up @@ -301,7 +301,7 @@ fn should_deploy_payable_contract() {
assert_eq!(result.exit_reason, ExitReason::Succeed(ExitSucceed::Returned));
assert_eq!(result.used_storage, 287);

let alice_balance = INITIAL_BALANCE - amount - 287 * <Runtime as Config>::StorageDepositPerByte::get();
let alice_balance = INITIAL_BALANCE - amount - 287 * EVM::get_storage_deposit_per_byte();
assert_eq!(balance(alice()), alice_balance);
assert_eq!(balance(contract_address), amount);

Expand Down Expand Up @@ -377,7 +377,7 @@ fn should_transfer_from_contract() {
assert_eq!(result.exit_reason, ExitReason::Succeed(ExitSucceed::Returned));
assert_eq!(result.used_storage, 892);

let alice_balance = INITIAL_BALANCE - 892 * <Runtime as Config>::StorageDepositPerByte::get();
let alice_balance = INITIAL_BALANCE - 892 * EVM::get_storage_deposit_per_byte();
assert_eq!(balance(alice()), alice_balance);

let contract_address = result.value;
Expand Down Expand Up @@ -477,7 +477,7 @@ fn contract_should_deploy_contracts() {
assert_eq!(result.exit_reason, ExitReason::Succeed(ExitSucceed::Returned));
assert_eq!(result.used_storage, 467);

let alice_balance = INITIAL_BALANCE - 467 * <Runtime as Config>::StorageDepositPerByte::get();
let alice_balance = INITIAL_BALANCE - 467 * EVM::get_storage_deposit_per_byte();

assert_eq!(balance(alice()), alice_balance);
let factory_contract_address = result.value;
Expand All @@ -488,7 +488,7 @@ fn contract_should_deploy_contracts() {
assert_eq!(balance(factory_contract_address), 0);
assert_eq!(
reserved_balance(factory_contract_address),
467 * <Runtime as Config>::StorageDepositPerByte::get()
467 * EVM::get_storage_deposit_per_byte()
);

// Factory.createContract
Expand All @@ -510,18 +510,18 @@ fn contract_should_deploy_contracts() {

assert_eq!(
balance(alice()),
alice_balance - amount - 281 * <Runtime as Config>::StorageDepositPerByte::get()
alice_balance - amount - 281 * EVM::get_storage_deposit_per_byte()
);
assert_eq!(balance(factory_contract_address), amount);
assert_eq!(
reserved_balance(factory_contract_address),
(467 + 128) * <Runtime as Config>::StorageDepositPerByte::get()
(467 + 128) * EVM::get_storage_deposit_per_byte()
);
let contract_address = H160::from_str("7b8f8ca099f6e33cf1817cf67d0556429cfc54e4").unwrap();
assert_eq!(balance(contract_address), 0);
assert_eq!(
reserved_balance(contract_address),
153 * <Runtime as Config>::StorageDepositPerByte::get()
153 * EVM::get_storage_deposit_per_byte()
);
});
}
Expand Down Expand Up @@ -555,7 +555,7 @@ fn contract_should_deploy_contracts_without_payable() {
.unwrap();
assert_eq!(result.exit_reason, ExitReason::Succeed(ExitSucceed::Returned));

let alice_balance = INITIAL_BALANCE - 464 * <Runtime as Config>::StorageDepositPerByte::get();
let alice_balance = INITIAL_BALANCE - 464 * EVM::get_storage_deposit_per_byte();

assert_eq!(balance(alice()), alice_balance);
let factory_contract_address = result.value;
Expand All @@ -582,12 +582,12 @@ fn contract_should_deploy_contracts_without_payable() {
assert_eq!(result.used_storage, 290);
assert_eq!(
balance(alice()),
alice_balance - (result.used_storage as u64 * <Runtime as Config>::StorageDepositPerByte::get())
alice_balance - (result.used_storage as u64 * EVM::get_storage_deposit_per_byte())
);
assert_eq!(balance(factory_contract_address), 0);
assert_eq!(
reserved_balance(factory_contract_address),
(464 + 128) * <Runtime as Config>::StorageDepositPerByte::get()
(464 + 128) * EVM::get_storage_deposit_per_byte()
);
});
}
Expand Down Expand Up @@ -621,7 +621,7 @@ fn deploy_factory() {
assert_eq!(result.used_storage, 461);
assert_eq!(
balance(alice()),
INITIAL_BALANCE - (result.used_storage as u64 * <Runtime as Config>::StorageDepositPerByte::get())
INITIAL_BALANCE - (result.used_storage as u64 * EVM::get_storage_deposit_per_byte())
);
});
}
Expand Down Expand Up @@ -788,7 +788,7 @@ fn should_transfer_maintainer() {
.unwrap();
assert_eq!(result.exit_reason, ExitReason::Succeed(ExitSucceed::Returned));
assert_eq!(result.used_storage, 461);
let alice_balance = INITIAL_BALANCE - 461 * <Runtime as Config>::StorageDepositPerByte::get();
let alice_balance = INITIAL_BALANCE - 461 * EVM::get_storage_deposit_per_byte();
let contract_address = result.value;

assert_eq!(balance(alice()), alice_balance);
Expand Down Expand Up @@ -857,7 +857,7 @@ fn should_deploy() {
let contract_address = result.value;

assert_eq!(result.used_storage, 284);
let alice_balance = INITIAL_BALANCE - 284 * <Runtime as Config>::StorageDepositPerByte::get();
let alice_balance = INITIAL_BALANCE - 284 * EVM::get_storage_deposit_per_byte();

assert_eq!(balance(alice()), alice_balance);

Expand Down Expand Up @@ -908,7 +908,7 @@ fn should_deploy() {
let code_size = Accounts::<Runtime>::get(contract_address).map_or(0, |account_info| -> u32 {
account_info.contract_info.map_or(0, |contract_info| CodeInfos::<Runtime>::get(contract_info.code_hash).map_or(0, |code_info| code_info.code_size))
});
assert_eq!(balance(alice()), INITIAL_BALANCE - DeploymentFee::get() - ((NewContractExtraBytes::get() + code_size) as u64 * StorageDepositPerByte::get()));
assert_eq!(balance(alice()), INITIAL_BALANCE - DeploymentFee::get() - ((NewContractExtraBytes::get() + code_size) as u64 * EVM::get_storage_deposit_per_byte()));
assert_eq!(Balances::free_balance(TreasuryAccount::get()), INITIAL_BALANCE + DeploymentFee::get());

// call method `multiply` will work
Expand Down Expand Up @@ -1065,7 +1065,7 @@ fn should_set_code() {
.unwrap();
let contract_address = result.value;
assert_eq!(result.used_storage, 284);
let alice_balance = INITIAL_BALANCE - 284 * <Runtime as Config>::StorageDepositPerByte::get();
let alice_balance = INITIAL_BALANCE - 284 * EVM::get_storage_deposit_per_byte();

assert_eq!(balance(alice()), alice_balance);
assert_eq!(reserved_balance(contract_address), 2840);
Expand Down Expand Up @@ -1210,7 +1210,7 @@ fn should_selfdestruct() {

let contract_address = result.value;
assert_eq!(result.used_storage, 287);
let alice_balance = INITIAL_BALANCE - 287 * <Runtime as Config>::StorageDepositPerByte::get() - amount;
let alice_balance = INITIAL_BALANCE - 287 * EVM::get_storage_deposit_per_byte() - amount;

assert_eq!(balance(alice()), alice_balance);

Expand Down Expand Up @@ -1250,7 +1250,7 @@ fn should_selfdestruct() {
assert_eq!(balance(contract_address), 1000);
assert_eq!(
reserved_balance(contract_address),
287 * <Runtime as Config>::StorageDepositPerByte::get()
287 * EVM::get_storage_deposit_per_byte()
);

// can't deploy at the same address until everything is wiped out
Expand Down Expand Up @@ -1327,7 +1327,7 @@ fn storage_limit_should_work() {
.unwrap();
assert_eq!(result.exit_reason, ExitReason::Succeed(ExitSucceed::Returned));
assert_eq!(result.used_storage, 516);
let alice_balance = INITIAL_BALANCE - 516 * <Runtime as Config>::StorageDepositPerByte::get();
let alice_balance = INITIAL_BALANCE - 516 * EVM::get_storage_deposit_per_byte();
assert_eq!(balance(alice()), alice_balance);

let factory_contract_address = result.value;
Expand All @@ -1338,7 +1338,7 @@ fn storage_limit_should_work() {
assert_eq!(balance(factory_contract_address), 0);
assert_eq!(
reserved_balance(factory_contract_address),
516 * <Runtime as Config>::StorageDepositPerByte::get()
516 * EVM::get_storage_deposit_per_byte()
);

// Factory.createContract(1)
Expand Down Expand Up @@ -1445,7 +1445,7 @@ fn evm_execute_mode_should_work() {
).unwrap();

new_test_ext().execute_with(|| {
let mut alice_balance = INITIAL_BALANCE - 516 * <Runtime as Config>::StorageDepositPerByte::get();
let mut alice_balance = INITIAL_BALANCE - 516 * EVM::get_storage_deposit_per_byte();

let result = <Runtime as Config>::Runner::create(
alice(),
Expand Down Expand Up @@ -1557,7 +1557,7 @@ fn evm_execute_mode_should_work() {
}
);

alice_balance -= 290 * <Runtime as Config>::StorageDepositPerByte::get();
alice_balance -= 290 * EVM::get_storage_deposit_per_byte();

assert_eq!(balance(alice()), alice_balance);

Expand Down
Loading

0 comments on commit 82d54ff

Please sign in to comment.