Skip to content
This repository has been archived by the owner on Feb 6, 2025. It is now read-only.

Commit

Permalink
update p2p handshake and tmp disable some validations
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing committed May 14, 2024
1 parent 9f0bd70 commit edc3580
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 60 deletions.
1 change: 1 addition & 0 deletions crates/bsc/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ serde_json.workspace = true
[features]
bsc = [
"reth-primitives/bsc",
"reth-network/bsc"
]
6 changes: 4 additions & 2 deletions crates/ethereum/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ impl EthBeaconConsensus {

impl Consensus for EthBeaconConsensus {
fn validate_header(&self, header: &SealedHeader) -> Result<(), ConsensusError> {
validation::validate_header_standalone(header, &self.chain_spec)?;
// TODO: revert after testing
//validation::validate_header_standalone(header, &self.chain_spec)?;
Ok(())
}

Expand All @@ -42,7 +43,8 @@ impl Consensus for EthBeaconConsensus {
header: &SealedHeader,
parent: &SealedHeader,
) -> Result<(), ConsensusError> {
header.validate_against_parent(parent, &self.chain_spec).map_err(ConsensusError::from)?;
// TODO: revert after testing
// header.validate_against_parent(parent, &self.chain_spec).map_err(ConsensusError::from)?;
Ok(())
}

Expand Down
27 changes: 12 additions & 15 deletions crates/net/eth-wire-types/src/upgrade_status.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,45 @@
use alloy_rlp::{RlpDecodable, RlpEncodable};
use serde::{Deserialize, Serialize};
use reth_codecs::derive_arbitrary;
use reth_primitives::{Bytes, B256, hex};

/// UpdateStatus packet introduced in BSC to notify peers whether to broadcast transaction or not.
/// It is used during the p2p handshake.
#[derive_arbitrary(rlp)]
#[derive(Debug, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct UpgradeStatus {
pub extension : UpgradeStatusExtension
pub extension: UpgradeStatusExtension,
}

/// The extension to define whether to enable or disable the flag.
/// This flag currently is ignored, and will be supported later.
#[derive_arbitrary(rlp)]
#[derive(Debug, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct UpgradeStatusExtension {
pub disable_peer_tx_broadcast: bool
// TODO: support disable_peer_tx_broadcast flag
pub disable_peer_tx_broadcast: bool,
}

#[cfg(test)]
mod tests {
use super::*;
use std::path::Path;
use alloy_rlp::Encodable;
use reth_primitives::hex;
use crate::{EthMessage, ProtocolMessage};

#[test]
fn test_encode_upgrade_status() {
let extension = UpgradeStatusExtension{disable_peer_tx_broadcast: true};
let extension = UpgradeStatusExtension { disable_peer_tx_broadcast: true };
let mut buffer = Vec::<u8>::new();
let _ = extension.encode(&mut buffer);
println!("extension hex: {}", hex::encode(buffer.clone()));
assert_eq!("c101", hex::encode(buffer.clone()));

let upgrade_status = UpgradeStatus{
extension: extension,
let upgrade_status = UpgradeStatus {
extension,
};
let mut buffer = Vec::<u8>::new();
let _ = upgrade_status.encode(&mut buffer);
println!("upgrade_status hex: {}", hex::encode(buffer.clone()));

// let result = alloy_rlp::encode(ProtocolMessage::from(EthMessage::UpgradeStatus(UpgradeStatus{
// extension: buffer,
// })));
// println!("message hex: {}", hex::encode(result))
assert_eq!("c2c101", hex::encode(buffer.clone()));
}
}

1 change: 1 addition & 0 deletions crates/net/eth-wire/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ arbitrary = [
"dep:proptest-derive",
]
optimism = ["reth-primitives/optimism"]
bsc = ["reth-primitives/bsc"]

[[test]]
name = "fuzz_roundtrip"
Expand Down
78 changes: 46 additions & 32 deletions crates/net/eth-wire/src/ethstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,42 +169,56 @@ impl<S, E> UnauthedEthStream<S>
}

