Skip to content

Commit

Permalink
cut deprecated network communications between peers
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <work@onurozkan.dev>
  • Loading branch information
onur-ozkan committed Dec 6, 2023
1 parent 8a333a4 commit 07b952d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
11 changes: 8 additions & 3 deletions mm2src/mm2_main/src/lp_native_dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use mm2_core::mm_ctx::{MmArc, MmCtx};
use mm2_err_handle::common_errors::InternalError;
use mm2_err_handle::prelude::*;
use mm2_event_stream::behaviour::{EventBehaviour, EventInitStatus};
use mm2_libp2p::behaviours::atomicdex::DEPRECATED_NETID_LIST;
use mm2_libp2p::{spawn_gossipsub, AdexBehaviourError, NodeType, RelayAddress, RelayAddressError, SwarmRuntime,
WssCerts};
use mm2_metrics::mm_gauge;
Expand Down Expand Up @@ -68,7 +69,7 @@ cfg_wasm32! {
pub mod init_metamask;
}

const NETID_8762_SEEDNODES: [&str; 3] = [
const DEFAULT_NETID_SEEDNODES: [&str; 3] = [
"streamseed1.komodo.earth",
"streamseed2.komodo.earth",
"streamseed3.komodo.earth",
Expand Down Expand Up @@ -267,7 +268,7 @@ impl MmInitError {
#[cfg(target_arch = "wasm32")]
fn default_seednodes(netid: u16) -> Vec<RelayAddress> {
if netid == 8762 {
NETID_8762_SEEDNODES
DEFAULT_NETID_SEEDNODES
.iter()
.map(|seed| RelayAddress::Dns(seed.to_string()))
.collect()
Expand All @@ -280,7 +281,7 @@ fn default_seednodes(netid: u16) -> Vec<RelayAddress> {
fn default_seednodes(netid: u16) -> Vec<RelayAddress> {
use crate::mm2::lp_network::addr_to_ipv4_string;
if netid == 8762 {
NETID_8762_SEEDNODES
DEFAULT_NETID_SEEDNODES
.iter()
.filter_map(|seed| addr_to_ipv4_string(seed).ok())
.map(RelayAddress::IPv4)
Expand Down Expand Up @@ -523,6 +524,10 @@ pub async fn init_p2p(ctx: MmArc) -> P2PResult<()> {
let i_am_seed = ctx.conf["i_am_seed"].as_bool().unwrap_or(false);
let netid = ctx.netid();

if !i_am_seed && DEPRECATED_NETID_LIST.contains(&netid) {
return MmError::err(P2PInitError::InvalidNetId(NetIdError::Deprecated { netid }));
}

let seednodes = seednodes(&ctx)?;

let ctx_on_poll = ctx.clone();
Expand Down
2 changes: 2 additions & 0 deletions mm2src/mm2_main/src/lp_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ pub fn addr_to_ipv4_string(address: &str) -> Result<String, MmError<ParseAddress
pub enum NetIdError {
#[display(fmt = "Netid {} is larger than max {}", netid, max_netid)]
LargerThanMax { netid: u16, max_netid: u16 },
#[display(fmt = "{} netid is deprecated.", netid)]
Deprecated { netid: u16 },
}

pub fn lp_ports(netid: u16) -> Result<(u16, u16, u16), MmError<NetIdError>> {
Expand Down
31 changes: 26 additions & 5 deletions mm2src/mm2_p2p/src/behaviours/atomicdex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use super::peers_exchange::{PeerAddresses, PeersExchange, PeersExchangeRequest,
use super::ping::AdexPing;
use super::request_response::{build_request_response_behaviour, PeerRequest, PeerResponse, RequestResponseBehaviour,
RequestResponseSender};
use crate::network::{get_all_network_seednodes, NETID_8762};
use crate::network::{get_all_network_seednodes, DEFAULT_NETID};
use crate::relay_address::{RelayAddress, RelayAddressError};
use crate::swarm_runtime::SwarmRuntime;
use crate::{NetworkInfo, NetworkPorts, RequestResponseBehaviourEvent};
Expand All @@ -51,6 +51,10 @@ const ANNOUNCE_INTERVAL: Duration = Duration::from_secs(600);
const ANNOUNCE_INITIAL_DELAY: Duration = Duration::from_secs(60);
const CHANNEL_BUF_SIZE: usize = 1024 * 8;

pub const DEPRECATED_NETID_LIST: &[u16] = &[
7777, // TODO: keep this as inaccessible for non-seed nodes until Q2 of 2024.
];

/// The structure is the same as `PeerResponse`,
/// but is used to prevent `PeerResponse` from being used outside the network implementation.
#[derive(Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -321,14 +325,31 @@ impl AtomicDexBehaviour {
fn spawn(&self, fut: impl Future<Output = ()> + Send + 'static) { self.runtime.spawn(fut) }

fn process_cmd(&mut self, cmd: AdexBehaviourCmd) -> Result<(), AdexBehaviourError> {
let deprecated_netid_topic =
|netid| format!("{netid} netid is deprecated, the netid for main network is {DEFAULT_NETID}");

match cmd {
AdexBehaviourCmd::Subscribe { topic } => {
self.core.gossipsub.subscribe(&IdentTopic::new(topic))?;
},
AdexBehaviourCmd::PublishMsg { topic, msg } => {
AdexBehaviourCmd::PublishMsg { mut topic, mut msg } => {
if DEPRECATED_NETID_LIST.contains(&self.netid) {
topic = deprecated_netid_topic(self.netid);
msg = Default::default();
};

self.core.gossipsub.publish(TopicHash::from_raw(topic), msg)?;
},
AdexBehaviourCmd::PublishMsgFrom { topic, msg, from } => {
AdexBehaviourCmd::PublishMsgFrom {
mut topic,
mut msg,
from,
} => {
if DEPRECATED_NETID_LIST.contains(&self.netid) {
topic = deprecated_netid_topic(self.netid);
msg = Default::default();
};

self.core
.gossipsub
.publish_from(TopicHash::from_raw(topic), msg, from)?;
Expand Down Expand Up @@ -641,7 +662,7 @@ fn start_gossipsub(
let mut gossipsub = Gossipsub::new(MessageAuthenticity::Author(local_peer_id), gossipsub_config)
.map_err(|e| AdexBehaviourError::InitializationError(e.to_owned()))?;

let floodsub = Floodsub::new(local_peer_id, netid != NETID_8762);
let floodsub = Floodsub::new(local_peer_id, netid != DEFAULT_NETID);

let mut peers_exchange = PeersExchange::new(network_info);
if !network_info.in_memory() {
Expand Down Expand Up @@ -735,7 +756,7 @@ fn start_gossipsub(
debug!("Swarm event {:?}", event);

if let SwarmEvent::Behaviour(event) = event {
if swarm.behaviour_mut().netid != NETID_8762 {
if swarm.behaviour_mut().netid != DEFAULT_NETID {
if let AdexBehaviourEvent::Floodsub(FloodsubEvent::Message(message)) = &event {
for topic in &message.topics {
if topic == &FloodsubTopic::new(PEERS_TOPIC) {
Expand Down
8 changes: 4 additions & 4 deletions mm2src/mm2_p2p/src/network.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::relay_address::RelayAddress;
use libp2p::PeerId;

pub const NETID_8762: u16 = 8762;
pub const DEFAULT_NETID: u16 = 8762;

#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
const ALL_NETID_8762_SEEDNODES: &[(&str, &str)] = &[
const ALL_DEFAULT_NETID_SEEDNODES: &[(&str, &str)] = &[
(
"12D3KooWHKkHiNhZtKceQehHhPqwU5W1jXpoVBgS1qst899GjvTm",
"168.119.236.251",
Expand Down Expand Up @@ -52,10 +52,10 @@ pub fn get_all_network_seednodes(_netid: u16) -> Vec<(PeerId, RelayAddress)> { V
pub fn get_all_network_seednodes(netid: u16) -> Vec<(PeerId, RelayAddress)> {
use std::str::FromStr;

if netid != NETID_8762 {
if netid != DEFAULT_NETID {
return Vec::new();
}
ALL_NETID_8762_SEEDNODES
ALL_DEFAULT_NETID_SEEDNODES
.iter()
.map(|(peer_id, ipv4)| {
let peer_id = PeerId::from_str(peer_id).expect("valid peer id");
Expand Down

0 comments on commit 07b952d

Please sign in to comment.