Skip to content

Commit

Permalink
Move WOTS pubkeys to PegOutGraphInput (#53)
Browse files Browse the repository at this point in the history
* refactor(tx-graph): include wots keys in PegOutGraphInput

* fix(agent): count blocks after withdrawal when constructing btc chain segment

* docs(primitives): clarify purpose of EXPECTED_BLOCK_COUNT

* refactor(agent): use expected block count from proof statements

* refactor(tx-graph): split PegOutGraphInput into p2p values and params values
  • Loading branch information
Rajil1213 authored Mar 5, 2025
1 parent a6df1bf commit 589693b
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 162 deletions.
94 changes: 51 additions & 43 deletions crates/agent/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ use strata_bridge_primitives::{
wots::{Assertions, PublicKeys as WotsPublicKeys, Signatures as WotsSignatures},
};
use strata_bridge_proof_primitives::L1TxWithProofBundle;
use strata_bridge_proof_protocol::BridgeProofInput;
use strata_bridge_proof_protocol::{
BridgeProofInput, REQUIRED_NUM_OF_HEADERS_AFTER_WITHDRAWAL_FULFILLMENT_TX,
};
use strata_bridge_proof_snark::{bridge_vk, prover};
use strata_bridge_stake_chain::{
prelude::{PreStakeTx, OPERATOR_FUNDS, STAKE_VOUT, WITHDRAWAL_FULFILLMENT_VOUT},
Expand All @@ -54,7 +56,7 @@ use strata_bridge_stake_chain::{
StakeChain,
};
use strata_bridge_tx_graph::{
peg_out_graph::{PegOutGraph, PegOutGraphConnectors, PegOutGraphInput},
peg_out_graph::{PegOutGraph, PegOutGraphConnectors, PegOutGraphInput, PegOutGraphParams},
transactions::prelude::*,
};
use strata_btcio::rpc::{
Expand Down Expand Up @@ -283,10 +285,14 @@ where
.unwrap(); // FIXME: Handle me

info!(action = "composing peg out graph input", %deposit_txid, %own_index);
let wots_public_keys = self
.public_db
.get_wots_public_keys(own_index, deposit_txid)
.await
.expect("should be able to get wots public keys")
.unwrap(); // FIXME: Handle me

let peg_out_graph_input = PegOutGraphInput {
deposit_amount: BRIDGE_DENOMINATION,
operator_pubkey: self.agent.public_key().x_only_public_key().0,
funding_amount: OPERATOR_FUNDS - SEGWIT_MIN_AMOUNT * 2,
stake_outpoint: OutPoint {
txid: stake_txid,
vout: STAKE_VOUT,
Expand All @@ -296,24 +302,22 @@ where
vout: WITHDRAWAL_FULFILLMENT_VOUT,
},
stake_hash: stake_data.hash,
wots_public_keys,
operator_pubkey: self.agent.public_key().x_only_public_key().0,
};
let graph_params = PegOutGraphParams {
deposit_amount: BRIDGE_DENOMINATION,
funding_amount: OPERATOR_FUNDS - SEGWIT_MIN_AMOUNT * 2,
};

info!(action = "generating pegout graph and connectors", %deposit_txid, %own_index);
let wots_public_keys = self
.public_db
.get_wots_public_keys(own_index, deposit_txid)
.await
.expect("should be able to get wots public keys")
.unwrap(); // FIXME: Handle me

let (peg_out_graph, _connectors) = PegOutGraph::generate(
peg_out_graph_input.clone(),
&self.build_context,
deposit_txid,
own_index,
graph_params,
StakeChainParams::default(),
vec![],
wots_public_keys,
)
.expect("must be able to generate tx graph");

Expand Down Expand Up @@ -551,12 +555,11 @@ where
} = details;
info!(event = "received covenant request for nonce", %deposit_txid, %sender_id, %own_index);

let wots_public_keys = self
.public_db
.get_wots_public_keys(sender_id, deposit_txid)
.await
.expect("should be able to get wots public keys")
.unwrap(); // FIXME: Handle me
let graph_params = PegOutGraphParams {
deposit_amount: BRIDGE_DENOMINATION,
funding_amount: OPERATOR_FUNDS - SEGWIT_MIN_AMOUNT * 2,
};

let (
PegOutGraph {
assert_chain,
Expand All @@ -569,10 +572,9 @@ where
peg_out_graph_input.clone(),
&self.build_context,
deposit_txid,
sender_id,
graph_params,
StakeChainParams::default(),
vec![],
wots_public_keys,
)
.expect("should be able to generate tx graph");

Expand Down Expand Up @@ -954,20 +956,20 @@ where
peg_out_graph_input,
} = details;
info!(event = "received covenant request for signatures", %deposit_txid, %sender_id, %own_index);
let wots_public_keys = self
.public_db
.get_wots_public_keys(sender_id, deposit_txid)
.await
.expect("should be able to get wots public keys")
.unwrap(); // FIXME: Handle me
let graph_params = {
let funding_amount = OPERATOR_FUNDS - SEGWIT_MIN_AMOUNT * 2;
PegOutGraphParams {
deposit_amount: BRIDGE_DENOMINATION,
funding_amount,
}
};
let (peg_out_graph, _connectors) = PegOutGraph::generate(
peg_out_graph_input,
&self.build_context,
deposit_txid,
sender_id,
graph_params,
StakeChainParams::default(),
vec![],
wots_public_keys,
)
.expect("should be able to generate tx graph");

Expand Down Expand Up @@ -1595,13 +1597,21 @@ where
.unwrap()
.unwrap(); // FIXME:
// Handle me
let wots_public_keys = self
.public_db
.get_wots_public_keys(own_index, deposit_txid)
.await
.unwrap()
.unwrap();

info!(action = "reconstructing pegout graph", %deposit_txid, %own_index);
let peg_out_graph_input = PegOutGraphInput {
let graph_params = PegOutGraphParams {
deposit_amount: BRIDGE_DENOMINATION,
operator_pubkey: own_pubkey,
// *2 for the two dust outputs in each stake transaction
funding_amount: OPERATOR_FUNDS - SEGWIT_MIN_AMOUNT * 2,
};
let peg_out_graph_input = PegOutGraphInput {
operator_pubkey: own_pubkey,
stake_outpoint: OutPoint {
txid: stake_txid,
vout: STAKE_VOUT,
Expand All @@ -1611,22 +1621,16 @@ where
vout: WITHDRAWAL_FULFILLMENT_VOUT,
},
stake_hash: stake_data.hash,
wots_public_keys,
};

let wots_public_keys = self
.public_db
.get_wots_public_keys(own_index, deposit_txid)
.await
.expect("should be able to get wots public keys")
.unwrap(); // FIXME: Handle me
let (peg_out_graph, connectors) = PegOutGraph::generate(
peg_out_graph_input,
&self.build_context,
deposit_txid,
own_index,
graph_params,
StakeChainParams::default(),
vec![],
wots_public_keys,
)
.expect("should be able to generate tx graph");

Expand Down Expand Up @@ -2128,7 +2132,6 @@ where
.0;

let l1_start_height = (checkpoint_info.l1_range.1.height() + 1) as u32;
let mut block_count = 0;

let btc_params = get_btc_params();

Expand All @@ -2149,6 +2152,7 @@ where
let mut checkpoint = None;

info!(action = "scanning blocks...", %deposit_txid, %withdrawal_fulfillment_txid, start_height=%height);
let mut num_blocks_after_fulfillment = 0;
let poll_interval = Duration::from_secs(self.btc_poll_interval.as_secs() / 2);
loop {
let block = self.agent.btc_client.get_block_at(height.into()).await;
Expand Down Expand Up @@ -2210,9 +2214,13 @@ where
blocks.push(block);
height += 1;

block_count += 1;
if withdrawal_fulfillment.is_some() {
num_blocks_after_fulfillment += 1;
}

if block_count >= EXPECTED_BLOCK_COUNT {
if num_blocks_after_fulfillment
> REQUIRED_NUM_OF_HEADERS_AFTER_WITHDRAWAL_FULFILLMENT_TX
{
info!(event = "blocks period complete", total_blocks = %headers.len());
break;
}
Expand Down
1 change: 1 addition & 0 deletions crates/bridge-proof/protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,4 @@ pub fn process_bridge_proof_outer(zkvm: &impl ZkVmEnv) {
}

pub use prover::{get_native_host, BridgeProver};
pub use statement::REQUIRED_NUM_OF_HEADERS_AFTER_WITHDRAWAL_FULFILLMENT_TX;
2 changes: 1 addition & 1 deletion crates/bridge-proof/protocol/src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
/// input
///
/// TODO: update this once this is fixed
const REQUIRED_NUM_OF_HEADERS_AFTER_WITHDRAWAL_FULFILLMENT_TX: usize = 30;
pub const REQUIRED_NUM_OF_HEADERS_AFTER_WITHDRAWAL_FULFILLMENT_TX: usize = 30;

/// The fixed withdrawal fee for Bitcoin transactions.
///
Expand Down
4 changes: 0 additions & 4 deletions crates/primitives/src/params/connectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,11 @@ const _: [(); 0] = [(); (NUM_PKS_A256 + NUM_PKS_A160 - TOTAL_VALUES)];

pub const BLOCK_TIME: Duration = Duration::from_secs(30);

pub const EXPECTED_BLOCK_COUNT: u32 = 100; // blocks

pub const PAYOUT_OPTIMISTIC_TIMELOCK: u32 = 500;

pub const PRE_ASSERT_TIMELOCK: u32 = PAYOUT_OPTIMISTIC_TIMELOCK + 100; // 100 is slack

// compile-time checks
const _: () = assert!(PRE_ASSERT_TIMELOCK > PAYOUT_OPTIMISTIC_TIMELOCK);

const _: u32 = PAYOUT_OPTIMISTIC_TIMELOCK - (EXPECTED_BLOCK_COUNT + 100); // 100 is slack

pub const PAYOUT_TIMELOCK: u32 = 288; // 2 day's worth of blocks in mainnet
Loading

0 comments on commit 589693b

Please sign in to comment.