Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
274 changes: 138 additions & 136 deletions Cargo.lock

Large diffs are not rendered by default.

82 changes: 42 additions & 40 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,56 +13,58 @@ name = "reth-bsc"
path = "src/main.rs"

[dependencies]
reth = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-cli = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-cli-commands = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-basic-payload-builder = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-db = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-engine-local = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-chainspec = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-cli-util = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-discv4 = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7", features = [
reth = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please be sure our are using https://github.com/bnb-chain/reth/tree/rebase-v1.10.2-bnb as we rebased the old reth and the history will not be the same

reth-cli = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-cli-commands = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-basic-payload-builder = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-db = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-engine-local = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-engine-tree = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-chainspec = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-cli-util = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-discv4 = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a", features = [
"test-utils",
] }
reth-engine-primitives = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-ethereum-forks = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7", features = [
reth-engine-primitives = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-ethereum-forks = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a", features = [
"serde",
] }
reth-ethereum-payload-builder = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-payload-builder-primitives = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-chain-state = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-ethereum-primitives = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-eth-wire = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-eth-wire-types = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-evm = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-evm-ethereum = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-ipc = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-metrics = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-node-core = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-revm = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-network = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7", features = [
reth-ethereum-payload-builder = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-payload-builder-primitives = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-chain-state = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-ethereum-primitives = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-eth-wire = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-eth-wire-types = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-evm = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-evm-ethereum = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-ipc = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-metrics = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-node-core = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-revm = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-network = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a", features = [
"test-utils",
] }
reth-network-p2p = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-network-api = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-node-ethereum = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7", features = [
reth-network-p2p = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-network-api = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-node-ethereum = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a", features = [
"test-utils",
] }
reth-network-peers = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-payload-primitives = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-ethereum-engine-primitives = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-primitives = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-primitives-traits = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-provider = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7", features = [
reth-network-peers = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-payload-primitives = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-ethereum-engine-primitives = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-primitives = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-primitives-traits = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-provider = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a", features = [
"test-utils",
] }
reth-rpc-eth-api = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-rpc-engine-api = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-tracing = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-trie-common = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-trie-db = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-tasks = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-transaction-pool = { git = "https://github.com/bnb-chain/reth.git", tag = "v0.0.7" }
reth-rpc-eth-api = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-rpc-engine-api = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-tracing = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-trie-common = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-trie-db = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-storage-api = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-tasks = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
reth-transaction-pool = { git = "https://github.com/galaio/bnb-reth.git", rev = "f3d3416a47059c8815891eee9997cb15b32b473a" }
revm = "29.0.0"

