2020-11-03 04:49:00 +00:00
|
|
|
use anyhow::{anyhow, bail, Context, Result};
|
2020-12-23 04:30:23 +00:00
|
|
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
|
|
|
use std::{fmt::Display, path::Path};
|
2020-11-03 03:23:03 +00:00
|
|
|
use uuid::Uuid;
|
2020-11-03 05:44:04 +00:00
|
|
|
|
2021-01-05 03:08:36 +00:00
|
|
|
pub mod alice;
|
|
|
|
pub mod bob;
|
2020-12-23 04:20:24 +00:00
|
|
|
|
2021-01-05 03:08:36 +00:00
|
|
|
pub use alice::Alice;
|
|
|
|
pub use bob::Bob;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
|
|
|
pub enum Swap {
|
|
|
|
Alice(Alice),
|
|
|
|
Bob(Bob),
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-23 04:26:08 +00:00
|
|
|
|
2020-11-03 04:26:47 +00:00
|
|
|
pub struct Database(sled::Db);
|
|
|
|
|
|
|
|
impl Database {
|
2020-10-13 22:32:25 +00:00
|
|
|
pub fn open(path: &Path) -> Result<Self> {
|
2020-10-21 22:52:57 +00:00
|
|
|
let db =
|
|
|
|
sled::open(path).with_context(|| format!("Could not open the DB at {:?}", path))?;
|
2020-10-13 22:32:25 +00:00
|
|
|
|
2020-11-03 04:26:47 +00:00
|
|
|
Ok(Database(db))
|
2020-10-13 22:32:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 04:26:47 +00:00
|
|
|
pub async fn insert_latest_state(&self, swap_id: Uuid, state: Swap) -> Result<()> {
|
2020-11-03 03:23:03 +00:00
|
|
|
let key = serialize(&swap_id)?;
|
2020-10-13 22:32:25 +00:00
|
|
|
let new_value = serialize(&state).context("Could not serialize new state value")?;
|
|
|
|
|
2020-11-03 04:26:47 +00:00
|
|
|
let old_value = self.0.get(&key)?;
|
2020-10-13 22:32:25 +00:00
|
|
|
|
2020-11-03 04:26:47 +00:00
|
|
|
self.0
|
2020-10-13 22:32:25 +00:00
|
|
|
.compare_and_swap(key, old_value, Some(new_value))
|
|
|
|
.context("Could not write in the DB")?
|
2020-10-21 22:52:57 +00:00
|
|
|
.context("Stored swap somehow changed, aborting saving")?;
|
2020-10-13 22:32:25 +00:00
|
|
|
|
2020-10-22 08:27:22 +00:00
|
|
|
// TODO: see if this can be done through sled config
|
2020-11-03 04:26:47 +00:00
|
|
|
self.0
|
2020-10-13 22:32:25 +00:00
|
|
|
.flush_async()
|
|
|
|
.await
|
|
|
|
.map(|_| ())
|
|
|
|
.context("Could not flush db")
|
|
|
|
}
|
|
|
|
|
2020-11-03 06:08:31 +00:00
|
|
|
pub fn get_state(&self, swap_id: Uuid) -> anyhow::Result<Swap> {
|
2020-11-03 03:23:03 +00:00
|
|
|
let key = serialize(&swap_id)?;
|
2020-10-13 22:32:25 +00:00
|
|
|
|
|
|
|
let encoded = self
|
2020-11-03 04:26:47 +00:00
|
|
|
.0
|
2020-10-13 22:32:25 +00:00
|
|
|
.get(&key)?
|
2020-12-15 10:26:02 +00:00
|
|
|
.ok_or_else(|| anyhow!("Swap with id {} not found in database", swap_id))?;
|
2020-10-13 22:32:25 +00:00
|
|
|
|
|
|
|
let state = deserialize(&encoded).context("Could not deserialize state")?;
|
|
|
|
Ok(state)
|
|
|
|
}
|
2020-11-03 04:49:00 +00:00
|
|
|
|
|
|
|
pub fn all(&self) -> Result<Vec<(Uuid, Swap)>> {
|
|
|
|
self.0
|
|
|
|
.iter()
|
|
|
|
.map(|item| match item {
|
|
|
|
Ok((key, value)) => {
|
|
|
|
let swap_id = deserialize::<Uuid>(&key);
|
|
|
|
let swap = deserialize::<Swap>(&value).context("failed to deserialize swap");
|
|
|
|
|
|
|
|
match (swap_id, swap) {
|
|
|
|
(Ok(swap_id), Ok(swap)) => Ok((swap_id, swap)),
|
|
|
|
(Ok(_), Err(err)) => Err(err),
|
|
|
|
_ => bail!("failed to deserialize swap"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => Err(err).context("failed to retrieve swap from DB"),
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
2020-10-13 22:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serialize<T>(t: &T) -> anyhow::Result<Vec<u8>>
|
|
|
|
where
|
|
|
|
T: Serialize,
|
|
|
|
{
|
|
|
|
Ok(serde_cbor::to_vec(t)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deserialize<T>(v: &[u8]) -> anyhow::Result<T>
|
|
|
|
where
|
|
|
|
T: DeserializeOwned,
|
|
|
|
{
|
|
|
|
Ok(serde_cbor::from_slice(&v)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-01-05 03:08:36 +00:00
|
|
|
use crate::database::{
|
|
|
|
alice::{Alice, AliceEndState},
|
|
|
|
bob::{Bob, BobEndState},
|
|
|
|
};
|
2020-11-03 04:26:47 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn can_write_and_read_to_multiple_keys() {
|
|
|
|
let db_dir = tempfile::tempdir().unwrap();
|
|
|
|
let db = Database::open(db_dir.path()).unwrap();
|
|
|
|
|
2020-12-23 01:08:51 +00:00
|
|
|
let state_1 = Swap::Alice(Alice::Done(AliceEndState::BtcRedeemed));
|
2020-11-03 04:26:47 +00:00
|
|
|
let swap_id_1 = Uuid::new_v4();
|
|
|
|
db.insert_latest_state(swap_id_1, state_1.clone())
|
|
|
|
.await
|
|
|
|
.expect("Failed to save second state");
|
|
|
|
|
2020-12-23 01:08:51 +00:00
|
|
|
let state_2 = Swap::Bob(Bob::Done(BobEndState::XmrRedeemed));
|
2020-11-03 04:26:47 +00:00
|
|
|
let swap_id_2 = Uuid::new_v4();
|
|
|
|
db.insert_latest_state(swap_id_2, state_2.clone())
|
|
|
|
.await
|
|
|
|
.expect("Failed to save first state");
|
|
|
|
|
|
|
|
let recovered_1 = db
|
2020-11-03 06:08:31 +00:00
|
|
|
.get_state(swap_id_1)
|
2020-11-03 04:26:47 +00:00
|
|
|
.expect("Failed to recover first state");
|
|
|
|
|
|
|
|
let recovered_2 = db
|
2020-11-03 06:08:31 +00:00
|
|
|
.get_state(swap_id_2)
|
2020-11-03 04:26:47 +00:00
|
|
|
.expect("Failed to recover second state");
|
|
|
|
|
|
|
|
assert_eq!(recovered_1, state_1);
|
|
|
|
assert_eq!(recovered_2, state_2);
|
2020-10-13 22:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2020-11-03 04:26:47 +00:00
|
|
|
async fn can_write_twice_to_one_key() {
|
2020-10-22 08:34:53 +00:00
|
|
|
let db_dir = tempfile::tempdir().unwrap();
|
|
|
|
let db = Database::open(db_dir.path()).unwrap();
|
2020-10-13 22:32:25 +00:00
|
|
|
|
2020-12-23 01:08:51 +00:00
|
|
|
let state = Swap::Alice(Alice::Done(AliceEndState::SafelyAborted));
|
2020-10-13 22:32:25 +00:00
|
|
|
|
2020-11-03 03:23:03 +00:00
|
|
|
let swap_id = Uuid::new_v4();
|
2020-11-03 04:26:47 +00:00
|
|
|
db.insert_latest_state(swap_id, state.clone())
|
2020-10-13 22:32:25 +00:00
|
|
|
.await
|
|
|
|
.expect("Failed to save state the first time");
|
2020-11-03 04:26:47 +00:00
|
|
|
let recovered = db
|
2020-11-03 06:08:31 +00:00
|
|
|
.get_state(swap_id)
|
2020-10-13 22:32:25 +00:00
|
|
|
.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
|
2020-11-03 04:26:47 +00:00
|
|
|
db.insert_latest_state(swap_id, recovered)
|
2020-10-13 22:32:25 +00:00
|
|
|
.await
|
|
|
|
.expect("Failed to save state the second time");
|
2020-11-03 04:26:47 +00:00
|
|
|
let recovered = db
|
2020-11-03 06:08:31 +00:00
|
|
|
.get_state(swap_id)
|
2020-10-13 22:32:25 +00:00
|
|
|
.expect("Failed to recover state the second time");
|
|
|
|
|
2020-11-03 04:26:47 +00:00
|
|
|
assert_eq!(recovered, state);
|
2020-10-13 22:32:25 +00:00
|
|
|
}
|
2020-11-03 04:49:00 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn can_fetch_all_keys() {
|
|
|
|
let db_dir = tempfile::tempdir().unwrap();
|
|
|
|
let db = Database::open(db_dir.path()).unwrap();
|
|
|
|
|
2020-12-23 01:08:51 +00:00
|
|
|
let state_1 = Swap::Alice(Alice::Done(AliceEndState::BtcPunished));
|
2020-11-03 04:49:00 +00:00
|
|
|
let swap_id_1 = Uuid::new_v4();
|
|
|
|
db.insert_latest_state(swap_id_1, state_1.clone())
|
|
|
|
.await
|
|
|
|
.expect("Failed to save second state");
|
|
|
|
|
2020-12-23 01:08:51 +00:00
|
|
|
let state_2 = Swap::Bob(Bob::Done(BobEndState::BtcPunished));
|
2020-11-03 04:49:00 +00:00
|
|
|
let swap_id_2 = Uuid::new_v4();
|
|
|
|
db.insert_latest_state(swap_id_2, state_2.clone())
|
|
|
|
.await
|
|
|
|
.expect("Failed to save first state");
|
|
|
|
|
|
|
|
let swaps = db.all().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(swaps.len(), 2);
|
|
|
|
assert!(swaps.contains(&(swap_id_1, state_1)));
|
|
|
|
assert!(swaps.contains(&(swap_id_2, state_2)));
|
|
|
|
}
|
2020-10-13 22:32:25 +00:00
|
|
|
}
|