Browse Source

Merge #569

569: Connect to maker behind domain r=thomaseizinger a=thomaseizinger



Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
new-http-api
bors[bot] 3 years ago
committed by GitHub
parent
commit
f4336db48b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      .github/workflows/ci.yml
  2. 7
      daemon/src/connection.rs
  3. 63
      daemon/src/taker.rs
  4. 2
      start_all.sh

2
.github/workflows/ci.yml

@ -108,7 +108,7 @@ jobs:
sleep 10s # Wait for maker to start\
# The maker-id is generated from the makers seed found in daemon/util/testnet_seeds/maker_seed
target/debug/taker --data-dir=/tmp/taker --maker-id 10d4ba2ac3f7a22da4009d813ff1bc3f404dfe2cc93a32bedf1512aa9951c95e testnet &
target/debug/taker --data-dir=/tmp/taker --maker localhost:9999 --maker-id 10d4ba2ac3f7a22da4009d813ff1bc3f404dfe2cc93a32bedf1512aa9951c95e testnet &
sleep 10s # Wait for taker to start
curl --fail http://localhost:8000/api/alive

7
daemon/src/connection.rs

@ -5,6 +5,7 @@ use futures::{FutureExt, StreamExt};
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};
use tokio::net::TcpStream;
use tokio::sync::watch;
use tokio_util::codec::FramedRead;
use xtra::prelude::MessageChannel;
@ -84,11 +85,11 @@ impl Actor {
ctx: &mut xtra::Context<Self>,
) -> Result<()> {
let (read, write, noise) = {
let socket = tokio::net::TcpSocket::new_v4().expect("Be able to create a socket");
let mut connection = socket.connect(maker_addr).await?;
let mut connection = TcpStream::connect(&maker_addr).await?;
let noise =
noise::initiator_handshake(&mut connection, &self.identity_sk, &maker_identity_pk)
.await?;
let (read, write) = connection.into_split();
(read, write, Arc::new(Mutex::new(noise)))
};
@ -117,6 +118,8 @@ impl Actor {
.send(ConnectionStatus::Online)
.expect("receiver to outlive the actor");
tracing::info!(address = %maker_addr, "Established connection to maker");
Ok(())
}

63
daemon/src/taker.rs

@ -30,9 +30,9 @@ const CONNECTION_RETRY_INTERVAL: Duration = Duration::from_secs(5);
#[derive(Parser)]
struct Opts {
/// The IP address of the other party (i.e. the maker).
#[clap(long, default_value = "127.0.0.1:9999")]
maker: SocketAddr,
/// The IP address or hostname of the other party (i.e. the maker).
#[clap(long)]
maker: String,
/// The public key of the maker as a 32 byte hex string.
#[clap(long, parse(try_from_str = parse_x25519_pubkey))]
@ -253,20 +253,7 @@ async fn main() -> Result<()> {
)
.await?;
while connection_actor_addr
.send(connection::Connect {
maker_identity_pk: opts.maker_id,
maker_addr: opts.maker,
})
.await?
.is_err()
{
sleep(CONNECTION_RETRY_INTERVAL).await;
tracing::debug!(
"Couldn't connect to the maker, retrying in {}...",
CONNECTION_RETRY_INTERVAL.as_secs()
);
}
connect(connection_actor_addr, opts.maker_id, opts.maker).await?;
tokio::spawn(wallet_sync::new(wallet, wallet_feed_sender));
let take_offer_channel = MessageChannel::<taker_cfd::TakeOffer>::clone_channel(&cfd_actor_addr);
@ -316,3 +303,45 @@ async fn main() -> Result<()> {
Ok(())
}
async fn connect(
connection_actor_addr: xtra::Address<connection::Actor>,
maker_identity_pk: x25519_dalek::PublicKey,
maker_addr: String,
) -> Result<()> {
let possible_addresses = tokio::net::lookup_host(&maker_addr)
.await?
.collect::<Vec<_>>();
tracing::debug!(
"Resolved {} to [{}]",
maker_addr,
itertools::join(possible_addresses.iter(), ",")
);
loop {
for address in &possible_addresses {
tracing::trace!("Connecting to {}", address);
let connect_msg = connection::Connect {
maker_identity_pk,
maker_addr: *address,
};
if let Err(e) = connection_actor_addr.send(connect_msg).await? {
tracing::debug!(%address, "Failed to establish connection: {:#}", e);
continue;
}
return Ok(());
}
tracing::debug!(
"Tried connecting to {} addresses without success, retrying in {} seconds",
possible_addresses.len(),
CONNECTION_RETRY_INTERVAL.as_secs()
);
sleep(CONNECTION_RETRY_INTERVAL).await;
}
}

2
start_all.sh

@ -5,4 +5,4 @@ export RUST_BACKTRACE=1
# A simple command to spin up the complete package, ie. both daemons and frontends.
# A single 'ctrl+c' stops all processes.
# The maker-id is generated from the makers seed found in daemon/util/testnet_seeds/maker_seed
(trap 'kill 0' SIGINT; cargo run --bin maker -- testnet & cargo run --bin taker -- --maker-id 10d4ba2ac3f7a22da4009d813ff1bc3f404dfe2cc93a32bedf1512aa9951c95e testnet -- & yarn --cwd=./maker-frontend dev & yarn --cwd=./taker-frontend dev)
(trap 'kill 0' SIGINT; cargo run --bin maker -- testnet & cargo run --bin taker -- --maker localhost:9999 --maker-id 10d4ba2ac3f7a22da4009d813ff1bc3f404dfe2cc93a32bedf1512aa9951c95e testnet -- & yarn --cwd=./maker-frontend dev & yarn --cwd=./taker-frontend dev)

Loading…
Cancel
Save