diff --git a/Cargo.lock b/Cargo.lock index 49efb3fb29..6e9b201b0a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1966,7 +1966,6 @@ name = "dc-rpc" version = "2.2.0" dependencies = [ "array-bytes", - "darwinia-evm", "dc-consensus", "dc-db", "dp-rpc", diff --git a/bin/node/runtime/pangolin/src/pallets/balances.rs b/bin/node/runtime/pangolin/src/pallets/balances.rs index f1a64d37d3..7915fc91bc 100644 --- a/bin/node/runtime/pangolin/src/pallets/balances.rs +++ b/bin/node/runtime/pangolin/src/pallets/balances.rs @@ -13,7 +13,7 @@ pub type NegativeImbalance = as Currency< >>::NegativeImbalance; frame_support::parameter_types! { - pub const ExistentialDeposit: Balance = 1 * COIN; + pub const ExistentialDeposit: Balance = 0; pub const MaxLocks: u32 = 50; } impl Config for Runtime { diff --git a/client/dvm/rpc/Cargo.toml b/client/dvm/rpc/Cargo.toml index 8b6cc188cb..3d73ac1c09 100644 --- a/client/dvm/rpc/Cargo.toml +++ b/client/dvm/rpc/Cargo.toml @@ -27,7 +27,6 @@ rand = { version = "0.7" } rlp = { version = "0.5.0" } sha3 = { version = "0.8" } # darwinia -darwinia-evm = { path = "../../../frame/evm" } dc-consensus = { path = "../consensus" } dc-db = { path = "../db" } dp-rpc = { path = "../../../primitives/rpc" } diff --git a/client/dvm/rpc/src/lib.rs b/client/dvm/rpc/src/lib.rs index cd8635b1fc..34ae9fec1c 100644 --- a/client/dvm/rpc/src/lib.rs +++ b/client/dvm/rpc/src/lib.rs @@ -18,10 +18,6 @@ mod eth; mod eth_pubsub; mod overrides; -pub use overrides::{OverrideHandle, RuntimeApiStorageOverride, SchemaV1Override, StorageOverride}; -// --- darwinia --- -use darwinia_evm::ExitReason; -// --- std --- pub use eth::{ EthApi, EthApiServer, EthFilterApi, EthFilterApiServer, EthTask, NetApi, NetApiServer, Web3Api, Web3ApiServer, @@ -31,8 +27,9 @@ use ethereum::{ Transaction as EthereumTransaction, TransactionMessage as EthereumTransactionMessage, }; use ethereum_types::H160; -use evm::ExitError; +use evm::{ExitError, ExitReason}; use jsonrpc_core::{Error, ErrorCode, Value}; +pub use overrides::{OverrideHandle, RuntimeApiStorageOverride, SchemaV1Override, StorageOverride}; pub mod frontier_backend_client { diff --git a/frame/evm/src/lib.rs b/frame/evm/src/lib.rs index 22516c373b..2dfd311298 100644 --- a/frame/evm/src/lib.rs +++ b/frame/evm/src/lib.rs @@ -33,11 +33,11 @@ pub use dp_evm::{ use frame_support::{ decl_error, decl_event, decl_module, decl_storage, dispatch::DispatchResultWithPostInfo, - traits::{Currency, ExistenceRequirement, Get}, + traits::{Currency, Get}, weights::{Pays, PostDispatchInfo, Weight}, }; use frame_system::RawOrigin; -use sp_core::{Hasher, H160, H256, U256}; +use sp_core::{H160, H256, U256}; use sp_runtime::{ traits::{BadOrigin, UniqueSaturatedInto}, AccountId32, DispatchResult, @@ -46,8 +46,7 @@ use sp_std::vec::Vec; // --- std --- #[cfg(feature = "std")] use codec::{Decode, Encode}; -use evm::Config as EvmConfig; -pub use evm::{ExitError, ExitFatal, ExitReason, ExitRevert, ExitSucceed}; +use evm::{Config as EvmConfig, ExitError, ExitReason}; #[cfg(feature = "std")] use serde::{Deserialize, Serialize}; @@ -82,52 +81,6 @@ pub trait EnsureAddressOrigin { ) -> Result; } -/// Ensure that the EVM address is the same as the Substrate address. This only works if the account -/// ID is `H160`. -pub struct EnsureAddressSame; - -impl EnsureAddressOrigin for EnsureAddressSame -where - OuterOrigin: Into, OuterOrigin>> + From>, -{ - type Success = H160; - - fn try_address_origin(address: &H160, origin: OuterOrigin) -> Result { - origin.into().and_then(|o| match o { - RawOrigin::Signed(who) if &who == address => Ok(who), - r => Err(OuterOrigin::from(r)), - }) - } -} - -/// Ensure that the origin is root. -pub struct EnsureAddressRoot(sp_std::marker::PhantomData); - -impl EnsureAddressOrigin for EnsureAddressRoot -where - OuterOrigin: Into, OuterOrigin>> + From>, -{ - type Success = (); - - fn try_address_origin(_address: &H160, origin: OuterOrigin) -> Result<(), OuterOrigin> { - origin.into().and_then(|o| match o { - RawOrigin::Root => Ok(()), - r => Err(OuterOrigin::from(r)), - }) - } -} - -/// Ensure that the origin never happens. -pub struct EnsureAddressNever(sp_std::marker::PhantomData); - -impl EnsureAddressOrigin for EnsureAddressNever { - type Success = AccountId; - - fn try_address_origin(_address: &H160, origin: OuterOrigin) -> Result { - Err(origin) - } -} - /// Ensure that the address is truncated hash of the origin. Only works if the account id is /// `AccountId32`. pub struct EnsureAddressTruncated; @@ -152,29 +105,6 @@ pub trait AddressMapping { fn into_account_id(address: H160) -> A; } -/// Identity address mapping. -pub struct IdentityAddressMapping; - -impl AddressMapping for IdentityAddressMapping { - fn into_account_id(address: H160) -> H160 { - address - } -} - -/// Hashed address mapping. -pub struct HashedAddressMapping(sp_std::marker::PhantomData); - -impl> AddressMapping for HashedAddressMapping { - fn into_account_id(address: H160) -> AccountId32 { - let mut data = [0u8; 24]; - data[0..4].copy_from_slice(b"evm:"); - data[4..24].copy_from_slice(&address[..]); - let hash = H::hash(&data); - - AccountId32::from(Into::<[u8; 32]>::into(hash)) - } -} - pub struct ConcatAddressMapping; /// The ConcatAddressMapping used for transfer from evm 20-length to substrate 32-length address @@ -199,61 +129,6 @@ pub trait AccountBasic { fn transfer(source: &H160, target: &H160, value: U256) -> Result<(), ExitError>; } -pub struct RawAccountBasic(sp_std::marker::PhantomData); - -impl AccountBasic for RawAccountBasic { - /// Get the account basic in EVM format. - fn account_basic(address: &H160) -> Account { - let account_id = T::AddressMapping::into_account_id(*address); - - let nonce = >::account_nonce(&account_id); - let balance = T::RingCurrency::free_balance(&account_id); - - Account { - nonce: U256::from(UniqueSaturatedInto::::unique_saturated_into(nonce)), - balance: U256::from(UniqueSaturatedInto::::unique_saturated_into(balance)), - } - } - - fn mutate_account_basic(address: &H160, new: Account) { - let account_id = T::AddressMapping::into_account_id(*address); - let current = T::RingAccountBasic::account_basic(address); - - if current.nonce < new.nonce { - // ASSUME: in one single EVM transaction, the nonce will not increase more than - // `u128::max_value()`. - for _ in 0..(new.nonce - current.nonce).low_u128() { - >::inc_account_nonce(&account_id); - } - } - - if current.balance > new.balance { - let diff = current.balance - new.balance; - T::RingCurrency::slash(&account_id, diff.low_u128().unique_saturated_into()); - } else if current.balance < new.balance { - let diff = new.balance - current.balance; - T::RingCurrency::deposit_creating(&account_id, diff.low_u128().unique_saturated_into()); - } - } - - fn transfer(source: &H160, target: &H160, value: U256) -> Result<(), ExitError> { - let source_account_id = T::AddressMapping::into_account_id(*source); - let target_account_id = T::AddressMapping::into_account_id(*target); - let value = value.low_u128().unique_saturated_into(); - let res = T::RingCurrency::transfer( - &source_account_id, - &target_account_id, - value, - ExistenceRequirement::AllowDeath, - ); - - match res { - Ok(()) => Ok(()), - Err(_) => Err(ExitError::Other("Transfer error".into())), - } - } -} - /// A mapping function that converts Ethereum gas to Substrate weight pub trait GasWeightMapping { fn gas_to_weight(gas: u64) -> Weight; diff --git a/frame/evm/src/tests.rs b/frame/evm/src/tests.rs index 537b1ea1ba..38f0c3d18f 100644 --- a/frame/evm/src/tests.rs +++ b/frame/evm/src/tests.rs @@ -3,7 +3,7 @@ use crate::{self as darwinia_evm, *}; use frame_support::{assert_ok, traits::GenesisBuild}; use frame_system::mocking::*; -use sp_core::{Blake2Hasher, H256}; +use sp_core::H256; use sp_runtime::{ testing::Header, traits::{BlakeTwo256, IdentityLookup}, @@ -96,13 +96,72 @@ impl IssuingHandler for EmptyIssuingHandler { } } +pub struct RawAccountBasic(sp_std::marker::PhantomData); + +impl AccountBasic for RawAccountBasic { + /// Get the account basic in EVM format. + fn account_basic(address: &H160) -> Account { + let account_id = T::AddressMapping::into_account_id(*address); + + let nonce = >::account_nonce(&account_id); + let balance = T::RingCurrency::free_balance(&account_id); + + Account { + nonce: U256::from(UniqueSaturatedInto::::unique_saturated_into(nonce)), + balance: U256::from(UniqueSaturatedInto::::unique_saturated_into(balance)), + } + } + + fn mutate_account_basic(address: &H160, new: Account) { + let account_id = T::AddressMapping::into_account_id(*address); + let current = T::RingAccountBasic::account_basic(address); + + if current.nonce < new.nonce { + // ASSUME: in one single EVM transaction, the nonce will not increase more than + // `u128::max_value()`. + for _ in 0..(new.nonce - current.nonce).low_u128() { + >::inc_account_nonce(&account_id); + } + } + + if current.balance > new.balance { + let diff = current.balance - new.balance; + T::RingCurrency::slash(&account_id, diff.low_u128().unique_saturated_into()); + } else if current.balance < new.balance { + let diff = new.balance - current.balance; + T::RingCurrency::deposit_creating(&account_id, diff.low_u128().unique_saturated_into()); + } + } + + fn transfer(_source: &H160, _target: &H160, _value: U256) -> Result<(), ExitError> { + Ok(()) + } +} + +/// Ensure that the origin is root. +pub struct EnsureAddressRoot(sp_std::marker::PhantomData); + +impl EnsureAddressOrigin for EnsureAddressRoot +where + OuterOrigin: Into, OuterOrigin>> + From>, +{ + type Success = (); + + fn try_address_origin(_address: &H160, origin: OuterOrigin) -> Result<(), OuterOrigin> { + origin.into().and_then(|o| match o { + RawOrigin::Root => Ok(()), + r => Err(OuterOrigin::from(r)), + }) + } +} + impl Config for Test { type FeeCalculator = FixedGasPrice; type GasWeightMapping = (); type CallOrigin = EnsureAddressRoot; - type WithdrawOrigin = EnsureAddressNever; + type WithdrawOrigin = EnsureAddressTruncated; - type AddressMapping = HashedAddressMapping; + type AddressMapping = ConcatAddressMapping; type RingCurrency = Ring; type KtonCurrency = Kton; diff --git a/script_tests/tests/1_test-transfer.js b/script_tests/tests/1_test-transfer.js index e0e11de038..004daaeb8b 100644 --- a/script_tests/tests/1_test-transfer.js +++ b/script_tests/tests/1_test-transfer.js @@ -68,7 +68,7 @@ describe("Test Transfer Balance", function () { const balanceTo = await web3.utils.fromWei(await web3.eth.getBalance(addressTo2), "ether"); expect(balanceFrom).to.be.equal("123446.78895799999999999"); - expect(balanceTo).to.be.equal("0"); + expect(balanceTo).to.be.equal("0.0000000000000001"); expect(await web3.eth.getTransactionCount(addressFrom, "latest")).to.eq(2); });