# alloy dependencies
Expand Down
66 changes: 50 additions & 16 deletions src/consensus/parlia/consensus.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use std::sync::Arc;
use std::sync::RwLock;
use std::time::SystemTime;
Expand All @@ -17,6 +18,7 @@ use super::{
use crate::consensus::parlia::constants::K_ANCESTOR_GENERATION_DEPTH;
use crate::consensus::parlia::go_rng::{RngSource, Shuffle};
use crate::consensus::parlia::provider::SnapshotProvider;
use crate::consensus::parlia::util::calculate_millisecond_timestamp;
use crate::consensus::parlia::util::is_breathe_block;
use crate::consensus::parlia::vote_pool::fetch_vote_by_block_hash;
use crate::consensus::parlia::VoteData;
Expand All @@ -25,17 +27,20 @@ use crate::consensus::parlia::SYSTEM_TXS_GAS_HARD_LIMIT;
use crate::consensus::parlia::SYSTEM_TXS_GAS_SOFT_LIMIT;
use crate::hardforks::BscHardforks;
use crate::node::evm::pre_execution::TURN_LENGTH_CACHE;
use crate::node::evm::util::get_cannonical_header_from_cache;
use crate::node::evm::util::get_header_by_hash_from_cache;
use alloy_consensus::{BlockHeader, Header};
use alloy_primitives::{Address, B256};
use alloy_rlp::Decodable;
use crate::metrics::BscFinalityMetrics;
use reth_chainspec::EthChainSpec;
use schnellru::ByLength;
use schnellru::LruMap;
use secp256k1::{
ecdsa::{RecoverableSignature, RecoveryId},
Message, SECP256K1,
};
use tracing::{debug, trace, warn};
use tracing::{debug, warn};

const RECOVERED_PROPOSER_CACHE_NUM: usize = 4096;
const ADDRESS_LENGTH: usize = 20; // Ethereum address length in bytes
Expand All @@ -45,6 +50,9 @@ lazy_static! {
static ref RECOVERED_PROPOSER_CACHE: RwLock<LruMap<B256, Address, ByLength>> = RwLock::new(LruMap::new(ByLength::new(RECOVERED_PROPOSER_CACHE_NUM as u32)));
}

pub static FINALITY_METRICS: Lazy<BscFinalityMetrics> =
Lazy::new(BscFinalityMetrics::default);

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Parlia<ChainSpec> {
pub spec: Arc<ChainSpec>,
Expand Down Expand Up @@ -413,13 +421,12 @@ where
pub fn back_off_time(&self, snap: &Snapshot, parent: &Header, header: &Header) -> u64 {
let validator = header.beneficiary;

tracing::trace!(
tracing::debug!(
target: "bsc::consensus::back_off_time",
block_number = header.number,
validator = ?validator,
snapshot_block = snap.block_number,
recent_proposers_count = snap.recent_proposers.len(),
recent_proposers = ?snap.recent_proposers,
"Calculating back_off_time"
);

Expand All @@ -438,10 +445,11 @@ where
if self.spec.is_planck_active_at_block(header.number) {
let counts = snap.count_recent_proposers();

// Avoid dumping the entire counts map at debug level to reduce per-block logging overhead.
tracing::trace!(
target: "bsc::consensus::back_off_time",
block_number = header.number,
counts = ?counts,
counts_len = counts.len(),
"Counted recent proposers"
);

Expand All @@ -452,7 +460,7 @@ where

let inturn_addr = snap.inturn_validator();
if snap.sign_recently_by_counts(inturn_addr, &counts) {
trace!(
debug!(
"in turn validator({:?}) has recently signed, skip initialBackOffTime",
inturn_addr
);
Expand Down Expand Up @@ -534,7 +542,7 @@ where
delay + back_off_steps[idx] * BACKOFF_TIME_OF_WIGGLE
};

tracing::trace!(
tracing::debug!(
target: "bsc::consensus::back_off_time",
block_number = header.number,
validator = ?validator,
Expand Down Expand Up @@ -567,14 +575,15 @@ where
delay_ms -= left_over_ms;
}

let mut time_for_mining_ms = period_ms / 2;
let last_block_in_turn = snap.last_block_in_one_turn(header.number);
if !last_block_in_turn {
time_for_mining_ms = period_ms;
}
if delay_ms > time_for_mining_ms {
delay_ms = time_for_mining_ms;
}
// TODO: remove it now, miner can produce block until timeout.
// let mut time_for_mining_ms = period_ms / 2;
// let last_block_in_turn = snap.last_block_in_one_turn(header.number);
// if !last_block_in_turn {
// time_for_mining_ms = period_ms;
// }
// if delay_ms > time_for_mining_ms {
// delay_ms = time_for_mining_ms;
// }
Comment on lines +578 to +586
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we keep it?


delay_ms
}
Expand Down Expand Up @@ -655,6 +664,7 @@ where
new_header: &mut Header,
) -> Result<(), ParliaConsensusError> {
let epoch_length = parent_snap.epoch_num;
tracing::debug!("Prepare turn length: block_number={}, epoch_length={}, is_bohr={}", new_header.number, epoch_length, self.spec.is_bohr_active_at_timestamp(new_header.number, new_header.timestamp));
if !new_header.number.is_multiple_of(epoch_length)
|| !self.spec.is_bohr_active_at_timestamp(new_header.number, new_header.timestamp)
{
Expand Down Expand Up @@ -711,7 +721,7 @@ where
}

// get justified number and hash from parent snapshot
let (justified_number, justified_hash) =
let (mut justified_number, mut justified_hash) =
(parent_snap.vote_data.target_number, parent_snap.vote_data.target_hash);
let mut times = 1;
if self
Expand Down Expand Up @@ -756,7 +766,20 @@ where
return Ok(());
}
};

// if justified_hash is zero, it loads genesis hash as source.
// once one attestation generated, attestation of snap would not be nil forever basically
// ref: https://github.com/bnb-chain/bsc/blob/583cfec3ea811fb124e6812aabd190555d5aeabc/consensus/parlia/parlia.go#L2161
if justified_hash == B256::ZERO {
match get_cannonical_header_from_cache(0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
match get_cannonical_header_from_cache(0) {
match get_canonical_header_from_cache(0) {

Some(genesis_header) => {
justified_number = genesis_header.number();
justified_hash = genesis_header.hash_slow();
}
None => {
return Err(ParliaConsensusError::HeaderNotFound { block_hash: B256::ZERO });
}
}
}
let mut attestation = VoteAttestation::new_with_vote_data(VoteData {
source_number: justified_number,
source_hash: justified_hash,
Expand Down Expand Up @@ -802,6 +825,17 @@ where
tracing::debug!(target: "parlia::consensus", "not enough unique votes after filtering, have={}, need={}", ordered_unique.len(), quorum);
return Ok(()); // If not enough unique votes, do not append attestation
}

// record finality duration in header
let finalized_header = get_header_by_hash_from_cache(&justified_hash)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metric collection is now in the attestation assembly critical path. If header lookup misses, we return error and skip attestation assembly.

.ok_or(ParliaConsensusError::HeaderNotFound { block_hash: justified_hash })?;
let finalized_ms = calculate_millisecond_timestamp(&finalized_header) as u128;
let now_ms = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_millis();
FINALITY_METRICS.finality_duration_ms.record(now_ms.abs_diff(finalized_ms) as f64);

// Aggregate signatures
let sigs: Vec<blst::min_pk::Signature> = ordered_unique
.iter()
Expand Down
2 changes: 1 addition & 1 deletion src/consensus/parlia/ramanujan_fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ where ChainSpec: EthChainSpec + BscHardforks + 'static,
if header_timestamp > present_timestamp {
delay_ms = header_timestamp - present_timestamp;
}
tracing::trace!(
tracing::debug!(
target: "bsc::miner",
block_number = header.number,
block_timestamp = header_timestamp,
Expand Down
2 changes: 1 addition & 1 deletion src/consensus/parlia/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ impl Snapshot {
if let Some(&times) = counts.get(&validator) {
let allowed = u64::from(self.turn_length.unwrap_or(1));
if u64::from(times) >= allowed {
tracing::debug!("Recently signed, validator: {:?}, block_number: {:?}, times: {:?}, allowed: {:?}", validator, self.block_number, times, allowed);
tracing::debug!("Recently signed, validator: {:?}, block_number: {:?}, times: {:?}, allowed: {:?}, counts: {:?}", validator, self.block_number, times, allowed, counts);
return true;
}
}
Expand Down
Loading
Loading