Add minimum monero confirmations to config use it for Bob

This commit is contained in:
Daniel Karzel 2021-01-05 17:09:55 +11:00
parent c6bc45e7a0
commit e031681369
12 changed files with 44 additions and 3 deletions

View File

@ -8,6 +8,7 @@ use tracing::info;
use uuid::Uuid; use uuid::Uuid;
use xmr_btc::{ use xmr_btc::{
bob::{self, State2}, bob::{self, State2},
config::Config,
ExpiredTimelocks, ExpiredTimelocks,
}; };
@ -59,6 +60,7 @@ pub async fn swap<R>(
monero_wallet: Arc<crate::monero::Wallet>, monero_wallet: Arc<crate::monero::Wallet>,
rng: R, rng: R,
swap_id: Uuid, swap_id: Uuid,
config: Config,
) -> Result<BobState> ) -> Result<BobState>
where where
R: RngCore + CryptoRng + Send, R: RngCore + CryptoRng + Send,
@ -72,6 +74,7 @@ where
monero_wallet, monero_wallet,
rng, rng,
swap_id, swap_id,
config,
) )
.await .await
} }
@ -110,6 +113,7 @@ pub async fn run_until<R>(
monero_wallet: Arc<crate::monero::Wallet>, monero_wallet: Arc<crate::monero::Wallet>,
mut rng: R, mut rng: R,
swap_id: Uuid, swap_id: Uuid,
config: Config,
) -> Result<BobState> ) -> Result<BobState>
where where
R: RngCore + CryptoRng + Send, R: RngCore + CryptoRng + Send,
@ -144,6 +148,7 @@ where
monero_wallet, monero_wallet,
rng, rng,
swap_id, swap_id,
config,
) )
.await .await
} }
@ -166,6 +171,7 @@ where
monero_wallet, monero_wallet,
rng, rng,
swap_id, swap_id,
config,
) )
.await .await
} }
@ -220,6 +226,7 @@ where
monero_wallet, monero_wallet,
rng, rng,
swap_id, swap_id,
config,
) )
.await .await
} }
@ -262,6 +269,7 @@ where
monero_wallet, monero_wallet,
rng, rng,
swap_id, swap_id,
config,
) )
.await .await
} }
@ -298,6 +306,7 @@ where
monero_wallet, monero_wallet,
rng, rng,
swap_id, swap_id,
config,
) )
.await .await
} }
@ -318,6 +327,7 @@ where
monero_wallet, monero_wallet,
rng, rng,
swap_id, swap_id,
config,
) )
.await .await
} }
@ -343,6 +353,7 @@ where
monero_wallet, monero_wallet,
rng, rng,
swap_id, swap_id,
config,
) )
.await .await
} }
@ -371,6 +382,7 @@ where
monero_wallet, monero_wallet,
rng, rng,
swap_id, swap_id,
config,
) )
.await .await
} }

View File

@ -136,6 +136,7 @@ async fn main() -> Result<()> {
config.bitcoin_cancel_timelock, config.bitcoin_cancel_timelock,
config.bitcoin_punish_timelock, config.bitcoin_punish_timelock,
refund_address, refund_address,
config.monero_finality_confirmations,
); );
let amounts = SwapAmounts { let amounts = SwapAmounts {
@ -159,6 +160,7 @@ async fn main() -> Result<()> {
db, db,
alice_peer_id, alice_peer_id,
alice_addr, alice_addr,
config,
) )
.await?; .await?;
} }
@ -234,6 +236,7 @@ async fn main() -> Result<()> {
db, db,
alice_peer_id, alice_peer_id,
alice_addr, alice_addr,
config,
) )
.await?; .await?;
} }
@ -301,6 +304,7 @@ async fn alice_swap(
swap.await swap.await
} }
#[allow(clippy::too_many_arguments)]
async fn bob_swap( async fn bob_swap(
swap_id: Uuid, swap_id: Uuid,
state: BobState, state: BobState,
@ -309,6 +313,7 @@ async fn bob_swap(
db: Database, db: Database,
alice_peer_id: PeerId, alice_peer_id: PeerId,
alice_addr: Multiaddr, alice_addr: Multiaddr,
config: Config,
) -> Result<BobState> { ) -> Result<BobState> {
let bob_behaviour = bob::Behaviour::default(); let bob_behaviour = bob::Behaviour::default();
let bob_transport = build(bob_behaviour.identity())?; let bob_transport = build(bob_behaviour.identity())?;
@ -324,6 +329,7 @@ async fn bob_swap(
monero_wallet.clone(), monero_wallet.clone(),
OsRng, OsRng,
swap_id, swap_id,
config,
); );
tokio::spawn(event_loop.run()); tokio::spawn(event_loop.run());

View File

@ -98,6 +98,7 @@ async fn happy_path() {
bob_xmr_wallet.clone(), bob_xmr_wallet.clone(),
OsRng, OsRng,
Uuid::new_v4(), Uuid::new_v4(),
config,
) )
.boxed(); .boxed();

View File

@ -83,6 +83,7 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
bob_xmr_wallet.clone(), bob_xmr_wallet.clone(),
OsRng, OsRng,
Uuid::new_v4(), Uuid::new_v4(),
config,
); );
let alice_db_datadir = tempdir().unwrap(); let alice_db_datadir = tempdir().unwrap();

