Skip to content

Commit

Permalink
Remove unnecessary config files, apply review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ozankaymak committed Sep 2, 2024
1 parent 85c7076 commit dec0c5b
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 262 deletions.
42 changes: 23 additions & 19 deletions core/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::{
use async_trait::async_trait;
use bitcoin::hashes::Hash;
use bitcoin::{address::NetworkUnchecked, Address, OutPoint, Transaction};
use bitcoincore_rpc::RawTx;
use secp256k1::schnorr;

/// Aggregator struct.
Expand Down Expand Up @@ -65,12 +64,12 @@ impl Aggregator {
self.config.operator_takes_after,
self.config.bridge_amount_sats,
);
tracing::debug!("SLASH_OR_TAKE_TX: {:?}", tx);
// tracing::debug!("SLASH_OR_TAKE_TX: {:?}", tx);
tracing::debug!("SLASH_OR_TAKE_TX weight: {:?}", tx.tx.weight());
let message: [u8; 32] = Actor::convert_tx_to_sighash_script_spend(&mut tx, 0, 0)
.unwrap()
.to_byte_array();
tracing::debug!("aggregate SLASH_OR_TAKE_TX message: {:?}", message);
// tracing::debug!("aggregate SLASH_OR_TAKE_TX message: {:?}", message);
let final_sig: [u8; 64] = aggregate_partial_signatures(
self.config.verifiers_public_keys.clone(),
None,
Expand All @@ -79,15 +78,15 @@ impl Aggregator {
partial_sigs,
message,
)?;
tracing::debug!("aggregate SLASH_OR_TAKE_TX final_sig: {:?}", final_sig);
tracing::debug!(
"aggregate SLASH_OR_TAKE_TX for verifiers: {:?}",
self.config.verifiers_public_keys.clone()
);
tracing::debug!(
"aggregate SLASH_OR_TAKE_TX for operator: {:?}",
operator_xonly_pk
);
// tracing::debug!("aggregate SLASH_OR_TAKE_TX final_sig: {:?}", final_sig);
// tracing::debug!(
// "aggregate SLASH_OR_TAKE_TX for verifiers: {:?}",
// self.config.verifiers_public_keys.clone()
// );
// tracing::debug!(
// "aggregate SLASH_OR_TAKE_TX for operator: {:?}",
// operator_xonly_pk
// );
Ok(final_sig)
}

Expand Down Expand Up @@ -137,6 +136,11 @@ impl Aggregator {
},
txout: slash_or_take_tx_handler.tx.output[0].clone(),
};
// tracing::debug!(
// "SERDE_UTXO: {:#?}",
// serde_json::to_string(&slash_or_take_utxo).unwrap()
// );

