Skip to content

Commit

Permalink
Collect and set Winternitz public keys in aggregator (#374)
Browse files Browse the repository at this point in the history
* aggregator-rpc: Add initial collect and set Winternitz pks.

* servers: Fix aggregator no watchtower endpoint.

* sql: Add winternitz_public_keys table.

* database: Add save_winternitz_public_key and get_winternitz_public_key.

* database: Accept vector of `WinternitzPublicKey`s in save_get_winternitz_public_key.

* verifier_rpc: Implement set_watchtower.

* aggregator: Fix wrong error message.

* tests: Move 2 aggregator RPC tests to aggregator unit tests.

* tests: Readd grpc_flow test.

* errors: Add new borsch error.

* servers: Small fixes.

* rpc: Add WinternitzPubkey wrapper.

* aggregator-rpc: Change log types in setup and add aggregator_setup_winternitz_public_keys test.

* schema: Rename winternitz_public_keys index to operator_id.

* database: Save both operator and watchtower id in winternitz_public_keys.

* aggregator: Fix wpk per time_tx bug.

* schema: Rename field in winternitz_public_keys.

* Mock crate separation from binaries (#375)

* mock_macro: Add initial.

* mock_macro: Add initialize_database.

* actor: Pilot mock_macro usage.

* mock_macro: Add needed imports for integration tests.

* tests: Use new mock_macro in integration tests.

* mock_macro: Add create_actors.

* mock_macro: Use create_actors everywhere and delete create_actors_grpc.

* mock_macro: Use initialize_database everywhere.

* mock_macro: Use create_test_config_with_thread_name in everywhere.

* test_utils: Rename mock_macro.

* cargo: Delete bin entry of `all_servers`.

* test_utils: Remove create_database and drop_database to add them in initialize_database.

* test_utils: Use get_postgresql_url in initialize_database.

* JsonRPC endpoint removals (#377)

* jsonrpc: Initial remove.

* operator: Remove old rpc test.

* verifier: Convert functions to pub.

* tests: Delete old file.

* cargo: Remove unused features for jsonrpsee.
  • Loading branch information
ceyhunsen authored Dec 16, 2024
1 parent 8037e9f commit bd5d8a0
Show file tree
Hide file tree
Showing 38 changed files with 1,667 additions and 2,172 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ serde_json = "1.0.128"
thiserror = "1.0.64"
tracing = { version = "0.1.40", default-features = false }
tracing-subscriber = { version = "0.3.18", features = ["json"] }
jsonrpsee = "0.22.5"
jsonrpsee = { version = "0.22.5", default-features = false }
async-trait = "0.1.83"
clap = "4.5.20"
toml = "0.8.19"
Expand Down
7 changes: 1 addition & 6 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ thiserror = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
tokio = { workspace = true, features = ["full"] }
jsonrpsee = { workspace = true, features = ["server", "http-client", "macros"] }
jsonrpsee = { workspace = true, features = ["http-client"] }
async-trait = { workspace = true }
futures = { workspace = true }
clap = { workspace = true, features = ["derive"] }
Expand Down Expand Up @@ -50,11 +50,6 @@ testing = []
name = "server"
path = "src/bin/server.rs"

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

[[bin]]
name = "config_generator"
path = "src/bin/config_generator.rs"
Expand Down
12 changes: 8 additions & 4 deletions core/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,11 @@ impl Actor {
#[cfg(test)]
mod tests {
use super::Actor;
use crate::config::BridgeConfig;
use crate::utils::initialize_logger;
use crate::{
actor::WinternitzDerivationPath, builder::transaction::TxHandler,
mock::database::create_test_config_with_thread_name,
create_test_config_with_thread_name, database::Database, initialize_database,
};
use bitcoin::{
absolute::Height, transaction::Version, Amount, Network, OutPoint, Transaction, TxIn, TxOut,
Expand All @@ -345,7 +347,9 @@ mod tests {
treepp::script,
};
use secp256k1::{rand, Secp256k1, SecretKey};
use std::env;
use std::str::FromStr;
use std::thread;

/// Returns a valid [`TxHandler`].
fn create_valid_mock_tx_handler(actor: &Actor) -> TxHandler {
Expand Down Expand Up @@ -508,7 +512,7 @@ mod tests {

#[tokio::test]
async fn derive_winternitz_pk_uniqueness() {
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let config = create_test_config_with_thread_name!("test_config.toml", None);
let actor = Actor::new(
config.secret_key,
config.winternitz_secret_key,
Expand All @@ -527,7 +531,7 @@ mod tests {

#[tokio::test]
async fn derive_winternitz_pk_fixed_pk() {
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let config = create_test_config_with_thread_name!("test_config.toml", None);
let actor = Actor::new(
config.secret_key,
Some(
Expand All @@ -549,7 +553,7 @@ mod tests {

#[tokio::test]
async fn sign_winternitz_signature() {
let config = create_test_config_with_thread_name("test_config.toml", None).await;
let config = create_test_config_with_thread_name!("test_config.toml", None);
let actor = Actor::new(
config.secret_key,
Some(
Expand Down
65 changes: 13 additions & 52 deletions core/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ use crate::{
clementine::{
clementine_operator_client::ClementineOperatorClient,
clementine_verifier_client::ClementineVerifierClient,
clementine_watchtower_client::ClementineWatchtowerClient,
},
},
traits::rpc::AggregatorServer,
utils::handle_taproot_witness_new,
ByteArray32, ByteArray66, EVMAddress, UTXO,
};
use async_trait::async_trait;
use bitcoin::{address::NetworkUnchecked, Address, OutPoint};
use bitcoin::{hashes::Hash, Txid};
use bitcoincore_rpc::RawTx;
Expand All @@ -40,6 +39,7 @@ pub struct Aggregator {
pub(crate) nofn_xonly_pk: secp256k1::XOnlyPublicKey,
pub(crate) verifier_clients: Vec<ClementineVerifierClient<tonic::transport::Channel>>,
pub(crate) operator_clients: Vec<ClementineOperatorClient<tonic::transport::Channel>>,
pub(crate) watchtower_clients: Vec<ClementineWatchtowerClient<tonic::transport::Channel>>,
}

impl Aggregator {
Expand Down Expand Up @@ -73,12 +73,23 @@ impl Aggregator {
let operator_clients =
rpc::get_clients(operator_endpoints, ClementineOperatorClient::connect).await?;

let watchtower_endpoints =
config
.watchtower_endpoints
.clone()
.ok_or(BridgeError::ConfigError(
"Couldn't find watchtower endpoints in config file!".to_string(),
))?;
let watchtower_clients =
rpc::get_clients(watchtower_endpoints, ClementineWatchtowerClient::connect).await?;

Ok(Aggregator {
db,
config,
nofn_xonly_pk,
verifier_clients,
operator_clients,
watchtower_clients,
})
}

Expand Down Expand Up @@ -352,53 +363,3 @@ impl Aggregator {
Ok((move_tx_handler.tx.raw_hex(), txid))
}
}

#[async_trait]
impl AggregatorServer for Aggregator {
async fn aggregate_pub_nonces_rpc(
&self,
pub_nonces: Vec<Vec<MuSigPubNonce>>,
) -> Result<Vec<MuSigAggNonce>, BridgeError> {
self.aggregate_pub_nonces(pub_nonces).await
}

async fn aggregate_slash_or_take_sigs_rpc(
&self,
deposit_outpoint: OutPoint,
kickoff_utxos: Vec<UTXO>,
agg_nonces: Vec<MuSigAggNonce>,
partial_sigs: Vec<Vec<MuSigPartialSignature>>,
) -> Result<Vec<schnorr::Signature>, BridgeError> {
self.aggregate_slash_or_take_sigs(deposit_outpoint, kickoff_utxos, agg_nonces, partial_sigs)
.await
}

async fn aggregate_operator_take_sigs_rpc(
&self,
deposit_outpoint: OutPoint,
kickoff_utxos: Vec<UTXO>,
agg_nonces: Vec<MuSigAggNonce>,
partial_sigs: Vec<Vec<MuSigPartialSignature>>,
) -> Result<Vec<schnorr::Signature>, BridgeError> {
self.aggregate_operator_take_sigs(deposit_outpoint, kickoff_utxos, agg_nonces, partial_sigs)
.await
}

async fn aggregate_move_tx_sigs_rpc(
&self,
deposit_outpoint: OutPoint,
recovery_taproot_address: Address<NetworkUnchecked>,
evm_address: EVMAddress,
agg_nonce: MuSigAggNonce,
partial_sigs: Vec<MuSigPartialSignature>,
) -> Result<(String, Txid), BridgeError> {
self.aggregate_move_tx_sigs(
deposit_outpoint,
recovery_taproot_address,
evm_address,
agg_nonce,
partial_sigs,
)
.await
}
}
49 changes: 0 additions & 49 deletions core/src/bin/all_servers.rs

This file was deleted.

115 changes: 58 additions & 57 deletions core/src/bin/server.rs
Original file line number Diff line number Diff line change
@@ -1,61 +1,62 @@
use clementine_core::servers::create_aggregator_server;
use clementine_core::servers::create_operator_server;
use clementine_core::servers::create_verifier_server;
use clementine_core::utils::get_configuration_for_binaries;
use clementine_core::{database::Database, extended_rpc::ExtendedRpc};
use std::process::exit;
// use clementine_core::servers::create_aggregator_server;
// use clementine_core::servers::create_operator_server;
// use clementine_core::servers::create_verifier_server;
// use clementine_core::utils::get_configuration_for_binaries;
// use clementine_core::{database::Database, extended_rpc::ExtendedRpc};
// use std::process::exit;

#[tokio::main]
async fn main() {
let (mut config, args) = get_configuration_for_binaries();

if !args.verifier_server && !args.operator_server && !args.aggregator_server {
eprintln!("No servers are specified. Please specify one.");
exit(1);
}

let rpc = ExtendedRpc::new(
config.bitcoin_rpc_url.clone(),
config.bitcoin_rpc_user.clone(),
config.bitcoin_rpc_password.clone(),
)
.await;

Database::run_schema_script(&config).await.unwrap();

let mut handles = vec![];

if args.verifier_server {
handles.push(
create_verifier_server(config.clone(), rpc.clone())
.await
.unwrap()
.1
.stopped(),
);
config.port += 1;

println!("Verifier server is started.");
}

if args.operator_server {
handles.push(
create_operator_server(config.clone(), rpc.clone())
.await
.unwrap()
.1
.stopped(),
);
config.port += 1;

println!("Operator server is started.");
}

if args.aggregator_server {
handles.push(create_aggregator_server(config).await.unwrap().1.stopped());

println!("Aggregator server is started.");
}

futures::future::join_all(handles).await;
panic!("grpc switch in progress. please inform us if you get this error.")
// let (mut config, args) = get_configuration_for_binaries();

// if !args.verifier_server && !args.operator_server && !args.aggregator_server {
// eprintln!("No servers are specified. Please specify one.");
// exit(1);
// }

// let rpc = ExtendedRpc::new(
// config.bitcoin_rpc_url.clone(),
// config.bitcoin_rpc_user.clone(),
// config.bitcoin_rpc_password.clone(),
// )
// .await;

// Database::run_schema_script(&config).await.unwrap();

// let mut handles = vec![];

// if args.verifier_server {
// handles.push(
// create_verifier_server(config.clone(), rpc.clone())
// .await
// .unwrap()
// .1
// .stopped(),
// );
// config.port += 1;

// println!("Verifier server is started.");
// }

// if args.operator_server {
// handles.push(
// create_operator_server(config.clone(), rpc.clone())
// .await
// .unwrap()
// .1
// .stopped(),
// );
// config.port += 1;

// println!("Operator server is started.");
// }

// if args.aggregator_server {
// handles.push(create_aggregator_server(config).await.unwrap().1.stopped());

// println!("Aggregator server is started.");
// }

// futures::future::join_all(handles).await;
}
3 changes: 3 additions & 0 deletions core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ pub struct BridgeConfig {
pub verifier_endpoints: Option<Vec<String>>,
/// Operator endpoint. For the aggregator only
pub operator_endpoints: Option<Vec<String>>,
/// Watchtower endpoint. For the aggregator only
pub watchtower_endpoints: Option<Vec<String>>,
/// PostgreSQL database host address.
pub db_host: String,
/// PostgreSQL database port.
Expand Down Expand Up @@ -153,6 +155,7 @@ impl Default for BridgeConfig {
all_operators_secret_keys: None,
verifier_endpoints: None,
operator_endpoints: None,
watchtower_endpoints: None,
db_host: "127.0.0.1".to_string(),
db_port: 5432,
db_user: "postgres".to_string(),
Expand Down
Loading

0 comments on commit bd5d8a0

Please sign in to comment.