Skip to content

Commit fe5faf2

Browse files
chore: add HaberFix upgrade and other optimization (bnb-chain#42)
* chore: add `HaberFix` upgrade and other optimization * trim system contracts bytecode
1 parent 4086c81 commit fe5faf2

File tree

14 files changed

+169
-123
lines changed

14 files changed

+169
-123
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -506,12 +506,11 @@ test-fuzz = "5"
506506
iai-callgrind = "0.11"
507507

508508
[patch.crates-io]
509-
revm = { git = "https://github.com/bnb-chain/revm", rev = "63b8ab893c6f1d2984131017de9872b2809b615b" }
510-
revm-interpreter = { git = "https://github.com/bnb-chain/revm", rev = "63b8ab893c6f1d2984131017de9872b2809b615b" }
511-
revm-precompile = { git = "https://github.com/bnb-chain/revm", rev = "63b8ab893c6f1d2984131017de9872b2809b615b" }
512-
revm-primitives = { git = "https://github.com/bnb-chain/revm", rev = "63b8ab893c6f1d2984131017de9872b2809b615b" }
509+
revm = { git = "https://github.com/bnb-chain/revm", rev = "dfca3edd9732372e19dea88462eaece1e74824f1" }
510+
revm-interpreter = { git = "https://github.com/bnb-chain/revm", rev = "dfca3edd9732372e19dea88462eaece1e74824f1" }
511+
revm-precompile = { git = "https://github.com/bnb-chain/revm", rev = "dfca3edd9732372e19dea88462eaece1e74824f1" }
512+
revm-primitives = { git = "https://github.com/bnb-chain/revm", rev = "dfca3edd9732372e19dea88462eaece1e74824f1" }
513513
alloy-chains = { git = "https://github.com/bnb-chain/alloy-chains-rs.git", rev = "b7c5379cf47345181f8dce350acafb958f47152a" }
514514

515-
516515
[patch."https://github.com/bluealloy/revm"]
517-
revm = { git = "https://github.com/bnb-chain/revm", rev = "63b8ab893c6f1d2984131017de9872b2809b615b" }
516+
revm = { git = "https://github.com/bnb-chain/revm", rev = "dfca3edd9732372e19dea88462eaece1e74824f1" }

crates/bsc/consensus/src/abi.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
use crate::{Parlia, VoteAddress, STAKE_HUB_CONTRACT, VALIDATOR_CONTRACT};
1+
use crate::{Parlia, VoteAddress};
22
use alloy_dyn_abi::{DynSolValue, FunctionExt, JsonAbiExt};
33
use lazy_static::lazy_static;
4-
use reth_primitives::{Address, BlockNumber, Bytes, U256};
4+
use reth_primitives::{
5+
system_contracts::{STAKE_HUB_CONTRACT, VALIDATOR_CONTRACT},
6+
Address, BlockNumber, Bytes, U256,
7+
};
58

69
lazy_static! {
710
pub static ref VALIDATOR_SET_ABI: &'static str = r#"
@@ -5871,7 +5874,7 @@ impl Parlia {
58715874
self.validator_abi_before_luban.function("getValidators").unwrap().first().unwrap()
58725875
};
58735876

5874-
(*VALIDATOR_CONTRACT, Bytes::from(function.abi_encode_input(&[]).unwrap()))
5877+
(VALIDATOR_CONTRACT.parse().unwrap(), Bytes::from(function.abi_encode_input(&[]).unwrap()))
58755878
}
58765879

58775880
pub fn unpack_data_into_validator_set_before_luban(&self, data: &[u8]) -> Vec<Address> {
@@ -5892,7 +5895,7 @@ impl Parlia {
58925895
pub fn get_current_validators(&self) -> (Address, Bytes) {
58935896
let function = self.validator_abi.function("getMiningValidators").unwrap().first().unwrap();
58945897

5895-
(*VALIDATOR_CONTRACT, Bytes::from(function.abi_encode_input(&[]).unwrap()))
5898+
(VALIDATOR_CONTRACT.parse().unwrap(), Bytes::from(function.abi_encode_input(&[]).unwrap()))
58965899
}
58975900

58985901
pub fn unpack_data_into_validator_set(&self, data: &[u8]) -> (Vec<Address>, Vec<VoteAddress>) {
@@ -5922,7 +5925,7 @@ impl Parlia {
59225925
self.stake_hub_abi.function("getValidatorElectionInfo").unwrap().first().unwrap();
59235926

59245927
(
5925-
*STAKE_HUB_CONTRACT,
5928+
STAKE_HUB_CONTRACT.parse().unwrap(),
59265929
Bytes::from(
59275930
function
59285931
.abi_encode_input(&[
@@ -5961,7 +5964,7 @@ impl Parlia {
59615964
let function =
59625965
self.stake_hub_abi.function("maxElectedValidators").unwrap().first().unwrap();
59635966

5964-
(*STAKE_HUB_CONTRACT, Bytes::from(function.abi_encode_input(&[]).unwrap()))
5967+
(STAKE_HUB_CONTRACT.parse().unwrap(), Bytes::from(function.abi_encode_input(&[]).unwrap()))
59655968
}
59665969

59675970
pub fn unpack_data_into_max_elected_validators(&self, data: &[u8]) -> U256 {

crates/bsc/consensus/src/system_tx.rs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
use crate::{
2-
Parlia, BSC_GOVERNOR_CONTRACT, BSC_TIMELOCK_CONTRACT, CROSS_CHAIN_CONTRACT, GOV_TOKEN_CONTRACT,
3-
LIGHT_CLIENT_CONTRACT, RELAYER_HUB_CONTRACT, RELAYER_INCENTIVIZE_CONTRACT, SLASH_CONTRACT,
4-
STAKE_HUB_CONTRACT, SYSTEM_REWARD_CONTRACT, TOKEN_HUB_CONTRACT, TOKEN_RECOVER_PORTAL_CONTRACT,
5-
VALIDATOR_CONTRACT,
6-
};
1+
use crate::Parlia;
72
use alloy_dyn_abi::{DynSolValue, JsonAbiExt};
8-
use reth_primitives::{Address, Bytes, Transaction, TxKind, TxLegacy, U256};
3+
use reth_primitives::{
4+
system_contracts::{
5+
CROSS_CHAIN_CONTRACT, GOVERNOR_CONTRACT, GOV_TOKEN_CONTRACT, LIGHT_CLIENT_CONTRACT,
6+
RELAYER_HUB_CONTRACT, RELAYER_INCENTIVIZE_CONTRACT, SLASH_CONTRACT, STAKE_HUB_CONTRACT,
7+
SYSTEM_REWARD_CONTRACT, TIMELOCK_CONTRACT, TOKEN_HUB_CONTRACT,
8+
TOKEN_RECOVER_PORTAL_CONTRACT, VALIDATOR_CONTRACT,
9+
},
10+
Address, Bytes, Transaction, TxKind, TxLegacy, U256,
11+
};
912

1013
/// Assemble system tx
1114
impl Parlia {
@@ -14,13 +17,13 @@ impl Parlia {
1417
let input = function.abi_encode_input(&[]).unwrap();
1518

1619
let contracts = vec![
17-
*VALIDATOR_CONTRACT,
18-
*SLASH_CONTRACT,
19-
*LIGHT_CLIENT_CONTRACT,
20-
*RELAYER_HUB_CONTRACT,
21-
*TOKEN_HUB_CONTRACT,
22-
*RELAYER_INCENTIVIZE_CONTRACT,
23-
*CROSS_CHAIN_CONTRACT,
20+
VALIDATOR_CONTRACT,
21+
SLASH_CONTRACT,
22+
LIGHT_CLIENT_CONTRACT,
23+
RELAYER_HUB_CONTRACT,
24+
TOKEN_HUB_CONTRACT,
25+
RELAYER_INCENTIVIZE_CONTRACT,
26+
CROSS_CHAIN_CONTRACT,
2427
];
2528

2629
contracts
@@ -33,7 +36,7 @@ impl Parlia {
3336
gas_price: 0,
3437
value: U256::ZERO,
3538
input: Bytes::from(input.clone()),
36-
to: TxKind::Call(contract),
39+
to: TxKind::Call(contract.parse().unwrap()),
3740
})
3841
})
3942
.collect()
@@ -44,11 +47,11 @@ impl Parlia {
4447
let input = function.abi_encode_input(&[]).unwrap();
4548

4649
let contracts = vec![
47-
*STAKE_HUB_CONTRACT,
48-
*BSC_GOVERNOR_CONTRACT,
49-
*GOV_TOKEN_CONTRACT,
50-
*BSC_TIMELOCK_CONTRACT,
51-
*TOKEN_RECOVER_PORTAL_CONTRACT,
50+
STAKE_HUB_CONTRACT,
51+
GOVERNOR_CONTRACT,
52+
GOV_TOKEN_CONTRACT,
53+
TIMELOCK_CONTRACT,
54+
TOKEN_RECOVER_PORTAL_CONTRACT,
5255
];
5356

5457
contracts
@@ -61,7 +64,7 @@ impl Parlia {
6164
gas_price: 0,
6265
value: U256::ZERO,
6366
input: Bytes::from(input.clone()),
64-
to: TxKind::Call(contract),
67+
to: TxKind::Call(contract.parse().unwrap()),
6568
})
6669
})
6770
.collect()
@@ -78,7 +81,7 @@ impl Parlia {
7881
gas_price: 0,
7982
value: U256::ZERO,
8083
input: Bytes::from(input),
81-
to: TxKind::Call(*SLASH_CONTRACT),
84+
to: TxKind::Call(SLASH_CONTRACT.parse().unwrap()),
8285
})
8386
}
8487

@@ -90,7 +93,7 @@ impl Parlia {
9093
gas_price: 0,
9194
value: U256::from(system_reward),
9295
input: Bytes::default(),
93-
to: TxKind::Call(*SYSTEM_REWARD_CONTRACT),
96+
to: TxKind::Call(SYSTEM_REWARD_CONTRACT.parse().unwrap()),
9497
})
9598
}
9699

@@ -105,7 +108,7 @@ impl Parlia {
105108
gas_price: 0,
106109
value: U256::from(block_reward),
107110
input: Bytes::from(input),
108-
to: TxKind::Call(*VALIDATOR_CONTRACT),
111+
to: TxKind::Call(VALIDATOR_CONTRACT.parse().unwrap()),
109112
})
110113
}
111114

@@ -130,7 +133,7 @@ impl Parlia {
130133
gas_price: 0,
131134
value: U256::ZERO,
132135
input: Bytes::from(input),
133-
to: TxKind::Call(*VALIDATOR_CONTRACT),
136+
to: TxKind::Call(VALIDATOR_CONTRACT.parse().unwrap()),
134137
})
135138
}
136139

