Skip to content

Commit 46f573b

Browse files
committed
[full_stack_target] don't allow double-confirming txn
1 parent 64bd2ea commit 46f573b

File tree

1 file changed

+51
-29
lines changed

1 file changed

+51
-29
lines changed

fuzz/fuzz_targets/full_stack_target.rs

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use secp256k1::key::{PublicKey,SecretKey};
3333
use secp256k1::Secp256k1;
3434

3535
use std::cell::RefCell;
36-
use std::collections::HashMap;
36+
use std::collections::{HashMap, hash_map};
3737
use std::cmp;
3838
use std::hash::Hash;
3939
use std::sync::Arc;
@@ -140,10 +140,11 @@ struct MoneyLossDetector<'a> {
140140

141141
peers: &'a RefCell<[bool; 256]>,
142142
funding_txn: Vec<Transaction>,
143+
txids_confirmed: HashMap<Sha256dHash, usize>,
143144
header_hashes: Vec<Sha256dHash>,
144145
height: usize,
145146
max_height: usize,
146-
147+
blocks_connected: u32,
147148
}
148149
impl<'a> MoneyLossDetector<'a> {
149150
pub fn new(peers: &'a RefCell<[bool; 256]>, manager: Arc<ChannelManager>, monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>, handler: PeerManager<Peer<'a>>) -> Self {
@@ -154,17 +155,34 @@ impl<'a> MoneyLossDetector<'a> {
154155

155156
peers,
156157
funding_txn: Vec::new(),
158+
txids_confirmed: HashMap::new(),
157159
header_hashes: vec![Default::default()],
158160
height: 0,
159161
max_height: 0,
162+
blocks_connected: 0,
160163
}
161164
}
162165

163-
fn connect_block(&mut self, txn: &[&Transaction], txn_idxs: &[u32]) {
164-
let header = BlockHeader { version: 0x20000000, prev_blockhash: self.header_hashes[self.height], merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
166+
fn connect_block(&mut self, all_txn: &[Transaction]) {
167+
let mut txn = Vec::with_capacity(all_txn.len());
168+
let mut txn_idxs = Vec::with_capacity(all_txn.len());
169+
for (idx, tx) in all_txn.iter().enumerate() {
170+
let txid = Sha256dHash::from_data(&serialize(tx).unwrap()[..]);
171+
match self.txids_confirmed.entry(txid) {
172+
hash_map::Entry::Vacant(e) => {
173+
e.insert(self.height);
174+
txn.push(tx);
175+
txn_idxs.push(idx as u32 + 1);
176+
},
177+
_ => {},
178+
}
179+
}
180+
181+
let header = BlockHeader { version: 0x20000000, prev_blockhash: self.header_hashes[self.height], merkle_root: Default::default(), time: self.blocks_connected, bits: 42, nonce: 42 };
165182
self.height += 1;
166-
self.manager.block_connected(&header, self.height as u32, txn, txn_idxs);
167-
(*self.monitor).block_connected(&header, self.height as u32, txn, txn_idxs);
183+
self.blocks_connected += 1;
184+
self.manager.block_connected(&header, self.height as u32, &txn[..], &txn_idxs[..]);
185+
(*self.monitor).block_connected(&header, self.height as u32, &txn[..], &txn_idxs[..]);
168186
if self.header_hashes.len() > self.height {
169187
self.header_hashes[self.height] = header.bitcoin_hash();
170188
} else {
@@ -180,6 +198,10 @@ impl<'a> MoneyLossDetector<'a> {
180198
let header = BlockHeader { version: 0x20000000, prev_blockhash: self.header_hashes[self.height], merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
181199
self.manager.block_disconnected(&header);
182200
self.monitor.block_disconnected(&header);
201+
let removal_height = self.height;
202+
self.txids_confirmed.retain(|_, height| {
203+
removal_height != *height
204+
});
183205
}
184206
}
185207
}
@@ -398,36 +420,36 @@ pub fn do_test(data: &[u8], logger: &Arc<Logger>) {
398420
}
399421
},
400422
10 => {
401-
for funding_generation in pending_funding_generation.drain(..) {
423+
'outer_loop: for funding_generation in pending_funding_generation.drain(..) {
402424
let mut tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: vec![TxOut {
403425
value: funding_generation.1, script_pubkey: funding_generation.2,
404426
}] };
405-
let funding_output = OutPoint::new(Sha256dHash::from_data(&serialize(&tx).unwrap()[..]), 0);
406-
let mut found_duplicate_txo = false;
407-
for chan in channelmanager.list_channels() {
408-
if chan.channel_id == funding_output.to_channel_id() {
409-
found_duplicate_txo = true;
427+
let funding_output = 'search_loop: loop {
428+
let funding_txid = Sha256dHash::from_data(&serialize(&tx).unwrap()[..]);
429+
if let None = loss_detector.txids_confirmed.get(&funding_txid) {
430+
let outpoint = OutPoint::new(funding_txid, 0);
431+
for chan in channelmanager.list_channels() {
432+
if chan.channel_id == outpoint.to_channel_id() {
433+
tx.version += 1;
434+
continue 'search_loop;
435+
}
436+
}
437+
break outpoint;
410438
}
411-
}
412-
if !found_duplicate_txo {
413-
channelmanager.funding_transaction_generated(&funding_generation.0, funding_output.clone());
414-
pending_funding_signatures.insert(funding_output, tx);
415-
}
439+
tx.version += 1;
440+
if tx.version > 0xff {
441+
continue 'outer_loop;
442+
}
443+
};
444+
channelmanager.funding_transaction_generated(&funding_generation.0, funding_output.clone());
445+
pending_funding_signatures.insert(funding_output, tx);
416446
}
417447
},
418448
11 => {
419449
if !pending_funding_relay.is_empty() {
420-
let mut txn = Vec::with_capacity(pending_funding_relay.len());
421-
let mut txn_idxs = Vec::with_capacity(pending_funding_relay.len());
422-
for (idx, tx) in pending_funding_relay.iter().enumerate() {
423-
txn.push(tx);
424-
txn_idxs.push(idx as u32 + 1);
425-
}
426-
427-
loss_detector.connect_block(&txn[..], &txn_idxs[..]);
428-
txn_idxs.clear();
450+
loss_detector.connect_block(&pending_funding_relay[..]);
429451
for _ in 2..100 {
430-
loss_detector.connect_block(&txn[..], &txn_idxs[..]);
452+
loss_detector.connect_block(&[]);
431453
}
432454
}
433455
for tx in pending_funding_relay.drain(..) {
@@ -437,11 +459,11 @@ pub fn do_test(data: &[u8], logger: &Arc<Logger>) {
437459
12 => {
438460
let txlen = slice_to_be16(get_slice!(2));
439461
if txlen == 0 {
440-
loss_detector.connect_block(&[], &[]);
462+
loss_detector.connect_block(&[]);
441463
} else {
442464
let txres: Result<Transaction, _> = deserialize(get_slice!(txlen));
443465
if let Ok(tx) = txres {
444-
loss_detector.connect_block(&[&tx], &[1]);
466+
loss_detector.connect_block(&[tx]);
445467
} else {
446468
return;
447469
}

0 commit comments

Comments
 (0)