Move protocol parent states into appropriate module

pull/125/head
rishflab 3 years ago
parent f64eede5d8
commit dcea54dbf1

@ -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,
};

@ -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,
};

@ -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,
};

@ -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<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)]
pub struct State0 {
pub a: bitcoin::SecretKey,

@ -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<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(
state: AliceState,
event_loop_handle: EventLoopHandle,

@ -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,

@ -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<R>(

@ -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;

@ -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;

@ -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;

@ -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;

@ -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;

@ -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;

Loading…
Cancel
Save