@@ -16,6 +16,7 @@ use bitcoin::blockdata::transaction::{TxIn,TxOut,SigHashType,Transaction};
16
16
use bitcoin:: blockdata:: transaction:: OutPoint as BitcoinOutPoint ;
17
17
use bitcoin:: blockdata:: script:: Script ;
18
18
use bitcoin:: network:: serialize;
19
+ use bitcoin:: network:: serialize:: BitcoinHash ;
19
20
use bitcoin:: network:: encodable:: { ConsensusDecodable , ConsensusEncodable } ;
20
21
use bitcoin:: util:: hash:: Sha256dHash ;
21
22
use bitcoin:: util:: bip143;
@@ -114,12 +115,13 @@ pub struct SimpleManyChannelMonitor<Key> {
114
115
}
115
116
116
117
impl < Key : Send + cmp:: Eq + hash:: Hash > ChainListener for SimpleManyChannelMonitor < Key > {
117
- fn block_connected ( & self , _header : & BlockHeader , height : u32 , txn_matched : & [ & Transaction ] , _indexes_of_txn_matched : & [ u32 ] ) {
118
+ fn block_connected ( & self , header : & BlockHeader , height : u32 , txn_matched : & [ & Transaction ] , _indexes_of_txn_matched : & [ u32 ] ) {
119
+ let block_hash = header. bitcoin_hash ( ) ;
118
120
let mut new_events: Vec < events:: Event > = Vec :: with_capacity ( 0 ) ;
119
121
{
120
- let monitors = self . monitors . lock ( ) . unwrap ( ) ;
121
- for monitor in monitors. values ( ) {
122
- let ( txn_outputs, spendable_outputs) = monitor. block_connected ( txn_matched, height, & * self . broadcaster ) ;
122
+ let mut monitors = self . monitors . lock ( ) . unwrap ( ) ;
123
+ for monitor in monitors. values_mut ( ) {
124
+ let ( txn_outputs, spendable_outputs) = monitor. block_connected ( txn_matched, height, & block_hash , & * self . broadcaster ) ;
123
125
if spendable_outputs. len ( ) > 0 {
124
126
new_events. push ( events:: Event :: SpendableOutputs {
125
127
outputs : spendable_outputs,
@@ -279,6 +281,12 @@ pub struct ChannelMonitor {
279
281
280
282
destination_script : Script ,
281
283
284
+ // We simply modify last_block_hash in Channel's block_connected so that serialization is
285
+ // consistent but hopefully the users' copy handles block_connected in a consistent way.
286
+ // (we do *not*, however, update them in insert_combine to ensure any local user copies keep
287
+ // their last_block_hash from its state and not based on updated copies that didn't run through
288
+ // the full block_connected).
289
+ pub ( crate ) last_block_hash : Sha256dHash ,
282
290
secp_ctx : Secp256k1 < secp256k1:: All > , //TODO: dedup this a bit...
283
291
logger : Arc < Logger > ,
284
292
}
@@ -307,6 +315,7 @@ impl Clone for ChannelMonitor {
307
315
payment_preimages : self . payment_preimages . clone ( ) ,
308
316
309
317
destination_script : self . destination_script . clone ( ) ,
318
+ last_block_hash : self . last_block_hash . clone ( ) ,
310
319
secp_ctx : self . secp_ctx . clone ( ) ,
311
320
logger : self . logger . clone ( ) ,
312
321
}
@@ -378,6 +387,7 @@ impl ChannelMonitor {
378
387
payment_preimages : HashMap :: new ( ) ,
379
388
destination_script : destination_script,
380
389
390
+ last_block_hash : Default :: default ( ) ,
381
391
secp_ctx : Secp256k1 :: new ( ) ,
382
392
logger,
383
393
}
@@ -759,17 +769,30 @@ impl ChannelMonitor {
759
769
writer. write_all ( payment_preimage) ?;
760
770
}
761
771
772
+ self . last_block_hash . write ( writer) ?;
762
773
self . destination_script . write ( writer) ?;
763
774
764
775
Ok ( ( ) )
765
776
}
766
777
767
778
/// Writes this monitor into the given writer, suitable for writing to disk.
779
+ ///
780
+ /// Note that the deserializer is only implemented for (Sha256dHash, ChannelMonitor), which
781
+ /// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
782
+ /// the "reorg path" (ie not just starting at the same height but starting at the highest
783
+ /// common block that appears on your best chain as well as on the chain which contains the
784
+ /// last block hash returned) upon deserializing the object!
768
785
pub fn write_for_disk < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
769
786
self . write ( writer, true )
770
787
}
771
788
772
789
/// Encodes this monitor into the given writer, suitable for sending to a remote watchtower
790
+ ///
791
+ /// Note that the deserializer is only implemented for (Sha256dHash, ChannelMonitor), which
792
+ /// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
793
+ /// the "reorg path" (ie not just starting at the same height but starting at the highest
794
+ /// common block that appears on your best chain as well as on the chain which contains the
795
+ /// last block hash returned) upon deserializing the object!
773
796
pub fn write_for_watchtower < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
774
797
self . write ( writer, false )
775
798
}
@@ -1283,7 +1306,7 @@ impl ChannelMonitor {
1283
1306
( Vec :: new ( ) , Vec :: new ( ) )
1284
1307
}
1285
1308
1286
- fn block_connected ( & self , txn_matched : & [ & Transaction ] , height : u32 , broadcaster : & BroadcasterInterface ) -> ( Vec < ( Sha256dHash , Vec < TxOut > ) > , Vec < SpendableOutputDescriptor > ) {
1309
+ fn block_connected ( & mut self , txn_matched : & [ & Transaction ] , height : u32 , block_hash : & Sha256dHash , broadcaster : & BroadcasterInterface ) -> ( Vec < ( Sha256dHash , Vec < TxOut > ) > , Vec < SpendableOutputDescriptor > ) {
1287
1310
let mut watch_outputs = Vec :: new ( ) ;
1288
1311
let mut spendable_outputs = Vec :: new ( ) ;
1289
1312
for tx in txn_matched {
@@ -1344,6 +1367,7 @@ impl ChannelMonitor {
1344
1367
}
1345
1368
}
1346
1369
}
1370
+ self . last_block_hash = block_hash. clone ( ) ;
1347
1371
( watch_outputs, spendable_outputs)
1348
1372
}
1349
1373
@@ -1382,7 +1406,7 @@ impl ChannelMonitor {
1382
1406
1383
1407
const MAX_ALLOC_SIZE : usize = 64 * 1024 ;
1384
1408
1385
- impl < R : :: std:: io:: Read > ReadableArgs < R , Arc < Logger > > for ChannelMonitor {
1409
+ impl < R : :: std:: io:: Read > ReadableArgs < R , Arc < Logger > > for ( Sha256dHash , ChannelMonitor ) {
1386
1410
fn read ( reader : & mut R , logger : Arc < Logger > ) -> Result < Self , DecodeError > {
1387
1411
let secp_ctx = Secp256k1 :: new ( ) ;
1388
1412
macro_rules! unwrap_obj {
@@ -1578,9 +1602,10 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for ChannelMonitor {
1578
1602
}
1579
1603
}
1580
1604
1605
+ let last_block_hash: Sha256dHash = Readable :: read ( reader) ?;
1581
1606
let destination_script = Readable :: read ( reader) ?;
1582
1607
1583
- Ok ( ChannelMonitor {
1608
+ Ok ( ( last_block_hash . clone ( ) , ChannelMonitor {
1584
1609
funding_txo,
1585
1610
commitment_transaction_number_obscure_factor,
1586
1611
@@ -1603,9 +1628,10 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for ChannelMonitor {
1603
1628
payment_preimages,
1604
1629
1605
1630
destination_script,
1631
+ last_block_hash,
1606
1632
secp_ctx,
1607
1633
logger,
1608
- } )
1634
+ } ) )
1609
1635
}
1610
1636
1611
1637
}
0 commit comments