Skip to content

Commit

Permalink
Use hardcoded seednodes for 7777 netid if not set in config.
Browse files Browse the repository at this point in the history
  • Loading branch information
artemii235 committed Nov 24, 2020
1 parent dc46f4c commit 907308a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 31 deletions.
49 changes: 38 additions & 11 deletions mm2src/lp_native_dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use serde_json::{self as json};
use std::ffi::CString;
use std::fs;
use std::io::{Read, Write};
use std::net::{IpAddr, Ipv4Addr};
use std::net::{IpAddr, Ipv4Addr, ToSocketAddrs};
use std::os::raw::c_char;
use std::path::Path;
use std::str;
Expand Down Expand Up @@ -311,6 +311,29 @@ fn test_ip(_ctx: &MmArc, _ip: IpAddr) -> Result<(Sender<()>, u16), String> {
Ok((shutdown_tx, 80))
}

fn seed_to_ipv4_string(seed: &str) -> Option<String> {
match seed.to_socket_addrs() {
Ok(mut iter) => match iter.next() {
Some(addr) => {
if addr.is_ipv4() {
Some(addr.ip().to_string())
} else {
log!("Seed " (seed) " resolved to IPv6 " (addr) " which is not supported");
None
}
},
None => {
log!("Seed " (seed) " to_socket_addrs empty iter");
None
},
},
Err(e) => {
log!("Error " (e) " resolving " (seed));
None
},
}
}

