mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-03-03 03:59:25 -05:00
Move ASB network protocols into network module
Adds asb module to the network module.
This commit is contained in:
parent
ca6cb0a76a
commit
da7c3a9c9a
@ -1,5 +1,6 @@
|
|||||||
mod impl_from_rr_event;
|
mod impl_from_rr_event;
|
||||||
|
|
||||||
|
pub mod asb;
|
||||||
pub mod cbor_request_response;
|
pub mod cbor_request_response;
|
||||||
pub mod encrypted_signature;
|
pub mod encrypted_signature;
|
||||||
pub mod json_pull_codec;
|
pub mod json_pull_codec;
|
||||||
|
3
swap/src/network/asb.rs
Normal file
3
swap/src/network/asb.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
pub mod behaviour;
|
||||||
|
mod execution_setup;
|
||||||
|
pub mod spot_price;
|
@ -1,13 +1,15 @@
|
|||||||
use crate::network::quote::BidQuote;
|
|
||||||
use crate::network::{encrypted_signature, quote, transfer_proof};
|
|
||||||
use crate::protocol::alice::event_loop::LatestRate;
|
|
||||||
use crate::protocol::alice::{execution_setup, spot_price, State3};
|
|
||||||
use crate::{env, monero};
|
|
||||||
use anyhow::{anyhow, Error};
|
use anyhow::{anyhow, Error};
|
||||||
use libp2p::request_response::{RequestId, ResponseChannel};
|
use libp2p::request_response::{RequestId, ResponseChannel};
|
||||||
use libp2p::{NetworkBehaviour, PeerId};
|
use libp2p::{NetworkBehaviour, PeerId};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::network::asb::{execution_setup, spot_price};
|
||||||
|
use crate::network::quote::BidQuote;
|
||||||
|
use crate::network::{encrypted_signature, quote, transfer_proof};
|
||||||
|
use crate::protocol::alice::event_loop::LatestRate;
|
||||||
|
use crate::protocol::alice::State3;
|
||||||
|
use crate::{env, monero};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum OutEvent {
|
pub enum OutEvent {
|
||||||
SwapRequestDeclined {
|
SwapRequestDeclined {
|
@ -1,18 +1,20 @@
|
|||||||
use crate::network::cbor_request_response::CborCodec;
|
use std::collections::VecDeque;
|
||||||
use crate::network::spot_price;
|
use std::fmt::Debug;
|
||||||
use crate::network::spot_price::{BlockchainNetwork, SpotPriceProtocol};
|
use std::task::{Context, Poll};
|
||||||
use crate::protocol::alice;
|
|
||||||
use crate::protocol::alice::event_loop::LatestRate;
|
|
||||||
use crate::{env, monero};
|
|
||||||
use libp2p::request_response::{
|
use libp2p::request_response::{
|
||||||
ProtocolSupport, RequestResponseConfig, RequestResponseEvent, RequestResponseMessage,
|
ProtocolSupport, RequestResponseConfig, RequestResponseEvent, RequestResponseMessage,
|
||||||
ResponseChannel,
|
ResponseChannel,
|
||||||
};
|
};
|
||||||
use libp2p::swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters};
|
use libp2p::swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters};
|
||||||
use libp2p::{NetworkBehaviour, PeerId};
|
use libp2p::{NetworkBehaviour, PeerId};
|
||||||
use std::collections::VecDeque;
|
|
||||||
use std::fmt::Debug;
|
use crate::network::cbor_request_response::CborCodec;
|
||||||
use std::task::{Context, Poll};
|
use crate::network::spot_price;
|
||||||
|
use crate::network::spot_price::{BlockchainNetwork, SpotPriceProtocol};
|
||||||
|
use crate::protocol::alice;
|
||||||
|
use crate::protocol::alice::event_loop::LatestRate;
|
||||||
|
use crate::{env, monero};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum OutEvent {
|
pub enum OutEvent {
|
||||||
@ -302,16 +304,19 @@ impl Error {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
|
||||||
use crate::asb::Rate;
|
|
||||||
use crate::env::GetConfig;
|
|
||||||
use crate::monero;
|
|
||||||
use crate::network::test::{await_events_or_timeout, connect, new_swarm};
|
|
||||||
use crate::protocol::{alice, bob};
|
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use libp2p::Swarm;
|
use libp2p::Swarm;
|
||||||
use rust_decimal::Decimal;
|
use rust_decimal::Decimal;
|
||||||
|
|
||||||
|
use crate::asb::Rate;
|
||||||
|
use crate::env::GetConfig;
|
||||||
|
use crate::monero;
|
||||||
|
use crate::network::asb;
|
||||||
|
use crate::network::test::{await_events_or_timeout, connect, new_swarm};
|
||||||
|
use crate::protocol::bob;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
impl Default for AliceBehaviourValues {
|
impl Default for AliceBehaviourValues {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -349,7 +354,7 @@ mod tests {
|
|||||||
|
|
||||||
test.construct_and_send_request(btc_to_swap);
|
test.construct_and_send_request(btc_to_swap);
|
||||||
test.assert_error(
|
test.assert_error(
|
||||||
alice::spot_price::Error::BalanceTooLow {
|
asb::spot_price::Error::BalanceTooLow {
|
||||||
balance: monero::Amount::ZERO,
|
balance: monero::Amount::ZERO,
|
||||||
buy: btc_to_swap,
|
buy: btc_to_swap,
|
||||||
},
|
},
|
||||||
@ -375,7 +380,7 @@ mod tests {
|
|||||||
|
|
||||||
test.construct_and_send_request(btc_to_swap);
|
test.construct_and_send_request(btc_to_swap);
|
||||||
test.assert_error(
|
test.assert_error(
|
||||||
alice::spot_price::Error::BalanceTooLow {
|
asb::spot_price::Error::BalanceTooLow {
|
||||||
balance: monero::Amount::ZERO,
|
balance: monero::Amount::ZERO,
|
||||||
buy: btc_to_swap,
|
buy: btc_to_swap,
|
||||||
},
|
},
|
||||||
@ -398,7 +403,7 @@ mod tests {
|
|||||||
let btc_to_swap = bitcoin::Amount::from_btc(0.01).unwrap();
|
let btc_to_swap = bitcoin::Amount::from_btc(0.01).unwrap();
|
||||||
test.construct_and_send_request(btc_to_swap);
|
test.construct_and_send_request(btc_to_swap);
|
||||||
test.assert_error(
|
test.assert_error(
|
||||||
alice::spot_price::Error::BalanceTooLow {
|
asb::spot_price::Error::BalanceTooLow {
|
||||||
balance,
|
balance,
|
||||||
buy: btc_to_swap,
|
buy: btc_to_swap,
|
||||||
},
|
},
|
||||||
@ -417,7 +422,7 @@ mod tests {
|
|||||||
let btc_to_swap = bitcoin::Amount::from_btc(0.0001).unwrap();
|
let btc_to_swap = bitcoin::Amount::from_btc(0.0001).unwrap();
|
||||||
test.construct_and_send_request(btc_to_swap);
|
test.construct_and_send_request(btc_to_swap);
|
||||||
test.assert_error(
|
test.assert_error(
|
||||||
alice::spot_price::Error::AmountBelowMinimum {
|
asb::spot_price::Error::AmountBelowMinimum {
|
||||||
buy: btc_to_swap,
|
buy: btc_to_swap,
|
||||||
min: min_buy,
|
min: min_buy,
|
||||||
},
|
},
|
||||||
@ -440,7 +445,7 @@ mod tests {
|
|||||||
|
|
||||||
test.construct_and_send_request(btc_to_swap);
|
test.construct_and_send_request(btc_to_swap);
|
||||||
test.assert_error(
|
test.assert_error(
|
||||||
alice::spot_price::Error::AmountAboveMaximum {
|
asb::spot_price::Error::AmountAboveMaximum {
|
||||||
buy: btc_to_swap,
|
buy: btc_to_swap,
|
||||||
max: max_buy,
|
max: max_buy,
|
||||||
},
|
},
|
||||||
@ -460,7 +465,7 @@ mod tests {
|
|||||||
let btc_to_swap = bitcoin::Amount::from_btc(0.01).unwrap();
|
let btc_to_swap = bitcoin::Amount::from_btc(0.01).unwrap();
|
||||||
test.construct_and_send_request(btc_to_swap);
|
test.construct_and_send_request(btc_to_swap);
|
||||||
test.assert_error(
|
test.assert_error(
|
||||||
alice::spot_price::Error::ResumeOnlyMode,
|
asb::spot_price::Error::ResumeOnlyMode,
|
||||||
bob::spot_price::Error::NoSwapsAccepted,
|
bob::spot_price::Error::NoSwapsAccepted,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
@ -475,7 +480,7 @@ mod tests {
|
|||||||
let btc_to_swap = bitcoin::Amount::from_btc(0.01).unwrap();
|
let btc_to_swap = bitcoin::Amount::from_btc(0.01).unwrap();
|
||||||
test.construct_and_send_request(btc_to_swap);
|
test.construct_and_send_request(btc_to_swap);
|
||||||
test.assert_error(
|
test.assert_error(
|
||||||
alice::spot_price::Error::LatestRateFetchFailed(Box::new(TestRateError {})),
|
asb::spot_price::Error::LatestRateFetchFailed(Box::new(TestRateError {})),
|
||||||
bob::spot_price::Error::Other,
|
bob::spot_price::Error::Other,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
@ -492,7 +497,7 @@ mod tests {
|
|||||||
|
|
||||||
test.construct_and_send_request(btc_to_swap);
|
test.construct_and_send_request(btc_to_swap);
|
||||||
test.assert_error(
|
test.assert_error(
|
||||||
alice::spot_price::Error::SellQuoteCalculationFailed(anyhow!(
|
asb::spot_price::Error::SellQuoteCalculationFailed(anyhow!(
|
||||||
"Error text irrelevant, won't be checked here"
|
"Error text irrelevant, won't be checked here"
|
||||||
)),
|
)),
|
||||||
bob::spot_price::Error::Other,
|
bob::spot_price::Error::Other,
|
||||||
@ -510,7 +515,7 @@ mod tests {
|
|||||||
let btc_to_swap = bitcoin::Amount::from_btc(0.01).unwrap();
|
let btc_to_swap = bitcoin::Amount::from_btc(0.01).unwrap();
|
||||||
test.construct_and_send_request(btc_to_swap);
|
test.construct_and_send_request(btc_to_swap);
|
||||||
test.assert_error(
|
test.assert_error(
|
||||||
alice::spot_price::Error::BlockchainNetworkMismatch {
|
asb::spot_price::Error::BlockchainNetworkMismatch {
|
||||||
cli: BlockchainNetwork {
|
cli: BlockchainNetwork {
|
||||||
bitcoin: bitcoin::Network::Testnet,
|
bitcoin: bitcoin::Network::Testnet,
|
||||||
monero: monero::Network::Stagenet,
|
monero: monero::Network::Stagenet,
|
||||||
@ -549,7 +554,7 @@ mod tests {
|
|||||||
|
|
||||||
test.send_request(request);
|
test.send_request(request);
|
||||||
test.assert_error(
|
test.assert_error(
|
||||||
alice::spot_price::Error::BlockchainNetworkMismatch {
|
asb::spot_price::Error::BlockchainNetworkMismatch {
|
||||||
cli: BlockchainNetwork {
|
cli: BlockchainNetwork {
|
||||||
bitcoin: bitcoin::Network::Bitcoin,
|
bitcoin: bitcoin::Network::Bitcoin,
|
||||||
monero: monero::Network::Mainnet,
|
monero: monero::Network::Mainnet,
|
||||||
@ -574,7 +579,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct SpotPriceTest {
|
struct SpotPriceTest {
|
||||||
alice_swarm: Swarm<alice::spot_price::Behaviour<TestRate>>,
|
alice_swarm: Swarm<asb::spot_price::Behaviour<TestRate>>,
|
||||||
bob_swarm: Swarm<spot_price::Behaviour>,
|
bob_swarm: Swarm<spot_price::Behaviour>,
|
||||||
|
|
||||||
alice_peer_id: PeerId,
|
alice_peer_id: PeerId,
|
||||||
@ -628,7 +633,7 @@ mod tests {
|
|||||||
) {
|
) {
|
||||||
match await_events_or_timeout(self.alice_swarm.next(), self.bob_swarm.next()).await {
|
match await_events_or_timeout(self.alice_swarm.next(), self.bob_swarm.next()).await {
|
||||||
(
|
(
|
||||||
alice::spot_price::OutEvent::ExecutionSetupParams { btc, xmr, .. },
|
asb::spot_price::OutEvent::ExecutionSetupParams { btc, xmr, .. },
|
||||||
spot_price::OutEvent::Message { message, .. },
|
spot_price::OutEvent::Message { message, .. },
|
||||||
) => {
|
) => {
|
||||||
assert_eq!(alice_assert, (btc, xmr));
|
assert_eq!(alice_assert, (btc, xmr));
|
||||||
@ -654,22 +659,22 @@ mod tests {
|
|||||||
|
|
||||||
async fn assert_error(
|
async fn assert_error(
|
||||||
&mut self,
|
&mut self,
|
||||||
alice_assert: alice::spot_price::Error,
|
alice_assert: asb::spot_price::Error,
|
||||||
bob_assert: bob::spot_price::Error,
|
bob_assert: bob::spot_price::Error,
|
||||||
) {
|
) {
|
||||||
match await_events_or_timeout(self.alice_swarm.next(), self.bob_swarm.next()).await {
|
match await_events_or_timeout(self.alice_swarm.next(), self.bob_swarm.next()).await {
|
||||||
(
|
(
|
||||||
alice::spot_price::OutEvent::Error { error, .. },
|
asb::spot_price::OutEvent::Error { error, .. },
|
||||||
spot_price::OutEvent::Message { message, .. },
|
spot_price::OutEvent::Message { message, .. },
|
||||||
) => {
|
) => {
|
||||||
// TODO: Somehow make PartialEq work on Alice's spot_price::Error
|
// TODO: Somehow make PartialEq work on Alice's spot_price::Error
|
||||||
match (alice_assert, error) {
|
match (alice_assert, error) {
|
||||||
(
|
(
|
||||||
alice::spot_price::Error::BalanceTooLow {
|
asb::spot_price::Error::BalanceTooLow {
|
||||||
balance: balance1,
|
balance: balance1,
|
||||||
buy: buy1,
|
buy: buy1,
|
||||||
},
|
},
|
||||||
alice::spot_price::Error::BalanceTooLow {
|
asb::spot_price::Error::BalanceTooLow {
|
||||||
balance: balance2,
|
balance: balance2,
|
||||||
buy: buy2,
|
buy: buy2,
|
||||||
},
|
},
|
||||||
@ -678,11 +683,11 @@ mod tests {
|
|||||||
assert_eq!(buy1, buy2);
|
assert_eq!(buy1, buy2);
|
||||||
}
|
}
|
||||||
(
|
(
|
||||||
alice::spot_price::Error::BlockchainNetworkMismatch {
|
asb::spot_price::Error::BlockchainNetworkMismatch {
|
||||||
cli: cli1,
|
cli: cli1,
|
||||||
asb: asb1,
|
asb: asb1,
|
||||||
},
|
},
|
||||||
alice::spot_price::Error::BlockchainNetworkMismatch {
|
asb::spot_price::Error::BlockchainNetworkMismatch {
|
||||||
cli: cli2,
|
cli: cli2,
|
||||||
asb: asb2,
|
asb: asb2,
|
||||||
},
|
},
|
||||||
@ -691,24 +696,24 @@ mod tests {
|
|||||||
assert_eq!(asb1, asb2);
|
assert_eq!(asb1, asb2);
|
||||||
}
|
}
|
||||||
(
|
(
|
||||||
alice::spot_price::Error::AmountBelowMinimum { .. },
|
asb::spot_price::Error::AmountBelowMinimum { .. },
|
||||||
alice::spot_price::Error::AmountBelowMinimum { .. },
|
asb::spot_price::Error::AmountBelowMinimum { .. },
|
||||||
)
|
)
|
||||||
| (
|
| (
|
||||||
alice::spot_price::Error::AmountAboveMaximum { .. },
|
asb::spot_price::Error::AmountAboveMaximum { .. },
|
||||||
alice::spot_price::Error::AmountAboveMaximum { .. },
|
asb::spot_price::Error::AmountAboveMaximum { .. },
|
||||||
)
|
)
|
||||||
| (
|
| (
|
||||||
alice::spot_price::Error::LatestRateFetchFailed(_),
|
asb::spot_price::Error::LatestRateFetchFailed(_),
|
||||||
alice::spot_price::Error::LatestRateFetchFailed(_),
|
asb::spot_price::Error::LatestRateFetchFailed(_),
|
||||||
)
|
)
|
||||||
| (
|
| (
|
||||||
alice::spot_price::Error::SellQuoteCalculationFailed(_),
|
asb::spot_price::Error::SellQuoteCalculationFailed(_),
|
||||||
alice::spot_price::Error::SellQuoteCalculationFailed(_),
|
asb::spot_price::Error::SellQuoteCalculationFailed(_),
|
||||||
)
|
)
|
||||||
| (
|
| (
|
||||||
alice::spot_price::Error::ResumeOnlyMode,
|
asb::spot_price::Error::ResumeOnlyMode,
|
||||||
alice::spot_price::Error::ResumeOnlyMode,
|
asb::spot_price::Error::ResumeOnlyMode,
|
||||||
) => {}
|
) => {}
|
||||||
(alice_assert, error) => {
|
(alice_assert, error) => {
|
||||||
panic!("Expected: {:?} Actual: {:?}", alice_assert, error)
|
panic!("Expected: {:?} Actual: {:?}", alice_assert, error)
|
@ -1,12 +1,14 @@
|
|||||||
//! Run an XMR/BTC swap in the role of Alice.
|
//! Run an XMR/BTC swap in the role of Alice.
|
||||||
//! Alice holds XMR and wishes receive BTC.
|
//! Alice holds XMR and wishes receive BTC.
|
||||||
use crate::database::Database;
|
|
||||||
use crate::env::Config;
|
|
||||||
use crate::{bitcoin, monero};
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub use self::behaviour::{Behaviour, OutEvent};
|
use crate::database::Database;
|
||||||
|
use crate::env::Config;
|
||||||
|
pub use crate::network::asb::behaviour::{Behaviour, OutEvent};
|
||||||
|
use crate::{bitcoin, monero};
|
||||||
|
|
||||||
pub use self::event_loop::{EventLoop, EventLoopHandle};
|
pub use self::event_loop::{EventLoop, EventLoopHandle};
|
||||||
pub use self::recovery::cancel::cancel;
|
pub use self::recovery::cancel::cancel;
|
||||||
pub use self::recovery::punish::punish;
|
pub use self::recovery::punish::punish;
|
||||||
@ -17,11 +19,8 @@ pub use self::recovery::{cancel, punish, redeem, refund, safely_abort};
|
|||||||
pub use self::state::*;
|
pub use self::state::*;
|
||||||
pub use self::swap::{run, run_until};
|
pub use self::swap::{run, run_until};
|
||||||
|
|
||||||
mod behaviour;
|
|
||||||
pub mod event_loop;
|
pub mod event_loop;
|
||||||
mod execution_setup;
|
|
||||||
mod recovery;
|
mod recovery;
|
||||||
mod spot_price;
|
|
||||||
pub mod state;
|
pub mod state;
|
||||||
pub mod swap;
|
pub mod swap;
|
||||||
|
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
use crate::asb::Rate;
|
use crate::asb::Rate;
|
||||||
use crate::database::Database;
|
use crate::database::Database;
|
||||||
use crate::env::Config;
|
use crate::env::Config;
|
||||||
|
use crate::network::asb::spot_price::Error;
|
||||||
use crate::network::quote::BidQuote;
|
use crate::network::quote::BidQuote;
|
||||||
use crate::network::transfer_proof;
|
use crate::network::transfer_proof;
|
||||||
use crate::protocol::alice::spot_price::Error;
|
|
||||||
use crate::protocol::alice::{AliceState, Behaviour, OutEvent, State0, State3, Swap};
|
use crate::protocol::alice::{AliceState, Behaviour, OutEvent, State0, State3, Swap};
|
||||||
use crate::{bitcoin, kraken, monero};
|
use crate::{bitcoin, kraken, monero};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user