From 664511ec37942e46355af1db4a3c4ef1d08f18b6 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Wed, 21 Jun 2023 11:25:43 +0800 Subject: [PATCH 1/3] Fix base fee calculation --- lib/ain-cpp-imports/src/bridge.rs | 1 + lib/ain-cpp-imports/src/lib.rs | 8 +++ lib/ain-evm/src/block.rs | 88 +++++++++++++++++++++++-------- src/chainparams.cpp | 1 + src/consensus/params.h | 1 + src/ffi/ffiexports.cpp | 4 ++ src/ffi/ffiexports.h | 1 + 7 files changed, 82 insertions(+), 22 deletions(-) diff --git a/lib/ain-cpp-imports/src/bridge.rs b/lib/ain-cpp-imports/src/bridge.rs index 811903d44c4..fff5ca9b67b 100644 --- a/lib/ain-cpp-imports/src/bridge.rs +++ b/lib/ain-cpp-imports/src/bridge.rs @@ -16,5 +16,6 @@ pub mod ffi { fn getMinRelayTxFee() -> u64; fn getEthPrivKey(key_id: [u8; 20]) -> [u8; 32]; fn getStateInputJSON() -> String; + fn pastChangiIntermediateHeight2() -> bool; } } diff --git a/lib/ain-cpp-imports/src/lib.rs b/lib/ain-cpp-imports/src/lib.rs index a148efc0288..95b3841f516 100644 --- a/lib/ain-cpp-imports/src/lib.rs +++ b/lib/ain-cpp-imports/src/lib.rs @@ -49,6 +49,9 @@ mod ffi { pub fn getStateInputJSON() -> String { unimplemented!("{}", UNIMPL_MSG) } + pub fn pastChangiIntermediateHeight2() -> bool { + unimplemented!("{}", UNIMPL_MSG) + } } pub fn get_chain_id() -> Result> { @@ -119,5 +122,10 @@ pub fn get_state_input_json() -> Option { } } +pub fn past_changi_intermediate_height_2_height() -> Result> { + let height = ffi::pastChangiIntermediateHeight2(); + Ok(height) +} + #[cfg(test)] mod tests {} diff --git a/lib/ain-evm/src/block.rs b/lib/ain-evm/src/block.rs index c6009e527db..cf754b7863b 100644 --- a/lib/ain-evm/src/block.rs +++ b/lib/ain-evm/src/block.rs @@ -47,6 +47,65 @@ impl BlockHandler { self.storage.set_base_fee(block.header.hash(), base_fee); } + pub fn get_base_fee( + &self, + parent_gas_used: u64, + parent_gas_target: u64, + parent_base_fee: U256, + base_fee_max_change_denominator: U256, + initial_base_fee: U256, + ) -> U256 { + match ain_cpp_imports::past_changi_intermediate_height_2_height() + .expect("Unable to get Changi intermediate height 2") + { + true => match parent_gas_used.cmp(&parent_gas_target) { + Ordering::Equal => parent_base_fee, + Ordering::Greater => { + let gas_used_delta = parent_gas_used - parent_gas_target; + let base_fee_per_gas_delta = max( + parent_base_fee * gas_used_delta + / parent_gas_target + / base_fee_max_change_denominator, + U256::one(), + ); + + max(parent_base_fee + base_fee_per_gas_delta, initial_base_fee) + } + Ordering::Less => { + let gas_used_delta = parent_gas_target - parent_gas_used; + let base_fee_per_gas_delta = parent_base_fee * gas_used_delta + / parent_gas_target + / base_fee_max_change_denominator; + + max(parent_base_fee - base_fee_per_gas_delta, initial_base_fee) + } + }, + false => match parent_gas_used.cmp(&parent_gas_target) { + // TODO: incorrect calculation, remove before mainnet launch + Ordering::Less => parent_base_fee, + Ordering::Equal => { + let gas_used_delta = parent_gas_used - parent_gas_target; + let base_fee_per_gas_delta = max( + parent_base_fee * gas_used_delta + / parent_gas_target + / base_fee_max_change_denominator, + U256::one(), + ); + + max(parent_base_fee + base_fee_per_gas_delta, initial_base_fee) + } + Ordering::Greater => { + let gas_used_delta = parent_gas_target - parent_gas_used; + let base_fee_per_gas_delta = parent_base_fee * gas_used_delta + / parent_gas_target + / base_fee_max_change_denominator; + + max(parent_base_fee - base_fee_per_gas_delta, initial_base_fee) + } + }, + } + } + pub fn calculate_base_fee(&self, parent_hash: H256) -> U256 { // constants let initial_base_fee = U256::from(10_000_000_000u64); // wei @@ -72,28 +131,13 @@ impl BlockHandler { let parent_gas_target = parent_block.header.gas_limit.as_u64() / elasticity_multiplier.as_u64(); - match parent_gas_used.cmp(&parent_gas_target) { - Ordering::Less => parent_base_fee, - Ordering::Equal => { - let gas_used_delta = parent_gas_used - parent_gas_target; - let base_fee_per_gas_delta = max( - parent_base_fee * gas_used_delta - / parent_gas_target - / base_fee_max_change_denominator, - U256::one(), - ); - - max(parent_base_fee + base_fee_per_gas_delta, initial_base_fee) - } - Ordering::Greater => { - let gas_used_delta = parent_gas_target - parent_gas_used; - let base_fee_per_gas_delta = parent_base_fee * gas_used_delta - / parent_gas_target - / base_fee_max_change_denominator; - - max(parent_base_fee - base_fee_per_gas_delta, initial_base_fee) - } - } + self.get_base_fee( + parent_gas_used, + parent_gas_target, + parent_base_fee, + base_fee_max_change_denominator, + initial_base_fee, + ) } pub fn fee_history( diff --git a/src/chainparams.cpp b/src/chainparams.cpp index 93bd2c8bc41..7924a74c05d 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -637,6 +637,7 @@ class CChangiParams : public CChainParams { consensus.GrandCentralEpilogueHeight = 1438200; consensus.NextNetworkUpgradeHeight = 1586750; consensus.ChangiIntermediateHeight = 1717800; + consensus.ChangiIntermediateHeight2 = 1717493; consensus.pos.diffLimit = uint256S("00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); consensus.pos.nTargetTimespan = 5 * 60; // 5 min == 10 blocks diff --git a/src/consensus/params.h b/src/consensus/params.h index ed1429a639f..b71b224020f 100644 --- a/src/consensus/params.h +++ b/src/consensus/params.h @@ -103,6 +103,7 @@ struct Params { int GrandCentralEpilogueHeight; int NextNetworkUpgradeHeight; int ChangiIntermediateHeight; // To be changed to NextNetworkUpgradeHeight on mainnet release + int ChangiIntermediateHeight2; // To be changed to NextNetworkUpgradeHeight on mainnet release /** Foundation share after AMK, normalized to COIN = 100% */ CAmount foundationShareDFIP1; diff --git a/src/ffi/ffiexports.cpp b/src/ffi/ffiexports.cpp index dd1deecda41..d0536b7e129 100644 --- a/src/ffi/ffiexports.cpp +++ b/src/ffi/ffiexports.cpp @@ -201,3 +201,7 @@ std::array getEthPrivKey(std::array keyID) { rust::string getStateInputJSON() { return gArgs.GetArg("-ethstartstate", ""); } + +bool pastChangiIntermediateHeight2() { + return ::ChainActive().Height() >= Params().GetConsensus().ChangiIntermediateHeight2; +} diff --git a/src/ffi/ffiexports.h b/src/ffi/ffiexports.h index 1d3a29f0386..2c6cfd2394d 100644 --- a/src/ffi/ffiexports.h +++ b/src/ffi/ffiexports.h @@ -17,5 +17,6 @@ uint64_t getNativeTxSize(rust::Vec rawTransaction); uint64_t getMinRelayTxFee(); std::array getEthPrivKey(std::array keyID); rust::string getStateInputJSON(); +bool pastChangiIntermediateHeight2(); #endif // DEFI_FFI_FFIEXPORTS_H From dde6436b3ad102d97806bb44d69e294eedb49644 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Wed, 21 Jun 2023 11:48:16 +0800 Subject: [PATCH 2/3] Move old calculation to function --- lib/ain-evm/src/block.rs | 63 +++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/lib/ain-evm/src/block.rs b/lib/ain-evm/src/block.rs index cf754b7863b..1ed8058322b 100644 --- a/lib/ain-evm/src/block.rs +++ b/lib/ain-evm/src/block.rs @@ -47,6 +47,39 @@ impl BlockHandler { self.storage.set_base_fee(block.header.hash(), base_fee); } + fn pre_changi_intermediate_2_base_fee_calculation( + &self, + parent_gas_used: u64, + parent_gas_target: u64, + parent_base_fee: U256, + base_fee_max_change_denominator: U256, + initial_base_fee: U256, + ) -> U256 { + match parent_gas_used.cmp(&parent_gas_target) { + // TODO: incorrect calculation, remove before mainnet launch + Ordering::Less => parent_base_fee, + Ordering::Equal => { + let gas_used_delta = parent_gas_used - parent_gas_target; + let base_fee_per_gas_delta = max( + parent_base_fee * gas_used_delta + / parent_gas_target + / base_fee_max_change_denominator, + U256::one(), + ); + + max(parent_base_fee + base_fee_per_gas_delta, initial_base_fee) + } + Ordering::Greater => { + let gas_used_delta = parent_gas_target - parent_gas_used; + let base_fee_per_gas_delta = parent_base_fee * gas_used_delta + / parent_gas_target + / base_fee_max_change_denominator; + + max(parent_base_fee - base_fee_per_gas_delta, initial_base_fee) + } + } + } + pub fn get_base_fee( &self, parent_gas_used: u64, @@ -80,29 +113,13 @@ impl BlockHandler { max(parent_base_fee - base_fee_per_gas_delta, initial_base_fee) } }, - false => match parent_gas_used.cmp(&parent_gas_target) { - // TODO: incorrect calculation, remove before mainnet launch - Ordering::Less => parent_base_fee, - Ordering::Equal => { - let gas_used_delta = parent_gas_used - parent_gas_target; - let base_fee_per_gas_delta = max( - parent_base_fee * gas_used_delta - / parent_gas_target - / base_fee_max_change_denominator, - U256::one(), - ); - - max(parent_base_fee + base_fee_per_gas_delta, initial_base_fee) - } - Ordering::Greater => { - let gas_used_delta = parent_gas_target - parent_gas_used; - let base_fee_per_gas_delta = parent_base_fee * gas_used_delta - / parent_gas_target - / base_fee_max_change_denominator; - - max(parent_base_fee - base_fee_per_gas_delta, initial_base_fee) - } - }, + false => self.pre_changi_intermediate_2_base_fee_calculation( + parent_gas_used, + parent_gas_target, + parent_base_fee, + base_fee_max_change_denominator, + initial_base_fee, + ), } } From a6d3611ad10cd4394c5bd30ca477ad6cc6945f08 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Wed, 21 Jun 2023 14:27:20 +0800 Subject: [PATCH 3/3] Add tests --- lib/ain-evm/src/block.rs | 141 +++++++++++++++++++++++++++++++++------ 1 file changed, 119 insertions(+), 22 deletions(-) diff --git a/lib/ain-evm/src/block.rs b/lib/ain-evm/src/block.rs index 1ed8058322b..dffb8b2464b 100644 --- a/lib/ain-evm/src/block.rs +++ b/lib/ain-evm/src/block.rs @@ -80,6 +80,38 @@ impl BlockHandler { } } + pub fn post_changi_intermediate_2_base_fee_calculation( + &self, + parent_gas_used: u64, + parent_gas_target: u64, + parent_base_fee: U256, + base_fee_max_change_denominator: U256, + initial_base_fee: U256, + ) -> U256 { + match parent_gas_used.cmp(&parent_gas_target) { + Ordering::Equal => parent_base_fee, + Ordering::Greater => { + let gas_used_delta = parent_gas_used - parent_gas_target; + let base_fee_per_gas_delta = max( + parent_base_fee * gas_used_delta + / parent_gas_target + / base_fee_max_change_denominator, + U256::one(), + ); + + max(parent_base_fee + base_fee_per_gas_delta, initial_base_fee) + } + Ordering::Less => { + let gas_used_delta = parent_gas_target - parent_gas_used; + let base_fee_per_gas_delta = parent_base_fee * gas_used_delta + / parent_gas_target + / base_fee_max_change_denominator; + + max(parent_base_fee - base_fee_per_gas_delta, initial_base_fee) + } + } + } + pub fn get_base_fee( &self, parent_gas_used: u64, @@ -91,28 +123,13 @@ impl BlockHandler { match ain_cpp_imports::past_changi_intermediate_height_2_height() .expect("Unable to get Changi intermediate height 2") { - true => match parent_gas_used.cmp(&parent_gas_target) { - Ordering::Equal => parent_base_fee, - Ordering::Greater => { - let gas_used_delta = parent_gas_used - parent_gas_target; - let base_fee_per_gas_delta = max( - parent_base_fee * gas_used_delta - / parent_gas_target - / base_fee_max_change_denominator, - U256::one(), - ); - - max(parent_base_fee + base_fee_per_gas_delta, initial_base_fee) - } - Ordering::Less => { - let gas_used_delta = parent_gas_target - parent_gas_used; - let base_fee_per_gas_delta = parent_base_fee * gas_used_delta - / parent_gas_target - / base_fee_max_change_denominator; - - max(parent_base_fee - base_fee_per_gas_delta, initial_base_fee) - } - }, + true => self.post_changi_intermediate_2_base_fee_calculation( + parent_gas_used, + parent_gas_target, + parent_base_fee, + base_fee_max_change_denominator, + initial_base_fee, + ), false => self.pre_changi_intermediate_2_base_fee_calculation( parent_gas_used, parent_gas_target, @@ -350,3 +367,83 @@ pub fn new_block_from_json(path: PathBuf, state_root: H256) -> Result