let mut tx_handler = TransactionBuilder::create_operator_takes_tx(
bridge_fund_outpoint,
slash_or_take_utxo,
Expand All @@ -146,12 +150,12 @@ impl Aggregator {
self.config.operator_takes_after,
self.config.bridge_amount_sats,
);
tracing::debug!(
"OPERATOR_TAKES_TX with operator_idx:{:?} {:?}",
operator_idx,
tx_handler.tx
);
tracing::debug!("OPERATOR_TAKES_TX_HEX: {:?}", tx_handler.tx.raw_hex());
// tracing::debug!(
// "OPERATOR_TAKES_TX with operator_idx:{:?} {:?}",
// operator_idx,
// tx_handler.tx
// );
// tracing::debug!("OPERATOR_TAKES_TX_HEX: {:?}", tx_handler.tx.raw_hex());
tracing::debug!("OPERATOR_TAKES_TX weight: {:?}", tx_handler.tx.weight());
let message: [u8; 32] = Actor::convert_tx_to_sighash_pubkey_spend(&mut tx_handler, 0)
.unwrap()
Expand All @@ -164,7 +168,7 @@ impl Aggregator {
partial_sigs,
message,
)?;
tracing::debug!("OPERATOR_TAKES_TX final_sig: {:?}", final_sig);
// tracing::debug!("OPERATOR_TAKES_TX final_sig: {:?}", final_sig);
Ok(final_sig)
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ pub struct BridgeConfig {
pub operator_takes_after: u32,
/// Bridge amount in satoshis.
pub bridge_amount_sats: u64,
/// Number of kickoff UTXOs per funding transaction.
pub num_kickoff_utxos_per_tx: usize,
/// Operator: number of kickoff UTXOs per funding transaction.
pub operator_num_kickoff_utxos_per_tx: usize,
/// Threshold for confirmation.
pub confirmation_threshold: u32,
/// Bitcoin remote procedure call URL.
Expand Down Expand Up @@ -144,7 +144,7 @@ impl Default for BridgeConfig {
user_takes_after: 5,
operator_takes_after: 5,
bridge_amount_sats: 100_000_000,
num_kickoff_utxos_per_tx: 10,
operator_num_kickoff_utxos_per_tx: 10,
confirmation_threshold: 1,
network: Network::Regtest,
bitcoin_rpc_url: "http://127.0.0.1:18443".to_string(),
Expand Down
2 changes: 2 additions & 0 deletions core/src/database/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ impl Database {
Ok(None)
} else {
// Fetch the raw signed transaction
let db_transaction = self.begin_transaction().await?;
let qr_tx: (String,) = sqlx::query_as(
"SELECT raw_signed_tx FROM deposit_kickoff_generator_txs ORDER BY id DESC LIMIT 1;",
)
Expand All @@ -212,6 +213,7 @@ impl Database {
.bind(txid)
.execute(&self.connection)
.await?;
db_transaction.commit().await?;

// Return the UTXO
Ok(Some(UTXO { outpoint, txout }))
Expand Down
16 changes: 8 additions & 8 deletions core/src/musig2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,19 @@ pub fn create_key_agg_ctx(
.map(|pk| musig2::secp256k1::PublicKey::from_slice(&pk.serialize()).unwrap())
.collect::<Vec<musig2::secp256k1::PublicKey>>();
let key_agg_ctx_raw = KeyAggContext::new(musig_pks)?;
tracing::debug!(
"UNTWEAKED AGGREGATED PUBKEY: {:?}",
key_agg_ctx_raw.aggregated_pubkey::<musig2::secp256k1::PublicKey>()
);
// tracing::debug!(
// "UNTWEAKED AGGREGATED PUBKEY: {:?}",
// key_agg_ctx_raw.aggregated_pubkey::<musig2::secp256k1::PublicKey>()
// );
if tweak_flag {
let key_agg_ctx = match tweak {
Some(scalar) => key_agg_ctx_raw.with_taproot_tweak(&scalar.to_byte_array())?,
None => key_agg_ctx_raw.with_unspendable_taproot_tweak()?,
};
tracing::debug!(
"TWEAKED AGGREGATED PUBKEY: {:?}",
key_agg_ctx.aggregated_pubkey::<musig2::secp256k1::PublicKey>()
);
// tracing::debug!(
// "TWEAKED AGGREGATED PUBKEY: {:?}",
// key_agg_ctx.aggregated_pubkey::<musig2::secp256k1::PublicKey>()
// );
Ok(key_agg_ctx)
} else {
if tweak.is_some() {
Expand Down
20 changes: 14 additions & 6 deletions core/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::errors::BridgeError;
use crate::extended_rpc::ExtendedRpc;
use crate::musig2::AggregateFromPublicKeys;
use crate::traits::rpc::OperatorRpcServer;
use crate::transaction_builder::TransactionBuilder;
use crate::transaction_builder::{TransactionBuilder, KICKOFF_UTXO_AMOUNT_SATS};
use crate::utils::handle_taproot_witness_new;
use crate::{script_builder, utils, EVMAddress, UTXO};
use bitcoin::address::NetworkUnchecked;
Expand Down Expand Up @@ -156,7 +156,8 @@ where
// the kickoff outputs, the penultimante output is the change output,
// and the last output is the anyonecanpay output for fee bumping.
if funding_utxo.txout.value.to_sat()
< (100_000 * self.config.num_kickoff_utxos_per_tx + 2000) as u64
< (KICKOFF_UTXO_AMOUNT_SATS * self.config.operator_num_kickoff_utxos_per_tx as u64
+ 2000)
{
// TODO: Change this amount
return Err(BridgeError::OperatorFundingUtxoAmountNotEnough(
Expand All @@ -169,13 +170,20 @@ where
&self.nofn_xonly_pk,
&self.signer.xonly_public_key,
self.config.network,
self.config.num_kickoff_utxos_per_tx,
self.config.operator_num_kickoff_utxos_per_tx,
);
let sig = self
.signer
.sign_taproot_pubkey_spend(&mut kickoff_tx_handler, 0, None)?;
handle_taproot_witness_new(&mut kickoff_tx_handler, &[sig.as_ref()], 0, None)?;

// tracing::debug!(
// "Created kickoff tx with weight: {:#?}",
// kickoff_tx_handler.tx.weight()
// );
// tracing::debug!(
// "Created kickoff tx with vsize: {:#?}",
// kickoff_tx_handler.tx.vsize()
// );
// tracing::debug!(
// "For operator index: {:?} Kickoff tx handler: {:#?}",
// self.idx,
Expand All @@ -185,7 +193,7 @@ where
let change_utxo = UTXO {
outpoint: OutPoint {
txid: kickoff_tx_handler.tx.compute_txid(),
vout: self.config.num_kickoff_utxos_per_tx as u32,
vout: self.config.operator_num_kickoff_utxos_per_tx as u32,
},
txout: kickoff_tx_handler.tx.output[1].clone(),
};
Expand Down Expand Up @@ -223,7 +231,7 @@ where
.add_deposit_kickoff_generator_tx(
kickoff_tx_handler.tx.compute_txid(),
kickoff_tx_handler.tx.raw_hex(),
self.config.num_kickoff_utxos_per_tx,
self.config.operator_num_kickoff_utxos_per_tx,
funding_utxo.outpoint.txid,
)
.await?;
Expand Down
21 changes: 11 additions & 10 deletions core/src/transaction_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub const MOVE_TX_MIN_RELAY_FEE: u64 = 305;
pub const SLASH_OR_TAKE_TX_MIN_RELAY_FEE: u64 = 305;
pub const WITHDRAWAL_TX_MIN_RELAY_FEE: u64 = 305;
pub const OPERATOR_TAKES_TX_MIN_RELAY_FEE: u64 = 305;
pub const KICKOFF_UTXO_AMOUNT_SATS: u64 = 100_000;

impl TransactionBuilder {
/// Creates a new `TransactionBuilder`.
Expand Down Expand Up @@ -203,7 +204,7 @@ impl TransactionBuilder {
network: bitcoin::Network,
num_kickoff_utxos_per_tx: usize,
) -> TxHandler {
let kickoff_tx_min_relay_fee = 2000; // TODO: Change this with variable kickoff utxos per txs
let kickoff_tx_min_relay_fee = 154 + 43 * num_kickoff_utxos_per_tx;
let tx_ins = TransactionBuilder::create_tx_ins(vec![funding_utxo.outpoint]);
let musig2_and_operator_script = script_builder::create_musig2_and_operator_multisig_script(
nofn_xonly_pk,
Expand All @@ -216,13 +217,13 @@ impl TransactionBuilder {
);
let operator_address = Address::p2tr(&utils::SECP, *operator_xonly_pk, None, network);
let change_amount = funding_utxo.txout.value
- Amount::from_sat(100_000 * num_kickoff_utxos_per_tx as u64)
- Amount::from_sat(KICKOFF_UTXO_AMOUNT_SATS * num_kickoff_utxos_per_tx as u64)
- script_builder::anyone_can_spend_txout().value
- Amount::from_sat(kickoff_tx_min_relay_fee);
- Amount::from_sat(kickoff_tx_min_relay_fee as u64);

let mut tx_outs_raw = vec![
(
Amount::from_sat(100_000), // TODO: Change this to a constant
Amount::from_sat(KICKOFF_UTXO_AMOUNT_SATS),
musig2_and_operator_address.script_pubkey(),
);
num_kickoff_utxos_per_tx
Expand Down Expand Up @@ -277,12 +278,12 @@ impl TransactionBuilder {

let (kickoff_utxo_address, kickoff_utxo_spend_info) =
Self::create_kickoff_address(nofn_xonly_pk, operator_xonly_pk, network);
tracing::debug!(
"kickoff_utxo_script_pubkey: {:?}",
kickoff_utxo_address.script_pubkey()
);
tracing::debug!("kickoff_utxo_spend_info: {:?}", kickoff_utxo_spend_info);
tracing::debug!("kickoff_utxooo: {:?}", kickoff_utxo);
// tracing::debug!(
// "kickoff_utxo_script_pubkey: {:?}",
// kickoff_utxo_address.script_pubkey()
// );
// tracing::debug!("kickoff_utxo_spend_info: {:?}", kickoff_utxo_spend_info);
// tracing::debug!("kickoff_utxooo: {:?}", kickoff_utxo);
let musig2_and_operator_script = script_builder::create_musig2_and_operator_multisig_script(
nofn_xonly_pk,
operator_xonly_pk,
Expand Down
23 changes: 11 additions & 12 deletions core/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::musig2::{
self, AggregateFromPublicKeys, MuSigAggNonce, MuSigPartialSignature, MuSigPubNonce,
};
use crate::traits::rpc::VerifierRpcServer;
use crate::transaction_builder::{TransactionBuilder, TxHandler};
use crate::transaction_builder::{TransactionBuilder, TxHandler, KICKOFF_UTXO_AMOUNT_SATS};
use crate::{utils, EVMAddress, UTXO};
use bitcoin::address::NetworkUnchecked;
use bitcoin::hashes::Hash;
Expand Down Expand Up @@ -150,7 +150,7 @@ where

for (i, kickoff_utxo) in kickoff_utxos.iter().enumerate() {
let value = kickoff_utxo.txout.value;
if value.to_sat() < 100_000 {
if value.to_sat() < KICKOFF_UTXO_AMOUNT_SATS {
return Err(BridgeError::InvalidKickoffUtxo);
}

Expand Down Expand Up @@ -329,19 +329,18 @@ where
self.config.operator_takes_after,
self.config.bridge_amount_sats,
);
// println!("Slash or take tx handler: {:?}", slash_or_take_tx_handler);
let slash_or_take_sighash =
Actor::convert_tx_to_sighash_script_spend(&mut slash_or_take_tx_handler, 0, 0)
.unwrap();
tracing::debug!(
"Verify SLASH_OR_TAKE_TX message: {:?}",
slash_or_take_sighash
);
tracing::debug!("Verify SLASH_OR_TAKE_SIG: {:?}", slash_or_take_sigs[index]);
tracing::debug!(
"Verify SLASH_OR_TAKE_TX operator xonly_pk: {:?}",
self.operator_xonly_pks[index]
);
// tracing::debug!(
// "Verify SLASH_OR_TAKE_TX message: {:?}",
// slash_or_take_sighash
// );
// tracing::debug!("Verify SLASH_OR_TAKE_SIG: {:?}", slash_or_take_sigs[index]);
// tracing::debug!(
// "Verify SLASH_OR_TAKE_TX operator xonly_pk: {:?}",
// self.operator_xonly_pks[index]
// );
utils::SECP
.verify_schnorr(
&slash_or_take_sigs[index],
Expand Down
6 changes: 3 additions & 3 deletions core/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub async fn run_single_deposit(
let rpc = create_extended_rpc!(config);

let (verifiers, operators, aggregator) =
create_verifiers_and_operators("test_config_flow.toml").await;
create_verifiers_and_operators("test_config.toml").await;

// println!("Operators: {:#?}", operators);
// println!("Verifiers: {:#?}", verifiers);
Expand Down Expand Up @@ -100,7 +100,7 @@ pub async fn run_single_deposit(
txout: operator_funding_txout,
};

tracing::debug!("Operator {:?} funding utxo: {:?}", i, operator_funding_utxo);
// tracing::debug!("Operator {:?} funding utxo: {:?}", i, operator_funding_utxo);
client
.set_funding_utxo_rpc(operator_funding_utxo)
.await
Expand Down Expand Up @@ -194,7 +194,7 @@ pub async fn run_single_deposit(
)
.await
.unwrap();
tracing::debug!("Move tx: {:#?}", move_tx);
// tracing::debug!("Move tx: {:#?}", move_tx);
// tracing::debug!("Move tx_hex: {:?}", move_tx_handler.tx.raw_hex());
tracing::debug!("Move tx weight: {:?}", move_tx.weight());
let move_txid = rpc.send_raw_transaction(&move_tx).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion core/tests/data/test_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ num_operators = 5
bridge_amount_sats = 100000000
user_takes_after = 5
operator_takes_after = 5
num_kickoff_utxos_per_tx = 10
operator_num_kickoff_utxos_per_tx = 10
confirmation_threshold = 1
network = "regtest"
bitcoin_rpc_url = "http://127.0.0.1:18443"
Expand Down
48 changes: 0 additions & 48 deletions core/tests/data/test_config_flow.toml

This file was deleted.

Loading

0 comments on commit dec0c5b

Please sign in to comment.