Skip to content

Commit

Permalink
feat(network): hardcode the external address of the node
Browse files Browse the repository at this point in the history
  • Loading branch information
ShahakShama authored and eitanm-starkware committed Aug 18, 2024
1 parent 0b88013 commit 7e04ae8
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 10 deletions.
10 changes: 10 additions & 0 deletions config/papyrus/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@
"pointer_target": "chain_id",
"privacy": "Public"
},
"network.hardcoded_external_multiaddr": {
"description": "The external address other peers see this node. If this is set, the node will not try to find out which addresses it has and will write this address as external instead",
"privacy": "Public",
"value": ""
},
"network.hardcoded_external_multiaddr.#is_none": {
"description": "Flag for an optional field.",
"privacy": "TemporaryValue",
"value": true
},
"network.idle_connection_timeout": {
"description": "Amount of time in seconds that a connection with no active sessions will stay alive.",
"privacy": "Public",
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_network/src/e2e_broadcast_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async fn create_swarm(bootstrap_peer_multiaddr: Option<Multiaddr>) -> Swarm<Mixe
fn create_network_manager(
swarm: Swarm<MixedBehaviour>,
) -> GenericNetworkManager<Swarm<MixedBehaviour>> {
GenericNetworkManager::generic_new(swarm)
GenericNetworkManager::generic_new(swarm, None)
}

const BUFFER_SIZE: usize = 100;
Expand Down
11 changes: 11 additions & 0 deletions crates/papyrus_network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct NetworkConfig {
#[validate(custom = "validate_vec_u256")]
#[serde(deserialize_with = "deserialize_optional_vec_u8")]
pub(crate) secret_key: Option<Vec<u8>>,
pub hardcoded_external_multiaddr: Option<Multiaddr>,
pub chain_id: ChainId,
}

Expand Down Expand Up @@ -96,6 +97,15 @@ impl SerializeConfig for NetworkConfig {
will be used.",
ParamPrivacyInput::Private,
)]);
config.extend(ser_optional_param(
&self.bootstrap_peer_multiaddr,
Multiaddr::empty(),
"hardcoded_external_multiaddr",
"The external address other peers see this node. If this is set, the node will not \
try to find out which addresses it has and will write this address as external \
instead",
ParamPrivacyInput::Public,
));
config
}
}
Expand All @@ -109,6 +119,7 @@ impl Default for NetworkConfig {
idle_connection_timeout: Duration::from_secs(120),
bootstrap_peer_multiaddr: None,
secret_key: None,
hardcoded_external_multiaddr: None,
chain_id: ChainId::Mainnet,
}
}
Expand Down
26 changes: 22 additions & 4 deletions crates/papyrus_network/src/network_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub struct GenericNetworkManager<SwarmT: SwarmTrait> {
messages_to_broadcast_receivers: StreamHashMap<TopicHash, Receiver<Bytes>>,
broadcasted_messages_senders: HashMap<TopicHash, Sender<(Bytes, ReportSender)>>,
reported_peer_receivers: FuturesUnordered<BoxFuture<'static, Option<PeerId>>>,
hardcoded_external_multiaddr: Option<Multiaddr>,
// Fields for metrics
num_active_inbound_sessions: usize,
num_active_outbound_sessions: usize,
Expand All @@ -74,10 +75,18 @@ impl<SwarmT: SwarmTrait> GenericNetworkManager<SwarmT> {
}
}