@@ -161,7 +164,7 @@ impl Parlia {
161164
gas_price: 0,
162165
value: U256::ZERO,
163166
input: Bytes::from(input),
164-
to: TxKind::Call(*VALIDATOR_CONTRACT),
167+
to: TxKind::Call(VALIDATOR_CONTRACT.parse().unwrap()),
165168
})
166169
}
167170
}

crates/bsc/consensus/src/util.rs

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,9 @@
11
use crate::EXTRA_SEAL_LEN;
22
use alloy_rlp::Encodable;
3-
use lazy_static::lazy_static;
43
use reth_primitives::{
5-
keccak256, Address, BufMut, BytesMut, Header, TransactionSigned, B256, B64, U256,
4+
keccak256, system_contracts::SYSTEM_CONTRACTS_SET, Address, BufMut, BytesMut, Header,
5+
TransactionSigned, B256, B64, U256,
66
};
7-
use std::str::FromStr;
8-
9-
lazy_static! {
10-
// preset contracts
11-
pub static ref VALIDATOR_CONTRACT: Address = Address::from_str("0x0000000000000000000000000000000000001000").unwrap();
12-
pub static ref SLASH_CONTRACT: Address = Address::from_str("0x0000000000000000000000000000000000001001").unwrap();
13-
pub static ref SYSTEM_REWARD_CONTRACT: Address = Address::from_str("0x0000000000000000000000000000000000001002").unwrap();
14-
pub static ref LIGHT_CLIENT_CONTRACT: Address = Address::from_str("0x0000000000000000000000000000000000001003").unwrap();
15-
pub static ref TOKEN_HUB_CONTRACT: Address = Address::from_str("0x0000000000000000000000000000000000001004").unwrap();
16-
pub static ref RELAYER_INCENTIVIZE_CONTRACT: Address = Address::from_str("0x0000000000000000000000000000000000001005").unwrap();
17-
pub static ref RELAYER_HUB_CONTRACT: Address = Address::from_str("0x0000000000000000000000000000000000001006").unwrap();
18-
pub static ref GOV_HUB_CONTRACT: Address = Address::from_str("0x0000000000000000000000000000000000001007").unwrap();
19-
pub static ref CROSS_CHAIN_CONTRACT: Address = Address::from_str("0x0000000000000000000000000000000000002000").unwrap();
20-
pub static ref STAKE_HUB_CONTRACT: Address = Address::from_str("0x0000000000000000000000000000000000002002").unwrap();
21-
pub static ref BSC_GOVERNOR_CONTRACT: Address = Address::from_str("0x0000000000000000000000000000000000002004").unwrap();
22-
pub static ref GOV_TOKEN_CONTRACT: Address = Address::from_str("0x0000000000000000000000000000000000002005").unwrap();
23-
pub static ref BSC_TIMELOCK_CONTRACT: Address = Address::from_str("0x0000000000000000000000000000000000002006").unwrap();
24-
pub static ref TOKEN_RECOVER_PORTAL_CONTRACT: Address = Address::from_str("0x0000000000000000000000000000000000003000").unwrap();
25-
26-
pub static ref SYSTEM_CONTRACTS: Vec<Address> = vec![
27-
*VALIDATOR_CONTRACT,
28-
*SLASH_CONTRACT,
29-
*SYSTEM_REWARD_CONTRACT,
30-
*LIGHT_CLIENT_CONTRACT,
31-
*TOKEN_HUB_CONTRACT,
32-
*RELAYER_INCENTIVIZE_CONTRACT,
33-
*RELAYER_HUB_CONTRACT,
34-
*GOV_HUB_CONTRACT,
35-
*CROSS_CHAIN_CONTRACT,
36-
*STAKE_HUB_CONTRACT,
37-
*BSC_GOVERNOR_CONTRACT,
38-
*GOV_TOKEN_CONTRACT,
39-
*BSC_TIMELOCK_CONTRACT,
40-
*TOKEN_RECOVER_PORTAL_CONTRACT,
41-
];
42-
}
437

