Move Swap state out of storage

pull/36/head
Lucas Soriano del Pino 4 years ago committed by rishflab
parent ac2cfd7f65
commit 33a111d879

@ -32,7 +32,8 @@ use crate::{
transport::SwapTransport,
TokioExecutor,
},
storage::{self, Database},
state,
storage::Database,
SwapAmounts, PUNISH_TIMELOCK, REFUND_TIMELOCK,
};
use xmr_btc::{
@ -177,7 +178,7 @@ pub async fn swap(
};
let swap_id = Uuid::new_v4();
db.insert_latest_state(swap_id, storage::Alice::Handshaken(state3.clone()).into())
db.insert_latest_state(swap_id, state::Alice::Handshaken(state3.clone()).into())
.await?;
info!("Handshake complete, we now have State3 for Alice.");
@ -205,14 +206,14 @@ pub async fn swap(
public_spend_key,
public_view_key,
}) => {
db.insert_latest_state(swap_id, storage::Alice::BtcLocked(state3.clone()).into())
db.insert_latest_state(swap_id, state::Alice::BtcLocked(state3.clone()).into())
.await?;
let (transfer_proof, _) = monero_wallet
.transfer(public_spend_key, public_view_key, amount)
.await?;
db.insert_latest_state(swap_id, storage::Alice::XmrLocked(state3.clone()).into())
db.insert_latest_state(swap_id, state::Alice::XmrLocked(state3.clone()).into())
.await?;
let mut guard = network.as_ref().lock().await;
@ -223,7 +224,7 @@ pub async fn swap(
GeneratorState::Yielded(Action::RedeemBtc(tx)) => {
db.insert_latest_state(
swap_id,
storage::Alice::BtcRedeemable {
state::Alice::BtcRedeemable {
state: state3.clone(),
redeem_tx: tx.clone(),
}
@ -237,11 +238,8 @@ pub async fn swap(
let _ = bitcoin_wallet.broadcast_signed_transaction(tx).await?;
}
GeneratorState::Yielded(Action::PunishBtc(tx)) => {
db.insert_latest_state(
swap_id,
storage::Alice::BtcPunishable(state3.clone()).into(),
)
.await?;
db.insert_latest_state(swap_id, state::Alice::BtcPunishable(state3.clone()).into())
.await?;
let _ = bitcoin_wallet.broadcast_signed_transaction(tx).await?;
}
@ -251,7 +249,7 @@ pub async fn swap(
}) => {
db.insert_latest_state(
swap_id,
storage::Alice::BtcRefunded {
state::Alice::BtcRefunded {
state: state3.clone(),
spend_key,
view_key,
@ -265,7 +263,7 @@ pub async fn swap(
.await?;
}
GeneratorState::Complete(()) => {
db.insert_latest_state(swap_id, storage::Alice::SwapComplete.into())
db.insert_latest_state(swap_id, state::Alice::SwapComplete.into())
.await?;
return Ok(());

@ -30,7 +30,8 @@ use crate::{
transport::SwapTransport,
TokioExecutor,
},
storage::{self, Database},
state,
storage::Database,
Cmd, Rsp, SwapAmounts, PUNISH_TIMELOCK, REFUND_TIMELOCK,
};
use xmr_btc::{
@ -144,7 +145,7 @@ pub async fn swap(
};
let swap_id = Uuid::new_v4();
db.insert_latest_state(swap_id, storage::Bob::Handshaken(state2.clone()).into())
db.insert_latest_state(swap_id, state::Bob::Handshaken(state2.clone()).into())
.await?;
swarm.send_message2(alice.clone(), state2.next_message());
@ -172,11 +173,11 @@ pub async fn swap(
let _ = bitcoin_wallet
.broadcast_signed_transaction(signed_tx_lock)
.await?;
db.insert_latest_state(swap_id, storage::Bob::BtcLocked(state2.clone()).into())
db.insert_latest_state(swap_id, state::Bob::BtcLocked(state2.clone()).into())
.await?;
}
GeneratorState::Yielded(bob::Action::SendBtcRedeemEncsig(tx_redeem_encsig)) => {
db.insert_latest_state(swap_id, storage::Bob::XmrLocked(state2.clone()).into())
db.insert_latest_state(swap_id, state::Bob::XmrLocked(state2.clone()).into())
.await?;
let mut guard = network.as_ref().lock().await;
@ -196,7 +197,7 @@ pub async fn swap(
spend_key,
view_key,
}) => {
db.insert_latest_state(swap_id, storage::Bob::BtcRedeemed(state2.clone()).into())
db.insert_latest_state(swap_id, state::Bob::BtcRedeemed(state2.clone()).into())
.await?;
monero_wallet
@ -204,7 +205,7 @@ pub async fn swap(
.await?;
}
GeneratorState::Yielded(bob::Action::CancelBtc(tx_cancel)) => {
db.insert_latest_state(swap_id, storage::Bob::BtcRefundable(state2.clone()).into())
db.insert_latest_state(swap_id, state::Bob::BtcRefundable(state2.clone()).into())
.await?;
let _ = bitcoin_wallet
@ -212,7 +213,7 @@ pub async fn swap(
.await?;
}
GeneratorState::Yielded(bob::Action::RefundBtc(tx_refund)) => {
db.insert_latest_state(swap_id, storage::Bob::BtcRefundable(state2.clone()).into())
db.insert_latest_state(swap_id, state::Bob::BtcRefundable(state2.clone()).into())
.await?;
let _ = bitcoin_wallet
@ -220,7 +221,7 @@ pub async fn swap(
.await?;
}
GeneratorState::Complete(()) => {
db.insert_latest_state(swap_id, storage::Bob::SwapComplete.into())
db.insert_latest_state(swap_id, state::Bob::SwapComplete.into())
.await?;
return Ok(());

@ -6,6 +6,7 @@ pub mod bitcoin;
pub mod bob;
pub mod monero;
pub mod network;
pub mod state;
pub mod storage;
pub mod tor;

@ -0,0 +1,88 @@
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use xmr_btc::{alice, bob, monero, serde::monero_private_key};
#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub enum Swap {
Alice(Alice),
Bob(Bob),
}
#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub enum Alice {
Handshaken(alice::State3),
BtcLocked(alice::State3),
XmrLocked(alice::State3),
BtcRedeemable {
state: alice::State3,
redeem_tx: bitcoin::Transaction,
},
BtcPunishable(alice::State3),
BtcRefunded {
state: alice::State3,
#[serde(with = "monero_private_key")]
spend_key: monero::PrivateKey,
view_key: monero::PrivateViewKey,
},
SwapComplete,
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub enum Bob {
Handshaken(bob::State2),
BtcLocked(bob::State2),
XmrLocked(bob::State2),
BtcRedeemed(bob::State2),
BtcRefundable(bob::State2),
SwapComplete,
}
impl From<Alice> for Swap {
fn from(from: Alice) -> Self {
Swap::Alice(from)
}
}
impl From<Bob> for Swap {
fn from(from: Bob) -> Self {
Swap::Bob(from)
}
}
impl Display for Swap {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Swap::Alice(alice) => Display::fmt(alice, f),
Swap::Bob(bob) => Display::fmt(bob, f),
}
}
}
impl Display for Alice {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Alice::Handshaken(_) => f.write_str("Handshake complete"),
Alice::BtcLocked(_) => f.write_str("Bitcoin locked"),
Alice::XmrLocked(_) => f.write_str("Monero locked"),
Alice::BtcRedeemable { .. } => f.write_str("Bitcoin redeemable"),
Alice::BtcPunishable(_) => f.write_str("Bitcoin punishable"),
Alice::BtcRefunded { .. } => f.write_str("Monero refundable"),
Alice::SwapComplete => f.write_str("Swap complete"),
}
}
}
impl Display for Bob {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Bob::Handshaken(_) => f.write_str("Handshake complete"),
Bob::BtcLocked(_) | Bob::XmrLocked(_) | Bob::BtcRefundable(_) => {
f.write_str("Bitcoin refundable")
}
Bob::BtcRedeemed(_) => f.write_str("Monero redeemable"),
Bob::SwapComplete => f.write_str("Swap complete"),
}
}
}

@ -1,93 +1,8 @@
use crate::state::Swap;
use anyhow::{anyhow, bail, Context, Result};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{fmt::Display, path::Path};
use serde::{de::DeserializeOwned, Serialize};
use std::path::Path;
use uuid::Uuid;
use xmr_btc::{alice, bob, monero, serde::monero_private_key};
#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub enum Swap {
Alice(Alice),
Bob(Bob),
}
#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub enum Alice {
Handshaken(alice::State3),
BtcLocked(alice::State3),
XmrLocked(alice::State3),
BtcRedeemable {
state: alice::State3,
redeem_tx: bitcoin::Transaction,
},
BtcPunishable(alice::State3),
BtcRefunded {
state: alice::State3,
#[serde(with = "monero_private_key")]
spend_key: monero::PrivateKey,
view_key: monero::PrivateViewKey,
},
SwapComplete,
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub enum Bob {
Handshaken(bob::State2),
BtcLocked(bob::State2),
XmrLocked(bob::State2),
BtcRedeemed(bob::State2),
BtcRefundable(bob::State2),
SwapComplete,
}
impl From<Alice> for Swap {
fn from(from: Alice) -> Self {
Swap::Alice(from)
}
}
impl From<Bob> for Swap {
fn from(from: Bob) -> Self {
Swap::Bob(from)
}
}
impl Display for Swap {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Swap::Alice(alice) => Display::fmt(alice, f),
Swap::Bob(bob) => Display::fmt(bob, f),
}
}
}
impl Display for Alice {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Alice::Handshaken(_) => f.write_str("Handshake complete"),
Alice::BtcLocked(_) => f.write_str("Bitcoin locked"),
Alice::XmrLocked(_) => f.write_str("Monero locked"),
Alice::BtcRedeemable { .. } => f.write_str("Bitcoin redeemable"),
Alice::BtcPunishable(_) => f.write_str("Bitcoin punishable"),
Alice::BtcRefunded { .. } => f.write_str("Monero refundable"),
Alice::SwapComplete => f.write_str("Swap complete"),
}
}
}
impl Display for Bob {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Bob::Handshaken(_) => f.write_str("Handshake complete"),
Bob::BtcLocked(_) | Bob::XmrLocked(_) | Bob::BtcRefundable(_) => {
f.write_str("Bitcoin refundable")
}
Bob::BtcRedeemed(_) => f.write_str("Monero redeemable"),
Bob::SwapComplete => f.write_str("Swap complete"),
}
}
}
pub struct Database(sled::Db);
@ -168,6 +83,8 @@ where
#[cfg(test)]
mod tests {
use crate::state::{Alice, Bob};
use super::*;
#[tokio::test]

@ -68,8 +68,7 @@ mod e2e_test {
let alice_transport = build(alice_behaviour.identity()).unwrap();
let db_dir = tempdir().unwrap();
let db = Database::open(db_dir.path()).unwrap();
let db = Database::open(std::path::Path::new("/home/luckysori/test/xmr_btc_swap")).unwrap();
let alice_swap = alice::swap(
alice_btc_wallet.clone(),
alice_xmr_wallet.clone(),

Loading…
Cancel
Save