Skip to content

Commit

Permalink
Implement Sqlite backends for Contacts service and Wallet peer db (#1071
Browse files Browse the repository at this point in the history
)

Merge pull request #1071

This PR adds a Sqlite backend for the Contacts Service and the Peer
database for the wallet

It also update the respective integration tests to test using both the
sqlite and memory db’s
  • Loading branch information
CjS77 committed Nov 29, 2019
2 parents 35a9f21 + 4accd2a commit d70951a
Show file tree
Hide file tree
Showing 20 changed files with 619 additions and 98 deletions.
2 changes: 1 addition & 1 deletion base_layer/core/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ pub const BASE_NODE_SERVICE_REQUEST_TIMEOUT: Duration = Duration::from_secs(60);
pub const BASE_NODE_SERVICE_DESIRED_RESPONSE_FRACTION: f32 = 0.6;

/// The allocated waiting time for a request waiting for service responses from the mempools of remote base nodes.
pub const MEMPOOL_SERVICE_REQUEST_TIMEOUT: Duration = Duration::from_secs(5);
pub const MEMPOOL_SERVICE_REQUEST_TIMEOUT: Duration = Duration::from_secs(60);
4 changes: 2 additions & 2 deletions base_layer/wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ time = {version = "0.1.39"}
derive-error = "0.0.4"
digest = "0.8.0"
serde = {version = "1.0.89", features = ["derive"] }
serde_json = "1.0"
serde_json = "1.0.39"
crossbeam-channel = "0.3.8"
log = "0.4.6"
log4rs = {version = "0.8.3", features = ["console_appender", "file_appender", "file", "yaml_format"]}
Expand All @@ -40,7 +40,7 @@ tempdir = "0.3.7"
[dev-dependencies]
env_logger = "0.6.2"
prost = "0.5.0"
tari_test_utils = {path="../../infrastructure/test_utils"}
tari_test_utils = { path = "../../infrastructure/test_utils", version = "^0.0"}

[features]
test_harness = []
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
DROP TABLE IF EXISTS sent_messages;
DROP TABLE IF EXISTS received_messages;
DROP TABLE IF EXISTS contacts;
DROP TABLE IF EXISTS settings;
-- DROP TABLE IF EXISTS sent_messages;
-- DROP TABLE IF EXISTS received_messages;
-- DROP TABLE IF EXISTS settings;
48 changes: 22 additions & 26 deletions base_layer/wallet/migrations/2019-06-26-130555_initial/up.sql
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
CREATE TABLE sent_messages (
id TEXT PRIMARY KEY NOT NULL,
source_pub_key TEXT NOT NULL,
dest_pub_key TEXT NOT NULL,
message TEXT NOT NULL,
timestamp DATETIME NOT NULL,
acknowledged INTEGER NOT NULL DEFAULT 0,
is_read INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY(dest_pub_key) REFERENCES contacts(pub_key)
);
-- CREATE TABLE sent_messages (
-- id TEXT PRIMARY KEY NOT NULL,
-- source_pub_key TEXT NOT NULL,
-- dest_pub_key TEXT NOT NULL,
-- message TEXT NOT NULL,
-- timestamp DATETIME NOT NULL,
-- acknowledged INTEGER NOT NULL DEFAULT 0,
-- is_read INTEGER NOT NULL DEFAULT 0,
-- FOREIGN KEY(dest_pub_key) REFERENCES contacts(pub_key)
-- );

CREATE TABLE received_messages (
id BLOB PRIMARY KEY NOT NULL,
source_pub_key TEXT NOT NULL,
dest_pub_key TEXT NOT NULL,
message TEXT NOT NULL,
timestamp DATETIME NOT NULL
);
-- CREATE TABLE received_messages (
-- id BLOB PRIMARY KEY NOT NULL,
-- source_pub_key TEXT NOT NULL,
-- dest_pub_key TEXT NOT NULL,
-- message TEXT NOT NULL,
-- timestamp DATETIME NOT NULL
-- );

-- CREATE TABLE settings (
-- pub_key TEXT PRIMARY KEY NOT NULL,
-- screen_name TEXT NOT NULL
-- )

CREATE TABLE contacts (
pub_key TEXT PRIMARY KEY NOT NULL UNIQUE,
screen_name TEXT NOT NULL,
address TEXT NOT NULL
);

CREATE TABLE settings (
pub_key TEXT PRIMARY KEY NOT NULL,
screen_name TEXT NOT NULL
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS contacts;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE contacts (
public_key BLOB PRIMARY KEY NOT NULL UNIQUE,
alias TEXT NOT NULL
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS peers;
4 changes: 4 additions & 0 deletions base_layer/wallet/migrations/2019-11-26-120903_peers/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE peers (
public_key BLOB PRIMARY KEY NOT NULL UNIQUE,
peer TEXT NOT NULL
);
10 changes: 10 additions & 0 deletions base_layer/wallet/src/contacts_service/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use crate::contacts_service::storage::database::DbKey;
use derive_error::Error;
use diesel::result::Error as DieselError;
use tari_service_framework::reply_channel::TransportChannelError;

#[derive(Debug, Error, PartialEq)]
Expand All @@ -40,8 +41,17 @@ pub enum ContactsServiceStorageError {
DuplicateContact,
/// This write operation is not supported for provided DbKey
OperationNotSupported,
/// Error converting a type
ConversionError,
/// Could not find all values specified for batch operation
ValuesNotFound,
#[error(non_std, no_from)]
ValueNotFound(DbKey),
#[error(msg_embedded, non_std, no_from)]
UnexpectedResult(String),
R2d2Error,
DieselError(DieselError),
DieselConnectionError(diesel::ConnectionError),
#[error(msg_embedded, no_from, non_std)]
DatabaseMigrationError(String),
}
4 changes: 2 additions & 2 deletions base_layer/wallet/src/contacts_service/storage/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub enum DbKey {

pub enum DbValue {
Contact(Box<Contact>),
Contacts(Box<Vec<Contact>>),
Contacts(Vec<Contact>),
}

pub enum DbKeyValuePair {
Expand Down Expand Up @@ -96,7 +96,7 @@ where T: ContactsBackend
DbKey::Contacts,
ContactsServiceStorageError::UnexpectedResult("Could not retrieve contacts".to_string()),
),
Ok(Some(DbValue::Contacts(c))) => Ok(*c),
Ok(Some(DbValue::Contacts(c))) => Ok(c),
Ok(Some(other)) => unexpected_result(DbKey::Contacts, other),
Err(e) => log_error(DbKey::Contacts, e),
}?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl ContactsBackend for ContactsServiceMemoryDatabase {
.iter()
.find(|v| &v.public_key == pk)
.map(|c| DbValue::Contact(Box::new(c.clone()))),
DbKey::Contacts => Some(DbValue::Contacts(Box::new(db.contacts.clone()))),
DbKey::Contacts => Some(DbValue::Contacts(db.contacts.clone())),
};

Ok(result)
Expand Down
1 change: 1 addition & 0 deletions base_layer/wallet/src/contacts_service/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@

pub mod database;
pub mod memory_db;
pub mod sqlite_db;
Loading

0 comments on commit d70951a

Please sign in to comment.