Skip to content
This repository has been archived by the owner on Mar 13, 2023. It is now read-only.

Merge withdraw and kton into new transfer precompile #654

Merged
merged 48 commits into from Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
7053920
New precompile named `transfer`
boundless-forest May 31, 2021
fda883a
Delete kton precompile
boundless-forest May 31, 2021
b324924
Rename action things
boundless-forest May 31, 2021
9fb363b
Add prefix to ringback and kton execution
boundless-forest May 31, 2021
c2c0422
Update ci test precompile address
boundless-forest May 31, 2021
520fad2
Add test for is_kton_transfer
boundless-forest Jun 1, 2021
229514c
Clean code
boundless-forest Jun 1, 2021
15fb6f7
Update caller address
boundless-forest Jun 1, 2021
4e1d0b3
Fix the failed unit test
boundless-forest Jun 1, 2021
edcbcf8
Check input length
boundless-forest Jun 2, 2021
84ad489
Merge branch 'master' into bear-precompiles-to-one
boundless-forest Jun 3, 2021
3580efa
Update conflict issues
boundless-forest Jun 3, 2021
148e14a
Default choose ring back
boundless-forest Jun 3, 2021
ed57840
Revert unit test change
boundless-forest Jun 3, 2021
38b4ed6
Format
boundless-forest Jun 3, 2021
dfa2a78
Selector to usize
boundless-forest Jun 3, 2021
2306c8a
For fix unit test
boundless-forest Jun 3, 2021
e4b8711
fix typo
AurevoirXavier Jun 3, 2021
2b0f5e3
Rename ring transfer
boundless-forest Jun 3, 2021
2dded42
Use the same way with pangolin runtime
boundless-forest Jun 4, 2021
df38d28
update format
AurevoirXavier Jun 7, 2021
b8e5dc2
need help (tmp)
AurevoirXavier Jun 7, 2021
87ac5b9
Fix ring transfer remaining balance issue
boundless-forest Jun 8, 2021
a951e08
Merge branch 'master' into bear-precompiles-to-one
boundless-forest Jun 8, 2021
30ec578
Fix conflict and add licenses
boundless-forest Jun 8, 2021
23fa190
Skip these test now
boundless-forest Jun 8, 2021
df66b2b
Fix unit test part 1
boundless-forest Jun 8, 2021
d17f5e4
Fix unit test part 2
boundless-forest Jun 8, 2021
bd6b4d6
Update comments
boundless-forest Jun 8, 2021
d44e5f4
Add `account_balance`, `mutate_account_balance`
boundless-forest Jun 8, 2021
16ab4f7
Rename `mutate_account_basic` to `mutate_account_basic_balance`
boundless-forest Jun 8, 2021
5ffebd8
Use unified muta_*
boundless-forest Jun 8, 2021
989775b
Run tests
boundless-forest Jun 8, 2021
1ff55c9
Make review work easier
boundless-forest Jun 8, 2021
70d9fc3
Fix ci test
boundless-forest Jun 8, 2021
1ea0dff
Merge remote-tracking branch 'upstream/master' into bear-precompiles-…
boundless-forest Jun 9, 2021
d3bce70
Use new array_bytes function
boundless-forest Jun 9, 2021
1922adb
Chores things
boundless-forest Jun 9, 2021
6c0d94c
Add unit tests
boundless-forest Jun 9, 2021
34dab80
Update trait bound
boundless-forest Jun 10, 2021
9a6f6fc
Remove test hex crate
boundless-forest Jun 15, 2021
2f667f3
Replace `hex_into_unchecked` with `hex_try_into`
boundless-forest Jun 15, 2021
4502d1a
format is std? can not use std in runtime
hackfisher Jun 15, 2021
ef5b9ff
Remove unused variable
boundless-forest Jun 15, 2021
cd8cf28
eliminate warning
AurevoirXavier Jun 22, 2021
23bd486
generic `AccountId` in runtime
AurevoirXavier Jun 22, 2021
8e789da
more clear `address_build`
AurevoirXavier Jun 22, 2021
e40d781
remove unused flag
AurevoirXavier Jun 22, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 9 additions & 30 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions Cargo.toml
Expand Up @@ -37,10 +37,9 @@ members = [
"frame/evm/precompile/contracts/dispatch",
"frame/evm/precompile/contracts/ed25519",
"frame/evm/precompile/contracts/empty",
"frame/evm/precompile/contracts/kton",
"frame/evm/precompile/contracts/modexp",
"frame/evm/precompile/contracts/simple",
"frame/evm/precompile/contracts/withdraw",
"frame/evm/precompile/contracts/transfer",
"frame/evm/precompile/contracts/sha3fips",
"frame/header-mmr",
"frame/header-mmr/rpc",
Expand Down
6 changes: 2 additions & 4 deletions bin/node/runtime/pangolin/Cargo.toml
Expand Up @@ -30,9 +30,8 @@ darwinia-ethereum-issuing = { default-features = false, path = "../../
darwinia-ethereum-relay = { default-features = false, path = "../../../../frame/bridge/ethereum/relay" }
darwinia-evm = { default-features = false, path = "../../../../frame/evm" }
darwinia-evm-precompile-issuing = { default-features = false, path = "../../../../frame/evm/precompile/contracts/issuing" }
darwinia-evm-precompile-kton = { default-features = false, path = "../../../../frame/evm/precompile/contracts/kton" }
darwinia-evm-precompile-simple = { default-features = false, path = "../../../../frame/evm/precompile/contracts/simple" }
darwinia-evm-precompile-withdraw = { default-features = false, path = "../../../../frame/evm/precompile/contracts/withdraw" }
darwinia-evm-precompile-transfer = { default-features = false, path = "../../../../frame/evm/precompile/contracts/transfer" }
darwinia-header-mmr = { default-features = false, path = "../../../../frame/header-mmr" }
darwinia-header-mmr-rpc-runtime-api = { default-features = false, path = "../../../../frame/header-mmr/rpc/runtime-api" }
darwinia-relay-authorities = { default-features = false, path = "../../../../frame/bridge/relay-authorities" }
Expand Down Expand Up @@ -128,9 +127,8 @@ std = [
"darwinia-ethereum-relay/std",
"darwinia-evm/std",
"darwinia-evm-precompile-issuing/std",
"darwinia-evm-precompile-kton/std",
"darwinia-evm-precompile-simple/std",
"darwinia-evm-precompile-withdraw/std",
"darwinia-evm-precompile-transfer/std",
"darwinia-header-mmr/std",
"darwinia-header-mmr-rpc-runtime-api/std",
"darwinia-relay-authorities/std",
Expand Down
26 changes: 9 additions & 17 deletions bin/node/runtime/pangolin/src/pallets/evm_.rs
@@ -1,7 +1,6 @@
pub use darwinia_evm_precompile_issuing::Issuing;
pub use darwinia_evm_precompile_kton::Kton as KtonPrecompile;
pub use darwinia_evm_precompile_simple::{ECRecover, Identity, Ripemd160, Sha256};
pub use darwinia_evm_precompile_withdraw::WithDraw;
pub use darwinia_evm_precompile_transfer::Transfer;

// --- crates.io ---
use evm::{Context, ExitError, ExitSucceed};
Expand All @@ -15,31 +14,24 @@ use dp_evm::{Precompile, PrecompileSet};
use dvm_ethereum::account_basic::{DvmAccountBasic, KtonRemainBalance, RingRemainBalance};

pub struct PangolinPrecompiles<R>(PhantomData<R>);
impl<R: dvm_ethereum::Config> PrecompileSet for PangolinPrecompiles<R> {
impl<R: darwinia_evm::Config> PrecompileSet for PangolinPrecompiles<R> {
fn execute(
address: H160,
input: &[u8],
target_gas: Option<u64>,
context: &Context,
) -> Option<Result<(ExitSucceed, Vec<u8>, u64), ExitError>> {
let to_address = |n: u64| -> H160 { H160::from_low_u64_be(n) };
let addr = |n: u64| -> H160 { H160::from_low_u64_be(n) };

match address {
// Ethereum precompiles
_ if address == to_address(1) => Some(ECRecover::execute(input, target_gas, context)),
_ if address == to_address(2) => Some(Sha256::execute(input, target_gas, context)),
_ if address == to_address(3) => Some(Ripemd160::execute(input, target_gas, context)),
_ if address == to_address(4) => Some(Identity::execute(input, target_gas, context)),
_ if address == addr(1) => Some(ECRecover::execute(input, target_gas, context)),
_ if address == addr(2) => Some(Sha256::execute(input, target_gas, context)),
_ if address == addr(3) => Some(Ripemd160::execute(input, target_gas, context)),
_ if address == addr(4) => Some(Identity::execute(input, target_gas, context)),
// Darwinia precompiles
_ if address == to_address(21) => {
Some(<WithDraw<R>>::execute(input, target_gas, context))
}
_ if address == to_address(22) => {
Some(<KtonPrecompile<R>>::execute(input, target_gas, context))
}
_ if address == to_address(23) => {
Some(<Issuing<R>>::execute(input, target_gas, context))
}
_ if address == addr(21) => Some(<Transfer<R>>::execute(input, target_gas, context)),
_ if address == addr(23) => Some(<Issuing<R>>::execute(input, target_gas, context)),
_ => None,
}
}
Expand Down
3 changes: 2 additions & 1 deletion frame/dvm/Cargo.toml
Expand Up @@ -39,10 +39,11 @@ sp-std = { default-features = false, git = "https://github.com/darwini
[dev-dependencies]
# crates.io
array-bytes = { version = "1.3.3" }
ethabi = { git = "https://github.com/darwinia-network/ethabi.git", branch = "xavier-no-std", default-features = false }
# darwinia
darwinia-balances = { path = "../balances" }
darwinia-evm-precompile-simple = { path = "../evm/precompile/contracts/simple" }
darwinia-evm-precompile-withdraw = { path = "../evm/precompile/contracts/withdraw" }
darwinia-evm-precompile-transfer = { path = "../evm/precompile/contracts/transfer" }

[features]
default = ["std"]
Expand Down
80 changes: 39 additions & 41 deletions frame/dvm/src/account_basic.rs
Expand Up @@ -101,7 +101,7 @@ impl<T: Config> RemainBalanceOp<T, KtonBalance<T>> for KtonRemainBalance {
}

pub struct DvmAccountBasic<T, C, RB>(sp_std::marker::PhantomData<(T, C, RB)>);
impl<T: Config, C, RB> AccountBasic for DvmAccountBasic<T, C, RB>
impl<T: Config, C, RB> AccountBasic<T> for DvmAccountBasic<T, C, RB>
where
RB: RemainBalanceOp<T, C::Balance>,
C: Currency<T::AccountId>,
Expand All @@ -110,6 +110,35 @@ where
fn account_basic(address: &H160) -> EVMAccount {
let account_id = <T as darwinia_evm::Config>::AddressMapping::into_account_id(*address);
let nonce = <frame_system::Pallet<T>>::account_nonce(&account_id);

EVMAccount {
nonce: nonce.saturated_into::<u128>().into(),
balance: Self::account_balance(&account_id),
}
}

/// Mutate the basic account
fn mutate_account_basic_balance(address: &H160, new_balance: U256) {
let account_id = <T as darwinia_evm::Config>::AddressMapping::into_account_id(*address);
Self::mutate_account_balance(&account_id, new_balance)
}

/// Transfer value
fn transfer(source: &H160, target: &H160, value: U256) -> Result<(), ExitError> {
let source_account = Self::account_basic(source);
ensure!(source_account.balance >= value, ExitError::OutOfGas);
let new_source_balance = source_account.balance.saturating_sub(value);
Self::mutate_account_basic_balance(source, new_source_balance);

let target_account = Self::account_basic(target);
let new_target_balance = target_account.balance.saturating_add(value);
Self::mutate_account_basic_balance(target, new_target_balance);

Ok(())
}

/// Get account balance
fn account_balance(account_id: &T::AccountId) -> U256 {
let helper = U256::from(POW_9);

// Get balance from Currency
Expand All @@ -125,24 +154,20 @@ where
.checked_add(remaining_balance)
.unwrap_or_default();

EVMAccount {
nonce: nonce.saturated_into::<u128>().into(),
balance: final_balance,
}
final_balance
}

/// Mutate the basic account
fn mutate_account_basic(address: &H160, new: EVMAccount) {
/// Mutate account balance
fn mutate_account_balance(account_id: &T::AccountId, new_balance: U256) {
let helper = U256::from(POW_9);

let account_id = <T as darwinia_evm::Config>::AddressMapping::into_account_id(*address);
let current = Self::account_basic(address);
let current = Self::account_balance(account_id);
let dvm_balance: U256 = RB::remaining_balance(&account_id)
.saturated_into::<u128>()
.into();

let nb = new.balance;
match current.balance {
let nb = new_balance;
match current {
cb if cb > nb => {
let diff = cb - nb;
let (diff_balance, diff_remaining_balance) = diff.div_mod(helper);
Expand Down Expand Up @@ -207,11 +232,9 @@ where
let ring_existential_deposit = U256::from(ring_existential_deposit) * helper;
let kton_existential_deposit = U256::from(kton_existential_deposit) * helper;

let ring_account = T::RingAccountBasic::account_basic(address);
let kton_account = T::KtonAccountBasic::account_basic(address);
if ring_account.balance < ring_existential_deposit
&& kton_account.balance < kton_existential_deposit
{
let ring_account = T::RingAccountBasic::account_balance(&account_id);
let kton_account = T::KtonAccountBasic::account_balance(&account_id);
if ring_account < ring_existential_deposit && kton_account < kton_existential_deposit {
<RingRemainBalance as RemainBalanceOp<T, RingBalance<T>>>::remove_remaining_balance(
&account_id,
);
Expand All @@ -220,29 +243,4 @@ where
);
}
}

fn transfer(source: &H160, target: &H160, value: U256) -> Result<(), ExitError> {
let source_account = Self::account_basic(source);
ensure!(source_account.balance >= value, ExitError::OutOfGas);
let new_source_balance = source_account.balance.saturating_sub(value);
Self::mutate_account_basic(
source,
EVMAccount {
nonce: source_account.nonce,
balance: new_source_balance,
},
);

let target_account = Self::account_basic(target);
let new_target_balance = target_account.balance.saturating_add(value);
Self::mutate_account_basic(
target,
EVMAccount {
nonce: target_account.nonce,
balance: new_target_balance,
},
);

Ok(())
}
}