Skip to content

Commit

Permalink
eip1559 support
Browse files Browse the repository at this point in the history
  • Loading branch information
harryliisme3 committed Jul 5, 2022
1 parent d9028af commit a6789f6
Show file tree
Hide file tree
Showing 67 changed files with 2,018 additions and 448 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ build_release_goleveldb: tendermint_goleveldb
cargo build --release --bins -p abciapp -p finutils
$(call pack,release)

check: tendermint_goleveldb
cargo check --release --bins -p abciapp -p finutils
$(call pack,release)

# Build for goleveldb
build_release_musl_goleveldb: tendermint_goleveldb
cargo build --release --bins -p abciapp -p finutils --target=x86_64-unknown-linux-musl
Expand Down
3 changes: 2 additions & 1 deletion src/components/abciapp/src/abci/server/callback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,8 @@ pub fn end_block(
if td_height <= CFG.checkpoint.disable_evm_block_height
|| td_height >= CFG.checkpoint.enable_frc20_height
{
let _ = s.account_base_app.write().end_block(req);
// let _ =
s.account_base_app.write().end_block(req);
}

// mint coinbase, cache system transactions to ledger
Expand Down
51 changes: 26 additions & 25 deletions src/components/contracts/baseapp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
[package]
name = "baseapp"
version = "0.1.0"
authors = ["FindoraNetwork"]
description = "Base application for tendermint abci"
edition = "2021"
homepage = "https://findora.org/technology"
repository = "https://github.com/findoranetwork/platform/"
description = "Base application for tendermint abci"
name = "baseapp"
readme = "README.md"
repository = "https://github.com/findoranetwork/platform/"
version = "0.1.0"

[dependencies]
abci = { git = "https://github.com/FindoraNetwork/rust-abci", tag = "v0.7.2" }
ethereum = { version = "0.9.0", default-features = false, features = ["with-serde"] }
ethereum-types = { version = "0.12", default-features = false }
abci = {git = "https://github.com/FindoraNetwork/rust-abci", tag = "v0.7.2"}
ethereum = {version = "0.10.0", default-features = false, features = ["with-serde"]}
ethereum-types = {version = "0.12", default-features = false}
futures = "0.3.16"
lazy_static = "1.4.0"
ledger = { path = "../../../ledger" }
ledger = {path = "../../../ledger"}
log = "0.4"
parking_lot = "0.11.1"
primitive-types = { version = "0.10.1", default-features = false, features = ["rlp", "byteorder", "serde"] }
primitive-types = {version = "0.10.1", default-features = false, features = ["rlp", "byteorder", "serde"]}
protobuf = "2.16"
ruc = "1.0"
serde = { version = "1.0.124", features = ["derive"] }
serde = {version = "1.0.124", features = ["derive"]}
serde_json = "1.0.40"
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v0.1.4" }
storage = {git = "https://github.com/FindoraNetwork/storage.git", tag = "v0.1.4"}

# primitives
fp-core = { path = "../primitives/core" }
fp-evm = { path = "../primitives/evm" }
fp-traits = { path = "../primitives/traits" }
fp-types = { path = "../primitives/types" }
fp-utils = { path = "../primitives/utils" }
fp-core = {path = "../primitives/core"}
fp-evm = {path = "../primitives/evm"}
fp-traits = {path = "../primitives/traits"}
fp-types = {path = "../primitives/types"}
fp-utils = {path = "../primitives/utils"}

# modules
module-account = { path = "../modules/account"}
module-ethereum = { path = "../modules/ethereum"}
module-evm = { path = "../modules/evm"}
module-template = { path = "../modules/template"}
module-xhub = { path = "../modules/xhub"}
module-account = {path = "../modules/account"}
module-ethereum = {path = "../modules/ethereum"}
module-evm = {path = "../modules/evm"}
module-template = {path = "../modules/template"}
module-xhub = {path = "../modules/xhub"}

evm-precompile-basic = { path = "../modules/evm/precompile/basic" }
evm-precompile-frc20 = { path = "../modules/evm/precompile/frc20" }
evm-precompile-modexp = { path = "../modules/evm/precompile/modexp" }
evm-precompile-sha3fips = { path = "../modules/evm/precompile/sha3fips" }
evm-precompile-basic = {path = "../modules/evm/precompile/basic"}
evm-precompile-frc20 = {path = "../modules/evm/precompile/frc20"}
evm-precompile-modexp = {path = "../modules/evm/precompile/modexp"}
evm-precompile-runtime = {path = "../modules/evm/precompile/runtime"}
evm-precompile-sha3fips = {path = "../modules/evm/precompile/sha3fips"}

[features]
abci_mock = []
82 changes: 81 additions & 1 deletion src/components/contracts/baseapp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ mod notify;

use crate::modules::ModuleManager;
use abci::Header;
use ethereum::BlockV0 as Block;
use ethereum::BlockV2 as Block;
use evm_precompile_runtime::{self, FindoraPrecompiles};
use fp_core::{
account::SmartAccount,
context::{Context, RunTxMode},
Expand Down Expand Up @@ -51,6 +52,23 @@ const CHAIN_STATE_PATH: &str = "state.db";
const CHAIN_HISTORY_DATA_PATH: &str = "history.db";
const CHAIN_STATE_MIN_VERSIONS: u64 = 4 * 60 * 24 * 90;

const INITIAL_BASE_FEE: u64 = 1000000000;
const ELASTICITY_MULTIPLIER: u64 = 2;
const BASE_FEE_MAX_CHANGE_DENOMINATOR: u64 = 8;

#[inline(always)]
pub fn get_initial_base_fee() -> U256 {
U256::from(INITIAL_BASE_FEE)
}
#[inline(always)]
pub fn get_elasticity_multiplier() -> U256 {
U256::from(ELASTICITY_MULTIPLIER)
}
#[inline(always)]
pub fn get_base_fee_max_change_denominator() -> U256 {
U256::from(BASE_FEE_MAX_CHANGE_DENOMINATOR)
}

pub struct BaseApp {
/// application name from abci.Info
pub name: String,
Expand Down Expand Up @@ -107,6 +125,10 @@ impl module_ethereum::Config for BaseApp {
type Runner = module_evm::runtime::runner::ActionRunner<Self>;
}

parameter_types! {
pub PrecompilesValue: FindoraPrecompiles<BaseApp> = FindoraPrecompiles::<_>::new();
}

impl module_evm::Config for BaseApp {
type AccountAsset = module_account::App<Self>;
type AddressMapping = EthereumAddressMapping;
Expand All @@ -126,6 +148,8 @@ impl module_evm::Config for BaseApp {
evm_precompile_sha3fips::Sha3FIPS512,
evm_precompile_frc20::FRC20<Self>,
);
type PrecompilesType = FindoraPrecompiles<Self>;
type PrecompilesValue = PrecompilesValue;
}

impl module_xhub::Config for BaseApp {
Expand Down Expand Up @@ -423,4 +447,60 @@ impl BaseProvider for BaseApp {
None
}
}

/// Return the base fee at the given height.
#[allow(clippy::comparison_chain, clippy::question_mark)]
fn base_fee(&self, id: Option<BlockId>) -> Option<U256> {
let mut comp_gas = std::vec::Vec::new();
let block = self.current_block(id)?;
let mut parent_block =
self.current_block(Some(BlockId::Hash(block.header.parent_hash)));
if parent_block.is_none() {
return None;
}
let parent_gas_used = parent_block.as_ref().unwrap().header.gas_used;
let parent_gas_target = parent_block.as_ref().unwrap().header.gas_limit
/ get_elasticity_multiplier();
comp_gas.push((parent_gas_used, parent_gas_target));

while parent_block.as_ref().is_some() {
parent_block = self.current_block(Some(BlockId::Hash(
parent_block.as_ref().unwrap().header.parent_hash,
)));
let parent_gas_used = parent_block.as_ref().unwrap().header.gas_used;
let parent_gas_target = parent_block.as_ref().unwrap().header.gas_limit
/ get_elasticity_multiplier();
comp_gas.push((parent_gas_used, parent_gas_target));
}

let mut base_fee = get_initial_base_fee();
comp_gas.pop();
while !comp_gas.is_empty() {
let (parent_gas_used, parent_gas_target) = comp_gas.pop().unwrap();
if parent_gas_used > parent_gas_target {
let gas_used_delta = parent_gas_used - parent_gas_target;
let base_fee_delta = std::cmp::max(
base_fee * gas_used_delta
/ parent_gas_target
/ get_base_fee_max_change_denominator(),
U256::from(1),
);
base_fee += base_fee_delta;
} else if parent_gas_used < parent_gas_target {
let gas_used_delta = parent_gas_target - parent_gas_used;
let base_fee_delta = base_fee * gas_used_delta
/ parent_gas_target
/ get_base_fee_max_change_denominator();
base_fee -= base_fee_delta;
}
}

Some(base_fee)
}

/// Return `true` if the request BlockId is post-eip1559.
/// Do not be used.
fn is_eip1559(&self, _id: Option<BlockId>) -> bool {
false
}
}
10 changes: 10 additions & 0 deletions src/components/contracts/modules/account/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,14 @@ impl<C: Config> AccountAsset<Address> for App<C> {
) -> Result<()> {
Allowances::insert(ctx.state.write().borrow_mut(), owner, spender, &amount)
}

fn income(ctx: &Context, who: &Address, value: U256) -> Result<()> {
if value.is_zero() {
return Ok(());
}

let mut sa = Self::account_of(ctx, who, None).c(d!("account does not exist"))?;
sa.balance = sa.balance.checked_add(value).c(d!("balance overflow"))?;
AccountStore::insert(ctx.state.write().borrow_mut(), who, &sa)
}
}
4 changes: 2 additions & 2 deletions src/components/contracts/modules/ethereum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ readme = "README.md"

[dependencies]
abci = { git = "https://github.com/FindoraNetwork/rust-abci", tag = "v0.7.2" }
ethereum = { version = "0.9.0", default-features = false, features = ["with-serde"] }
ethereum = { version = "0.10.0", default-features = false, features = ["with-serde"] }
ethereum-types = { version = "0.12", default-features = false }
evm = { version = "0.29.0", default-features = false, features = ["with-serde"] }
evm = { version = "0.33.0", default-features = false, features = ["with-serde"] }
log = "0.4"
rand = "0.8"
rlp = "0.5"
Expand Down
Loading

0 comments on commit a6789f6

Please sign in to comment.