Skip to content

feat: add flags to disable tx broadcast & receive #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: scroll
Choose a base branch
from
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
14 changes: 14 additions & 0 deletions crates/net/network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ pub struct NetworkConfig<C, N: NetworkPrimitives = EthNetworkPrimitives> {
pub extra_protocols: RlpxSubProtocols,
/// Whether to disable transaction gossip
pub tx_gossip_disabled: bool,
/// Whether to disable transaction broadcast
pub tx_gossip_broadcast_disabled: bool,
/// Whether to disable transaction receive
pub tx_gossip_receive_disabled: bool,
/// How to instantiate transactions manager.
pub transactions_manager_config: TransactionsManagerConfig,
/// The NAT resolver for external IP
Expand Down Expand Up @@ -211,6 +215,10 @@ pub struct NetworkConfigBuilder<N: NetworkPrimitives = EthNetworkPrimitives> {
head: Option<Head>,
/// Whether tx gossip is disabled
tx_gossip_disabled: bool,
/// Whether to disable transaction broadcast
tx_gossip_broadcast_disabled: bool,
/// Whether to disable transaction receiving
tx_gossip_receive_disabled: bool,
/// The block importer type
block_import: Option<Box<dyn BlockImport<N::NewBlockPayload>>>,
/// How to instantiate transactions manager.
Expand Down Expand Up @@ -258,6 +266,8 @@ impl<N: NetworkPrimitives> NetworkConfigBuilder<N> {
extra_protocols: Default::default(),
head: None,
tx_gossip_disabled: false,
tx_gossip_broadcast_disabled: false,
tx_gossip_receive_disabled: false,
block_import: None,
transactions_manager_config: Default::default(),
nat: None,
Expand Down Expand Up @@ -608,6 +618,8 @@ impl<N: NetworkPrimitives> NetworkConfigBuilder<N> {
extra_protocols,
head,
tx_gossip_disabled,
tx_gossip_broadcast_disabled,
tx_gossip_receive_disabled,
block_import,
transactions_manager_config,
nat,
Expand Down Expand Up @@ -678,6 +690,8 @@ impl<N: NetworkPrimitives> NetworkConfigBuilder<N> {
extra_protocols,
fork_filter,
tx_gossip_disabled,
tx_gossip_broadcast_disabled,
tx_gossip_receive_disabled,
transactions_manager_config,
nat,
handshake,
Expand Down
4 changes: 4 additions & 0 deletions crates/net/network/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ impl<N: NetworkPrimitives> NetworkManager<N> {
dns_discovery_config,
extra_protocols,
tx_gossip_disabled,
tx_gossip_broadcast_disabled,
tx_gossip_receive_disabled,
transactions_manager_config: _,
nat,
handshake,
Expand Down Expand Up @@ -333,6 +335,8 @@ impl<N: NetworkPrimitives> NetworkManager<N> {
network_mode,
Arc::new(AtomicU64::new(chain_id)),
tx_gossip_disabled,
tx_gossip_broadcast_disabled,
tx_gossip_receive_disabled,
discv4,
discv5,
event_sender.clone(),
Expand Down
20 changes: 20 additions & 0 deletions crates/net/network/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ impl<N: NetworkPrimitives> NetworkHandle<N> {
network_mode: NetworkMode,
chain_id: Arc<AtomicU64>,
tx_gossip_disabled: bool,
tx_gossip_broadcast_disabled: bool,
tx_gossip_receive_disabled: bool,
discv4: Option<Discv4>,
discv5: Option<Discv5>,
event_sender: EventSender<NetworkEvent<PeerRequest<N>>>,
Expand All @@ -80,6 +82,8 @@ impl<N: NetworkPrimitives> NetworkHandle<N> {
initial_sync_done: Arc::new(AtomicBool::new(false)),
chain_id,
tx_gossip_disabled,
tx_gossip_broadcast_disabled,
tx_gossip_receive_disabled,
discv4,
discv5,
event_sender,
Expand Down Expand Up @@ -191,6 +195,18 @@ impl<N: NetworkPrimitives> NetworkHandle<N> {
self.inner.tx_gossip_disabled
}

/// Whether tx gossip broadcast is disabled
#[allow(clippy::missing_const_for_fn)]
pub fn tx_gossip_broadcast_disabled(&self) -> bool {
self.inner.tx_gossip_broadcast_disabled
}

/// Whether tx gossip receiving is disabled
#[allow(clippy::missing_const_for_fn)]
pub fn tx_gossip_receive_disabled(&self) -> bool {
self.inner.tx_gossip_receive_disabled
}

/// Returns the secret key used for authenticating sessions.
#[allow(clippy::missing_const_for_fn)]
pub fn secret_key(&self) -> &SecretKey {
Expand Down Expand Up @@ -474,6 +490,10 @@ struct NetworkInner<N: NetworkPrimitives = EthNetworkPrimitives> {
chain_id: Arc<AtomicU64>,
/// Whether to disable transaction gossip
tx_gossip_disabled: bool,
/// Whether to disable transaction broadcast
tx_gossip_broadcast_disabled: bool,
/// Whether to disable transaction receive
tx_gossip_receive_disabled: bool,
/// The instance of the discv4 service
discv4: Option<Discv4>,
/// The instance of the discv5 service
Expand Down
12 changes: 6 additions & 6 deletions crates/net/network/src/transactions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ impl<Pool: TransactionPool, N: NetworkPrimitives, PBundle: TransactionPolicies>
if self.network.is_initially_syncing() {
return
}
if self.network.tx_gossip_disabled() {
if self.network.tx_gossip_disabled() || self.network.tx_gossip_receive_disabled() {
return
}

Expand Down Expand Up @@ -822,7 +822,7 @@ where
if self.network.is_initially_syncing() {
return
}
if self.network.tx_gossip_disabled() {
if self.network.tx_gossip_disabled() || self.network.tx_gossip_broadcast_disabled() {
return
}

Expand Down Expand Up @@ -981,7 +981,7 @@ where
propagation_mode: PropagationMode,
) -> PropagatedTransactions {
let mut propagated = PropagatedTransactions::default();
if self.network.tx_gossip_disabled() {
if self.network.tx_gossip_disabled() || self.network.tx_gossip_broadcast_disabled() {
return propagated
}

Expand Down Expand Up @@ -1089,7 +1089,7 @@ where
response: oneshot::Sender<RequestResult<PooledTransactions<N::PooledTransaction>>>,
) {
if let Some(peer) = self.peers.get_mut(&peer_id) {
if self.network.tx_gossip_disabled() {
if self.network.tx_gossip_disabled() || self.network.tx_gossip_broadcast_disabled() {
let _ = response.send(Ok(PooledTransactions::default()));
return
}
Expand Down Expand Up @@ -1184,7 +1184,7 @@ where
// Send a `NewPooledTransactionHashes` to the peer with up to
// `SOFT_LIMIT_COUNT_HASHES_IN_NEW_POOLED_TRANSACTIONS_BROADCAST_MESSAGE`
// transactions in the pool.
if self.network.is_initially_syncing() || self.network.tx_gossip_disabled() {
if self.network.is_initially_syncing() || self.network.tx_gossip_disabled() || self.network.tx_gossip_broadcast_disabled() {
trace!(target: "net::tx", ?peer_id, "Skipping transaction broadcast: node syncing or gossip disabled");
return
}
Expand Down Expand Up @@ -1288,7 +1288,7 @@ where
if self.network.is_initially_syncing() {
return
}
if self.network.tx_gossip_disabled() {
if self.network.tx_gossip_disabled() || self.network.tx_gossip_receive_disabled() {
return
}

Expand Down
54 changes: 42 additions & 12 deletions crates/scroll/node/src/builder/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,48 @@ use std::fmt::Debug;

/// The network builder for Scroll.
#[derive(Debug, Default, Clone, Copy)]
pub struct ScrollNetworkBuilder;
pub struct ScrollNetworkBuilder {
/// Disable transaction pool broadcast
pub disable_txpool_broadcast: bool,
/// Disable transaction pool receive
pub disable_txpool_receive: bool,
}

impl ScrollNetworkBuilder {
/// Returns the [`NetworkConfig`] that contains the settings to launch the p2p network.
///
/// This applies the configured [`ScrollNetworkBuilder`] settings.
pub fn network_config<Node>(
&self,
ctx: &BuilderContext<Node>,
) -> eyre::Result<NetworkConfig<<Node as FullNodeTypes>::Provider, ScrollNetworkPrimitives>>
where
Node:
FullNodeTypes<Types: NodeTypes<ChainSpec = ScrollChainSpec, Primitives = ScrollPrimitives>>,
{
let Self { disable_txpool_broadcast, disable_txpool_receive } = self.clone();

// get the header transform.
let chain_spec = ctx.chain_spec();
let transform = ScrollHeaderTransform { chain_spec };

let config = ctx.network_config()?;

// set the network mode to work.
let network_config = NetworkConfig {
network_mode: NetworkMode::Work,
header_transform: Box::new(transform),
// When `sequencer_endpoint` is configured, the node will forward all transactions to a
// Sequencer node for execution and inclusion on L1, and disable its own txpool
// gossip broadcast/receive to prevent other parties in the network from learning about them.
tx_gossip_broadcast_disabled: disable_txpool_broadcast,
tx_gossip_receive_disabled: disable_txpool_receive,
..config
};

Ok(network_config)
}
}

impl<Node, Pool> NetworkBuilder<Node, Pool> for ScrollNetworkBuilder
where
Expand All @@ -37,18 +78,7 @@ where
ctx: &BuilderContext<Node>,
pool: Pool,
) -> eyre::Result<Self::Network> {
// get the header transform.
let chain_spec = ctx.chain_spec();
let transform = ScrollHeaderTransform { chain_spec };

// set the network mode to work.
let config = ctx.network_config()?;
let config = NetworkConfig {
network_mode: NetworkMode::Work,
header_transform: Box::new(transform),
..config
};

let network = NetworkManager::builder(config).await?;
let handle = ctx.start_network(network, pool);
info!(target: "reth::cli", enode=%handle.local_node_record(), "P2P networking initialized");
Expand Down
2 changes: 1 addition & 1 deletion crates/scroll/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl ScrollNode {
.pool(ScrollPoolBuilder::default())
.executor(ScrollExecutorBuilder::default())
.payload(BasicPayloadServiceBuilder::new(ScrollPayloadBuilderBuilder::default()))
.network(ScrollNetworkBuilder)
.network(ScrollNetworkBuilder::default())
.executor(ScrollExecutorBuilder)
.consensus(ScrollConsensusBuilder)
}
Expand Down
Loading