448
const SECONDS_PER_DAY: u64 = 86400; // 24 * 60 * 60
459

@@ -66,7 +30,7 @@ pub fn is_system_transaction(tx: &TransactionSigned, sender: Address, header: &H
6630

6731
/// whether the contract is system or not
6832
pub fn is_invoke_system_contract(addr: &Address) -> bool {
69-
SYSTEM_CONTRACTS.contains(addr)
33+
SYSTEM_CONTRACTS_SET.contains(addr)
7034
}
7135

7236
pub fn hash_with_chain_id(header: &Header, chain_id: u64) -> B256 {
@@ -166,4 +130,13 @@ mod tests {
166130
println!("encode hash: {:?}", hex::encode(hash.as_slice()));
167131
assert_eq!(hex::encode(hash.as_slice()), expected_hash);
168132
}
133+
134+
#[test]
135+
fn test_is_system_contract() {
136+
let addr1 = address!("0000000000000000000000000000000000001000");
137+
let addr2 = address!("0000000000000000000000000000000000001100");
138+
139+
assert_eq!(super::is_invoke_system_contract(&addr1), true);
140+
assert_eq!(super::is_invoke_system_contract(&addr2), false);
141+
}
169142
}

crates/bsc/evm/src/post_execution.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use crate::{BscBlockExecutionError, BscBlockExecutor};
22
use bitset::BitSet;
33
use reth_bsc_consensus::{
44
get_top_validators_by_voting_power, is_breathe_block, ElectedValidators, ValidatorElectionInfo,
5-
COLLECT_ADDITIONAL_VOTES_REWARD_RATIO, DIFF_INTURN, MAX_SYSTEM_REWARD, SYSTEM_REWARD_CONTRACT,
6-
SYSTEM_REWARD_PERCENT,
5+
COLLECT_ADDITIONAL_VOTES_REWARD_RATIO, DIFF_INTURN, MAX_SYSTEM_REWARD, SYSTEM_REWARD_PERCENT,
76
};
87
use reth_errors::{BlockExecutionError, BlockValidationError, ProviderError};
98
use reth_evm::ConfigureEvm;
109
use reth_primitives::{
1110
hex,
1211
parlia::{Snapshot, VoteAddress, VoteAttestation},
12+
system_contracts::SYSTEM_REWARD_CONTRACT,
1313
Address, BlockWithSenders, GotExpected, Header, Receipt, TransactionSigned, U256,
1414
};
1515
use reth_provider::ParliaProvider;
@@ -287,8 +287,12 @@ where
287287
.increment_balances(balance_increment)
288288
.map_err(|_| BlockValidationError::IncrementBalanceFailed)?;
289289

290-
let system_reward_balance =
291-
self.state.basic(*SYSTEM_REWARD_CONTRACT).unwrap().unwrap_or_default().balance;
290+
let system_reward_balance = self
291+
.state
292+
.basic(SYSTEM_REWARD_CONTRACT.parse().unwrap())
293+
.unwrap()
294+
.unwrap_or_default()
295+
.balance;
292296
if !self.parlia().chain_spec().is_kepler_active_at_timestamp(header.timestamp) &&
293297
system_reward_balance < U256::from(MAX_SYSTEM_REWARD)
294298
{

crates/chainspec/src/spec.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pub static BSC_MAINNET: Lazy<Arc<ChainSpec>> = Lazy::new(|| {
8787
(Hardfork::FeynmanFix, ForkCondition::Timestamp(1713419340)),
8888
(Hardfork::Cancun, ForkCondition::Timestamp(1718863500)),
8989
(Hardfork::Haber, ForkCondition::Timestamp(1718863500)),
90+
(Hardfork::HaberFix, ForkCondition::Timestamp(1720591588)),
9091
]),
9192
deposit_contract: None,
9293
base_fee_params: BaseFeeParamsKind::Constant(BaseFeeParams::new(1, 1)),
@@ -137,6 +138,7 @@ pub static BSC_TESTNET: Lazy<Arc<ChainSpec>> = Lazy::new(|| {
137138
(Hardfork::FeynmanFix, ForkCondition::Timestamp(1711342800)),
138139
(Hardfork::Cancun, ForkCondition::Timestamp(1713330442)),
139140
(Hardfork::Haber, ForkCondition::Timestamp(1716962820)),
141+
(Hardfork::HaberFix, ForkCondition::Timestamp(1719986788)),
140142
]),
141143
deposit_contract: None,
142144
base_fee_params: BaseFeeParamsKind::Constant(BaseFeeParams::new(1, 1)),
@@ -1002,6 +1004,21 @@ impl ChainSpec {
10021004
self.is_fork_active_at_timestamp(Hardfork::Feynman, timestamp)
10031005
}
10041006

1007+
/// Convenience method to check if [`Hardfork::FeynmanFix`] is firstly active at a given
1008+
/// timestamp and parent timestamp.
1009+
#[cfg(feature = "bsc")]
1010+
#[inline]
1011+
pub fn is_on_feynman_fix_at_timestamp(&self, timestamp: u64, parent_timestamp: u64) -> bool {
1012+
self.fork(Hardfork::FeynmanFix).transitions_at_timestamp(timestamp, parent_timestamp)
1013+
}
1014+
1015+
/// Convenience method to check if [`Hardfork::FeynmanFix`] is active at a given timestamp.
1016+
#[cfg(feature = "bsc")]
1017+
#[inline]
1018+
pub fn is_feynman_fix_active_at_timestamp(&self, timestamp: u64) -> bool {
1019+
self.is_fork_active_at_timestamp(Hardfork::FeynmanFix, timestamp)
1020+
}
1021+
10051022
/// Convenience method to check if [`Hardfork::Haber`] is firstly active at a given timestamp
10061023
/// and parent timestamp.
10071024
#[cfg(feature = "bsc")]
@@ -1017,6 +1034,21 @@ impl ChainSpec {
10171034
self.is_fork_active_at_timestamp(Hardfork::Haber, timestamp)
10181035
}
10191036

1037+
/// Convenience method to check if [`Hardfork::HaberFix`] is firstly active at a given timestamp
1038+
/// and parent timestamp.
1039+
#[cfg(feature = "bsc")]
1040+
#[inline]
1041+
pub fn is_on_haber_fix_at_timestamp(&self, timestamp: u64, parent_timestamp: u64) -> bool {
1042+
self.fork(Hardfork::HaberFix).transitions_at_timestamp(timestamp, parent_timestamp)
1043+
}
1044+
1045+
/// Convenience method to check if [`Hardfork::HaberFix`] is active at a given timestamp.
1046+
#[cfg(feature = "bsc")]
1047+
#[inline]
1048+
pub fn is_haber_fix_active_at_timestamp(&self, timestamp: u64) -> bool {
1049+
self.is_fork_active_at_timestamp(Hardfork::HaberFix, timestamp)
1050+
}
1051+
10201052
/// Creates a [`ForkFilter`] for the block described by [Head].
10211053
pub fn fork_filter(&self, head: Head) -> ForkFilter {
10221054
let forks = self.forks_iter().filter_map(|(_, condition)| {

crates/ethereum-forks/src/hardfork.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ pub enum Hardfork {
116116
// ArbOS20Atlas,
117117
/// BSC `Haber` hardfork
118118
Haber,
119+
/// BSC `Haber` hardfork
120+
HaberFix,
119121

120122
// Upcoming
121123
/// Prague: <https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/prague.md>

0 commit comments

Comments
 (0)