-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from cspr-rad/cherry-pick-da
Data Availability.
- Loading branch information
Showing
30 changed files
with
1,068 additions
and
39 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
[package] | ||
name = "kairos-data" | ||
version.workspace = true | ||
edition.workspace = true | ||
license.workspace = true | ||
|
||
[dependencies] | ||
diesel = { version = "2.1", features = ["postgres", "chrono", "numeric"] } | ||
deadpool-diesel = { version = "0.6", features = ["postgres", "tracing"]} | ||
diesel_migrations = { version = "2.1", features = ["postgres"], optional = true } | ||
tokio = "1" | ||
tracing = "0.1" | ||
thiserror = "1.0" | ||
deadpool = "0.12" | ||
chrono = { version = "0.4", features = ["serde"] } | ||
bigdecimal = { version = "0.4", features = ["serde"] } | ||
serde_json = "1.0" | ||
serde = "1.0" | ||
kairos-circuit-logic = { path = "../kairos-prover/kairos-circuit-logic", features = ["serde", "asn1"] } | ||
hex = "0.4" | ||
anyhow = "1.0" | ||
diesel-derive-enum = { version = "2.1", features = ["postgres"] } | ||
|
||
[features] | ||
migrations = ["diesel_migrations"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# For documentation on how to configure this file, | ||
# see https://diesel.rs/guides/configuring-diesel-cli | ||
|
||
[print_schema] | ||
file = "src/schema.rs" | ||
custom_type_derives = ["diesel::query_builder::QueryId"] | ||
|
||
[migrations_directory] | ||
dir = "migrations" |
6 changes: 6 additions & 0 deletions
6
kairos-data/migrations/00000000000000_diesel_initial_setup/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-- This file was automatically created by Diesel to setup helper functions | ||
-- and other internal bookkeeping. This file is safe to edit, any future | ||
-- changes will be added to existing projects as new migrations. | ||
|
||
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); | ||
DROP FUNCTION IF EXISTS diesel_set_updated_at(); |
36 changes: 36 additions & 0 deletions
36
kairos-data/migrations/00000000000000_diesel_initial_setup/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
-- This file was automatically created by Diesel to setup helper functions | ||
-- and other internal bookkeeping. This file is safe to edit, any future | ||
-- changes will be added to existing projects as new migrations. | ||
|
||
|
||
|
||
|
||
-- Sets up a trigger for the given table to automatically set a column called | ||
-- `updated_at` whenever the row is modified (unless `updated_at` was included | ||
-- in the modified columns) | ||
-- | ||
-- # Example | ||
-- | ||
-- ```sql | ||
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW()); | ||
-- | ||
-- SELECT diesel_manage_updated_at('users'); | ||
-- ``` | ||
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ | ||
BEGIN | ||
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s | ||
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ | ||
BEGIN | ||
IF ( | ||
NEW IS DISTINCT FROM OLD AND | ||
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at | ||
) THEN | ||
NEW.updated_at := current_timestamp; | ||
END IF; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE transactions; |
10 changes: 10 additions & 0 deletions
10
kairos-data/migrations/2024-06-13-092829_transactions/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE TYPE transaction AS ENUM ('deposit', 'transfer', 'withdrawal'); | ||
CREATE TABLE transactions ( | ||
"timestamp" timestamp DEFAULT CURRENT_TIMESTAMP, | ||
public_key varchar NOT NULL, | ||
nonce numeric, | ||
trx transaction NOT NULL, | ||
amount numeric NOT NULL, | ||
recipient varchar, | ||
PRIMARY KEY ("timestamp", amount, public_key) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use deadpool_diesel::Error as DDError; | ||
use deadpool_diesel::InteractError as DDIError; | ||
use diesel::result::Error as DieselError; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum DBError { | ||
#[error("There was an issue connecting to the database: {0}")] | ||
ConnectionError(#[from] DDError), | ||
#[error("There was an error in sync code executing in a separate thread: {0}")] | ||
InteractError(#[from] DDIError), | ||
#[error("Pool error: {0}")] | ||
PoolError(#[from] deadpool::managed::PoolError<deadpool_diesel::Error>), | ||
#[error("Diesel error: {0}")] | ||
DieselError(#[from] DieselError), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use deadpool_diesel::postgres::{Manager, Runtime}; | ||
use diesel::{prelude::*, select, sql_types::Text}; | ||
use tracing::{debug, info}; | ||
|
||
#[cfg(feature = "migrations")] | ||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; | ||
|
||
pub use deadpool_diesel::postgres::Pool; | ||
pub use diesel::{insert_into, prelude}; | ||
|
||
pub mod errors; | ||
pub mod schema; | ||
pub mod transaction; | ||
|
||
#[cfg(feature = "migrations")] | ||
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!(); | ||
|
||
pub async fn new(conn_str: &str) -> Result<Pool, errors::DBError> { | ||
info!("Connecting to database: {}", conn_str); | ||
let manager = Manager::new(conn_str, Runtime::Tokio1); | ||
debug!("Setup DB manager."); | ||
let conn_pool = Pool::builder(manager).max_size(8).build().unwrap(); | ||
debug!("Initialized connection pool."); | ||
#[cfg(feature = "migrations")] | ||
run_migrations(&conn_pool).await; | ||
let conn = conn_pool.get().await?; | ||
let result = conn // do a quick query to test connection | ||
.interact(|conn| { | ||
let query = select("Hello world!".into_sql::<Text>()); | ||
query.get_result::<String>(conn) | ||
}) | ||
.await??; | ||
assert!(result == "Hello world!"); | ||
info!("Created connection pool!"); | ||
Ok(conn_pool) | ||
} | ||
|
||
// Function to run database migrations | ||
#[cfg(feature = "migrations")] | ||
async fn run_migrations(pool: &Pool) { | ||
let conn = pool.get().await.unwrap(); | ||
info!("Running migrations..."); | ||
conn.interact(|conn| { | ||
conn.run_pending_migrations(MIGRATIONS).map(|migrations| { | ||
for migration in migrations { | ||
info!("Applying migration: {}", migration) | ||
} | ||
}) | ||
}) | ||
.await | ||
.unwrap() | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// @generated automatically by Diesel CLI. | ||
|
||
pub mod sql_types { | ||
#[derive(diesel::query_builder::QueryId, diesel::sql_types::SqlType)] | ||
#[diesel(postgres_type(name = "transaction"))] | ||
pub struct Transaction; | ||
} | ||
|
||
diesel::table! { | ||
use diesel::sql_types::*; | ||
use super::sql_types::Transaction; | ||
|
||
transactions (timestamp, amount, public_key) { | ||
timestamp -> Timestamp, | ||
public_key -> Varchar, | ||
nonce -> Nullable<Numeric>, | ||
trx -> Transaction, | ||
amount -> Numeric, | ||
recipient -> Nullable<Varchar>, | ||
} | ||
} |
Oops, something went wrong.