Move definitions out of lib.rs

This commit is contained in:
Franck Royer 2021-01-21 13:43:25 +11:00
parent b88a777bae
commit f2a25ee49b
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
16 changed files with 85 additions and 81 deletions

View File

@ -10,7 +10,7 @@ pub use ::bitcoin::{util::amount::Amount, Address, Network, Transaction, Txid};
pub use ecdsa_fun::{adaptor::EncryptedSignature, fun::Scalar, Signature};
pub use wallet::Wallet;
use crate::{bitcoin::timelocks::BlockHeight, config::Config, ExpiredTimelocks};
use crate::{bitcoin::timelocks::BlockHeight, config::Config};
use ::bitcoin::{
hashes::{hex::ToHex, Hash},
secp256k1,
@ -25,6 +25,7 @@ use rand::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize};
use sha2::Sha256;
use std::str::FromStr;
use timelocks::ExpiredTimelocks;
// TODO: Configurable tx-fee (note: parties have to agree prior to swapping)
// Current reasoning:

View File

@ -1,6 +1,7 @@
use serde::{Deserialize, Serialize};
use std::ops::Add;
use serde::{Deserialize, Serialize};
/// Represent a timelock, expressed in relative block height as defined in
/// [BIP68](https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki).
/// E.g. The timelock expires 10 blocks after the reference transaction is
@ -47,3 +48,10 @@ impl Add<Timelock> for BlockHeight {
BlockHeight(self.0 + rhs.0)
}
}
#[derive(Debug, Clone, Copy)]
pub enum ExpiredTimelocks {
None,
Cancel,
Punish,
}

View File

@ -2,8 +2,7 @@ use crate::{
bitcoin::{EncryptedSignature, TxCancel, TxRefund},
monero,
monero::monero_private_key,
protocol::{alice, alice::AliceState},
SwapAmounts,
protocol::{alice, alice::AliceState, SwapAmounts},
};
use ::bitcoin::hashes::core::fmt::Display;
use serde::{Deserialize, Serialize};

View File

@ -1,7 +1,6 @@
use crate::{
monero::TransferProof,
protocol::{bob, bob::BobState},
SwapAmounts,
protocol::{bob, bob::BobState, SwapAmounts},
};
use ::bitcoin::hashes::core::fmt::Display;
use monero_harness::rpc::wallet::BlockHeight;

View File

@ -26,37 +26,3 @@ pub mod seed;
pub mod trace;
mod fs;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};
/// XMR/BTC swap amounts.
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)]
// TODO(Franck): review necessity of this struct
pub struct SwapAmounts {
/// Amount of BTC to swap.
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
pub btc: bitcoin::Amount,
/// Amount of XMR to swap.
#[serde(with = "monero::monero_amount")]
pub xmr: monero::Amount,
}
// TODO: Display in XMR and BTC (not picos and sats).
impl Display for SwapAmounts {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{} sats for {} piconeros",
self.btc.as_sat(),
self.xmr.as_piconero()
)
}
}
#[derive(Debug, Clone, Copy)]
pub enum ExpiredTimelocks {
None,
Cancel,
Punish,
}

View File

@ -13,22 +13,24 @@
#![forbid(unsafe_code)]
#![allow(non_snake_case)]
use crate::cli::{Command, Options, Resume};
use std::sync::Arc;
use anyhow::{Context, Result};
use prettytable::{row, Table};
use std::sync::Arc;
use structopt::StructOpt;
use tracing::{info, log::LevelFilter};
use uuid::Uuid;
use swap::{
bitcoin,
config::Config,
database::Database,
monero,
protocol::{alice, bob, bob::Builder},
protocol::{alice, bob, bob::Builder, SwapAmounts},
trace::init_tracing,
SwapAmounts,
};
use tracing::{info, log::LevelFilter};
use uuid::Uuid;
use crate::cli::{Command, Options, Resume};
mod cli;

View File

@ -1,3 +1,4 @@
use crate::protocol::{alice, bob};
use async_trait::async_trait;
use futures::prelude::*;
use libp2p::{
@ -8,8 +9,6 @@ use serde::{Deserialize, Serialize};
use std::{fmt::Debug, io, marker::PhantomData};
use tracing::debug;
use crate::protocol::{alice, bob};
/// Time to wait for a response back once we send a request.
pub const TIMEOUT: u64 = 3600; // One hour.

View File

@ -1,3 +1,7 @@
use crate::monero;
use bitcoin::hashes::core::{fmt, fmt::Display};
use serde::{Deserialize, Serialize};
pub mod alice;
pub mod bob;
@ -6,3 +10,26 @@ pub struct StartingBalances {
pub xmr: crate::monero::Amount,
pub btc: bitcoin::Amount,
}
/// XMR/BTC swap amounts.
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct SwapAmounts {
/// Amount of BTC to swap.
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
pub btc: bitcoin::Amount,
/// Amount of XMR to swap.
#[serde(with = "monero::monero_amount")]
pub xmr: crate::monero::Amount,
}
// TODO: Display in XMR and BTC (not picos and sats).
impl Display for SwapAmounts {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{} sats for {} piconeros",
self.btc.as_sat(),
self.xmr.as_piconero()
)
}
}

