Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into cctl-modify-verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
marijanp committed Jul 23, 2024
2 parents f660259 + b718d64 commit ff60661
Show file tree
Hide file tree
Showing 38 changed files with 1,175 additions and 105 deletions.
365 changes: 346 additions & 19 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ members = [
"kairos-tx",
"kairos-prover/kairos-circuit-logic",
"demo-contract-tests",
"kairos-contracts/demo-contract/contract-utils"
"kairos-contracts/demo-contract/contract-utils",
"kairos-data"
]

[workspace.package]
Expand Down
6 changes: 3 additions & 3 deletions flake.lock

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

22 changes: 22 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,22 @@
'';
};

kairosServerMigrations = pkgs.runCommand "kairos-server-migrations" { } ''
mkdir $out
ln -s ${./kairos-data}
for dir in ${./kairos-data/migrations}/*/; do
# Check if up.sql exists
if [ -f "''${dir}up.sql" ]; then
# Get the directory name without the trailing slash
dir_name=$(basename "$dir")
new_file="$out/''${dir_name}-up.sql"
ln -s "''${dir}up.sql" "$new_file"
else
echo "No up.sql found in $dir"
fi
done
'';

kairosNodeAttrs = {
src = lib.fileset.toSource {
root = ./.;
Expand All @@ -137,6 +153,7 @@
./demo-contract-tests
./kairos-cli
./kairos-crypto
./kairos-data
./kairos-server
./kairos-test-utils
./kairos-tx
Expand All @@ -150,20 +167,23 @@
nativeBuildInputs = [ pkgs.binaryen pkgs.lld pkgs.llvmPackages.bintools pkgs.pkg-config ];
buildInputs = with pkgs; [
openssl.dev
postgresql.lib
] ++ lib.optionals stdenv.isDarwin [
libiconv
darwin.apple_sdk.frameworks.Security
darwin.apple_sdk.frameworks.SystemConfiguration
];
checkInputs = [
inputs'.cctl.packages.cctl
pkgs.postgresql
];

CASPER_CHAIN_NAME = "cspr-dev-cctl";
PATH_TO_WASM_BINARIES = "${self'.packages.kairos-contracts}/bin";
PATH_TO_SESSION_BINARIES = "${self'.packages.kairos-session-code}/bin";
CCTL_CONFIG = "${cctlConfig.config}";
CCTL_CHAINSPEC = "${cctlConfig.chainspec}";
KAIROS_SERVER_MIGRATIONS = kairosServerMigrations;

meta.mainProgram = "kairos-server";
};
Expand All @@ -178,7 +198,9 @@
PATH_TO_SESSION_BINARIES = "${self'.packages.kairos-session-code}/bin";
CCTL_CONFIG = "${cctlConfig.config}";
CCTL_CHAINSPEC = "${cctlConfig.chainspec}";
KAIROS_SERVER_MIGRATIONS = kairosServerMigrations;
inputsFrom = [ self'.packages.kairos self'.packages.kairos-contracts ];
packages = [ pkgs.diesel-cli ];
};

packages = {
Expand Down
3 changes: 2 additions & 1 deletion kairos-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ license.workspace = true

[features]
default = ["demo"]
all-tests = ["cctl-tests"]
all-tests = ["cctl-tests", "database"]
cctl-tests = []
demo = ["dep:kairos-test-utils", "dep:tokio", "dep:dotenvy"]
database = ["kairos-server/database", "kairos-test-utils/database"]

[dependencies]
dotenvy = { version = "0.15", optional = true }
Expand Down
20 changes: 16 additions & 4 deletions kairos-cli/tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use std::path::PathBuf;

use casper_client::types::DeployHash;
use casper_client_hashing::Digest;
#[cfg(feature = "database")]
use kairos_test_utils::postgres::PostgresDB;
use kairos_test_utils::{
cctl::{CCTLNetwork, DeployableContract},
kairos,
kairos::Kairos,
};

// Helper function to get the path to a fixture file
Expand Down Expand Up @@ -38,6 +40,9 @@ async fn deposit_successful_with_ed25519() {

let contract_hash = network.get_contract_hash_for(hash_name);

#[cfg(feature = "database")]
let postgres = PostgresDB::run(None).unwrap();

let node = network
.nodes
.first()
Expand All @@ -50,9 +55,16 @@ async fn deposit_successful_with_ed25519() {
))
.unwrap();

let kairos = kairos::Kairos::run(&casper_rpc_url, &casper_sse_url, None, Some(contract_hash))
.await
.unwrap();
let kairos = Kairos::run(
&casper_rpc_url,
&casper_sse_url,
None,
Some(contract_hash),
#[cfg(feature = "database")]
&postgres.connection.clone().into(),
)
.await
.unwrap();

tokio::task::spawn_blocking(move || {
let depositor_secret_key_path = network
Expand Down
32 changes: 16 additions & 16 deletions kairos-contracts/Cargo.lock

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

25 changes: 25 additions & 0 deletions kairos-data/Cargo.toml
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"]
9 changes: 9 additions & 0 deletions kairos-data/diesel.toml
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"
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 kairos-data/migrations/00000000000000_diesel_initial_setup/up.sql
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;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE transactions;
10 changes: 10 additions & 0 deletions kairos-data/migrations/2024-06-13-092829_transactions/up.sql
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)
);
16 changes: 16 additions & 0 deletions kairos-data/src/errors.rs
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),
}
Loading

0 comments on commit ff60661

Please sign in to comment.