From dcea54dbf1ef8dac36bd1aba0fa960dfeccb1f1d Mon Sep 17 00:00:00 2001 From: rishflab Date: Fri, 8 Jan 2021 10:52:07 +1100 Subject: [PATCH] Move protocol parent states into appropriate module --- swap/src/database/alice.rs | 2 +- swap/src/database/bob.rs | 2 +- swap/src/main.rs | 2 +- swap/src/protocol/alice/state.rs | 72 +++++++++++++++++- swap/src/protocol/alice/swap.rs | 76 +------------------ swap/src/protocol/bob/state.rs | 41 +++++++++- swap/src/protocol/bob/swap.rs | 40 +--------- swap/tests/happy_path_restart_alice.rs | 2 +- .../happy_path_restart_bob_after_comm.rs | 2 +- .../happy_path_restart_bob_before_comm.rs | 2 +- swap/tests/punish.rs | 2 +- swap/tests/refund_restart_alice.rs | 2 +- swap/tests/testutils/mod.rs | 2 +- 13 files changed, 123 insertions(+), 124 deletions(-) diff --git a/swap/src/database/alice.rs b/swap/src/database/alice.rs index 4b6cfdf5..8b26c014 100644 --- a/swap/src/database/alice.rs +++ b/swap/src/database/alice.rs @@ -5,7 +5,7 @@ use crate::{ bitcoin::{EncryptedSignature, TxCancel, TxRefund}, monero, monero::monero_private_key, - protocol::{alice, alice::swap::AliceState}, + protocol::{alice, alice::AliceState}, SwapAmounts, }; diff --git a/swap/src/database/bob.rs b/swap/src/database/bob.rs index 1d833b90..54b4249b 100644 --- a/swap/src/database/bob.rs +++ b/swap/src/database/bob.rs @@ -2,7 +2,7 @@ use ::bitcoin::hashes::core::fmt::Display; use serde::{Deserialize, Serialize}; use crate::{ - protocol::{bob, bob::swap::BobState}, + protocol::{bob, bob::BobState}, SwapAmounts, }; diff --git a/swap/src/main.rs b/swap/src/main.rs index 66db10ae..74db62e3 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -26,7 +26,7 @@ use swap::{ database::{Database, Swap}, monero, network::transport::build, - protocol::{alice, alice::swap::AliceState, bob, bob::swap::BobState}, + protocol::{alice, alice::AliceState, bob, bob::BobState}, trace::init_tracing, SwapAmounts, }; diff --git a/swap/src/protocol/alice/state.rs b/swap/src/protocol/alice/state.rs index 8f4c4cf8..292fe03d 100644 --- a/swap/src/protocol/alice/state.rs +++ b/swap/src/protocol/alice/state.rs @@ -3,24 +3,90 @@ use ecdsa_fun::{ adaptor::{Adaptor, EncryptedSignature}, nonce::Deterministic, }; +use libp2p::request_response::ResponseChannel; use rand::{CryptoRng, RngCore}; use serde::{Deserialize, Serialize}; use sha2::Sha256; - +use std::fmt; use tracing::info; use crate::{ bitcoin, bitcoin::{ current_epoch, timelocks::Timelock, wait_for_cancel_timelock_to_expire, GetBlockHeight, - TransactionBlockHeight, WatchForRawTransaction, + TransactionBlockHeight, TxCancel, TxRefund, WatchForRawTransaction, }, monero, monero::CreateWalletForOutput, + network::request_response::AliceToBob, protocol::{alice, bob}, - ExpiredTimelocks, + ExpiredTimelocks, SwapAmounts, }; +#[derive(Debug)] +pub enum AliceState { + Started { + amounts: SwapAmounts, + state0: State0, + }, + Negotiated { + channel: Option>, + amounts: SwapAmounts, + state3: Box, + }, + BtcLocked { + channel: Option>, + amounts: SwapAmounts, + state3: Box, + }, + XmrLocked { + state3: Box, + }, + EncSigLearned { + encrypted_signature: EncryptedSignature, + state3: Box, + }, + BtcRedeemed, + BtcCancelled { + tx_cancel: TxCancel, + state3: Box, + }, + BtcRefunded { + spend_key: monero::PrivateKey, + state3: Box, + }, + BtcPunishable { + tx_refund: TxRefund, + state3: Box, + }, + XmrRefunded, + CancelTimelockExpired { + state3: Box, + }, + BtcPunished, + SafelyAborted, +} + +impl fmt::Display for AliceState { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + AliceState::Started { .. } => write!(f, "started"), + AliceState::Negotiated { .. } => write!(f, "negotiated"), + AliceState::BtcLocked { .. } => write!(f, "btc is locked"), + AliceState::XmrLocked { .. } => write!(f, "xmr is locked"), + AliceState::EncSigLearned { .. } => write!(f, "encrypted signature is learned"), + AliceState::BtcRedeemed => write!(f, "btc is redeemed"), + AliceState::BtcCancelled { .. } => write!(f, "btc is cancelled"), + AliceState::BtcRefunded { .. } => write!(f, "btc is refunded"), + AliceState::BtcPunished => write!(f, "btc is punished"), + AliceState::SafelyAborted => write!(f, "safely aborted"), + AliceState::BtcPunishable { .. } => write!(f, "btc is punishable"), + AliceState::XmrRefunded => write!(f, "xmr is refunded"), + AliceState::CancelTimelockExpired { .. } => write!(f, "cancel timelock is expired"), + } + } +} + #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] pub struct State0 { pub a: bitcoin::SecretKey, diff --git a/swap/src/protocol/alice/swap.rs b/swap/src/protocol/alice/swap.rs index 4058de51..4afaa682 100644 --- a/swap/src/protocol/alice/swap.rs +++ b/swap/src/protocol/alice/swap.rs @@ -6,103 +6,35 @@ use futures::{ future::{select, Either}, pin_mut, }; -use libp2p::request_response::ResponseChannel; use rand::{CryptoRng, RngCore}; -use std::{fmt, sync::Arc}; +use std::sync::Arc; use tracing::info; use uuid::Uuid; use crate::{ bitcoin, - bitcoin::{ - EncryptedSignature, TransactionBlockHeight, TxCancel, TxRefund, WatchForRawTransaction, - }, + bitcoin::{TransactionBlockHeight, WatchForRawTransaction}, config::Config, database::{Database, Swap}, monero, monero::CreateWalletForOutput, - network::request_response::AliceToBob, protocol::alice::{ event_loop::EventLoopHandle, - state::{State0, State3}, steps::{ build_bitcoin_punish_transaction, build_bitcoin_redeem_transaction, extract_monero_private_key, lock_xmr, negotiate, publish_bitcoin_punish_transaction, publish_bitcoin_redeem_transaction, publish_cancel_transaction, wait_for_bitcoin_encrypted_signature, wait_for_bitcoin_refund, wait_for_locked_bitcoin, }, + AliceState, }, - ExpiredTimelocks, SwapAmounts, + ExpiredTimelocks, }; trait Rng: RngCore + CryptoRng + Send {} impl Rng for T where T: RngCore + CryptoRng + Send {} -#[derive(Debug)] -pub enum AliceState { - Started { - amounts: SwapAmounts, - state0: State0, - }, - Negotiated { - channel: Option>, - amounts: SwapAmounts, - state3: Box, - }, - BtcLocked { - channel: Option>, - amounts: SwapAmounts, - state3: Box, - }, - XmrLocked { - state3: Box, - }, - EncSigLearned { - encrypted_signature: EncryptedSignature, - state3: Box, - }, - BtcRedeemed, - BtcCancelled { - tx_cancel: TxCancel, - state3: Box, - }, - BtcRefunded { - spend_key: monero::PrivateKey, - state3: Box, - }, - BtcPunishable { - tx_refund: TxRefund, - state3: Box, - }, - XmrRefunded, - CancelTimelockExpired { - state3: Box, - }, - BtcPunished, - SafelyAborted, -} - -impl fmt::Display for AliceState { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - AliceState::Started { .. } => write!(f, "started"), - AliceState::Negotiated { .. } => write!(f, "negotiated"), - AliceState::BtcLocked { .. } => write!(f, "btc is locked"), - AliceState::XmrLocked { .. } => write!(f, "xmr is locked"), - AliceState::EncSigLearned { .. } => write!(f, "encrypted signature is learned"), - AliceState::BtcRedeemed => write!(f, "btc is redeemed"), - AliceState::BtcCancelled { .. } => write!(f, "btc is cancelled"), - AliceState::BtcRefunded { .. } => write!(f, "btc is refunded"), - AliceState::BtcPunished => write!(f, "btc is punished"), - AliceState::SafelyAborted => write!(f, "safely aborted"), - AliceState::BtcPunishable { .. } => write!(f, "btc is punishable"), - AliceState::XmrRefunded => write!(f, "xmr is refunded"), - AliceState::CancelTimelockExpired { .. } => write!(f, "cancel timelock is expired"), - } - } -} - pub async fn swap( state: AliceState, event_loop_handle: EventLoopHandle, diff --git a/swap/src/protocol/bob/state.rs b/swap/src/protocol/bob/state.rs index a3b72b51..0ab75c40 100644 --- a/swap/src/protocol/bob/state.rs +++ b/swap/src/protocol/bob/state.rs @@ -7,6 +7,7 @@ use ecdsa_fun::{ use rand::{CryptoRng, RngCore}; use serde::{Deserialize, Serialize}; use sha2::Sha256; +use std::fmt; use crate::{ bitcoin::{ @@ -17,9 +18,47 @@ use crate::{ monero, monero::monero_private_key, protocol::{alice, bob}, - ExpiredTimelocks, + ExpiredTimelocks, SwapAmounts, }; +#[derive(Debug, Clone)] +pub enum BobState { + Started { + state0: State0, + amounts: SwapAmounts, + }, + Negotiated(State2), + BtcLocked(State3), + XmrLocked(State4), + EncSigSent(State4), + BtcRedeemed(State5), + CancelTimelockExpired(State4), + BtcCancelled(State4), + BtcRefunded(State4), + XmrRedeemed, + BtcPunished, + SafelyAborted, +} + +impl fmt::Display for BobState { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + BobState::Started { .. } => write!(f, "started"), + BobState::Negotiated(..) => write!(f, "negotiated"), + BobState::BtcLocked(..) => write!(f, "btc is locked"), + BobState::XmrLocked(..) => write!(f, "xmr is locked"), + BobState::EncSigSent(..) => write!(f, "encrypted signature is sent"), + BobState::BtcRedeemed(..) => write!(f, "btc is redeemed"), + BobState::CancelTimelockExpired(..) => write!(f, "cancel timelock is expired"), + BobState::BtcCancelled(..) => write!(f, "btc is cancelled"), + BobState::BtcRefunded(..) => write!(f, "btc is refunded"), + BobState::XmrRedeemed => write!(f, "xmr is redeemed"), + BobState::BtcPunished => write!(f, "btc is punished"), + BobState::SafelyAborted => write!(f, "safely aborted"), + } + } +} + #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] pub struct State0 { b: bitcoin::SecretKey, diff --git a/swap/src/protocol/bob/swap.rs b/swap/src/protocol/bob/swap.rs index b3bea27e..8c0f774b 100644 --- a/swap/src/protocol/bob/swap.rs +++ b/swap/src/protocol/bob/swap.rs @@ -1,7 +1,7 @@ use anyhow::{bail, Result}; use async_recursion::async_recursion; use rand::{CryptoRng, RngCore}; -use std::{fmt, sync::Arc}; +use std::sync::Arc; use tokio::select; use tracing::info; use uuid::Uuid; @@ -13,44 +13,6 @@ use crate::{ ExpiredTimelocks, SwapAmounts, }; -#[derive(Debug, Clone)] -pub enum BobState { - Started { - state0: State0, - amounts: SwapAmounts, - }, - Negotiated(State2), - BtcLocked(State3), - XmrLocked(State4), - EncSigSent(State4), - BtcRedeemed(State5), - CancelTimelockExpired(State4), - BtcCancelled(State4), - BtcRefunded(State4), - XmrRedeemed, - BtcPunished, - SafelyAborted, -} - -impl fmt::Display for BobState { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - BobState::Started { .. } => write!(f, "started"), - BobState::Negotiated(..) => write!(f, "negotiated"), - BobState::BtcLocked(..) => write!(f, "btc is locked"), - BobState::XmrLocked(..) => write!(f, "xmr is locked"), - BobState::EncSigSent(..) => write!(f, "encrypted signature is sent"), - BobState::BtcRedeemed(..) => write!(f, "btc is redeemed"), - BobState::CancelTimelockExpired(..) => write!(f, "cancel timelock is expired"), - BobState::BtcCancelled(..) => write!(f, "btc is cancelled"), - BobState::BtcRefunded(..) => write!(f, "btc is refunded"), - BobState::XmrRedeemed => write!(f, "xmr is redeemed"), - BobState::BtcPunished => write!(f, "btc is punished"), - BobState::SafelyAborted => write!(f, "safely aborted"), - } - } -} - // TODO(Franck): Make this a method on a struct #[allow(clippy::too_many_arguments)] pub async fn swap( diff --git a/swap/tests/happy_path_restart_alice.rs b/swap/tests/happy_path_restart_alice.rs index 5ac972ca..aaf6b4bf 100644 --- a/swap/tests/happy_path_restart_alice.rs +++ b/swap/tests/happy_path_restart_alice.rs @@ -7,7 +7,7 @@ use swap::{ config::Config, database::Database, monero, - protocol::{alice, alice::swap::AliceState, bob}, + protocol::{alice, alice::AliceState, bob}, }; use tempfile::tempdir; use testcontainers::clients::Cli; diff --git a/swap/tests/happy_path_restart_bob_after_comm.rs b/swap/tests/happy_path_restart_bob_after_comm.rs index 80f34f52..94705cba 100644 --- a/swap/tests/happy_path_restart_bob_after_comm.rs +++ b/swap/tests/happy_path_restart_bob_after_comm.rs @@ -7,7 +7,7 @@ use swap::{ config::Config, database::Database, monero, - protocol::{alice, bob, bob::swap::BobState}, + protocol::{alice, bob, bob::BobState}, }; use tempfile::tempdir; use testcontainers::clients::Cli; diff --git a/swap/tests/happy_path_restart_bob_before_comm.rs b/swap/tests/happy_path_restart_bob_before_comm.rs index 2694d053..8f8e2510 100644 --- a/swap/tests/happy_path_restart_bob_before_comm.rs +++ b/swap/tests/happy_path_restart_bob_before_comm.rs @@ -7,7 +7,7 @@ use swap::{ config::Config, database::Database, monero, - protocol::{alice, alice::swap::AliceState, bob, bob::swap::BobState}, + protocol::{alice, alice::AliceState, bob, bob::BobState}, }; use tempfile::tempdir; use testcontainers::clients::Cli; diff --git a/swap/tests/punish.rs b/swap/tests/punish.rs index 0be3e032..badd8bc4 100644 --- a/swap/tests/punish.rs +++ b/swap/tests/punish.rs @@ -10,7 +10,7 @@ use swap::{ bitcoin, config::Config, monero, - protocol::{alice, alice::swap::AliceState, bob, bob::swap::BobState}, + protocol::{alice, alice::AliceState, bob, bob::BobState}, }; use testcontainers::clients::Cli; use testutils::init_tracing; diff --git a/swap/tests/refund_restart_alice.rs b/swap/tests/refund_restart_alice.rs index 29f9782a..dbc14862 100644 --- a/swap/tests/refund_restart_alice.rs +++ b/swap/tests/refund_restart_alice.rs @@ -8,7 +8,7 @@ use swap::{ config::Config, database::Database, monero, - protocol::{alice, alice::swap::AliceState, bob, bob::swap::BobState}, + protocol::{alice, alice::AliceState, bob, bob::BobState}, }; use tempfile::tempdir; use testcontainers::clients::Cli; diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index 6c905442..7eb0693d 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -9,7 +9,7 @@ use swap::{ database::Database, monero, network::transport::build, - protocol::{alice, alice::swap::AliceState, bob, bob::swap::BobState}, + protocol::{alice, alice::AliceState, bob, bob::BobState}, SwapAmounts, }; use tempfile::tempdir;