pub(crate) fn generic_new(swarm: SwarmT) -> Self {
// TODO(shahak): remove the hardcoded_external_multiaddr arg once we manage external addresses
// in a behaviour.
pub(crate) fn generic_new(
mut swarm: SwarmT,
hardcoded_external_multiaddr: Option<Multiaddr>,
) -> Self {
gauge!(papyrus_metrics::PAPYRUS_NUM_CONNECTED_PEERS, 0f64);
let reported_peer_receivers = FuturesUnordered::new();
reported_peer_receivers.push(futures::future::pending().boxed());
if let Some(address) = hardcoded_external_multiaddr.clone() {
swarm.add_external_address(address);
}
Self {
swarm,
inbound_protocol_to_buffer_size: HashMap::new(),
Expand All @@ -89,6 +98,7 @@ impl<SwarmT: SwarmTrait> GenericNetworkManager<SwarmT> {
messages_to_broadcast_receivers: StreamHashMap::new(HashMap::new()),
broadcasted_messages_senders: HashMap::new(),
reported_peer_receivers,
hardcoded_external_multiaddr,
num_active_inbound_sessions: 0,
num_active_outbound_sessions: 0,
}
Expand Down Expand Up @@ -258,13 +268,14 @@ impl<SwarmT: SwarmTrait> GenericNetworkManager<SwarmT> {
}
SwarmEvent::NewListenAddr { address, .. } => {
// TODO(shahak): Find a better way to filter private addresses.
if !is_localhost(&address) {
if !is_localhost(&address) && self.hardcoded_external_multiaddr.is_none() {
self.swarm.add_external_address(address);
}
}
SwarmEvent::IncomingConnection { .. }
| SwarmEvent::Dialing { .. }
| SwarmEvent::NewExternalAddrCandidate { .. } => {}
| SwarmEvent::NewExternalAddrCandidate { .. }
| SwarmEvent::NewExternalAddrOfPeer { .. } => {}
_ => {
error!("Unexpected event {event:?}");
}
Expand Down Expand Up @@ -556,6 +567,7 @@ impl NetworkManager {
session_timeout,
idle_connection_timeout,
bootstrap_peer_multiaddr,
hardcoded_external_multiaddr,
secret_key,
chain_id,
} = config;
Expand All @@ -565,6 +577,7 @@ impl NetworkManager {
// format!("/ip4/0.0.0.0/udp/{quic_port}/quic-v1"),
format!("/ip4/0.0.0.0/tcp/{tcp_port}"),
];

let swarm = build_swarm(listen_addresses, idle_connection_timeout, secret_key, |key| {
mixed_behaviour::MixedBehaviour::new(
key,
Expand All @@ -573,7 +586,12 @@ impl NetworkManager {
chain_id,
)
});
Self::generic_new(swarm)
let hardcoded_external_multiaddr = hardcoded_external_multiaddr.map(|address| {
address.with_p2p(*swarm.local_peer_id()).expect(
"hardcoded_external_multiaddr has a peer id different than the local peer id",
)
});
Self::generic_new(swarm, hardcoded_external_multiaddr)
}

pub fn get_local_peer_id(&self) -> String {
Expand Down
3 changes: 2 additions & 1 deletion crates/papyrus_network/src/network_manager/swarm_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use libp2p::gossipsub::{SubscriptionError, TopicHash};
use libp2p::swarm::dial_opts::DialOpts;
use libp2p::swarm::{DialError, NetworkBehaviour, SwarmEvent};
use libp2p::{Multiaddr, PeerId, StreamProtocol, Swarm};
use tracing::error;
use tracing::{error, info};

use crate::gossipsub_impl::Topic;
use crate::mixed_behaviour;
Expand Down Expand Up @@ -102,6 +102,7 @@ impl SwarmTrait for Swarm<mixed_behaviour::MixedBehaviour> {
}

fn add_external_address(&mut self, address: Multiaddr) {
info!("Found new external address of this node: {address:?}");
self.add_external_address(address);
}

Expand Down
8 changes: 4 additions & 4 deletions crates/papyrus_network/src/network_manager/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ async fn register_sqmr_protocol_client_and_use_channels() {
mock_swarm.first_polled_event_notifier = Some(event_notifier);

// network manager to register subscriber
let mut network_manager = GenericNetworkManager::generic_new(mock_swarm);
let mut network_manager = GenericNetworkManager::generic_new(mock_swarm, None);

// register subscriber and send payload
let mut payload_sender = network_manager.register_sqmr_protocol_client::<Vec<u8>, Vec<u8>>(
Expand Down Expand Up @@ -279,7 +279,7 @@ async fn process_incoming_query() {
let get_responses_fut = mock_swarm.get_responses_sent_to_inbound_session(inbound_session_id);
let mut get_supported_inbound_protocol_fut = mock_swarm.get_supported_inbound_protocol();

let mut network_manager = GenericNetworkManager::generic_new(mock_swarm);
let mut network_manager = GenericNetworkManager::generic_new(mock_swarm, None);

let mut inbound_payload_receiver = network_manager
.register_sqmr_protocol_server::<Vec<u8>, Vec<u8>>(protocol.to_string(), BUFFER_SIZE);
Expand Down Expand Up @@ -315,7 +315,7 @@ async fn broadcast_message() {
let mut mock_swarm = MockSwarm::default();
let mut messages_we_broadcasted_stream = mock_swarm.stream_messages_we_broadcasted();

let mut network_manager = GenericNetworkManager::generic_new(mock_swarm);
let mut network_manager = GenericNetworkManager::generic_new(mock_swarm, None);

let mut messages_to_broadcast_sender = network_manager
.register_broadcast_topic(topic.clone(), BUFFER_SIZE)
Expand Down Expand Up @@ -351,7 +351,7 @@ async fn receive_broadcasted_message_and_report_it() {
)));
let mut reported_peer_receiver = mock_swarm.get_reported_peers_stream();

let mut network_manager = GenericNetworkManager::generic_new(mock_swarm);
let mut network_manager = GenericNetworkManager::generic_new(mock_swarm, None);

let mut broadcasted_messages_receiver = network_manager
.register_broadcast_topic::<Bytes>(topic.clone(), BUFFER_SIZE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ expression: dumped_default_config
"value": "SN_MAIN",
"privacy": "Public"
},
"network.hardcoded_external_multiaddr": {
"description": "The external address other peers see this node. If this is set, the node will not try to find out which addresses it has and will write this address as external instead",
"value": "",
"privacy": "Public"
},
"network.hardcoded_external_multiaddr.#is_none": {
"description": "Flag for an optional field.",
"value": true,
"privacy": "TemporaryValue"
},
"network.idle_connection_timeout": {
"description": "Amount of time in seconds that a connection with no active sessions will stay alive.",
"value": {
Expand Down

0 comments on commit 7e04ae8

Please sign in to comment.