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

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