View File

@ -106,6 +106,7 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() {
bob_xmr_wallet.clone(), bob_xmr_wallet.clone(),
OsRng, OsRng,
bob_swap_id, bob_swap_id,
config,
) )
.await .await
.unwrap(); .unwrap();
@ -134,6 +135,7 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() {
bob_xmr_wallet, bob_xmr_wallet,
OsRng, OsRng,
bob_swap_id, bob_swap_id,
config,
) )
.await .await
.unwrap(); .unwrap();

View File

@ -95,6 +95,7 @@ async fn given_bob_restarts_after_xmr_is_locked_resume_swap() {
bob_xmr_wallet.clone(), bob_xmr_wallet.clone(),
OsRng, OsRng,
bob_swap_id, bob_swap_id,
Config::regtest(),
) )
}; };
@ -120,6 +121,7 @@ async fn given_bob_restarts_after_xmr_is_locked_resume_swap() {
bob_xmr_wallet.clone(), bob_xmr_wallet.clone(),
OsRng, OsRng,
bob_swap_id, bob_swap_id,
Config::regtest(),
); );
let bob_final_state = select! { let bob_final_state = select! {

View File

@ -84,6 +84,7 @@ async fn alice_punishes_if_bob_never_acts_after_fund() {
bob_xmr_wallet.clone(), bob_xmr_wallet.clone(),
OsRng, OsRng,
Uuid::new_v4(), Uuid::new_v4(),
config,
) )
.boxed(); .boxed();

View File

@ -81,6 +81,7 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() {
bob_xmr_wallet.clone(), bob_xmr_wallet.clone(),
OsRng, OsRng,
Uuid::new_v4(), Uuid::new_v4(),
Config::regtest(),
); );
let alice_swap_id = Uuid::new_v4(); let alice_swap_id = Uuid::new_v4();

View File

@ -176,6 +176,7 @@ pub async fn init_bob_state(
config.bitcoin_cancel_timelock, config.bitcoin_cancel_timelock,
config.bitcoin_punish_timelock, config.bitcoin_punish_timelock,
refund_address, refund_address,
config.monero_finality_confirmations,
); );
BobState::Started { state0, amounts } BobState::Started { state0, amounts }

View File