/// * `ctx_cb` - callback used to share the `MmCtx` ID with the call site.
pub async fn lp_init(mypubport: u16, ctx: MmArc) -> Result<(), String> {
log! ({"lp_init] version: {} DT {}", MM_VERSION, MM_DATETIME});
Expand Down Expand Up @@ -438,7 +461,21 @@ pub async fn lp_init(mypubport: u16, ctx: MmArc) -> Result<(), String> {

#[cfg(not(feature = "native"))]
try_s!(ctx.send_to_helpers().await);
const NETID_7777_SEEDNODES: &[&str] = &["seed1.kmd.io:0", "seed2.kmd.io:0", "seed3.kmd.io:0"];
let seednodes: Option<Vec<String>> = try_s!(json::from_value(ctx.conf["seednodes"].clone()));
let seednodes = match seednodes {
Some(s) => s,
None => {
if ctx.netid() == 7777 {
NETID_7777_SEEDNODES
.iter()
.filter_map(|seed| seed_to_ipv4_string(*seed))
.collect()
} else {
vec![]
}
},
};

let key_pair = ctx.secp256k1_key_pair.as_option().unwrap();

Expand All @@ -463,17 +500,7 @@ pub async fn lp_init(mypubport: u16, ctx: MmArc) -> Result<(), String> {
let p2p_context = P2PContext::new(cmd_tx);
p2p_context.store_to_mm_arc(&ctx);
spawn(p2p_event_process_loop(ctx.clone(), event_rx, i_am_seed));
/*
if i_am_seed {
log!("Before relay node");
let (tx, peer_id) = relay_node(ctx.clone(), myipaddr, mypubport, seednodes.clone());
log!("After relay node");
try_s!(ctx.gossip_sub_cmd_queue.pin(tx));
try_s!(ctx.peer_id.pin(peer_id));
}
*/

// try_s!(lp_initpeers(&ctx, netid, seednodes).await);
let balance_update_ordermatch_handler = BalanceUpdateOrdermatchHandler::new(ctx.clone());
register_balance_update_handler(ctx.clone(), Box::new(balance_update_ordermatch_handler)).await;

Expand Down
9 changes: 4 additions & 5 deletions mm2src/mm2_libp2p/src/atomicdex_behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ pub fn start_gossipsub(
ip: IpAddr,
port: u16,
spawn_fn: fn(Box<dyn Future<Output = ()> + Send + Unpin + 'static>) -> (),
to_dial: Option<Vec<String>>,
to_dial: Vec<String>,
my_privkey: &mut [u8],
i_am_relay: bool,
on_poll: impl Fn(&AtomicDexSwarm) + Send + 'static,
Expand Down Expand Up @@ -560,11 +560,12 @@ pub fn start_gossipsub(
let (event_tx, event_rx) = channel(CHANNEL_BUF_SIZE);

let bootstrap: Vec<Multiaddr> = to_dial
.unwrap_or_default()
.into_iter()
.map(|addr| parse_relay_address(addr, port))
.collect();

let (mesh_n_low, mesh_n, mesh_n_high) = if i_am_relay { (3, 6, 8) } else { (2, 3, 4) };

// Create a Swarm to manage peers and events
let mut swarm = {
// to set default parameters for gossipsub use:
Expand All @@ -578,8 +579,6 @@ pub fn start_gossipsub(
MessageId(s.finish().to_string())
};

let (mesh_n_low, mesh_n, mesh_n_high) = if i_am_relay { (3, 6, 8) } else { (2, 3, 4) };

// set custom gossipsub
let gossipsub_config = GossipsubConfigBuilder::new()
.message_id_fn(message_id_fn)
Expand Down Expand Up @@ -618,7 +617,7 @@ pub fn start_gossipsub(
swarm.floodsub.subscribe(FloodsubTopic::new(PEERS_TOPIC.to_owned()));
let addr = format!("/ip4/{}/tcp/{}", ip, port);
libp2p::Swarm::listen_on(&mut swarm, addr.parse().unwrap()).unwrap();
for relay in &bootstrap {
for relay in bootstrap.choose_multiple(&mut thread_rng(), mesh_n) {
match libp2p::Swarm::dial_addr(&mut swarm, relay.clone()) {
Ok(_) => println!("Dialed {}", relay),
Err(e) => println!("Dial {:?} failed: {:?}", relay, e),
Expand Down
30 changes: 15 additions & 15 deletions mm2src/mm2_libp2p/src/atomicdex_behaviour/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct Node {
}

impl Node {
fn spawn<F>(ip: String, port: u16, seednodes: Option<Vec<String>>, on_event: F) -> Node
fn spawn<F>(ip: String, port: u16, seednodes: Vec<String>, on_event: F) -> Node
where
F: Fn(mpsc::Sender<AdexBehaviourCmd>, AdexBehaviourEvent) + Send + 'static,
{
Expand Down Expand Up @@ -82,7 +82,7 @@ fn test_request_response_ok() {

let request_received = Arc::new(AtomicBool::new(false));
let request_received_cpy = request_received.clone();
let node1 = Node::spawn("127.0.0.1".into(), 57783, None, move |mut cmd_tx, event| {
let node1 = Node::spawn("127.0.0.1".into(), 57783, vec![], move |mut cmd_tx, event| {
let (request, response_channel) = match event {
AdexBehaviourEvent::PeerRequest {
request,
Expand All @@ -106,7 +106,7 @@ fn test_request_response_ok() {
let mut node2 = Node::spawn(
"127.0.0.1".into(),
57784,
Some(vec!["/ip4/127.0.0.1/tcp/57783".into()]),
vec!["/ip4/127.0.0.1/tcp/57783".into()],
|_, _| (),
);

Expand Down Expand Up @@ -190,31 +190,31 @@ fn test_request_response_ok_three_peers() {
let request_handler = Arc::new(Mutex::new(RequestHandler::default()));

let handler = request_handler.clone();
let _receiver1 = Node::spawn("127.0.0.1".into(), 57790, None, move |cmd_tx, event| {
let _receiver1 = Node::spawn("127.0.0.1".into(), 57790, vec![], move |cmd_tx, event| {
let mut handler = handler.lock().unwrap();
handler.handle(cmd_tx, event)
});

let handler = request_handler.clone();
let _receiver2 = Node::spawn("127.0.0.1".into(), 57791, None, move |cmd_tx, event| {
let _receiver2 = Node::spawn("127.0.0.1".into(), 57791, vec![], move |cmd_tx, event| {
let mut handler = handler.lock().unwrap();
handler.handle(cmd_tx, event)
});

let handler = request_handler.clone();
let _receiver3 = Node::spawn("127.0.0.1".into(), 57792, None, move |cmd_tx, event| {
let _receiver3 = Node::spawn("127.0.0.1".into(), 57792, vec![], move |cmd_tx, event| {
let mut handler = handler.lock().unwrap();
handler.handle(cmd_tx, event)
});

let mut sender = Node::spawn(
"127.0.0.1".into(),
57784,
Some(vec![
vec![
"/ip4/127.0.0.1/tcp/57790".into(),
"/ip4/127.0.0.1/tcp/57791".into(),
"/ip4/127.0.0.1/tcp/57792".into(),
]),
],
|_, _| (),
);

Expand All @@ -240,7 +240,7 @@ fn test_request_response_none() {

let request_received = Arc::new(AtomicBool::new(false));
let request_received_cpy = request_received.clone();
let _node1 = Node::spawn("127.0.0.1".into(), 57786, None, move |mut cmd_tx, event| {
let _node1 = Node::spawn("127.0.0.1".into(), 57786, vec![], move |mut cmd_tx, event| {
let (request, response_channel) = match event {
AdexBehaviourEvent::PeerRequest {
request,
Expand All @@ -262,7 +262,7 @@ fn test_request_response_none() {
let mut node2 = Node::spawn(
"127.0.0.1".into(),
57785,
Some(vec!["/ip4/127.0.0.1/tcp/57786".into()]),
vec!["/ip4/127.0.0.1/tcp/57786".into()],
|_, _| (),
);

Expand All @@ -287,7 +287,7 @@ fn test_request_response_none() {
fn test_request_peers_ok_three_peers() {
let _ = env_logger::try_init();

let receiver1 = Node::spawn("127.0.0.1".into(), 57800, None, move |mut cmd_tx, event| {
let receiver1 = Node::spawn("127.0.0.1".into(), 57800, vec![], move |mut cmd_tx, event| {
let (request, response_channel) = match event {
AdexBehaviourEvent::PeerRequest {
request,
Expand All @@ -305,7 +305,7 @@ fn test_request_peers_ok_three_peers() {
.unwrap();
});

let receiver2 = Node::spawn("127.0.0.1".into(), 57801, None, move |mut cmd_tx, event| {
let receiver2 = Node::spawn("127.0.0.1".into(), 57801, vec![], move |mut cmd_tx, event| {
let (request, response_channel) = match event {
AdexBehaviourEvent::PeerRequest {
request,
Expand All @@ -325,7 +325,7 @@ fn test_request_peers_ok_three_peers() {
.unwrap();
});

let receiver3 = Node::spawn("127.0.0.1".into(), 57802, None, move |mut cmd_tx, event| {
let receiver3 = Node::spawn("127.0.0.1".into(), 57802, vec![], move |mut cmd_tx, event| {
let (request, response_channel) = match event {
AdexBehaviourEvent::PeerRequest {
request,
Expand All @@ -347,11 +347,11 @@ fn test_request_peers_ok_three_peers() {
let mut sender = Node::spawn(
"127.0.0.1".into(),
57784,
Some(vec![
vec![
"/ip4/127.0.0.1/tcp/57800".into(),
"/ip4/127.0.0.1/tcp/57801".into(),
"/ip4/127.0.0.1/tcp/57802".into(),
]),
],
|_, _| (),
);

Expand Down

0 comments on commit 907308a

Please sign in to comment.