Skip to content
Closed
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
6 changes: 2 additions & 4 deletions examples/bsc-p2p/src/handshake.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::upgrade_status::{UpgradeStatus, UpgradeStatusExtension};
use crate::upgrade_status::UpgradeStatus;
use alloy_rlp::Decodable;
use futures::SinkExt;
use reth_eth_wire::{
Expand Down Expand Up @@ -26,9 +26,7 @@ impl BscHandshake {
) -> Result<UnifiedStatus, EthStreamError> {
if negotiated_status.version > EthVersion::Eth66 {
// Send upgrade status message allowing peer to broadcast transactions
let upgrade_msg = UpgradeStatus {
extension: UpgradeStatusExtension { disable_peer_tx_broadcast: false },
};
let upgrade_msg = UpgradeStatus::allow_broadcast();
unauth.start_send_unpin(upgrade_msg.into_rlpx())?;

// Receive peer's upgrade status response
Expand Down
38 changes: 36 additions & 2 deletions examples/bsc-p2p/src/upgrade_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ impl Decodable for UpgradeStatus {
}

impl UpgradeStatus {
/// Creates a new `UpgradeStatus` with the specified extension.
#[allow(dead_code)]
pub fn new(extension: UpgradeStatusExtension) -> Self {
Self { extension }
}

/// Creates a new `UpgradeStatus` that allows peer transaction broadcasting.
pub fn allow_broadcast() -> Self {
Self { extension: UpgradeStatusExtension::allow_broadcast() }
}

/// Creates a new `UpgradeStatus` that disables peer transaction broadcasting.
#[allow(dead_code)]
pub fn disable_broadcast() -> Self {
Self { extension: UpgradeStatusExtension::disable_broadcast() }
}

/// Encode the upgrade status message into RLPx bytes.
pub fn into_rlpx(self) -> Bytes {
let mut out = BytesMut::new();
Expand All @@ -44,11 +61,28 @@ impl UpgradeStatus {
}

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

impl UpgradeStatusExtension {
/// Creates a new `UpgradeStatusExtension` with the specified broadcast setting.
#[allow(dead_code)]
pub fn new(disable_peer_tx_broadcast: bool) -> Self {
Self { disable_peer_tx_broadcast }
}

/// Creates a new `UpgradeStatusExtension` that allows peer transaction broadcasting.
pub fn allow_broadcast() -> Self {
Self { disable_peer_tx_broadcast: false }
}

/// Creates a new `UpgradeStatusExtension` that disables peer transaction broadcasting.
#[allow(dead_code)]
pub fn disable_broadcast() -> Self {
Self { disable_peer_tx_broadcast: true }
}
}
1 change: 1 addition & 0 deletions examples/bsc-p2p/tests/it/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(missing_docs)]

mod p2p;
mod upgrade_status;

const fn main() {}
65 changes: 65 additions & 0 deletions examples/bsc-p2p/tests/it/upgrade_status.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use alloy_rlp::{Decodable, Encodable};
use example_bsc_p2p::upgrade_status::{UpgradeStatus, UpgradeStatusExtension};

#[test]
fn test_upgrade_status_extension_constructors() {
// Test new constructor
let extension = UpgradeStatusExtension::new(true);
assert!(extension.disable_peer_tx_broadcast);

let extension = UpgradeStatusExtension::new(false);
assert!(!extension.disable_peer_tx_broadcast);

// Test allow_broadcast constructor
let extension = UpgradeStatusExtension::allow_broadcast();
assert!(!extension.disable_peer_tx_broadcast);

// Test disable_broadcast constructor
let extension = UpgradeStatusExtension::disable_broadcast();
assert!(extension.disable_peer_tx_broadcast);
}

#[test]
fn test_upgrade_status_constructors() {
// Test new constructor
let extension = UpgradeStatusExtension::new(true);
let status = UpgradeStatus::new(extension);
assert!(status.extension.disable_peer_tx_broadcast);

// Test allow_broadcast constructor
let status = UpgradeStatus::allow_broadcast();
assert!(!status.extension.disable_peer_tx_broadcast);

// Test disable_broadcast constructor
let status = UpgradeStatus::disable_broadcast();
assert!(status.extension.disable_peer_tx_broadcast);
}

#[test]
fn test_upgrade_status_rlpx_encoding() {
// Test that into_rlpx() produces valid bytes
let status = UpgradeStatus::allow_broadcast();
let encoded = status.into_rlpx();
assert!(!encoded.is_empty());

let status = UpgradeStatus::disable_broadcast();
let encoded = status.into_rlpx();
assert!(!encoded.is_empty());
}

#[test]
fn test_upgrade_status_extension_encoding_decoding() {
// Test encoding and decoding for allow_broadcast
let original = UpgradeStatusExtension::allow_broadcast();
let mut encoded = Vec::new();
original.encode(&mut encoded);
let decoded = UpgradeStatusExtension::decode(&mut encoded.as_slice()).unwrap();
assert_eq!(original, decoded);

// Test encoding and decoding for disable_broadcast
let original = UpgradeStatusExtension::disable_broadcast();
let mut encoded = Vec::new();
original.encode(&mut encoded);
let decoded = UpgradeStatusExtension::decode(&mut encoded.as_slice()).unwrap();
assert_eq!(original, decoded);
}
Loading