Skip to content

Commit 8b9e99d

Browse files
committed
adtd relayer call take1 take2 disproved
1 parent 1e57a9e commit 8b9e99d

File tree

3 files changed

+108
-5
lines changed

3 files changed

+108
-5
lines changed

crates/client/src/client.rs

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::chain::chain::Chain;
2-
use crate::chain::chain_adaptor::{GoatNetwork, OperatorData, PeginData, get_chain_adaptor};
2+
use crate::chain::chain_adaptor::{
3+
BitcoinTx, GoatNetwork, OperatorData, PeginData, get_chain_adaptor,
4+
};
35
use crate::chain::goat_adaptor::GoatInitConfig;
46
use crate::esplora::get_esplora_url;
57
use anyhow::format_err;
@@ -100,4 +102,74 @@ impl BitVM2Client {
100102
pub async fn get_operator_data(&self, graph_id: Uuid) -> anyhow::Result<OperatorData> {
101103
self.chain_service.adaptor.get_operator_data(graph_id).await
102104
}
105+
106+
pub async fn finish_withdraw_happy_path(
107+
&self,
108+
graph_id: &Uuid,
109+
tx: &bitcoin::Transaction,
110+
) -> anyhow::Result<()> {
111+
let raw_take1_tx = self.tx_reconstruct(tx);
112+
let (_root, proof, _leaf, height, index) =
113+
self.get_btc_tx_proof_info(&tx.compute_txid()).await?;
114+
Ok(self
115+
.chain_service
116+
.adaptor
117+
.finish_withdraw_happy_path(graph_id, &raw_take1_tx, height, &proof, index)
118+
.await?)
119+
}
120+
121+
pub async fn finish_withdraw_unhappy_path(
122+
&self,
123+
graph_id: &Uuid,
124+
tx: &bitcoin::Transaction,
125+
) -> anyhow::Result<()> {
126+
let raw_take2_tx = self.tx_reconstruct(tx);
127+
let (_root, proof, _leaf, height, index) =
128+
self.get_btc_tx_proof_info(&tx.compute_txid()).await?;
129+
Ok(self
130+
.chain_service
131+
.adaptor
132+
.finish_withdraw_unhappy_path(graph_id, &raw_take2_tx, height, &proof, index)
133+
.await?)
134+
}
135+
136+
pub async fn finish_withdraw_disproved(
137+
&self,
138+
graph_id: &Uuid,
139+
tx: &bitcoin::Transaction,
140+
) -> anyhow::Result<()> {
141+
let raw_disprove_tx = self.tx_reconstruct(tx);
142+
let (_root, proof, _leaf, height, index) =
143+
self.get_btc_tx_proof_info(&tx.compute_txid()).await?;
144+
Ok(self
145+
.chain_service
146+
.adaptor
147+
.finish_withdraw_disproved(graph_id, &raw_disprove_tx, height, &proof, index)
148+
.await?)
149+
}
150+
151+
pub async fn get_btc_tx_proof_info(
152+
&self,
153+
tx_id: &Txid,
154+
) -> anyhow::Result<([u8; 32], Vec<[u8; 32]>, [u8; 32], u64, u64)> {
155+
let (root, proof_info) = self.get_bitc_merkle_proof(tx_id).await?;
156+
let proof: Vec<[u8; 32]> = proof_info.merkle.iter().map(|v| v.to_byte_array()).collect();
157+
let leaf = tx_id.to_byte_array();
158+
Ok((
159+
root.to_byte_array(),
160+
proof,
161+
leaf,
162+
proof_info.block_height as u64,
163+
proof_info.pos as u64,
164+
))
165+
}
166+
167+
fn tx_reconstruct(&self, tx: &bitcoin::Transaction) -> BitcoinTx {
168+
BitcoinTx {
169+
version: tx.version.0 as u32,
170+
lock_time: tx.lock_time.to_consensus_u32(),
171+
input_vector: bitcoin::consensus::serialize(&tx.input),
172+
output_vector: bitcoin::consensus::serialize(&tx.output),
173+
}
174+
}
103175
}