View File

@ -21,9 +21,8 @@ use crate::{
transport::build,
Seed as NetworkSeed,
},
protocol::bob,
protocol::{bob, SwapAmounts},
seed::Seed,
SwapAmounts,
};
use anyhow::{bail, Result};
use libp2p::{

View File

@ -1,14 +1,15 @@
use crate::{
bitcoin,
bitcoin::{
current_epoch, timelocks::Timelock, wait_for_cancel_timelock_to_expire, GetBlockHeight,
TransactionBlockHeight, TxCancel, TxRefund, WatchForRawTransaction,
current_epoch,
timelocks::{ExpiredTimelocks, Timelock},
wait_for_cancel_timelock_to_expire, GetBlockHeight, TransactionBlockHeight, TxCancel,
TxRefund, WatchForRawTransaction,
},
monero,
monero::CreateWalletForOutput,
network::request_response::AliceToBob,
protocol::{alice, bob},
ExpiredTimelocks, SwapAmounts,
protocol::{alice, bob, SwapAmounts},
};
use anyhow::{anyhow, Context, Result};
use ecdsa_fun::{

View File

@ -1,16 +1,3 @@
use anyhow::{Context, Result};
use ecdsa_fun::{adaptor::Adaptor, nonce::Deterministic};
use futures::{
future::{select, Either},
pin_mut,
};
use libp2p::request_response::ResponseChannel;
use rand::rngs::OsRng;
use sha2::Sha256;
use std::sync::Arc;
use tokio::time::timeout;
use tracing::{info, trace};
use crate::{
bitcoin,
bitcoin::{
@ -27,9 +14,21 @@ use crate::{
protocol::{
alice,
alice::{event_loop::EventLoopHandle, SwapResponse},
SwapAmounts,
},
SwapAmounts,
};
use anyhow::{Context, Result};
use ecdsa_fun::{adaptor::Adaptor, nonce::Deterministic};
use futures::{
future::{select, Either},
pin_mut,
};
use libp2p::request_response::ResponseChannel;
use rand::rngs::OsRng;
use sha2::Sha256;
use std::sync::Arc;
use tokio::time::timeout;
use tracing::{info, trace};
pub async fn negotiate(
state0: alice::State0,

View File

@ -2,7 +2,10 @@
//! Alice holds XMR and wishes receive BTC.
use crate::{
bitcoin,
bitcoin::{TransactionBlockHeight, WaitForTransactionFinality, WatchForRawTransaction},
bitcoin::{
timelocks::ExpiredTimelocks, TransactionBlockHeight, WaitForTransactionFinality,
WatchForRawTransaction,
},
config::Config,
database,
database::Database,
@ -22,7 +25,6 @@ use crate::{
AliceState,
},
},
ExpiredTimelocks,
};
use anyhow::{bail, Result};
use async_recursion::async_recursion;

View File

@ -11,9 +11,8 @@ use crate::{
peer_tracker::{self, PeerTracker},
transport::build,
},
protocol::{alice, bob},
protocol::{alice, bob, SwapAmounts},
seed::Seed,
SwapAmounts,
};
use anyhow::{bail, Result};
use libp2p::{core::Multiaddr, identity::Keypair, NetworkBehaviour, PeerId};

View File

@ -1,14 +1,15 @@
use crate::{
bitcoin::{
self, current_epoch, timelocks::Timelock, wait_for_cancel_timelock_to_expire,
BroadcastSignedTransaction, BuildTxLockPsbt, GetBlockHeight, GetNetwork, GetRawTransaction,
Transaction, TransactionBlockHeight, TxCancel, Txid, WatchForRawTransaction,
self, current_epoch,
timelocks::{ExpiredTimelocks, Timelock},
wait_for_cancel_timelock_to_expire, BroadcastSignedTransaction, BuildTxLockPsbt,
GetBlockHeight, GetNetwork, GetRawTransaction, Transaction, TransactionBlockHeight,
TxCancel, Txid, WatchForRawTransaction,
},
config::Config,
monero,
monero::{monero_private_key, TransferProof},
protocol::{alice, bob},
ExpiredTimelocks, SwapAmounts,
protocol::{alice, bob, SwapAmounts},
};
use anyhow::{anyhow, Result};
use ecdsa_fun::{

View File

@ -1,10 +1,13 @@
use crate::{
bitcoin,
bitcoin::timelocks::ExpiredTimelocks,
config::Config,
database::{Database, Swap},
monero,
protocol::bob::{self, event_loop::EventLoopHandle, state::*, SwapRequest},
ExpiredTimelocks, SwapAmounts,
protocol::{
bob::{self, event_loop::EventLoopHandle, state::*, SwapRequest},
SwapAmounts,
},
};
use anyhow::{bail, Result};
use async_recursion::async_recursion;

View File

@ -9,9 +9,8 @@ use swap::{
bitcoin,
config::Config,
monero,
protocol::{alice, alice::AliceState, bob, bob::BobState},
protocol::{alice, alice::AliceState, bob, bob::BobState, SwapAmounts},
seed::Seed,
SwapAmounts,
};
use tempfile::tempdir;
use testcontainers::{clients::Cli, Container};