Skip to content

Commit

Permalink
Add sqlite for fedimint storage
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyRonning committed May 14, 2024
1 parent 01bc9f5 commit bf47e8f
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ rusqlite = { version = "0.31.0", features = ["sqlcipher"] }
diesel = { version = "2.1.6", features = ["sqlite", "chrono", "r2d2"] }
diesel_migrations = { version = "2.1.0", features = ["sqlite"] }
uuid = { version = "1.8", features = ["v4"] }
async-trait = "0.1.77"
bincode = "1.3.3"

bitcoin = { version = "0.29.2", features = ["base64"] }
bip39 = "2.0.0"
Expand Down
1 change: 1 addition & 0 deletions migrations/2024-05-13-234832_create_config/down.sql
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DROP TABLE fedimint;
DROP TABLE profile;
5 changes: 5 additions & 0 deletions migrations/2024-05-13-234832_create_config/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ CREATE TABLE profile (
id CHAR(36) PRIMARY KEY NOT NULL,
seed_words CHAR(255) NOT NULL
);

CREATE TABLE fedimint (
id CHAR(36) PRIMARY KEY NOT NULL,
value BLOB NOT NULL
);
17 changes: 13 additions & 4 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use tokio::sync::RwLock;
use crate::{
bridge::{self, CoreUIMsg, UICoreMsg},
conf::{self, get_mnemonic},
db::DBConnection,
Message,
};
use crate::{
Expand All @@ -38,6 +39,7 @@ struct HarborCore {
mnemonic: Mnemonic,
tx: Sender<Message>,
clients: Arc<RwLock<HashMap<FederationId, FedimintClient>>>,
storage: Arc<dyn DBConnection + Send + Sync>,
stop: Arc<AtomicBool>,
}

Expand Down Expand Up @@ -136,9 +138,14 @@ impl HarborCore {
return Err(anyhow!("Federation already added"));
}

let client =
FedimintClient::new(invite_code, &self.mnemonic, self.network, self.stop.clone())
.await?;
let client = FedimintClient::new(
self.storage.clone(),
invite_code,
&self.mnemonic,
self.network,
self.stop.clone(),
)
.await?;

clients.insert(client.fedimint_client.federation_id(), client);

Expand Down Expand Up @@ -180,12 +187,13 @@ pub fn run_core() -> Subscription<Message> {
"password123".to_string(),
);

let mnemonic = get_mnemonic(db).expect("should get seed");
let mnemonic = get_mnemonic(db.clone()).expect("should get seed");

let stop = Arc::new(AtomicBool::new(false));

// fixme, properly initialize this
let client = FedimintClient::new(
db.clone(),
InviteCode::from_str("fed11qgqzc2nhwden5te0vejkg6tdd9h8gepwvejkg6tdd9h8garhduhx6at5d9h8jmn9wshxxmmd9uqqzgxg6s3evnr6m9zdxr6hxkdkukexpcs3mn7mj3g5pc5dfh63l4tj6g9zk4er").unwrap(),
&mnemonic,
network,
Expand All @@ -203,6 +211,7 @@ pub fn run_core() -> Subscription<Message> {
}

let core = HarborCore {
storage: db,
balance,
tx,
mnemonic,
Expand Down
27 changes: 26 additions & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::db_models::{NewProfile, Profile};
use crate::db_models::{Fedimint, NewFedimint, NewProfile, Profile};
use diesel::{
connection::SimpleConnection,
r2d2::{ConnectionManager, Pool},
Expand Down Expand Up @@ -31,6 +31,15 @@ pub trait DBConnection {

// Inserts a new profile into the DB
fn insert_new_profile(&self, new_profile: NewProfile) -> anyhow::Result<Profile>;

// Inserts a new federation into the DB
fn insert_new_federation(&self, f: NewFedimint) -> anyhow::Result<Fedimint>;

// gets the federation data for a specific federation
fn get_federation_value(&self, id: String) -> anyhow::Result<Option<Vec<u8>>>;

// updates the federation data
fn update_fedimint_data(&self, id: String, value: Vec<u8>) -> anyhow::Result<()>;
}

pub(crate) struct SQLConnection {
Expand All @@ -50,6 +59,22 @@ impl DBConnection for SQLConnection {
let conn = &mut self.db.get()?;
new_profile.insert(conn)
}

fn get_federation_value(&self, id: String) -> anyhow::Result<Option<Vec<u8>>> {
let conn = &mut self.db.get()?;
Fedimint::get_value(conn, id)
}

fn insert_new_federation(&self, f: NewFedimint) -> anyhow::Result<Fedimint> {
let conn = &mut self.db.get()?;
f.insert(conn)
}

fn update_fedimint_data(&self, id: String, value: Vec<u8>) -> anyhow::Result<()> {
let conn = &mut self.db.get()?;
let f = Fedimint { id, value };
f.update(conn)
}
}

#[derive(Debug)]
Expand Down
57 changes: 57 additions & 0 deletions src/db_models/fedimint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::db_models::schema::fedimint;
use diesel::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(
QueryableByName, Queryable, AsChangeset, Serialize, Deserialize, Debug, Clone, PartialEq,
)]
#[diesel(table_name = fedimint)]
pub struct Fedimint {
pub id: String,
pub value: Vec<u8>,
}

impl Fedimint {
pub fn get_value(conn: &mut SqliteConnection, id: String) -> anyhow::Result<Option<Vec<u8>>> {
Ok(fedimint::table
.filter(fedimint::id.eq(id))
.first::<Fedimint>(conn)
.optional()?
.map(|v| v.value))
}

pub fn update(&self, conn: &mut SqliteConnection) -> anyhow::Result<()> {
let _ = diesel::update(fedimint::table)
.filter(fedimint::id.eq(self.id.clone()))
.set(fedimint::value.eq(self.value.clone()))
.execute(conn)?;

Ok(())
}
}

#[derive(Insertable)]
#[diesel(table_name = fedimint)]
pub struct NewFedimint {
pub id: String,
pub value: Vec<u8>,
}

impl From<&NewFedimint> for Fedimint {
fn from(new_fedimint: &NewFedimint) -> Self {
Fedimint {
id: new_fedimint.id.clone(),
value: new_fedimint.value.clone(),
}
}
}

impl NewFedimint {
pub fn insert(&self, conn: &mut SqliteConnection) -> anyhow::Result<Fedimint> {
let _ = diesel::insert_into(fedimint::table)
.values(self)
.execute(conn)?;

Ok(self.into())
}
}
3 changes: 3 additions & 0 deletions src/db_models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
pub mod profile;
pub use profile::*;

pub mod fedimint;
pub use fedimint::*;

pub mod schema;
12 changes: 12 additions & 0 deletions src/db_models/schema.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
// @generated automatically by Diesel CLI.

diesel::table! {
fedimint (id) {
id -> Text,
value -> Binary,
}
}

diesel::table! {
profile (id) {
id -> Text,
seed_words -> Text,
}
}

diesel::allow_tables_to_appear_in_same_query!(
fedimint,
profile,
);
Loading

0 comments on commit bf47e8f

Please sign in to comment.