Move protocol parent states into appropriate module

This commit is contained in:
rishflab 2021-01-08 10:52:07 +11:00
parent f64eede5d8
commit dcea54dbf1
13 changed files with 123 additions and 124 deletions

View File

@ -5,7 +5,7 @@ use crate::{
bitcoin::{EncryptedSignature, TxCancel, TxRefund}, bitcoin::{EncryptedSignature, TxCancel, TxRefund},
monero, monero,
monero::monero_private_key, monero::monero_private_key,
protocol::{alice, alice::swap::AliceState}, protocol::{alice, alice::AliceState},
SwapAmounts, SwapAmounts,
}; };

View File

@ -2,7 +2,7 @@ use ::bitcoin::hashes::core::fmt::Display;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{ use crate::{
protocol::{bob, bob::swap::BobState}, protocol::{bob, bob::BobState},
SwapAmounts, SwapAmounts,
}; };

View File

@ -26,7 +26,7 @@ use swap::{
database::{Database, Swap}, database::{Database, Swap},
monero, monero,
network::transport::build, network::transport::build,
protocol::{alice, alice::swap::AliceState, bob, bob::swap::BobState}, protocol::{alice, alice::AliceState, bob, bob::BobState},
trace::init_tracing, trace::init_tracing,
SwapAmounts, SwapAmounts,
}; };

View File

@ -3,24 +3,90 @@ use ecdsa_fun::{
adaptor::{Adaptor, EncryptedSignature}, adaptor::{Adaptor, EncryptedSignature},
nonce::Deterministic, nonce::Deterministic,
}; };
use libp2p::request_response::ResponseChannel;
use rand::{CryptoRng, RngCore}; use rand::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sha2::Sha256; use sha2::Sha256;
use std::fmt;
use tracing::info; use tracing::info;
use crate::{ use crate::{
bitcoin, bitcoin,
bitcoin::{ bitcoin::{
current_epoch, timelocks::Timelock, wait_for_cancel_timelock_to_expire, GetBlockHeight, current_epoch, timelocks::Timelock, wait_for_cancel_timelock_to_expire, GetBlockHeight,
TransactionBlockHeight, WatchForRawTransaction, TransactionBlockHeight, TxCancel, TxRefund, WatchForRawTransaction,
}, },
monero, monero,
monero::CreateWalletForOutput, monero::CreateWalletForOutput,
network::request_response::AliceToBob,
protocol::{alice, bob}, protocol::{alice, bob},
ExpiredTimelocks, ExpiredTimelocks, SwapAmounts,
}; };
#[derive(Debug)]
pub enum AliceState {
Started {
amounts: SwapAmounts,
state0: State0,
},
Negotiated {
channel: Option<ResponseChannel<AliceToBob>>,
amounts: SwapAmounts,
state3: Box<State3>,
},
BtcLocked {
channel: Option<ResponseChannel<AliceToBob>>,
amounts: SwapAmounts,
state3: Box<State3>,
},
XmrLocked {
state3: Box<State3>,
},
EncSigLearned {
encrypted_signature: EncryptedSignature,
state3: Box<State3>,
},
BtcRedeemed,
BtcCancelled {
tx_cancel: TxCancel,
state3: Box<State3>,
},
BtcRefunded {
spend_key: monero::PrivateKey,
state3: Box<State3>,
},
BtcPunishable {
tx_refund: TxRefund,
state3: Box<State3>,
},
XmrRefunded,
CancelTimelockExpired {
state3: Box<State3>,
},
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)] #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub struct State0 { pub struct State0 {
pub a: bitcoin::SecretKey, pub a: bitcoin::SecretKey,

View File

