Skip to content

Commit

Permalink
Eip1559 (#780)
Browse files Browse the repository at this point in the history
* merge evm runners

* remove legacy action

* use new FeeCalcultor

* fix test evm_tx_hash

* fix gas_price

Co-authored-by: simonjiao <[email protected]>
  • Loading branch information
simonjiao and simonjiao authored Jan 3, 2023
1 parent b1efab6 commit d0bf224
Show file tree
Hide file tree
Showing 16 changed files with 235 additions and 829 deletions.
10 changes: 7 additions & 3 deletions src/components/contracts/baseapp/src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use fp_core::{
context::Context,
transaction::{ActionResult, SignedExtension},
};
use fp_traits::account::{AccountAsset, FeeCalculator};
use fp_traits::{account::AccountAsset, evm::FeeCalculator};
use fp_types::crypto::Address;
use primitive_types::U256;
use ruc::*;
Expand Down Expand Up @@ -64,7 +64,9 @@ impl SignedExtension for CheckFee {
type Pre = (Address, U256);

fn validate(&self, ctx: &Context, who: &Self::AccountId) -> Result<()> {
let min_fee = <BaseApp as module_account::Config>::FeeCalculator::min_fee();
let min_fee = <BaseApp as module_account::Config>::FeeCalculator::min_gas_price(
ctx.header.height as u64,
);
let tx_fee = match self.0 {
None => min_fee,
Some(fee) => {
Expand All @@ -85,7 +87,9 @@ impl SignedExtension for CheckFee {
}

fn pre_execute(self, ctx: &Context, who: &Self::AccountId) -> Result<Self::Pre> {
let min_fee = <BaseApp as module_account::Config>::FeeCalculator::min_fee();
let min_fee = <BaseApp as module_account::Config>::FeeCalculator::min_gas_price(
ctx.header.height as u64,
);
let tx_fee = match self.0 {
None => min_fee,
Some(fee) => {
Expand Down
18 changes: 5 additions & 13 deletions src/components/contracts/baseapp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ use fp_core::{
transaction::{ActionResult, Executable, ValidateUnsigned},
};
use fp_evm::BlockId;
use fp_traits::evm::FeeCalculator as FeeCalculator2;
use fp_traits::{
account::{AccountAsset, FeeCalculator},
account::AccountAsset,
base::BaseProvider,
evm::{DecimalsMapping, EthereumAddressMapping, EthereumDecimalsMapping},
evm::{
DecimalsMapping, EthereumAddressMapping, EthereumDecimalsMapping, FeeCalculator,
},
};
use fp_types::{actions::xhub::NonConfidentialOutput, actions::Action, crypto::Address};
use lazy_static::lazy_static;
Expand Down Expand Up @@ -99,17 +100,8 @@ pub struct BaseApp {

impl module_template::Config for BaseApp {}

pub struct StableTxFee;

impl FeeCalculator for StableTxFee {
fn min_fee() -> U256 {
// TX_FEE_MIN
U256::from(1_0000_0000_0000_0000_u64)
}
}

impl module_account::Config for BaseApp {
type FeeCalculator = StableTxFee;
type FeeCalculator = ();
}

parameter_types! {
Expand Down
4 changes: 2 additions & 2 deletions src/components/contracts/modules/account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ mod tests;
use abci::{RequestQuery, ResponseQuery};
use fp_core::{context::Context, module::AppModule};
use fp_traits::{
account::{AccountAsset, FeeCalculator},
evm::{DecimalsMapping, EthereumDecimalsMapping},
account::AccountAsset,
evm::{DecimalsMapping, EthereumDecimalsMapping, FeeCalculator},
};
use fp_types::crypto::Address;
use std::marker::PhantomData;
Expand Down
126 changes: 40 additions & 86 deletions src/components/contracts/modules/ethereum/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,8 @@ impl<C: Config> App<C> {
let _chain_id;
let mut _access_list = vec![];

let is_eip1559 = match &transaction {
Transaction::Legacy(legacy_transaction_transaction) => {
let transaction = legacy_transaction_transaction;
match &transaction {
Transaction::Legacy(transaction) => {
nonce = transaction.nonce;
gas_price = transaction.gas_price;
max_fee_per_gas = transaction.gas_price;
Expand All @@ -270,11 +269,8 @@ impl<C: Config> App<C> {
Some(chain_id) => chain_id,
None => return Err(eg!("Must provide chainId")),
};

false
}
Transaction::EIP1559(eip1559_transaction_transaction) => {
let transaction = eip1559_transaction_transaction;
Transaction::EIP1559(transaction) => {
nonce = transaction.nonce;
gas_limit = transaction.gas_limit;
action = transaction.action;
Expand All @@ -285,16 +281,12 @@ impl<C: Config> App<C> {
gas_price = transaction.max_fee_per_gas;
_chain_id = transaction.chain_id;
_access_list = transaction.access_list.clone();

true
}
_ => {
return Err(eg!("Transaction Type Error"));
return Err(eg!("Unsupported Transaction Type"));
}
};

// let gas_limit = transaction.gas_limit;

let execute_ret = Self::execute_transaction(
ctx,
source,
Expand All @@ -306,7 +298,6 @@ impl<C: Config> App<C> {
Some(max_priority_fee_per_gas),
Some(nonce),
action,
is_eip1559,
);

if let Err(e) = execute_ret {
Expand Down Expand Up @@ -474,81 +465,44 @@ impl<C: Config> App<C> {
max_priority_fee_per_gas: Option<U256>,
nonce: Option<U256>,
action: ethereum::TransactionAction,
is_eip1559: bool,
) -> Result<(Option<H160>, Option<H160>, CallOrCreateInfo)> {
if is_eip1559 {
match action {
ethereum::TransactionAction::Call(target) => {
let res = C::Runner::call_eip1559(
ctx,
EvmAction::CallEip1559 {
source: from,
target,
input,
value,
gas_limit: gas_limit.low_u64(),
max_fee_per_gas,
max_priority_fee_per_gas,
nonce,
},
C::config(),
)?;

Ok((Some(target), None, CallOrCreateInfo::Call(res)))
}
ethereum::TransactionAction::Create => {
let res = C::Runner::create_eip1559(
ctx,
EvmAction::CreateEip1559 {
source: from,
init: input,
value,
gas_limit: gas_limit.low_u64(),
max_fee_per_gas,
max_priority_fee_per_gas,
nonce,
},
C::config(),
)?;

Ok((None, Some(res.value), CallOrCreateInfo::Create(res)))
}
match action {
ethereum::TransactionAction::Call(target) => {
let res = C::Runner::call(
ctx,
EvmAction::Call {
source: from,
target,
input,
value,
gas_limit: gas_limit.low_u64(),
gas_price,
max_fee_per_gas,
max_priority_fee_per_gas,
nonce,
},
C::config(),
)?;

Ok((Some(target), None, CallOrCreateInfo::Call(res)))
}
} else {
match action {
ethereum::TransactionAction::Call(target) => {
let res = C::Runner::call(
ctx,
EvmAction::Call {
source: from,
target,
input,
value,
gas_limit: gas_limit.low_u64(),
gas_price,
nonce,
},
C::config(),
)?;

Ok((Some(target), None, CallOrCreateInfo::Call(res)))
}
ethereum::TransactionAction::Create => {
let res = C::Runner::create(
ctx,
EvmAction::Create {
source: from,
init: input,
value,
gas_limit: gas_limit.low_u64(),
gas_price,
nonce,
},
C::config(),
)?;

Ok((None, Some(res.value), CallOrCreateInfo::Create(res)))
}
ethereum::TransactionAction::Create => {
let res = C::Runner::create(
ctx,
EvmAction::Create {
source: from,
init: input,
value,
gas_limit: gas_limit.low_u64(),
gas_price,
max_fee_per_gas,
max_priority_fee_per_gas,
nonce,
},
C::config(),
)?;

Ok((None, Some(res.value), CallOrCreateInfo::Create(res)))
}
}
}
Expand Down
86 changes: 33 additions & 53 deletions src/components/contracts/modules/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,41 +184,37 @@ impl<C: Config> ValidateUnsigned for App<C> {
gas_limit,
value,
chain_id,
max_fee_per_gas,
max_priority_fee_per_gas,
is_eip1559,
);

match transaction {
Transaction::Legacy(t) => {
nonce = t.nonce;
gas_price = t.gas_price;
gas_limit = t.gas_limit;
value = t.value;
chain_id = match t.signature.chain_id() {
_max_fee_per_gas,
_max_priority_fee_per_gas,
_is_eip1559,
) = match transaction {
Transaction::Legacy(t) => (
t.nonce,
t.gas_price,
t.gas_limit,
t.value,
match t.signature.chain_id() {
Some(chain_id) => chain_id,
None => return Err(eg!("Must provide chainId")),
};
is_eip1559 = false;

max_fee_per_gas = U256::zero();
max_priority_fee_per_gas = U256::zero();
}
Transaction::EIP1559(t) => {
nonce = t.nonce;
gas_limit = t.gas_limit;
max_fee_per_gas = t.max_fee_per_gas;
max_priority_fee_per_gas = t.max_priority_fee_per_gas;
value = t.value;
chain_id = t.chain_id;
is_eip1559 = true;

gas_price = U256::zero();
}
},
U256::zero(),
U256::zero(),
false,
),
Transaction::EIP1559(t) => (
t.nonce,
U256::zero(),
t.gas_limit,
t.value,
t.chain_id,
t.max_fee_per_gas,
t.max_priority_fee_per_gas,
true,
),
_ => {
return Err(eg!("Transaction Type Error"));
}
}
};

if chain_id != C::ChainId::get() {
return Err(eg!(format!(
Expand All @@ -240,16 +236,13 @@ impl<C: Config> ValidateUnsigned for App<C> {
)));
}

if !is_eip1559 {
let min_gas_price =
C::FeeCalculator::min_gas_price(ctx.header.height as u64);
let min_gas_price = C::FeeCalculator::min_gas_price(ctx.header.height as u64);

if gas_price < min_gas_price {
return Err(eg!(format!(
"InvalidGasPrice: got {}, but the minimum gas price is {}",
gas_price, min_gas_price
)));
}
if gas_price < min_gas_price {
return Err(eg!(format!(
"InvalidGasPrice: got {}, but the minimum gas price is {}",
gas_price, min_gas_price
)));
}

let account_id = C::AddressMapping::convert_to_account_id(origin);
Expand All @@ -263,20 +256,7 @@ impl<C: Config> ValidateUnsigned for App<C> {
)));
}

let fee = if !is_eip1559 {
gas_price.saturating_mul(gas_limit)
} else {
let max_base_fee = max_fee_per_gas
.checked_mul(gas_limit)
.ok_or(eg!("FeeOverflow"))?;
let max_priority_fee = max_priority_fee_per_gas
.checked_mul(gas_limit)
.ok_or(eg!("FeeOverflow"))?;
max_base_fee
.checked_add(max_priority_fee)
.ok_or(eg!("FeeOverflow"))?
};

let fee = gas_price.saturating_mul(gas_limit);
let total_payment = value.saturating_add(fee);

if account.balance < total_payment {
Expand Down
Loading

0 comments on commit d0bf224

Please sign in to comment.