Skip to content

Commit

Permalink
Merge branch 'main' into test-take-sig
Browse files Browse the repository at this point in the history
  • Loading branch information
ozankaymak authored Sep 6, 2024
2 parents d0167e8 + d90cf47 commit 4db363e
Show file tree
Hide file tree
Showing 17 changed files with 622 additions and 236 deletions.
3 changes: 0 additions & 3 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ musig2 = { workspace = true }
[features]
default = []
mock_rpc = []
verifier_server = []
operator_server = []
aggregator_server = []
testing = []

[[bin]]
Expand Down
25 changes: 17 additions & 8 deletions core/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use crate::{
EVMAddress, UTXO,
};
use async_trait::async_trait;
use bitcoin::hashes::Hash;
use bitcoin::{address::NetworkUnchecked, Address, OutPoint, Transaction};
use bitcoin::{address::NetworkUnchecked, Address, OutPoint};
use bitcoin::{hashes::Hash, Txid};
use bitcoincore_rpc::RawTx;
use secp256k1::schnorr;

/// Aggregator struct.
Expand Down Expand Up @@ -51,7 +52,7 @@ impl Aggregator {
operator_xonly_pk: secp256k1::XOnlyPublicKey,
operator_idx: usize,
agg_nonce: &MuSigAggNonce,
partial_sigs: Vec<[u8; 32]>,
partial_sigs: Vec<MuSigPartialSignature>,
) -> Result<[u8; 64], BridgeError> {
let mut tx = TransactionBuilder::create_slash_or_take_tx(
deposit_outpoint,
Expand Down Expand Up @@ -97,7 +98,7 @@ impl Aggregator {
operator_xonly_pk: &secp256k1::XOnlyPublicKey,
operator_idx: usize,
agg_nonce: &MuSigAggNonce,
partial_sigs: Vec<[u8; 32]>,
partial_sigs: Vec<MuSigPartialSignature>,
) -> Result<[u8; 64], BridgeError> {
let move_tx_handler = TransactionBuilder::create_move_tx(
deposit_outpoint,
Expand Down Expand Up @@ -178,7 +179,7 @@ impl Aggregator {
evm_address: &EVMAddress,
recovery_taproot_address: &Address<NetworkUnchecked>,
agg_nonce: &MuSigAggNonce,
partial_sigs: Vec<[u8; 32]>,
partial_sigs: Vec<MuSigPartialSignature>,
) -> Result<[u8; 64], BridgeError> {
let mut tx = TransactionBuilder::create_move_tx(
deposit_outpoint,
Expand Down Expand Up @@ -231,6 +232,13 @@ impl Aggregator {
agg_nonces: Vec<MuSigAggNonce>,
partial_sigs: Vec<Vec<MuSigPartialSignature>>,
) -> Result<Vec<schnorr::Signature>, BridgeError> {
tracing::debug!(
"Aggregate slash or take sigs called with inputs: {:?}\n {:?}\n{:?}\n{:?}",
deposit_outpoint,
kickoff_utxos,
agg_nonces,
partial_sigs
);
let mut slash_or_take_sigs = Vec::new();
for i in 0..partial_sigs[0].len() {
let agg_sig = self.aggregate_slash_or_take_partial_sigs(
Expand Down Expand Up @@ -280,7 +288,7 @@ impl Aggregator {
evm_address: EVMAddress,
agg_nonce: MuSigAggNonce,
partial_sigs: Vec<MuSigPartialSignature>,
) -> Result<Transaction, BridgeError> {
) -> Result<(String, Txid), BridgeError> {
let agg_move_tx_final_sig = self.aggregate_move_partial_sigs(
deposit_outpoint,
&evm_address,
Expand All @@ -303,7 +311,8 @@ impl Aggregator {
let move_tx_witness_elements = vec![move_tx_sig.serialize().to_vec()];
handle_taproot_witness_new(&mut move_tx_handler, &move_tx_witness_elements, 0, Some(0))?;

Ok(move_tx_handler.tx)
let txid = move_tx_handler.tx.compute_txid();
Ok((move_tx_handler.tx.raw_hex(), txid))
}
}

Expand Down Expand Up @@ -345,7 +354,7 @@ impl AggregatorServer for Aggregator {
evm_address: EVMAddress,
agg_nonce: MuSigAggNonce,
partial_sigs: Vec<MuSigPartialSignature>,
) -> Result<Transaction, BridgeError> {
) -> Result<(String, Txid), BridgeError> {
self.aggregate_move_tx_sigs(
deposit_outpoint,
recovery_taproot_address,
Expand Down
17 changes: 13 additions & 4 deletions core/src/bin/all_servers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use clementine_core::{cli, extended_rpc::ExtendedRpc, servers::create_verifiers_and_operators};
use clementine_core::{
cli, extended_rpc::ExtendedRpc, musig2::AggregateFromPublicKeys,
servers::create_verifiers_and_operators,
};

#[tokio::main]
async fn main() {
Expand All @@ -16,19 +19,25 @@ async fn main() {
"OPERATOR_URLS={}",
operator_clients
.iter()
.map(|(_, _, addr)| format!("http://localhost:{}", addr.port()))
.map(|(_, _, addr)| format!("http://127.0.0.1:{}", addr.port()))
.collect::<Vec<_>>()
.join(",")
);
println!(
"VERIFIER_URLS={}",
verifier_clients
.iter()
.map(|(_, _, addr)| format!("http://localhost:{}", addr.port()))
.map(|(_, _, addr)| format!("http://127.0.0.1:{}", addr.port()))
.collect::<Vec<_>>()
.join(",")
);
println!("AGGREGATOR_URL={}", aggregator.2);
let xonly =
secp256k1::XOnlyPublicKey::from_musig2_pks(config.verifiers_public_keys, None, false);
println!(
"AGGREGATOR_URL={}",
format!("http://127.0.0.1:{}", aggregator.2.port())
);
println!("VERIFIER_PKS={}", xonly.to_string());

// Stop all servers
for (_, handle, _) in operator_clients {
Expand Down
103 changes: 49 additions & 54 deletions core/src/bin/server.rs
Original file line number Diff line number Diff line change
@@ -1,71 +1,66 @@
#[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;
use clementine_core::{cli, database::common::Database, extended_rpc::ExtendedRpc};
use std::process::exit;

#[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 args = match cli::parse() {
Ok(args) => args,
Err(e) => {
eprintln!("{e}");
exit(1);
}
};

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 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 database = Database::new(config.clone()).await.unwrap();
database.init_from_schema().await.unwrap();
database.close().await;

let mut handles = vec![];
let mut handles = vec![];

#[cfg(feature = "verifier_server")]
{
handles.push(
create_verifier_server(config.clone(), rpc.clone())
.await
.unwrap()
.1
.stopped(),
);
config.port += 1;
}
if args.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;
}
tracing::trace!("Verifier servers are started.");
}

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

futures::future::join_all(handles).await;
tracing::trace!("Operator servers are started.");
}

#[cfg(not(any(
feature = "verifier_server",
feature = "operator_server",
feature = "aggregator_server"
)))]
{
println!("No server features are enabled. Exiting...");
if args.aggregator_server {
handles.push(create_aggregator_server(config).await.unwrap().1.stopped());
}

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

futures::future::join_all(handles).await;
}
9 changes: 9 additions & 0 deletions core/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ use std::process::exit;
pub struct Args {
/// TOML formatted configuration file.
pub config_file: PathBuf,
/// Enable verifier server.
#[clap(short, long)]
pub verifier_server: bool,
/// Enable operator server.
#[clap(short, long)]
pub operator_server: bool,
/// Enable aggregator server.
#[clap(short, long)]
pub aggregator_server: bool,
}

/// Parse all the command line arguments and generate a `BridgeConfig`.
Expand Down
4 changes: 2 additions & 2 deletions core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ impl Default for BridgeConfig {
port: 3030,
secret_key: secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng()),
verifiers_public_keys: vec![],
num_verifiers: 5,
num_verifiers: 7,
operators_xonly_pks: vec![],
num_operators: 5,
num_operators: 3,
user_takes_after: 5,
operator_takes_after: 5,
bridge_amount_sats: 100_000_000,
Expand Down
Loading

0 comments on commit 4db363e

Please sign in to comment.