@ -359,6 +359,7 @@ pub struct State0 {
cancel_timelock: Timelock, cancel_timelock: Timelock,
punish_timelock: Timelock, punish_timelock: Timelock,
refund_address: bitcoin::Address, refund_address: bitcoin::Address,
min_monero_confirmations: u32,
} }
impl State0 { impl State0 {
@ -369,6 +370,7 @@ impl State0 {
cancel_timelock: Timelock, cancel_timelock: Timelock,
punish_timelock: Timelock, punish_timelock: Timelock,
refund_address: bitcoin::Address, refund_address: bitcoin::Address,
min_monero_confirmations: u32,
) -> Self { ) -> Self {
let b = bitcoin::SecretKey::new_random(rng); let b = bitcoin::SecretKey::new_random(rng);
@ -384,6 +386,7 @@ impl State0 {
cancel_timelock, cancel_timelock,
punish_timelock, punish_timelock,
refund_address, refund_address,
min_monero_confirmations,
} }
} }
@ -432,6 +435,7 @@ impl State0 {
redeem_address: msg.redeem_address, redeem_address: msg.redeem_address,
punish_address: msg.punish_address, punish_address: msg.punish_address,
tx_lock, tx_lock,
min_monero_confirmations: self.min_monero_confirmations,
}) })
} }
} }
@ -453,6 +457,7 @@ pub struct State1 {
redeem_address: bitcoin::Address, redeem_address: bitcoin::Address,
punish_address: bitcoin::Address, punish_address: bitcoin::Address,
tx_lock: bitcoin::TxLock, tx_lock: bitcoin::TxLock,
min_monero_confirmations: u32,
} }
impl State1 { impl State1 {
@ -491,6 +496,7 @@ impl State1 {
tx_lock: self.tx_lock, tx_lock: self.tx_lock,
tx_cancel_sig_a: msg.tx_cancel_sig, tx_cancel_sig_a: msg.tx_cancel_sig,
tx_refund_encsig: msg.tx_refund_encsig, tx_refund_encsig: msg.tx_refund_encsig,
min_monero_confirmations: self.min_monero_confirmations,
}) })
} }
} }
@ -514,6 +520,7 @@ pub struct State2 {
pub tx_lock: bitcoin::TxLock, pub tx_lock: bitcoin::TxLock,
pub tx_cancel_sig_a: Signature, pub tx_cancel_sig_a: Signature,
pub tx_refund_encsig: EncryptedSignature, pub tx_refund_encsig: EncryptedSignature,
pub min_monero_confirmations: u32,
} }
impl State2 { impl State2 {
@ -558,6 +565,7 @@ impl State2 {
tx_lock: self.tx_lock, tx_lock: self.tx_lock,
tx_cancel_sig_a: self.tx_cancel_sig_a, tx_cancel_sig_a: self.tx_cancel_sig_a,
tx_refund_encsig: self.tx_refund_encsig, tx_refund_encsig: self.tx_refund_encsig,
min_monero_confirmations: self.min_monero_confirmations,
}) })
} }
} }
@ -581,6 +589,7 @@ pub struct State3 {
pub tx_lock: bitcoin::TxLock, pub tx_lock: bitcoin::TxLock,
pub tx_cancel_sig_a: Signature, pub tx_cancel_sig_a: Signature,
pub tx_refund_encsig: EncryptedSignature, pub tx_refund_encsig: EncryptedSignature,
pub min_monero_confirmations: u32,
} }
impl State3 { impl State3 {
@ -599,7 +608,7 @@ impl State3 {
self.v.public(), self.v.public(),
msg.tx_lock_proof, msg.tx_lock_proof,
self.xmr, self.xmr,
monero::MIN_CONFIRMATIONS, self.min_monero_confirmations,
) )
.await?; .await?;

View File

@ -8,6 +8,7 @@ pub struct Config {
pub bitcoin_finality_confirmations: u32, pub bitcoin_finality_confirmations: u32,
pub bitcoin_avg_block_time: Duration, pub bitcoin_avg_block_time: Duration,
pub monero_max_finality_time: Duration, pub monero_max_finality_time: Duration,
pub monero_finality_confirmations: u32,
pub bitcoin_cancel_timelock: Timelock, pub bitcoin_cancel_timelock: Timelock,
pub bitcoin_punish_timelock: Timelock, pub bitcoin_punish_timelock: Timelock,
pub bitcoin_network: ::bitcoin::Network, pub bitcoin_network: ::bitcoin::Network,
@ -24,6 +25,7 @@ impl Config {
// blockchain is slow // blockchain is slow
monero_max_finality_time: (*mainnet::MONERO_AVG_BLOCK_TIME).mul_f64(1.5) monero_max_finality_time: (*mainnet::MONERO_AVG_BLOCK_TIME).mul_f64(1.5)
* mainnet::MONERO_FINALITY_CONFIRMATIONS, * mainnet::MONERO_FINALITY_CONFIRMATIONS,
monero_finality_confirmations: mainnet::MONERO_FINALITY_CONFIRMATIONS,
bitcoin_cancel_timelock: mainnet::BITCOIN_CANCEL_TIMELOCK, bitcoin_cancel_timelock: mainnet::BITCOIN_CANCEL_TIMELOCK,
bitcoin_punish_timelock: mainnet::BITCOIN_PUNISH_TIMELOCK, bitcoin_punish_timelock: mainnet::BITCOIN_PUNISH_TIMELOCK,
bitcoin_network: ::bitcoin::Network::Bitcoin, bitcoin_network: ::bitcoin::Network::Bitcoin,
@ -40,6 +42,7 @@ impl Config {
// blockchain is slow // blockchain is slow
monero_max_finality_time: (*testnet::MONERO_AVG_BLOCK_TIME).mul_f64(1.5) monero_max_finality_time: (*testnet::MONERO_AVG_BLOCK_TIME).mul_f64(1.5)
* testnet::MONERO_FINALITY_CONFIRMATIONS, * testnet::MONERO_FINALITY_CONFIRMATIONS,
monero_finality_confirmations: testnet::MONERO_FINALITY_CONFIRMATIONS,
bitcoin_cancel_timelock: testnet::BITCOIN_CANCEL_TIMELOCK, bitcoin_cancel_timelock: testnet::BITCOIN_CANCEL_TIMELOCK,
bitcoin_punish_timelock: testnet::BITCOIN_PUNISH_TIMELOCK, bitcoin_punish_timelock: testnet::BITCOIN_PUNISH_TIMELOCK,
bitcoin_network: ::bitcoin::Network::Testnet, bitcoin_network: ::bitcoin::Network::Testnet,
@ -56,6 +59,7 @@ impl Config {
// blockchain is slow // blockchain is slow
monero_max_finality_time: (*regtest::MONERO_AVG_BLOCK_TIME).mul_f64(1.5) monero_max_finality_time: (*regtest::MONERO_AVG_BLOCK_TIME).mul_f64(1.5)
* regtest::MONERO_FINALITY_CONFIRMATIONS, * regtest::MONERO_FINALITY_CONFIRMATIONS,
monero_finality_confirmations: regtest::MONERO_FINALITY_CONFIRMATIONS,
bitcoin_cancel_timelock: regtest::BITCOIN_CANCEL_TIMELOCK, bitcoin_cancel_timelock: regtest::BITCOIN_CANCEL_TIMELOCK,
bitcoin_punish_timelock: regtest::BITCOIN_PUNISH_TIMELOCK, bitcoin_punish_timelock: regtest::BITCOIN_PUNISH_TIMELOCK,
bitcoin_network: ::bitcoin::Network::Regtest, bitcoin_network: ::bitcoin::Network::Regtest,
@ -96,7 +100,9 @@ mod testnet {
// This does not reflect recommended values for mainnet! // This does not reflect recommended values for mainnet!
pub static MONERO_FINALITY_CONFIRMATIONS: u32 = 5; pub static MONERO_FINALITY_CONFIRMATIONS: u32 = 5;
pub static MONERO_AVG_BLOCK_TIME: Lazy<Duration> = Lazy::new(|| Duration::from_secs(2 * 60)); // The average blocktime on Monero stagenet is not as constant as on mainnet,
// hence 4 minutes it set
pub static MONERO_AVG_BLOCK_TIME: Lazy<Duration> = Lazy::new(|| Duration::from_secs(4 * 60));
// This does not reflect recommended values for mainnet! // This does not reflect recommended values for mainnet!
pub static BITCOIN_CANCEL_TIMELOCK: Timelock = Timelock::new(6); pub static BITCOIN_CANCEL_TIMELOCK: Timelock = Timelock::new(6);

View File

@ -14,7 +14,6 @@ use rust_decimal::{
}; };
use std::{fmt::Display, str::FromStr}; use std::{fmt::Display, str::FromStr};
pub const MIN_CONFIRMATIONS: u32 = 10;
pub const PICONERO_OFFSET: u64 = 1_000_000_000_000; pub const PICONERO_OFFSET: u64 = 1_000_000_000_000;
pub fn random_private_key<R: RngCore + CryptoRng>(rng: &mut R) -> PrivateKey { pub fn random_private_key<R: RngCore + CryptoRng>(rng: &mut R) -> PrivateKey {