Skip to content

Commit

Permalink
Unify server binaries (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekrembal authored Aug 30, 2024
1 parent ccb2999 commit d783f65
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 144 deletions.
23 changes: 10 additions & 13 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,22 @@ musig2 = { workspace = true }

[features]
default = []
poc = []
mock_rpc = []
verifier_server = []
operator_server = []
aggregator_server = []
testing = []

[[bin]]
name = "verifier"
path = "src/bin/verifier.rs"
name = "server"
path = "src/bin/server.rs"

[[bin]]
name = "operator"
path = "src/bin/operator.rs"

[[bin]]
name = "user_deposit"
path = "src/bin/user_deposit.rs"

[[bin]]
name = "operators_and_verifiers"
path = "src/bin/operators_and_verifiers.rs"
name = "all_servers"
path = "src/bin/all_servers.rs"
required-features = ["testing"]

[[bin]]
name = "config_generator"
path = "src/bin/config_generator.rs"
required-features = ["testing"]
45 changes: 45 additions & 0 deletions core/src/bin/all_servers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use clementine_core::{cli, extended_rpc::ExtendedRpc, servers::create_verifiers_and_operators};

#[tokio::main]
async fn main() {
let config = cli::get_configuration();
let _rpc = ExtendedRpc::<bitcoincore_rpc::Client>::new(
config.bitcoin_rpc_url.clone(),
config.bitcoin_rpc_user.clone(),
config.bitcoin_rpc_password.clone(),
);

let (operator_clients, verifier_clients, aggregator) =
create_verifiers_and_operators("test_config.toml").await;

println!(
"OPERATOR_URLS={}",
operator_clients
.iter()
.map(|(_, _, addr)| format!("http://localhost:{}", addr.port()))
.collect::<Vec<_>>()
.join(",")
);
println!(
"VERIFIER_URLS={}",
verifier_clients
.iter()
.map(|(_, _, addr)| format!("http://localhost:{}", addr.port()))
.collect::<Vec<_>>()
.join(",")
);
println!("AGGREGATOR_URL={}", aggregator.2);

// Stop all servers
for (_, handle, _) in operator_clients {
handle.clone().stopped().await;
}

for (_, handle, _) in verifier_clients {
handle.clone().stopped().await;
}

aggregator.1.stopped().await;

println!("All servers stopped");
}
18 changes: 0 additions & 18 deletions core/src/bin/operator.rs

This file was deleted.

42 changes: 0 additions & 42 deletions core/src/bin/operators_and_verifiers.rs

This file was deleted.

71 changes: 71 additions & 0 deletions core/src/bin/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#[cfg(feature = "aggregator_server")]
use clementine_core::servers::create_aggregator_server;

#[cfg(feature = "operator_server")]
use clementine_core::servers::create_operator_server;

#[cfg(feature = "verifier_server")]
use clementine_core::servers::create_verifier_server;

#[tokio::main]
async fn main() {
#[cfg(any(
feature = "verifier_server",
feature = "operator_server",
feature = "aggregator_server"
))]
{
use clementine_core::{cli, database::common::Database, extended_rpc::ExtendedRpc};

let mut config = cli::get_configuration();
let rpc = ExtendedRpc::<bitcoincore_rpc::Client>::new(
config.bitcoin_rpc_url.clone(),
config.bitcoin_rpc_user.clone(),
config.bitcoin_rpc_password.clone(),
);

let database = Database::new(config.clone()).await.unwrap();
database.init_from_schema().await.unwrap();
database.close().await;

let mut handles = vec![];

#[cfg(feature = "verifier_server")]
{
handles.push(
create_verifier_server(config.clone(), rpc.clone())
.await
.unwrap()
.1
.stopped(),
);
config.port += 1;
}

#[cfg(feature = "operator_server")]
{
handles.push(
create_operator_server(config.clone(), rpc.clone())
.await
.unwrap()
.1
.stopped(),
);
config.port += 1;
}

#[cfg(feature = "aggregator_server")]
handles.push(create_aggregator_server(config).await.unwrap().1.stopped());

futures::future::join_all(handles).await;
}

#[cfg(not(any(
feature = "verifier_server",
feature = "operator_server",
feature = "aggregator_server"
)))]
{
println!("No server features are enabled. Exiting...");
}
}
21 changes: 0 additions & 21 deletions core/src/bin/user_deposit.rs

This file was deleted.

18 changes: 0 additions & 18 deletions core/src/bin/verifier.rs

This file was deleted.

16 changes: 8 additions & 8 deletions core/src/database/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use bitcoin::address::NetworkUnchecked;
use bitcoin::{Address, OutPoint, Txid};
use secp256k1::schnorr;
use sqlx::{Pool, Postgres, QueryBuilder};
use std::fs;

use super::wrapper::{AddressDB, EVMAddressDB, OutPointDB, SignatureDB, TxOutDB, TxidDB, UTXODB};

Expand Down Expand Up @@ -103,18 +102,19 @@ impl Database {
Ok(config)
}