@ -6,103 +6,35 @@ use futures::{
future::{select, Either}, future::{select, Either},
pin_mut, pin_mut,
}; };
use libp2p::request_response::ResponseChannel;
use rand::{CryptoRng, RngCore}; use rand::{CryptoRng, RngCore};
use std::{fmt, sync::Arc}; use std::sync::Arc;
use tracing::info; use tracing::info;
use uuid::Uuid; use uuid::Uuid;
use crate::{ use crate::{
bitcoin, bitcoin,
bitcoin::{ bitcoin::{TransactionBlockHeight, WatchForRawTransaction},
EncryptedSignature, TransactionBlockHeight, TxCancel, TxRefund, WatchForRawTransaction,
},
config::Config, config::Config,
database::{Database, Swap}, database::{Database, Swap},
monero, monero,
monero::CreateWalletForOutput, monero::CreateWalletForOutput,
network::request_response::AliceToBob,
protocol::alice::{ protocol::alice::{
event_loop::EventLoopHandle, event_loop::EventLoopHandle,
state::{State0, State3},
steps::{ steps::{
build_bitcoin_punish_transaction, build_bitcoin_redeem_transaction, build_bitcoin_punish_transaction, build_bitcoin_redeem_transaction,
extract_monero_private_key, lock_xmr, negotiate, publish_bitcoin_punish_transaction, extract_monero_private_key, lock_xmr, negotiate, publish_bitcoin_punish_transaction,
publish_bitcoin_redeem_transaction, publish_cancel_transaction, publish_bitcoin_redeem_transaction, publish_cancel_transaction,
wait_for_bitcoin_encrypted_signature, wait_for_bitcoin_refund, wait_for_locked_bitcoin, wait_for_bitcoin_encrypted_signature, wait_for_bitcoin_refund, wait_for_locked_bitcoin,
}, },
AliceState,
}, },
ExpiredTimelocks, SwapAmounts, ExpiredTimelocks,
}; };
trait Rng: RngCore + CryptoRng + Send {} trait Rng: RngCore + CryptoRng + Send {}
impl<T> Rng for T where T: RngCore + CryptoRng + Send {} impl<T> Rng for T where T: RngCore + CryptoRng + Send {}
#[derive(Debug)]
pub enum AliceState {
Started {
amounts: SwapAmounts,
state0: State0,
},
Negotiated {
channel: Option<ResponseChannel<AliceToBob>>,
amounts: SwapAmounts,
state3: Box<State3>,
},
BtcLocked {
channel: Option<ResponseChannel<AliceToBob>>,
amounts: SwapAmounts,
state3: Box<State3>,
},
XmrLocked {
state3: Box<State3>,
},
EncSigLearned {
encrypted_signature: EncryptedSignature,
state3: Box<State3>,
},
BtcRedeemed,
BtcCancelled {
tx_cancel: TxCancel,
state3: Box<State3>,
},
BtcRefunded {
spend_key: monero::PrivateKey,
state3: Box<State3>,
},
BtcPunishable {
tx_refund: TxRefund,
state3: Box<State3>,
},
XmrRefunded,
CancelTimelockExpired {
state3: Box<State3>,
},
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( pub async fn swap(
state: AliceState, state: AliceState,
event_loop_handle: EventLoopHandle, event_loop_handle: EventLoopHandle,

View File

@ -7,6 +7,7 @@ use ecdsa_fun::{
use rand::{CryptoRng, RngCore}; use rand::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sha2::Sha256; use sha2::Sha256;
use std::fmt;
use crate::{ use crate::{
bitcoin::{ bitcoin::{
@ -17,9 +18,47 @@ use crate::{
monero, monero,
monero::monero_private_key, monero::monero_private_key,
protocol::{alice, bob}, 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)] #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub struct State0 { pub struct State0 {
b: bitcoin::SecretKey, b: bitcoin::SecretKey,

View File

@ -1,7 +1,7 @@
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use async_recursion::async_recursion; use async_recursion::async_recursion;
use rand::{CryptoRng, RngCore}; use rand::{CryptoRng, RngCore};
use std::{fmt, sync::Arc}; use std::sync::Arc;
use tokio::select; use tokio::select;
use tracing::info; use tracing::info;
use uuid::Uuid; use uuid::Uuid;
@ -13,44 +13,6 @@ use crate::{
ExpiredTimelocks, SwapAmounts, 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 // TODO(Franck): Make this a method on a struct
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub async fn swap<R>( pub async fn swap<R>(

View File

@ -7,7 +7,7 @@ use swap::{
config::Config, config::Config,
database::Database, database::Database,
monero, monero,
protocol::{alice, alice::swap::AliceState, bob}, protocol::{alice, alice::AliceState, bob},
}; };
use tempfile::tempdir; use tempfile::tempdir;
use testcontainers::clients::Cli; use testcontainers::clients::Cli;

View File

@ -7,7 +7,7 @@ use swap::{
config::Config, config::Config,
database::Database, database::Database,
monero, monero,
protocol::{alice, bob, bob::swap::BobState}, protocol::{alice, bob, bob::BobState},
}; };
use tempfile::tempdir; use tempfile::tempdir;
use testcontainers::clients::Cli; use testcontainers::clients::Cli;

View File

@ -7,7 +7,7 @@ use swap::{
config::Config, config::Config,
database::Database, database::Database,
monero, monero,
protocol::{alice, alice::swap::AliceState, bob, bob::swap::BobState}, protocol::{alice, alice::AliceState, bob, bob::BobState},
}; };
use tempfile::tempdir; use tempfile::tempdir;
use testcontainers::clients::Cli; use testcontainers::clients::Cli;

View File

@ -10,7 +10,7 @@ use swap::{
bitcoin, bitcoin,
config::Config, config::Config,
monero, monero,
protocol::{alice, alice::swap::AliceState, bob, bob::swap::BobState}, protocol::{alice, alice::AliceState, bob, bob::BobState},
}; };
use testcontainers::clients::Cli; use testcontainers::clients::Cli;
use testutils::init_tracing; use testutils::init_tracing;

View File

@ -8,7 +8,7 @@ use swap::{
config::Config, config::Config,
database::Database, database::Database,
monero, monero,
protocol::{alice, alice::swap::AliceState, bob, bob::swap::BobState}, protocol::{alice, alice::AliceState, bob, bob::BobState},
}; };
use tempfile::tempdir; use tempfile::tempdir;
use testcontainers::clients::Cli; use testcontainers::clients::Cli;

View File

@ -9,7 +9,7 @@ use swap::{
database::Database, database::Database,
monero, monero,
network::transport::build, network::transport::build,
protocol::{alice, alice::swap::AliceState, bob, bob::swap::BobState}, protocol::{alice, alice::AliceState, bob, bob::BobState},
SwapAmounts, SwapAmounts,
}; };
use tempfile::tempdir; use tempfile::tempdir;