node/src/action.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ use bitvm2_lib::types::{
1212
use bitvm2_lib::verifier::export_challenge_tx;
1313
use bitvm2_lib::{committee::*, operator::*, verifier::*};
1414
use client::client::BitVM2Client;
15+
use goat::transactions::disprove::DisproveTransaction;
16+
use goat::transactions::take_1::Take1Transaction;
17+
use goat::transactions::take_2::Take2Transaction;
1518
use goat::transactions::{assert::utils::COMMIT_TX_NUM, pre_signed::PreSignedTransaction};
1619
use libp2p::gossipsub::MessageId;
1720
use libp2p::{PeerId, Swarm, gossipsub};
@@ -1177,7 +1180,8 @@ pub async fn recv_and_dispatch(
11771180
let graph = get_graph(&client, receive_data.instance_id, receive_data.graph_id).await?;
11781181
let take1_txid = graph.take1.tx().compute_txid();
11791182
if todo_funcs::tx_on_chain(&client, &take1_txid).await? {
1180-
// TODO: call L2: finishWithdrawHappyPath
1183+
finish_withdraw_happy_path(&client, &receive_data.graph_id, &graph.take1)
1184+
.await?;
11811185
update_graph_status_or_ipfs_base(
11821186
&client,
11831187
receive_data.graph_id,
@@ -1191,7 +1195,8 @@ pub async fn recv_and_dispatch(
11911195
(GOATMessageContent::Take2Sent(receive_data), Actor::Relayer) => {
11921196
let graph = get_graph(&client, receive_data.instance_id, receive_data.graph_id).await?;
11931197
if todo_funcs::tx_on_chain(&client, &graph.take2.tx().compute_txid()).await? {
1194-
// TODO: call L2: finishWithdrawUnhappyPath
1198+
finish_withdraw_unhappy_path(&client, &receive_data.graph_id, &graph.take2)
1199+
.await?;
11951200
update_graph_status_or_ipfs_base(
11961201
&client,
11971202
receive_data.graph_id,
@@ -1211,7 +1216,8 @@ pub async fn recv_and_dispatch(
12111216
)
12121217
.await?
12131218
{
1214-
// TODO: call L2: finishWithdrawDisproved
1219+
finish_withdraw_disproved(&client, &receive_data.graph_id, &graph.disprove)
1220+
.await?;
12151221
update_graph_status_or_ipfs_base(
12161222
&client,
12171223
receive_data.graph_id,
@@ -1336,6 +1342,31 @@ where
13361342
Ok(serde_json::from_str(txt.as_str())?)
13371343
}
13381344

1345+
/// l2 support
1346+
pub async fn finish_withdraw_happy_path(
1347+
client: &BitVM2Client,
1348+
graph_id: &Uuid,
1349+
take1: &Take1Transaction,
1350+
) -> Result<(), Box<dyn std::error::Error>> {
1351+
Ok(client.finish_withdraw_happy_path(graph_id, take1.tx()).await?)
1352+
}
1353+
pub async fn finish_withdraw_unhappy_path(
1354+
client: &BitVM2Client,
1355+
graph_id: &Uuid,
1356+
take2: &Take2Transaction,
1357+
) -> Result<(), Box<dyn std::error::Error>> {
1358+
Ok(client.finish_withdraw_unhappy_path(graph_id, take2.tx()).await?)
1359+
}
1360+
1361+
pub async fn finish_withdraw_disproved(
1362+
client: &BitVM2Client,
1363+
graph_id: &Uuid,
1364+
disprove: &DisproveTransaction,
1365+
) -> Result<(), Box<dyn std::error::Error>> {
1366+
Ok(client.finish_withdraw_disproved(graph_id, disprove.tx()).await?)
1367+
}
1368+
1369+
/// db support
13391370
pub async fn store_committee_pub_nonces(
13401371
client: &BitVM2Client,
13411372
instance_id: Uuid,

node/src/rpc_service/bitvm2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::str::FromStr;
99
use store::{BridgeInStatus, GrapRpcQueryData, Graph, GraphStatus, Instance};
1010
use uuid::Uuid;
1111

12-
pub const BTC_MAIN: &str = "main";
12+
pub const BTC_MAIN: &str = "bitcoin";
1313
pub const BTC_TEST_BLOCK_INTERVAL: u32 = 10;
1414
pub const BTC_MAIN_BLOCK_INTERVAL: u32 = 10;
1515
// the input to our `create_user` handler

0 commit comments

Comments
 (0)