Create Database trait

Use domain types in database API to prevent leaking of database types.
This trait will allow us to smoothly introduce the sqlite database.
This commit is contained in:
rishflab 2021-09-28 10:15:31 +10:00
parent a94c320021
commit da9d09aa5e
21 changed files with 351 additions and 253 deletions

View file

@ -1,16 +1,17 @@
use crate::bitcoin::{parse_rpc_error_code, RpcErrorCode, Txid, Wallet};
use crate::database::{SledDatabase, Swap};
use crate::protocol::bob::BobState;
use crate::protocol::Database;
use anyhow::{bail, Result};
use std::convert::TryInto;
use std::sync::Arc;
use uuid::Uuid;
pub async fn cancel(
swap_id: Uuid,
bitcoin_wallet: Arc<Wallet>,
db: SledDatabase,
db: Arc<dyn Database>,
) -> Result<(Txid, BobState)> {
let state = db.get_state(swap_id)?.try_into_bob()?.into();
let state = db.get_state(swap_id).await?.try_into()?;
let state6 = match state {
BobState::BtcLocked(state3) => state3.cancel(),
@ -48,8 +49,8 @@ pub async fn cancel(
};
let state = BobState::BtcCancelled(state6);
let db_state = state.clone().into();
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
db.insert_latest_state(swap_id, state.clone().into())
.await?;
Ok((txid, state))
}

View file

@ -1,16 +1,17 @@
use crate::bitcoin::Wallet;
use crate::database::{SledDatabase, Swap};
use crate::protocol::bob::BobState;
use crate::protocol::Database;
use anyhow::{bail, Result};
use std::convert::TryInto;
use std::sync::Arc;
use uuid::Uuid;
pub async fn refund(
swap_id: Uuid,
bitcoin_wallet: Arc<Wallet>,
db: SledDatabase,
db: Arc<dyn Database>,
) -> Result<BobState> {
let state = db.get_state(swap_id)?.try_into_bob()?.into();
let state = db.get_state(swap_id).await?.try_into()?;
let state6 = match state {
BobState::BtcLocked(state3) => state3.cancel(),
@ -35,9 +36,8 @@ pub async fn refund(
state6.publish_refund_btc(bitcoin_wallet.as_ref()).await?;
let state = BobState::BtcRefunded(state6);
let db_state = state.clone().into();
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
db.insert_latest_state(swap_id, state.clone().into())
.await?;
Ok(state)
}