@@ -222,8 +222,8 @@ impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref + Sync
222
222
let txn_outputs = monitor. block_connected ( txn_matched, height, & block_hash, & * self . broadcaster , & * self . fee_estimator , & * self . logger , & * self . utxo_pool ) ;
223
223
224
224
for ( ref txid, ref outputs) in txn_outputs {
225
- for ( idx, output ) in outputs. iter ( ) . enumerate ( ) {
226
- self . chain_monitor . install_watch_outpoint ( ( txid. clone ( ) , idx as u32 ) , & output . script_pubkey ) ;
225
+ for ( idx, script ) in outputs. iter ( ) {
226
+ self . chain_monitor . install_watch_outpoint ( ( txid. clone ( ) , * idx) , & script . script_pubkey ) ;
227
227
}
228
228
}
229
229
}
@@ -274,8 +274,8 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
274
274
self . chain_monitor . install_watch_tx ( & funding_txo. 0 . txid , & funding_txo. 1 ) ;
275
275
self . chain_monitor . install_watch_outpoint ( ( funding_txo. 0 . txid , funding_txo. 0 . index as u32 ) , & funding_txo. 1 ) ;
276
276
for ( txid, outputs) in monitor. get_outputs_to_watch ( ) . iter ( ) {
277
- for ( idx, script) in outputs. iter ( ) . enumerate ( ) {
278
- self . chain_monitor . install_watch_outpoint ( ( * txid, idx as u32 ) , script) ;
277
+ for ( idx, script) in outputs. iter ( ) {
278
+ self . chain_monitor . install_watch_outpoint ( ( txid. clone ( ) , * idx) , & script) ;
279
279
}
280
280
}
281
281
}
@@ -679,7 +679,7 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
679
679
// interface knows about the TXOs that we want to be notified of spends of. We could probably
680
680
// be smart and derive them from the above storage fields, but its much simpler and more
681
681
// Obviously Correct (tm) if we just keep track of them explicitly.
682
- outputs_to_watch : HashMap < Txid , Vec < Script > > ,
682
+ outputs_to_watch : HashMap < Txid , Vec < ( u32 , Script ) > > ,
683
683
684
684
#[ cfg( test) ]
685
685
pub onchain_tx_handler : OnchainTxHandler < ChanSigner > ,
@@ -1000,10 +1000,11 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
1000
1000
}
1001
1001
1002
1002
( self . outputs_to_watch . len ( ) as u64 ) . write ( writer) ?;
1003
- for ( txid, output_scripts ) in self . outputs_to_watch . iter ( ) {
1003
+ for ( txid, idx_scripts ) in self . outputs_to_watch . iter ( ) {
1004
1004
txid. write ( writer) ?;
1005
- ( output_scripts. len ( ) as u64 ) . write ( writer) ?;
1006
- for script in output_scripts. iter ( ) {
1005
+ ( idx_scripts. len ( ) as u64 ) . write ( writer) ?;
1006
+ for ( idx, script) in idx_scripts. iter ( ) {
1007
+ idx. write ( writer) ?;
1007
1008
script. write ( writer) ?;
1008
1009
}
1009
1010
}
@@ -1292,7 +1293,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1292
1293
/// transaction), which we must learn about spends of via block_connected().
1293
1294
///
1294
1295
/// (C-not exported) because we have no HashMap bindings
1295
- pub fn get_outputs_to_watch ( & self ) -> & HashMap < Txid , Vec < Script > > {
1296
+ pub fn get_outputs_to_watch ( & self ) -> & HashMap < Txid , Vec < ( u32 , Script ) > > {
1296
1297
& self . outputs_to_watch
1297
1298
}
1298
1299
@@ -1305,7 +1306,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1305
1306
pub fn get_monitored_outpoints ( & self ) -> Vec < ( Txid , u32 , & Script ) > {
1306
1307
let mut res = Vec :: with_capacity ( self . counterparty_commitment_txn_on_chain . len ( ) * 2 ) ;
1307
1308
for ( ref txid, & ( _, ref outputs) ) in self . counterparty_commitment_txn_on_chain . iter ( ) {
1308
- for ( idx, output) in outputs. iter ( ) . enumerate ( ) {
1309
+ for ( idx, output) in outputs. iter ( ) . enumerate ( ) {
1309
1310
res. push ( ( ( * txid) . clone ( ) , idx as u32 , output) ) ;
1310
1311
}
1311
1312
}
@@ -1354,8 +1355,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1354
1355
/// height > height + CLTV_SHARED_CLAIM_BUFFER. In any case, will install monitoring for
1355
1356
/// HTLC-Success/HTLC-Timeout transactions.
1356
1357
/// Return updates for HTLC pending in the channel and failed automatically by the broadcast of
1357
- /// revoked counterparty commitment tx
1358
- fn check_spend_counterparty_transaction < L : Deref > ( & mut self , tx : & Transaction , height : u32 , logger : & L ) -> ( Vec < OnchainRequest > , ( Txid , Vec < TxOut > ) ) where L :: Target : Logger {
1358
+ /// revoked remote commitment tx
1359
+ fn check_spend_counterparty_transaction < L : Deref > ( & mut self , tx : & Transaction , height : u32 , logger : & L ) -> ( Vec < OnchainRequest > , ( Txid , Vec < ( u32 , TxOut ) > ) ) where L :: Target : Logger {
1359
1360
// Most secp and related errors trying to create keys means we have no hope of constructing
1360
1361
// a spend transaction...so we return no transactions to broadcast
1361
1362
let mut claimable_outpoints = Vec :: new ( ) ;
@@ -1410,7 +1411,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1410
1411
if !claimable_outpoints. is_empty ( ) || per_commitment_option. is_some ( ) { // ie we're confident this is actually ours
1411
1412
// We're definitely a counterparty commitment transaction!
1412
1413
log_trace ! ( logger, "Got broadcast of revoked counterparty commitment transaction, going to generate general spend tx with {} inputs" , claimable_outpoints. len( ) ) ;
1413
- watch_outputs. append ( & mut tx. output . clone ( ) ) ;
1414
+ for ( idx, outp) in tx. output . iter ( ) . enumerate ( ) {
1415
+ watch_outputs. push ( ( idx as u32 , outp. clone ( ) ) ) ;
1416
+ }
1414
1417
self . counterparty_commitment_txn_on_chain . insert ( commitment_txid, ( commitment_number, tx. output . iter ( ) . map ( |output| { output. script_pubkey . clone ( ) } ) . collect ( ) ) ) ;
1415
1418
1416
1419
macro_rules! check_htlc_fails {
@@ -1457,7 +1460,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1457
1460
// already processed the block, resulting in the counterparty_commitment_txn_on_chain entry
1458
1461
// not being generated by the above conditional. Thus, to be safe, we go ahead and
1459
1462
// insert it here.
1460
- watch_outputs. append ( & mut tx. output . clone ( ) ) ;
1463
+ for ( idx, outp) in tx. output . iter ( ) . enumerate ( ) {
1464
+ watch_outputs. push ( ( idx as u32 , outp. clone ( ) ) ) ;
1465
+ }
1461
1466
self . counterparty_commitment_txn_on_chain . insert ( commitment_txid, ( commitment_number, tx. output . iter ( ) . map ( |output| { output. script_pubkey . clone ( ) } ) . collect ( ) ) ) ;
1462
1467
1463
1468
log_trace ! ( logger, "Got broadcast of non-revoked counterparty commitment transaction {}" , commitment_txid) ;
@@ -1546,7 +1551,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1546
1551
}
1547
1552
1548
1553
/// Attempts to claim a counterparty HTLC-Success/HTLC-Timeout's outputs using the revocation key
1549
- fn check_spend_counterparty_htlc < L : Deref > ( & mut self , tx : & Transaction , commitment_number : u64 , height : u32 , logger : & L ) -> ( Vec < OnchainRequest > , Option < ( Txid , Vec < TxOut > ) > ) where L :: Target : Logger {
1554
+ fn check_spend_counterparty_htlc < L : Deref > ( & mut self , tx : & Transaction , commitment_number : u64 , height : u32 , logger : & L ) -> ( Vec < OnchainRequest > , Option < ( Txid , Vec < ( u32 , TxOut ) > ) > ) where L :: Target : Logger {
1550
1555
let htlc_txid = tx. txid ( ) ;
1551
1556
if tx. input . len ( ) != 1 || tx. output . len ( ) != 1 || tx. input [ 0 ] . witness . len ( ) != 5 {
1552
1557
return ( Vec :: new ( ) , None )
@@ -1568,10 +1573,11 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1568
1573
log_trace ! ( logger, "Remote HTLC broadcast {}:{}" , htlc_txid, 0 ) ;
1569
1574
let malleable_justice_tx = PackageTemplate :: build_malleable_justice_tx ( per_commitment_point, per_commitment_key, self . counterparty_tx_cache . counterparty_delayed_payment_base_key , self . counterparty_tx_cache . counterparty_htlc_base_key , InputDescriptors :: RevokedOutput , htlc_txid, 0 , tx. output [ 0 ] . value , None , self . counterparty_tx_cache . on_counterparty_tx_csv ) ;
1570
1575
let claimable_outpoints = vec ! ( OnchainRequest { aggregation: true , bump_strategy: BumpStrategy :: RBF , feerate_previous: 0 , height_timer: None , absolute_timelock: height + self . counterparty_tx_cache. on_counterparty_tx_csv as u32 , height_original: height, content: malleable_justice_tx } ) ;
1571
- ( claimable_outpoints, Some ( ( htlc_txid, tx. output . clone ( ) ) ) )
1576
+ let outputs = vec ! [ ( 0 , tx. output[ 0 ] . clone( ) ) ] ;
1577
+ ( claimable_outpoints, Some ( ( htlc_txid, outputs) ) )
1572
1578
}
1573
1579
1574
- fn broadcast_by_holder_state ( & self , commitment_tx : & Transaction , holder_tx : & HolderSignedTx , height : u32 ) -> ( Vec < OnchainRequest > , Vec < TxOut > , Option < ( Script , PublicKey , PublicKey ) > ) {
1580
+ fn broadcast_by_holder_state ( & self , commitment_tx : & Transaction , holder_tx : & HolderSignedTx , height : u32 ) -> ( Vec < OnchainRequest > , Vec < ( u32 , TxOut ) > , Option < ( Script , PublicKey , PublicKey ) > ) {
1575
1581
let mut claim_requests = Vec :: with_capacity ( holder_tx. htlc_outputs . len ( ) ) ;
1576
1582
let mut watch_outputs = Vec :: with_capacity ( holder_tx. htlc_outputs . len ( ) ) ;
1577
1583
@@ -1589,7 +1595,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1589
1595
}
1590
1596
} else { None } , htlc. amount_msat , holder_tx. txid , transaction_output_index) ;
1591
1597
claim_requests. push ( OnchainRequest { aggregation : false , bump_strategy : BumpStrategy :: CPFP , feerate_previous : 0 , height_timer : None , absolute_timelock : height, height_original : height, content : holder_htlc_tx } ) ;
1592
- watch_outputs. push ( commitment_tx. output [ transaction_output_index as usize ] . clone ( ) ) ;
1598
+ watch_outputs. push ( ( transaction_output_index , commitment_tx. output [ transaction_output_index as usize ] . clone ( ) ) ) ;
1593
1599
}
1594
1600
}
1595
1601
@@ -1599,7 +1605,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1599
1605
/// Attempts to claim any claimable HTLCs in a commitment transaction which was not (yet)
1600
1606
/// revoked using data in holder_claimable_outpoints.
1601
1607
/// Should not be used if check_spend_revoked_transaction succeeds.
1602
- fn check_spend_holder_transaction < L : Deref > ( & mut self , tx : & Transaction , height : u32 , logger : & L ) -> ( Vec < OnchainRequest > , ( Txid , Vec < TxOut > ) ) where L :: Target : Logger {
1608
+ fn check_spend_holder_transaction < L : Deref > ( & mut self , tx : & Transaction , height : u32 , logger : & L ) -> ( Vec < OnchainRequest > , ( Txid , Vec < ( u32 , TxOut ) > ) ) where L :: Target : Logger {
1603
1609
let commitment_txid = tx. txid ( ) ;
1604
1610
let mut claim_requests = Vec :: new ( ) ;
1605
1611
let mut watch_outputs = Vec :: new ( ) ;
@@ -1743,7 +1749,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1743
1749
/// Eventually this should be pub and, roughly, implement ChainListener, however this requires
1744
1750
/// &mut self, as well as returns new spendable outputs and outpoints to watch for spending of
1745
1751
/// on-chain.
1746
- fn block_connected < B : Deref , F : Deref , L : Deref , U : Deref > ( & mut self , txn_matched : & [ & Transaction ] , height : u32 , block_hash : & BlockHash , broadcaster : B , fee_estimator : F , logger : L , utxo_pool : U ) -> Vec < ( Txid , Vec < TxOut > ) >
1752
+ fn block_connected < B : Deref , F : Deref , L : Deref , U : Deref > ( & mut self , txn_matched : & [ & Transaction ] , height : u32 , block_hash : & BlockHash , broadcaster : B , fee_estimator : F , logger : L , utxo_pool : U ) -> Vec < ( Txid , Vec < ( u32 , TxOut ) > ) >
1747
1753
where B :: Target : BroadcasterInterface ,
1748
1754
F :: Target : FeeEstimator ,
1749
1755
L :: Target : Logger ,
@@ -1839,8 +1845,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1839
1845
self . onchain_tx_handler . block_connected ( txn_matched, claimable_outpoints, height, & * broadcaster, & * fee_estimator, & * logger, & * utxo_pool) ;
1840
1846
1841
1847
self . last_block_hash = block_hash. clone ( ) ;
1842
- for & ( ref txid, ref output_scripts ) in watch_outputs. iter ( ) {
1843
- self . outputs_to_watch . insert ( txid. clone ( ) , output_scripts . iter ( ) . map ( |o| o . script_pubkey . clone ( ) ) . collect ( ) ) ;
1848
+ for & ( ref txid, ref idx_scripts ) in watch_outputs. iter ( ) {
1849
+ self . outputs_to_watch . insert ( txid. clone ( ) , idx_scripts . iter ( ) . map ( |o| ( o . 0 , o . 1 . script_pubkey . clone ( ) ) ) . collect ( ) ) ;
1844
1850
}
1845
1851
1846
1852
watch_outputs
@@ -2361,13 +2367,13 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
2361
2367
}
2362
2368
2363
2369
let outputs_to_watch_len: u64 = Readable :: read ( reader) ?;
2364
- let mut outputs_to_watch = HashMap :: with_capacity ( cmp:: min ( outputs_to_watch_len as usize , MAX_ALLOC_SIZE / ( mem:: size_of :: < Txid > ( ) + mem:: size_of :: < Vec < Script > > ( ) ) ) ) ;
2370
+ let mut outputs_to_watch = HashMap :: with_capacity ( cmp:: min ( outputs_to_watch_len as usize , MAX_ALLOC_SIZE / ( mem:: size_of :: < Txid > ( ) + mem:: size_of :: < u32 > ( ) + mem :: size_of :: < Vec < Script > > ( ) ) ) ) ;
2365
2371
for _ in 0 ..outputs_to_watch_len {
2366
2372
let txid = Readable :: read ( reader) ?;
2367
2373
let outputs_len: u64 = Readable :: read ( reader) ?;
2368
- let mut outputs = Vec :: with_capacity ( cmp:: min ( outputs_len as usize , MAX_ALLOC_SIZE / mem:: size_of :: < Script > ( ) ) ) ;
2374
+ let mut outputs = Vec :: with_capacity ( cmp:: min ( outputs_len as usize , MAX_ALLOC_SIZE / ( mem:: size_of :: < u32 > ( ) + mem :: size_of :: < Script > ( ) ) ) ) ;
2369
2375
for _ in 0 ..outputs_len {
2370
- outputs. push ( Readable :: read ( reader) ?) ;
2376
+ outputs. push ( ( Readable :: read ( reader) ?, Readable :: read ( reader ) ? ) ) ;
2371
2377
}
2372
2378
if let Some ( _) = outputs_to_watch. insert ( txid, outputs) {
2373
2379
return Err ( DecodeError :: InvalidValue ) ;
0 commit comments