// For BSC, UpgradeStatus message should be sent during handshake.
//#[cfg(feature = "bsc")]
if version > EthVersion::Eth66 {
// TODO: support disable_peer_tx_broadcast flag
// let extension = UpgradeStatusExtension{disable_peer_tx_broadcast: true};
// let mut buffer = Vec::<u8>::new();
// let _ = extension.encode(&mut buffer);
self.inner
.send(alloy_rlp::encode(ProtocolMessage::from(EthMessage::UpgradeStatus(UpgradeStatus {
extension: UpgradeStatusExtension { disable_peer_tx_broadcast: false },
}))).into())
.await?;
let their_msg_res = self.inner.next().await;
let their_msg = match their_msg_res {
Some(msg) => msg,
None => {
self.inner.disconnect(DisconnectReason::DisconnectRequested).await?;
return Err(EthStreamError::EthHandshakeError(EthHandshakeError::NoResponse));
}
}?;
let msg = match ProtocolMessage::decode_message(version, &mut their_msg.as_ref()) {
Ok(m) => m,
Err(err) => {
debug!("decode error in eth handshake: msg={their_msg:x}");
self.inner.disconnect(DisconnectReason::DisconnectRequested).await?;
return Err(EthStreamError::InvalidMessage(err));
#[cfg(feature = "bsc")]
{
if version > EthVersion::Eth66 {
self.inner
.send(alloy_rlp::encode(ProtocolMessage::from(EthMessage::UpgradeStatus(UpgradeStatus {
extension: UpgradeStatusExtension { disable_peer_tx_broadcast: false },
}))).into())
.await?;
let their_msg_res = self.inner.next().await;
let their_msg = match their_msg_res {
Some(msg) => msg,
None => {
self.inner.disconnect(DisconnectReason::DisconnectRequested).await?;
return Err(EthStreamError::EthHandshakeError(EthHandshakeError::NoResponse));
}
}?;
let msg = match ProtocolMessage::decode_message(version, &mut their_msg.as_ref()) {
Ok(m) => m,
Err(err) => {
debug!("decode error in eth handshake: msg={their_msg:x}");
self.inner.disconnect(DisconnectReason::DisconnectRequested).await?;
return Err(EthStreamError::InvalidMessage(err));
}
};
match msg.message {
EthMessage::UpgradeStatus(_) => {
// TODO: support disable_peer_tx_broadcast flag
let stream = EthStream::new(version, self.inner);
Ok((stream, resp))
}
_ => {
self.inner.disconnect(DisconnectReason::ProtocolBreach).await?;
Err(EthStreamError::EthHandshakeError(
EthHandshakeError::NonStatusMessageInHandshake,
))
}
}
};
// TODO: support disable_peer_tx_broadcast flag
debug!("msg {:?}", msg.message)
} else {
let stream = EthStream::new(version, self.inner);
Ok((stream, resp))
}
}

// now we can create the `EthStream` because the peer has successfully completed
// the handshake
let stream = EthStream::new(version, self.inner);
#[cfg(not(feature = "bsc"))] {
// now we can create the `EthStream` because the peer has successfully completed
// the handshake
let stream = EthStream::new(version, self.inner);

Ok((stream, resp))
Ok((stream, resp))
}
}
_ => {
self.inner.disconnect(DisconnectReason::ProtocolBreach).await?;
Expand Down
1 change: 1 addition & 0 deletions crates/net/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ default = ["serde"]
serde = ["dep:serde", "dep:humantime-serde", "secp256k1/serde", "enr/serde", "dep:serde_json"]
test-utils = ["reth-provider/test-utils", "dep:tempfile", "reth-transaction-pool/test-utils"]
geth-tests = []
bsc = ["reth-eth-wire/bsc"]

[[bench]]
name = "bench"
Expand Down
6 changes: 3 additions & 3 deletions crates/net/network/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,9 @@ where
// reject message in POS
if self.handle.mode().is_stake() {
// connections to peers which send invalid messages should be terminated
// self.swarm
// .sessions_mut()
// .disconnect(peer_id, Some(DisconnectReason::SubprotocolSpecific));
self.swarm
.sessions_mut()
.disconnect(peer_id, Some(DisconnectReason::SubprotocolSpecific));
only_pow(self);
} else {
only_pow(self);
Expand Down
8 changes: 0 additions & 8 deletions crates/primitives/src/chain/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ pub static BSC_MAINNET: Lazy<Arc<ChainSpec>> = Lazy::new(|| {
(Hardfork::Petersburg, ForkCondition::Block(0)),
(Hardfork::Istanbul, ForkCondition::Block(0)),
(Hardfork::MuirGlacier, ForkCondition::Block(0)),
(
Hardfork::Paris,
ForkCondition::TTD { fork_block: Some(0), total_difficulty: U256::ZERO },
),
(Hardfork::Ramanujan, ForkCondition::Block(0)),
(Hardfork::Niels, ForkCondition::Block(0)),
(Hardfork::MirrorSync, ForkCondition::Block(5184000)),
Expand Down Expand Up @@ -108,10 +104,6 @@ pub static BSC_TESTNET: Lazy<Arc<ChainSpec>> = Lazy::new(|| {
(Hardfork::Petersburg, ForkCondition::Block(0)),
(Hardfork::Istanbul, ForkCondition::Block(0)),
(Hardfork::MuirGlacier, ForkCondition::Block(0)),
(
Hardfork::Paris,
ForkCondition::TTD { fork_block: Some(0), total_difficulty: U256::ZERO },
),
(Hardfork::Ramanujan, ForkCondition::Block(1010000)),
(Hardfork::Niels, ForkCondition::Block(1014369)),
(Hardfork::MirrorSync, ForkCondition::Block(5582500)),
Expand Down
3 changes: 3 additions & 0 deletions crates/primitives/src/constants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ pub const RETH_CLIENT_VERSION: &str = concat!("reth/v", env!("CARGO_PKG_VERSION"
pub const SELECTOR_LEN: usize = 4;

/// Maximum extra data size in a block after genesis
#[cfg(not(feature="bsc"))]
pub const MAXIMUM_EXTRA_DATA_SIZE: usize = 32;
#[cfg(feature="bsc")]
pub const MAXIMUM_EXTRA_DATA_SIZE: usize = 1024*1024;

/// An EPOCH is a series of 32 slots.
pub const EPOCH_SLOTS: u64 = 32;
Expand Down

0 comments on commit edc3580

Please sign in to comment.