mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-01-12 07:59:33 -05:00
Support saving multiple swaps
By replacing `LAST_STATE_KEY` with a swap ID passed as an argument to inserting and reading from the database.
This commit is contained in:
parent
823add218e
commit
02075c2a1d
@ -39,6 +39,7 @@ tracing-futures = { version = "0.2", features = ["std-future", "futures-03"] }
|
|||||||
tracing-log = "0.1"
|
tracing-log = "0.1"
|
||||||
tracing-subscriber = { version = "0.2", default-features = false, features = ["fmt", "ansi", "env-filter"] }
|
tracing-subscriber = { version = "0.2", default-features = false, features = ["fmt", "ansi", "env-filter"] }
|
||||||
url = "2.1"
|
url = "2.1"
|
||||||
|
uuid = { version = "0.8", features = ["serde", "v4"] }
|
||||||
void = "1"
|
void = "1"
|
||||||
xmr-btc = { path = "../xmr-btc" }
|
xmr-btc = { path = "../xmr-btc" }
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@ use rand::rngs::OsRng;
|
|||||||
use std::{sync::Arc, time::Duration};
|
use std::{sync::Arc, time::Duration};
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
use tracing::{debug, info, warn};
|
use tracing::{debug, info, warn};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
mod amounts;
|
mod amounts;
|
||||||
mod message0;
|
mod message0;
|
||||||
@ -175,7 +176,8 @@ pub async fn swap(
|
|||||||
other => panic!("Unexpected event: {:?}", other),
|
other => panic!("Unexpected event: {:?}", other),
|
||||||
};
|
};
|
||||||
|
|
||||||
db.insert_latest_state(&storage::Alice::Handshaken(state3.clone()))
|
let swap_id = Uuid::new_v4();
|
||||||
|
db.insert_latest_state(swap_id, &storage::Alice::Handshaken(state3.clone()))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
info!("Handshake complete, we now have State3 for Alice.");
|
info!("Handshake complete, we now have State3 for Alice.");
|
||||||
@ -203,14 +205,14 @@ pub async fn swap(
|
|||||||
public_spend_key,
|
public_spend_key,
|
||||||
public_view_key,
|
public_view_key,
|
||||||
}) => {
|
}) => {
|
||||||
db.insert_latest_state(&storage::Alice::BtcLocked(state3.clone()))
|
db.insert_latest_state(swap_id, &storage::Alice::BtcLocked(state3.clone()))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let (transfer_proof, _) = monero_wallet
|
let (transfer_proof, _) = monero_wallet
|
||||||
.transfer(public_spend_key, public_view_key, amount)
|
.transfer(public_spend_key, public_view_key, amount)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
db.insert_latest_state(&storage::Alice::XmrLocked(state3.clone()))
|
db.insert_latest_state(swap_id, &storage::Alice::XmrLocked(state3.clone()))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let mut guard = network.as_ref().lock().await;
|
let mut guard = network.as_ref().lock().await;
|
||||||
@ -219,7 +221,7 @@ pub async fn swap(
|
|||||||
}
|
}
|
||||||
|
|
||||||
GeneratorState::Yielded(Action::RedeemBtc(tx)) => {
|
GeneratorState::Yielded(Action::RedeemBtc(tx)) => {
|
||||||
db.insert_latest_state(&storage::Alice::BtcRedeemable {
|
db.insert_latest_state(swap_id, &storage::Alice::BtcRedeemable {
|
||||||
state: state3.clone(),
|
state: state3.clone(),
|
||||||
redeem_tx: tx.clone(),
|
redeem_tx: tx.clone(),
|
||||||
})
|
})
|
||||||
@ -231,7 +233,7 @@ pub async fn swap(
|
|||||||
let _ = bitcoin_wallet.broadcast_signed_transaction(tx).await?;
|
let _ = bitcoin_wallet.broadcast_signed_transaction(tx).await?;
|
||||||
}
|
}
|
||||||
GeneratorState::Yielded(Action::PunishBtc(tx)) => {
|
GeneratorState::Yielded(Action::PunishBtc(tx)) => {
|
||||||
db.insert_latest_state(&storage::Alice::BtcPunishable(state3.clone()))
|
db.insert_latest_state(swap_id, &storage::Alice::BtcPunishable(state3.clone()))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let _ = bitcoin_wallet.broadcast_signed_transaction(tx).await?;
|
let _ = bitcoin_wallet.broadcast_signed_transaction(tx).await?;
|
||||||
@ -240,7 +242,7 @@ pub async fn swap(
|
|||||||
spend_key,
|
spend_key,
|
||||||
view_key,
|
view_key,
|
||||||
}) => {
|
}) => {
|
||||||
db.insert_latest_state(&storage::Alice::BtcRefunded {
|
db.insert_latest_state(swap_id, &storage::Alice::BtcRefunded {
|
||||||
state: state3.clone(),
|
state: state3.clone(),
|
||||||
spend_key,
|
spend_key,
|
||||||
view_key,
|
view_key,
|
||||||
@ -252,7 +254,7 @@ pub async fn swap(
|
|||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
GeneratorState::Complete(()) => {
|
GeneratorState::Complete(()) => {
|
||||||
db.insert_latest_state(&storage::Alice::SwapComplete)
|
db.insert_latest_state(swap_id, &storage::Alice::SwapComplete)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
@ -13,6 +13,7 @@ use rand::rngs::OsRng;
|
|||||||
use std::{process, sync::Arc, time::Duration};
|
use std::{process, sync::Arc, time::Duration};
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
use tracing::{debug, info, warn};
|
use tracing::{debug, info, warn};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
mod amounts;
|
mod amounts;
|
||||||
mod message0;
|
mod message0;
|
||||||
@ -142,7 +143,8 @@ pub async fn swap(
|
|||||||
other => panic!("unexpected event: {:?}", other),
|
other => panic!("unexpected event: {:?}", other),
|
||||||
};
|
};
|
||||||
|
|
||||||
db.insert_latest_state(&storage::Bob::Handshaken(state2.clone()))
|
let swap_id = Uuid::new_v4();
|
||||||
|
db.insert_latest_state(swap_id, &storage::Bob::Handshaken(state2.clone()))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
swarm.send_message2(alice.clone(), state2.next_message());
|
swarm.send_message2(alice.clone(), state2.next_message());
|
||||||
@ -170,11 +172,11 @@ pub async fn swap(
|
|||||||
let _ = bitcoin_wallet
|
let _ = bitcoin_wallet
|
||||||
.broadcast_signed_transaction(signed_tx_lock)
|
.broadcast_signed_transaction(signed_tx_lock)
|
||||||
.await?;
|
.await?;
|
||||||
db.insert_latest_state(&storage::Bob::BtcLocked(state2.clone()))
|
db.insert_latest_state(swap_id, &storage::Bob::BtcLocked(state2.clone()))
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
GeneratorState::Yielded(bob::Action::SendBtcRedeemEncsig(tx_redeem_encsig)) => {
|
GeneratorState::Yielded(bob::Action::SendBtcRedeemEncsig(tx_redeem_encsig)) => {
|
||||||
db.insert_latest_state(&storage::Bob::XmrLocked(state2.clone()))
|
db.insert_latest_state(swap_id, &storage::Bob::XmrLocked(state2.clone()))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let mut guard = network.as_ref().lock().await;
|
let mut guard = network.as_ref().lock().await;
|
||||||
@ -194,7 +196,7 @@ pub async fn swap(
|
|||||||
spend_key,
|
spend_key,
|
||||||
view_key,
|
view_key,
|
||||||
}) => {
|
}) => {
|
||||||
db.insert_latest_state(&storage::Bob::BtcRedeemed(state2.clone()))
|
db.insert_latest_state(swap_id, &storage::Bob::BtcRedeemed(state2.clone()))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
monero_wallet
|
monero_wallet
|
||||||
@ -202,7 +204,7 @@ pub async fn swap(
|
|||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
GeneratorState::Yielded(bob::Action::CancelBtc(tx_cancel)) => {
|
GeneratorState::Yielded(bob::Action::CancelBtc(tx_cancel)) => {
|
||||||
db.insert_latest_state(&storage::Bob::BtcRefundable(state2.clone()))
|
db.insert_latest_state(swap_id, &storage::Bob::BtcRefundable(state2.clone()))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let _ = bitcoin_wallet
|
let _ = bitcoin_wallet
|
||||||
@ -210,7 +212,7 @@ pub async fn swap(
|
|||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
GeneratorState::Yielded(bob::Action::RefundBtc(tx_refund)) => {
|
GeneratorState::Yielded(bob::Action::RefundBtc(tx_refund)) => {
|
||||||
db.insert_latest_state(&storage::Bob::BtcRefundable(state2.clone()))
|
db.insert_latest_state(swap_id, &storage::Bob::BtcRefundable(state2.clone()))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let _ = bitcoin_wallet
|
let _ = bitcoin_wallet
|
||||||
@ -218,7 +220,8 @@ pub async fn swap(
|
|||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
GeneratorState::Complete(()) => {
|
GeneratorState::Complete(()) => {
|
||||||
db.insert_latest_state(&storage::Bob::SwapComplete).await?;
|
db.insert_latest_state(swap_id, &storage::Bob::SwapComplete)
|
||||||
|
.await?;
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use uuid::Uuid;
|
||||||
use xmr_btc::{alice, bob, monero, serde::monero_private_key};
|
use xmr_btc::{alice, bob, monero, serde::monero_private_key};
|
||||||
|
|
||||||
#[allow(clippy::large_enum_variant)]
|
#[allow(clippy::large_enum_variant)]
|
||||||
@ -45,9 +46,6 @@ impl<T> Database<T>
|
|||||||
where
|
where
|
||||||
T: Serialize + DeserializeOwned,
|
T: Serialize + DeserializeOwned,
|
||||||
{
|
{
|
||||||
// TODO: serialize using lazy/one-time initlisation
|
|
||||||
const LAST_STATE_KEY: &'static str = "latest_state";
|
|
||||||
|
|
||||||
pub fn open(path: &Path) -> Result<Self> {
|
pub fn open(path: &Path) -> Result<Self> {
|
||||||
let db =
|
let db =
|
||||||
sled::open(path).with_context(|| format!("Could not open the DB at {:?}", path))?;
|
sled::open(path).with_context(|| format!("Could not open the DB at {:?}", path))?;
|
||||||
@ -60,8 +58,8 @@ where
|
|||||||
|
|
||||||
// TODO: Add method to update state
|
// TODO: Add method to update state
|
||||||
|
|
||||||
pub async fn insert_latest_state(&self, state: &T) -> Result<()> {
|
pub async fn insert_latest_state(&self, swap_id: Uuid, state: &T) -> Result<()> {
|
||||||
let key = serialize(&Self::LAST_STATE_KEY)?;
|
let key = serialize(&swap_id)?;
|
||||||
let new_value = serialize(&state).context("Could not serialize new state value")?;
|
let new_value = serialize(&state).context("Could not serialize new state value")?;
|
||||||
|
|
||||||
let old_value = self.db.get(&key)?;
|
let old_value = self.db.get(&key)?;
|
||||||
@ -79,8 +77,8 @@ where
|
|||||||
.context("Could not flush db")
|
.context("Could not flush db")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_latest_state(&self) -> anyhow::Result<T> {
|
pub fn get_latest_state(&self, swap_id: Uuid) -> anyhow::Result<T> {
|
||||||
let key = serialize(&Self::LAST_STATE_KEY)?;
|
let key = serialize(&swap_id)?;
|
||||||
|
|
||||||
let encoded = self
|
let encoded = self
|
||||||
.db
|
.db
|
||||||
@ -172,20 +170,21 @@ mod tests {
|
|||||||
tx_punish_sig,
|
tx_punish_sig,
|
||||||
};
|
};
|
||||||
|
|
||||||
db.insert_latest_state(&state)
|
let swap_id = Uuid::new_v4();
|
||||||
|
db.insert_latest_state(swap_id, &state)
|
||||||
.await
|
.await
|
||||||
.expect("Failed to save state the first time");
|
.expect("Failed to save state the first time");
|
||||||
let recovered: TestState = db
|
let recovered: TestState = db
|
||||||
.get_latest_state()
|
.get_latest_state(swap_id)
|
||||||
.expect("Failed to recover state the first time");
|
.expect("Failed to recover state the first time");
|
||||||
|
|
||||||
// We insert and recover twice to ensure database implementation allows the
|
// We insert and recover twice to ensure database implementation allows the
|
||||||
// caller to write to an existing key
|
// caller to write to an existing key
|
||||||
db.insert_latest_state(&recovered)
|
db.insert_latest_state(swap_id, &recovered)
|
||||||
.await
|
.await
|
||||||
.expect("Failed to save state the second time");
|
.expect("Failed to save state the second time");
|
||||||
let recovered: TestState = db
|
let recovered: TestState = db
|
||||||
.get_latest_state()
|
.get_latest_state(swap_id)
|
||||||
.expect("Failed to recover state the second time");
|
.expect("Failed to recover state the second time");
|
||||||
|
|
||||||
assert_eq!(state, recovered);
|
assert_eq!(state, recovered);
|
||||||
|
Loading…
Reference in New Issue
Block a user