Skip to content

Commit

Permalink
Implement Identify protocol to make network and version of ASB availa…
Browse files Browse the repository at this point in the history
…ble to peers
  • Loading branch information
binarybaron committed Apr 19, 2022
1 parent a962aa4 commit 9e96ef6
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Adjust quote based on Bitcoin balance.
If the max_buy_btc in the ASB config is higher than the available balance to trade it will return the max available balance discounting the locking fees for monero, in the case the balance is lower than the min_buy_btc config it will return 0 to the CLI. If the ASB returns a quote of 0 the CLI will not allow you continue with a trade.
- Reduce required confirmations for Bitcoin transactions from 2 to 1
- Both the ASB and CLI now support the [Identify](https://github.com/libp2p/specs/blob/master/identify/README.md) protocol. This makes its version and network (testnet/mainnet) avaliable to others

## [0.10.2] - 2021-12-25

Expand Down
29 changes: 28 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion swap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ ed25519-dalek = "1"
futures = { version = "0.3", default-features = false }
hex = "0.4"
itertools = "0.10"
libp2p = { git = "https://github.com/libp2p/rust-libp2p.git", default-features = false, features = [ "tcp-tokio", "yamux", "mplex", "dns-tokio", "noise", "request-response", "websocket", "ping", "rendezvous" ] }
libp2p = { git = "https://github.com/libp2p/rust-libp2p.git", default-features = false, features = [ "tcp-tokio", "yamux", "mplex", "dns-tokio", "noise", "request-response", "websocket", "ping", "rendezvous", "identify" ] }
monero = { version = "0.12", features = [ "serde_support" ] }
monero-rpc = { path = "../monero-rpc" }
pem = "1.0"
Expand Down
15 changes: 15 additions & 0 deletions swap/src/asb/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use libp2p::core::connection::ConnectionId;
use libp2p::core::muxing::StreamMuxerBox;
use libp2p::core::transport::Boxed;
use libp2p::dns::TokioDnsConfig;
use libp2p::identify::{Identify, IdentifyConfig, IdentifyEvent};
use libp2p::ping::{Ping, PingConfig, PingEvent};
use libp2p::request_response::{RequestId, ResponseChannel};
use libp2p::swarm::{
Expand Down Expand Up @@ -111,6 +112,7 @@ pub mod behaviour {
pub swap_setup: alice::Behaviour<LR>,
pub transfer_proof: transfer_proof::Behaviour,
pub encrypted_signature: encrypted_signature::Behaviour,
pub identify: Identify,

/// Ping behaviour that ensures that the underlying network connection
/// is still alive. If the ping fails a connection close event
Expand All @@ -128,8 +130,14 @@ pub mod behaviour {
latest_rate: LR,
resume_only: bool,
env_config: env::Config,
identify_params: (identity::Keypair, XmrBtcNamespace),
rendezvous_params: Option<(identity::Keypair, PeerId, Multiaddr, XmrBtcNamespace)>,
) -> Self {
let agentVersion = format!("asb/{} ({})", env!("CARGO_PKG_VERSION"), identify_params.1);
let protocolVersion = "/comit/xmr/btc/1.0.0".to_string();
let identifyConfig = IdentifyConfig::new(protocolVersion, identify_params.0.public())
.with_agent_version(agentVersion);

Self {
rendezvous: libp2p::swarm::toggle::Toggle::from(rendezvous_params.map(
|(identity, rendezvous_peer_id, rendezvous_address, namespace)| {
Expand All @@ -153,6 +161,7 @@ pub mod behaviour {
transfer_proof: transfer_proof::alice(),
encrypted_signature: encrypted_signature::alice(),
ping: Ping::new(PingConfig::new().with_keep_alive(true)),
identify: Identify::new(identifyConfig),
}
}
}
Expand All @@ -163,6 +172,12 @@ pub mod behaviour {
}
}

impl From<IdentifyEvent> for OutEvent {
fn from(_: IdentifyEvent) -> Self {
OutEvent::Other
}
}

impl From<libp2p::rendezvous::client::Event> for OutEvent {
fn from(event: libp2p::rendezvous::client::Event) -> Self {
OutEvent::Rendezvous(event)
Expand Down
14 changes: 4 additions & 10 deletions swap/src/bin/asb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,17 @@ async fn main() -> Result<()> {
};

let kraken_rate = KrakenRate::new(config.maker.ask_spread, kraken_price_updates);
let namespace = XmrBtcNamespace::from_is_testnet(testnet);

let mut swarm = swarm::asb(
&seed,
config.maker.min_buy_btc,
config.maker.max_buy_btc,
kraken_rate.clone(),
resume_only,
env_config,
config.network.rendezvous_point.map(|rendezvous_point| {
(
rendezvous_point,
if testnet {
XmrBtcNamespace::Testnet
} else {
XmrBtcNamespace::Mainnet
},
)
}),
namespace,
config.network.rendezvous_point,
)?;

for listen in config.network.listen.clone() {
Expand Down
16 changes: 14 additions & 2 deletions swap/src/bin/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async fn main() -> Result<()> {
monero_receive_address,
monero_daemon_address,
tor_socks5_port,
namespace,
} => {
let swap_id = Uuid::new_v4();

Expand All @@ -87,7 +88,12 @@ async fn main() -> Result<()> {
.context("Seller address must contain peer ID")?;
db.insert_address(seller_peer_id, seller.clone()).await?;

let behaviour = cli::Behaviour::new(seller_peer_id, env_config, bitcoin_wallet.clone());
let behaviour = cli::Behaviour::new(
seller_peer_id,
env_config,
bitcoin_wallet.clone(),
(seed.derive_libp2p_identity(), namespace),
);
let mut swarm =
swarm::cli(seed.derive_libp2p_identity(), tor_socks5_port, behaviour).await?;
swarm.behaviour_mut().add_address(seller_peer_id, seller);
Expand Down Expand Up @@ -243,6 +249,7 @@ async fn main() -> Result<()> {
bitcoin_target_block,
monero_daemon_address,
tor_socks5_port,
namespace,
} => {
cli::tracing::init(debug, json, data_dir.join("logs"), Some(swap_id))?;
let db = open_db(data_dir.join("sqlite")).await?;
Expand All @@ -264,7 +271,12 @@ async fn main() -> Result<()> {
let seller_peer_id = db.get_peer_id(swap_id).await?;
let seller_addresses = db.get_addresses(seller_peer_id).await?;

let behaviour = cli::Behaviour::new(seller_peer_id, env_config, bitcoin_wallet.clone());
let behaviour = cli::Behaviour::new(
seller_peer_id,
env_config,
bitcoin_wallet.clone(),
(seed.derive_libp2p_identity(), namespace),
);
let mut swarm =
swarm::cli(seed.derive_libp2p_identity(), tor_socks5_port, behaviour).await?;
let our_peer_id = swarm.local_peer_id();
Expand Down
18 changes: 17 additions & 1 deletion swap/src/cli/behaviour.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use crate::network::quote::BidQuote;
use crate::network::rendezvous::XmrBtcNamespace;
use crate::network::swap_setup::bob;
use crate::network::{encrypted_signature, quote, redial, transfer_proof};
use crate::protocol::bob::State2;
use crate::{bitcoin, env};
use anyhow::{anyhow, Error, Result};
use libp2p::core::Multiaddr;
use libp2p::identify::{Identify, IdentifyConfig, IdentifyEvent};
use libp2p::ping::{Ping, PingConfig, PingEvent};
use libp2p::request_response::{RequestId, ResponseChannel};
use libp2p::{NetworkBehaviour, PeerId};
use libp2p::{identity, NetworkBehaviour, PeerId};
use std::sync::Arc;
use std::time::Duration;

Expand Down Expand Up @@ -64,6 +66,7 @@ pub struct Behaviour {
pub transfer_proof: transfer_proof::Behaviour,
pub encrypted_signature: encrypted_signature::Behaviour,
pub redial: redial::Behaviour,
pub identify: Identify,

/// Ping behaviour that ensures that the underlying network connection is
/// still alive. If the ping fails a connection close event will be
Expand All @@ -76,14 +79,21 @@ impl Behaviour {
alice: PeerId,
env_config: env::Config,
bitcoin_wallet: Arc<bitcoin::Wallet>,
identify_params: (identity::Keypair, XmrBtcNamespace),
) -> Self {
let agentVersion = format!("cli/{} ({})", env!("CARGO_PKG_VERSION"), identify_params.1);
let protocolVersion = "/comit/xmr/btc/1.0.0".to_string();
let identifyConfig = IdentifyConfig::new(protocolVersion, identify_params.0.public())
.with_agent_version(agentVersion);

Self {
quote: quote::cli(),
swap_setup: bob::Behaviour::new(env_config, bitcoin_wallet),
transfer_proof: transfer_proof::bob(),
encrypted_signature: encrypted_signature::bob(),
redial: redial::Behaviour::new(alice, Duration::from_secs(2)),
ping: Ping::new(PingConfig::new().with_keep_alive(true)),
identify: Identify::new(identifyConfig),
}
}

Expand All @@ -100,3 +110,9 @@ impl From<PingEvent> for OutEvent {
OutEvent::Other
}
}

impl From<IdentifyEvent> for OutEvent {
fn from(_: IdentifyEvent) -> Self {
OutEvent::Other
}
}
18 changes: 9 additions & 9 deletions swap/src/cli/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ where
monero_receive_address,
monero_daemon_address,
tor_socks5_port,
namespace: XmrBtcNamespace::from_is_testnet(is_testnet),
},
}
}
Expand Down Expand Up @@ -179,6 +180,7 @@ where
bitcoin_target_block,
monero_daemon_address,
tor_socks5_port,
namespace: XmrBtcNamespace::from_is_testnet(is_testnet),
},
}
}
Expand Down Expand Up @@ -230,8 +232,8 @@ where
data_dir: data::data_dir_from(data, is_testnet)?,
cmd: Command::ListSellers {
rendezvous_point,
namespace: rendezvous_namespace_from(is_testnet),
tor_socks5_port,
namespace: XmrBtcNamespace::from_is_testnet(is_testnet),
},
},
RawCommand::ExportBitcoinWallet { bitcoin } => {
Expand Down Expand Up @@ -273,6 +275,7 @@ pub enum Command {
monero_receive_address: monero::Address,
monero_daemon_address: String,
tor_socks5_port: u16,
namespace: XmrBtcNamespace,
},
History,
Config,
Expand All @@ -292,6 +295,7 @@ pub enum Command {
bitcoin_target_block: usize,
monero_daemon_address: String,
tor_socks5_port: u16,
namespace: XmrBtcNamespace,
},
Cancel {
swap_id: Uuid,
Expand Down Expand Up @@ -562,14 +566,6 @@ mod data {
}
}

fn rendezvous_namespace_from(is_testnet: bool) -> XmrBtcNamespace {
if is_testnet {
XmrBtcNamespace::Testnet
} else {
XmrBtcNamespace::Mainnet
}
}

fn env_config_from(testnet: bool) -> env::Config {
if testnet {
env::Testnet::get_config()
Expand Down Expand Up @@ -1212,6 +1208,7 @@ mod tests {
.unwrap(),
monero_daemon_address: DEFAULT_MONERO_DAEMON_ADDRESS_STAGENET.to_string(),
tor_socks5_port: DEFAULT_SOCKS5_PORT,
namespace: XmrBtcNamespace::Testnet,
},
}
}
Expand All @@ -1231,6 +1228,7 @@ mod tests {
.unwrap(),
monero_daemon_address: DEFAULT_MONERO_DAEMON_ADDRESS.to_string(),
tor_socks5_port: DEFAULT_SOCKS5_PORT,
namespace: XmrBtcNamespace::Mainnet,
},
}
}
Expand All @@ -1248,6 +1246,7 @@ mod tests {
bitcoin_target_block: DEFAULT_BITCOIN_CONFIRMATION_TARGET_TESTNET,
monero_daemon_address: DEFAULT_MONERO_DAEMON_ADDRESS_STAGENET.to_string(),
tor_socks5_port: DEFAULT_SOCKS5_PORT,
namespace: XmrBtcNamespace::Testnet,
},
}
}
Expand All @@ -1264,6 +1263,7 @@ mod tests {
bitcoin_target_block: DEFAULT_BITCOIN_CONFIRMATION_TARGET,
monero_daemon_address: DEFAULT_MONERO_DAEMON_ADDRESS.to_string(),
tor_socks5_port: DEFAULT_SOCKS5_PORT,
namespace: XmrBtcNamespace::Mainnet,
},
}
}
Expand Down
10 changes: 10 additions & 0 deletions swap/src/network/rendezvous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,13 @@ impl From<XmrBtcNamespace> for Namespace {
}
}
}

impl XmrBtcNamespace {
pub fn from_is_testnet(testnet: bool) -> XmrBtcNamespace {
if testnet {
XmrBtcNamespace::Testnet
} else {
XmrBtcNamespace::Mainnet
}
}
}
6 changes: 4 additions & 2 deletions swap/src/network/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ pub fn asb<LR>(
latest_rate: LR,
resume_only: bool,
env_config: env::Config,
rendezvous_params: Option<(Multiaddr, XmrBtcNamespace)>,
namespace: XmrBtcNamespace,
rendezvous_point: Option<Multiaddr>,
) -> Result<Swarm<asb::Behaviour<LR>>>
where
LR: LatestRate + Send + 'static + Debug + Clone,
{
let identity = seed.derive_libp2p_identity();

let rendezvous_params = if let Some((address, namespace)) = rendezvous_params {
let rendezvous_params = if let Some(address) = rendezvous_point {
let peer_id = address
.extract_peer_id()
.context("Rendezvous node address must contain peer ID")?;
Expand All @@ -39,6 +40,7 @@ where
latest_rate,
resume_only,
env_config,
(identity.clone(), namespace),
rendezvous_params,
);

Expand Down

0 comments on commit 9e96ef6

Please sign in to comment.