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:
Lucas Soriano del Pino 2020-11-03 14:23:03 +11:00 committed by rishflab
parent 823add218e
commit 02075c2a1d
4 changed files with 30 additions and 25 deletions

View File

@ -39,6 +39,7 @@ tracing-futures = { version = "0.2", features = ["std-future", "futures-03"] }
tracing-log = "0.1"
tracing-subscriber = { version = "0.2", default-features = false, features = ["fmt", "ansi", "env-filter"] }
url = "2.1"
uuid = { version = "0.8", features = ["serde", "v4"] }
void = "1"
xmr-btc = { path = "../xmr-btc" }

View File

@ -13,6 +13,7 @@ use rand::rngs::OsRng;
use std::{sync::Arc, time::Duration};
use tokio::sync::Mutex;
use tracing::{debug, info, warn};
use uuid::Uuid;
mod amounts;
mod message0;
@ -175,7 +176,8 @@ pub async fn swap(
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?;
info!("Handshake complete, we now have State3 for Alice.");
@ -203,14 +205,14 @@ pub async fn swap(
public_spend_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?;
let (transfer_proof, _) = monero_wallet
.transfer(public_spend_key, public_view_key, amount)
.await?;
db.insert_latest_state(&storage::Alice::XmrLocked(state3.clone()))
db.insert_latest_state(swap_id, &storage::Alice::XmrLocked(state3.clone()))
.await?;
let mut guard = network.as_ref().lock().await;
@ -219,7 +221,7 @@ pub async fn swap(
}
GeneratorState::Yielded(Action::RedeemBtc(tx)) => {
db.insert_latest_state(&storage::Alice::BtcRedeemable {
db.insert_latest_state(swap_id, &storage::Alice::BtcRedeemable {
state: state3.clone(),
redeem_tx: tx.clone(),
})
@ -231,7 +233,7 @@ pub async fn swap(
let _ = bitcoin_wallet.broadcast_signed_transaction(tx).await?;
}
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?;
let _ = bitcoin_wallet.broadcast_signed_transaction(tx).await?;
@ -240,7 +242,7 @@ pub async fn swap(
spend_key,
view_key,
}) => {
db.insert_latest_state(&storage::Alice::BtcRefunded {
db.insert_latest_state(swap_id, &storage::Alice::BtcRefunded {
state: state3.clone(),
spend_key,
view_key,
@ -252,7 +254,7 @@ pub async fn swap(
.await?;
}
GeneratorState::Complete(()) => {
db.insert_latest_state(&storage::Alice::SwapComplete)
db.insert_latest_state(swap_id, &storage::Alice::SwapComplete)
.await?;
return Ok(());

View File

@ -13,6 +13,7 @@ use rand::rngs::OsRng;
use std::{process, sync::Arc, time::Duration};
use tokio::sync::Mutex;
use tracing::{debug, info, warn};
use uuid::Uuid;
mod amounts;
mod message0;
@ -142,7 +143,8 @@ pub async fn swap(
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?;
swarm.send_message2(alice.clone(), state2.next_message());
@ -170,11 +172,11 @@ pub async fn swap(
let _ = bitcoin_wallet
.broadcast_signed_transaction(signed_tx_lock)
.await?;
db.insert_latest_state(&storage::Bob::BtcLocked(state2.clone()))
db.insert_latest_state(swap_id, &storage::Bob::BtcLocked(state2.clone()))
.await?;
}
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?;
let mut guard = network.as_ref().lock().await;
@ -194,7 +196,7 @@ pub async fn swap(
spend_key,
view_key,
}) => {
db.insert_latest_state(&storage::Bob::BtcRedeemed(state2.clone()))
db.insert_latest_state(swap_id, &storage::Bob::BtcRedeemed(state2.clone()))
.await?;
monero_wallet
@ -202,7 +204,7 @@ pub async fn swap(
.await?;
}
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?;
let _ = bitcoin_wallet
@ -210,7 +212,7 @@ pub async fn swap(
.await?;
}
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?;
let _ = bitcoin_wallet
@ -218,7 +220,8 @@ pub async fn swap(
.await?;
}
GeneratorState::Complete(()) => {
db.insert_latest_state(&storage::Bob::SwapComplete).await?;
db.insert_latest_state(swap_id, &storage::Bob::SwapComplete)
.await?;
return Ok(());
}

View File

@ -1,6 +1,7 @@
use anyhow::{anyhow, Context, Result};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::path::Path;
use uuid::Uuid;
use xmr_btc::{alice, bob, monero, serde::monero_private_key};
#[allow(clippy::large_enum_variant)]
@ -45,9 +46,6 @@ impl<T> Database<T>
where
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> {
let db =
sled::open(path).with_context(|| format!("Could not open the DB at {:?}", path))?;
@ -60,8 +58,8 @@ where
// TODO: Add method to update state
pub async fn insert_latest_state(&self, state: &T) -> Result<()> {
let key = serialize(&Self::LAST_STATE_KEY)?;
pub async fn insert_latest_state(&self, swap_id: Uuid, state: &T) -> Result<()> {
let key = serialize(&swap_id)?;
let new_value = serialize(&state).context("Could not serialize new state value")?;
let old_value = self.db.get(&key)?;
@ -79,8 +77,8 @@ where
.context("Could not flush db")
}
pub fn get_latest_state(&self) -> anyhow::Result<T> {
let key = serialize(&Self::LAST_STATE_KEY)?;
pub fn get_latest_state(&self, swap_id: Uuid) -> anyhow::Result<T> {
let key = serialize(&swap_id)?;
let encoded = self
.db
@ -172,20 +170,21 @@ mod tests {
tx_punish_sig,
};
db.insert_latest_state(&state)
let swap_id = Uuid::new_v4();
db.insert_latest_state(swap_id, &state)
.await
.expect("Failed to save state the first time");
let recovered: TestState = db
.get_latest_state()
.get_latest_state(swap_id)
.expect("Failed to recover state the first time");
// We insert and recover twice to ensure database implementation allows the
// caller to write to an existing key
db.insert_latest_state(&recovered)
db.insert_latest_state(swap_id, &recovered)
.await
.expect("Failed to save state the second time");
let recovered: TestState = db
.get_latest_state()
.get_latest_state(swap_id)
.expect("Failed to recover state the second time");
assert_eq!(state, recovered);