/// Runs given SQL file to database. Database connection must be established
/// Runs given SQL string to database. Database connection must be established
/// before calling this function.
pub async fn run_sql_file(&self, sql_file: &str) -> Result<(), BridgeError> {
let contents = fs::read_to_string(sql_file).unwrap();

sqlx::raw_sql(contents.as_str())
.execute(&self.connection)
.await?;
pub async fn run_sql(&self, raw_sql: &str) -> Result<(), BridgeError> {
sqlx::raw_sql(raw_sql).execute(&self.connection).await?;

Ok(())
}

pub async fn init_from_schema(&self) -> Result<(), BridgeError> {
let schema = include_str!("../../../scripts/schema.sql");
self.run_sql(schema).await
}

/// Starts a database transaction.
///
/// Return value can be used for committing changes. If not committed,
Expand Down
6 changes: 1 addition & 5 deletions core/src/mock/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ macro_rules! create_test_config {
let config = Database::create_database(config, &$db_name).await.unwrap();

let database = Database::new(config.clone()).await.unwrap();
database
.run_sql_file("../scripts/schema.sql")
.await
.unwrap();

database.init_from_schema().await.unwrap();
database.close().await;

config
Expand Down
40 changes: 21 additions & 19 deletions scripts/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ BEGIN;

-- Verifier table for deposit details
/* This table holds the information related to a deposit. */
create table deposit_infos (
create table if not exists deposit_infos (
id serial primary key,
deposit_outpoint text not null check (deposit_outpoint ~ '^[a-fA-F0-9]{64}:(0|[1-9][0-9]{0,9})$'),
recovery_taproot_address text not null,
Expand All @@ -11,7 +11,7 @@ create table deposit_infos (
);

-- Verifier table for operator signatures, operator will read from here and send the take tx
create table operator_take_sigs (
create table if not exists operator_take_sigs (
id serial primary key,
deposit_outpoint text not null check (deposit_outpoint ~ '^[a-fA-F0-9]{64}:(0|[1-9][0-9]{0,9})$'),
bridge_fund_txout text not null,
Expand All @@ -26,7 +26,7 @@ For each deposit, we have (2 + num_operators) nonce triples. The first triple is
move_commit_tx, the second triple is for move_reveal_tx, and the rest is for operator_takes_tx
for each operator. Also for each triple, we hold the sig_hash to be signed to prevent reuse
of the nonces. */
create table nonces (
create table if not exists nonces (
idx serial primary key,
deposit_outpoint text not null check (deposit_outpoint ~ '^[a-fA-F0-9]{64}:(0|[1-9][0-9]{0,9})$'),
pub_nonce bytea not null check (length(pub_nonce) = 66),
Expand All @@ -47,15 +47,24 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

-- Create the trigger
CREATE TRIGGER prevent_sighash_update_trigger
BEFORE UPDATE ON nonces
FOR EACH ROW
EXECUTE FUNCTION prevent_sighash_update();
-- Create the trigger if not exists
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_trigger
WHERE tgname = 'prevent_sighash_update_trigger'
) THEN
CREATE TRIGGER prevent_sighash_update_trigger
BEFORE UPDATE ON nonces
FOR EACH ROW
EXECUTE FUNCTION prevent_sighash_update();
END IF;
END $$;

-- Verifier table for kickoff for deposits
/* This table holds the kickoff utxos sent by the operators for each deposit. */
create table deposit_kickoff_utxos (
create table if not exists deposit_kickoff_utxos (
id serial primary key,
deposit_outpoint text not null check (deposit_outpoint ~ '^[a-fA-F0-9]{64}:(0|[1-9][0-9]{0,9})$'),
kickoff_utxo jsonb not null,
Expand All @@ -67,7 +76,7 @@ create table deposit_kickoff_utxos (

-- Operator table for kickoff utxo and funding utxo for deposits
/* This table holds the funding utxos sent by the operators for each deposit. */
create table deposit_kickoff_generator_txs (
create table if not exists deposit_kickoff_generator_txs (
id serial primary key,
txid text not null check (txid ~ '^[a-fA-F0-9]{64}'),
raw_signed_tx text not null,
Expand All @@ -79,24 +88,17 @@ create table deposit_kickoff_generator_txs (

-- Operator table for kickoff utxo related to deposits
/* This table holds the kickoff utxos sent by the operators for each deposit. */
create table operators_kickoff_utxo (
create table if not exists operators_kickoff_utxo (
deposit_outpoint text primary key not null check (deposit_outpoint ~ '^[a-fA-F0-9]{64}:(0|[1-9][0-9]{0,9})$'),
kickoff_utxo jsonb not null,
created_at timestamp not null default now()
);

-- Operator table for funding utxo used for deposits
create table funding_utxos (
create table if not exists funding_utxos (
id serial primary key,
funding_utxo jsonb not null,
created_at timestamp not null default now()
);

-- Verifier table for kickoff merkle roots for deposits
-- create table kickoff_roots (
-- deposit_outpoint text primary key not null check (deposit_outpoint ~ '^[a-fA-F0-9]{64}:(0|[1-9][0-9]{0,9})$'),
-- kickoff_merkle_root text not null check (kickoff_merkle_root ~ '^[a-fA-F0-9]{64}'),
-- created_at timestamp not null default now()
-- );

COMMIT;

0 comments on commit d783f65

Please sign in to comment.