Skip to content
This repository was archived by the owner on Mar 23, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cnd/src/db/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
db::{load_swaps::LoadAcceptedSwap, Location, SaveMessage, Sqlite},
db::{load_swaps::LoadAcceptedSwap, SaveMessage, Sqlite},
quickcheck::Quickcheck,
swap_protocols::{
ledger::{Bitcoin, Ethereum},
Expand All @@ -25,7 +25,7 @@ macro_rules! db_roundtrip_test {
.tempfile()?
.into_temp_path();

let db = Sqlite::new(Location::OnDisk(&db_path))?;
let db = Sqlite::new(&db_path)?;

let saved_request = Request {
swap_id: *swap_id,
Expand Down
51 changes: 8 additions & 43 deletions cnd/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,17 @@ pub struct Sqlite {
uri: String,
}

/// Defines the storage location of our SQLite database
#[derive(Debug)]
pub enum Location<'p> {
OnDisk(&'p Path),
#[cfg(test)]
InMemory,
}

impl Sqlite {
/// Return a handle that can be used to access the database.
///
/// When this returns an Sqlite database exists at 'db', a
/// successful connection to the database has been made, and
/// the database migrations have been run.
pub fn new(location: Location<'_>) -> anyhow::Result<Self> {
let db = match location {
Location::OnDisk(path) => {
if path == Path::new(":memory:") {
anyhow::bail!("use Location::InMemory if you want an in-memory database!")
}

ensure_folder_tree_exists(path)?;

Sqlite {
uri: format!("file:{}", path.display()),
}
}
#[cfg(test)]
Location::InMemory => Sqlite {
uri: ":memory:".to_owned(),
},
pub fn new(path: &Path) -> anyhow::Result<Self> {
ensure_folder_tree_exists(path)?;

let db = Sqlite {
uri: format!("file:{}", path.display()),
};

let connection = db.connect()?;
Expand Down Expand Up @@ -91,7 +71,7 @@ mod tests {
fn can_create_a_new_temp_db() {
let path = temp_db();

let db = Sqlite::new(Location::OnDisk(&path));
let db = Sqlite::new(&path);

assert_that(&db).is_ok();
}
Expand All @@ -102,7 +82,7 @@ mod tests {
// validate assumptions: the db does not exist yet
assert_that(&path.as_path()).does_not_exist();

let db = Sqlite::new(Location::OnDisk(&path));
let db = Sqlite::new(&path);

assert_that(&db).is_ok();
assert_that(&path.as_path()).exists();
Expand All @@ -124,24 +104,9 @@ mod tests {
assert_that(&path).does_not_exist();
assert_that(&path.parent()).is_some().does_not_exist();

let db = Sqlite::new(Location::OnDisk(&path));
let db = Sqlite::new(&path);

assert_that(&db).is_ok();
assert_that(&path).exists();
}

#[test]
fn given_special_memory_path_does_not_create_a_file() {
let result = Sqlite::new(Location::InMemory);

assert_that(&result).is_ok();
assert_that(&Path::new(":memory:")).does_not_exist();
}

#[test]
fn given_memory_as_a_path_fails() {
let result = Sqlite::new(Location::OnDisk(Path::new(":memory:")));

assert_that(&result).is_err();
}
}
4 changes: 2 additions & 2 deletions cnd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use btsieve::{bitcoin::BitcoindConnector, ethereum::Web3Connector};
use cnd::{
config::{self, Settings},
db::{Location, SaveRfc003Messages, Sqlite},
db::{SaveRfc003Messages, Sqlite},
http_api::{self, route_factory},
network::{self, Network, SendRequest},
seed::{Seed, SwapSeed},
Expand Down Expand Up @@ -69,7 +69,7 @@ fn main() -> anyhow::Result<()> {
let local_peer_id = PeerId::from(local_key_pair.clone().public());
log::info!("Starting with peer_id: {}", local_peer_id);

let database = Sqlite::new(Location::OnDisk(&settings.database.sqlite))?;
let database = Sqlite::new(&settings.database.sqlite)?;

let transport = libp2p::build_development_transport(local_key_pair);
let behaviour = network::ComitNode::new(
Expand Down