forked from arkade-os/rust-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e_send_onchain_vtxo_and_boarding_output.rs
More file actions
162 lines (128 loc) · 5.05 KB
/
e2e_send_onchain_vtxo_and_boarding_output.rs
File metadata and controls
162 lines (128 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#![allow(clippy::unwrap_used)]
use crate::common::wait_until_balance;
use ark_client::wallet::OnchainWallet;
use bitcoin::address::NetworkUnchecked;
use bitcoin::key::Secp256k1;
use bitcoin::Amount;
use common::init_tracing;
use common::set_up_client;
use common::Nigiri;
use rand::thread_rng;
use std::str::FromStr;
use std::sync::Arc;
mod common;
#[tokio::test]
#[ignore]
pub async fn send_onchain_vtxo_and_boarding_output() {
init_tracing();
let nigiri = Arc::new(Nigiri::new());
let secp = Secp256k1::new();
let mut rng = thread_rng();
let (alice, alice_wallet) =
set_up_client("alice".to_string(), nigiri.clone(), secp.clone()).await;
let offchain_balance = alice.offchain_balance().await.unwrap();
assert_eq!(offchain_balance.total(), Amount::ZERO);
let alice_boarding_address = alice.get_boarding_address().unwrap();
let fund_amount = Amount::ONE_BTC;
nigiri
.faucet_fund(&alice_boarding_address, fund_amount)
.await;
// We give Alice two extra UTXOs to be able to bump the two transactions she needs to broadcast
// to commit her VTXO (and the change VTXO too!) to the blockchain.
let alice_onchain_address = alice.get_onchain_address().unwrap();
nigiri
.faucet_fund(&alice_onchain_address, Amount::from_sat(100_000))
.await;
nigiri
.faucet_fund(&alice_onchain_address, Amount::from_sat(100_000))
.await;
let offchain_balance = alice.offchain_balance().await.unwrap();
assert_eq!(offchain_balance.total(), Amount::ZERO);
alice.settle(&mut rng, false).await.unwrap();
wait_until_balance(&alice, fund_amount, Amount::ZERO)
.await
.unwrap();
// Ensure that the commitment TX is mined.
nigiri.mine(1).await;
alice_wallet.sync().await.unwrap();
let (alice_offchain_address, _) = alice.get_offchain_address().unwrap();
alice
.send_vtxo(alice_offchain_address, Amount::from_sat(100_000))
.await
.unwrap();
wait_until_balance(&alice, Amount::ZERO, fund_amount)
.await
.unwrap();
let unilateral_exit_trees = alice.build_unilateral_exit_trees().await.unwrap();
for (i, unilateral_exit_tree) in unilateral_exit_trees.iter().enumerate() {
while let Some(txid) = alice
.broadcast_next_unilateral_exit_node(unilateral_exit_tree)
.await
.expect("to broadcast unilateral exit node")
{
tracing::info!(i, %txid, "Broadcast virtual transaction");
// The transaction needs a confirmation so that we can bump the P2A output for the next
// transaction in the tree.
nigiri.mine(1).await;
alice_wallet.sync().await.unwrap();
}
tracing::debug!(i, "Finished with unilateral exit tree");
}
// Get one confirmation on the VTXO.
nigiri.mine(1).await;
wait_until_balance(&alice, Amount::ZERO, Amount::ZERO)
.await
.unwrap();
let alice_boarding_address = alice.get_boarding_address().unwrap();
nigiri
.faucet_fund(&alice_boarding_address, Amount::ONE_BTC)
.await;
let offchain_balance = alice.offchain_balance().await.unwrap();
assert_eq!(offchain_balance.confirmed(), Amount::ZERO);
assert_eq!(offchain_balance.pending(), Amount::ZERO);
// To be able to spend a VTXO it needs to have been confirmed for at least
// `unilateral_exit_delay` seconds.
//
// And to be able to spend a boarding output it needs to have been confirmed for at least
// `boarding_exit_delay` seconds.
//
// We take the larger value of the two here.
let boarding_exit_delay = alice.boarding_exit_delay_seconds();
let unilateral_vtxo_exit_delay = alice.unilateral_vtxo_exit_delay_seconds();
let blocktime_offset = boarding_exit_delay.max(unilateral_vtxo_exit_delay);
nigiri.set_outpoint_blocktime_offset(blocktime_offset);
let (tx, prevouts) = alice
.create_send_on_chain_transaction(
bitcoin::Address::<NetworkUnchecked>::from_str(
"bcrt1q8df4sx3hz63tq44ve3q6tr4qz0q30usk5sntpt",
)
.unwrap()
.assume_checked(),
Amount::from_btc(1.4).unwrap(),
)
.await
.unwrap();
// 1 boarding output and 2 VTXOs.
assert_eq!(tx.input.len(), 3);
assert_eq!(prevouts.len(), 3);
for (i, prevout) in prevouts.iter().enumerate() {
let script_pubkey = prevout.script_pubkey.clone();
let amount = prevout.value;
let spent_outputs = prevouts
.iter()
.map(|o| bitcoinconsensus::Utxo {
script_pubkey: o.script_pubkey.as_bytes().as_ptr(),
script_pubkey_len: o.script_pubkey.len() as u32,
value: o.value.to_sat() as i64,
})
.collect::<Vec<_>>();
bitcoinconsensus::verify(
script_pubkey.as_bytes(),
amount.to_sat(),
bitcoin::consensus::serialize(&tx).as_slice(),
Some(&spent_outputs),
i,
)
.expect("valid input");
}
}