@@ -28,10 +28,11 @@ use bitcoin::hash_types::{BlockHash, Txid};
28
28
29
29
use crate :: chain;
30
30
use crate :: chain:: chaininterface:: { BroadcasterInterface , FeeEstimator } ;
31
- #[ allow( unused_imports) ]
31
+ #[ cfg( peer_storage) ]
32
+ use crate :: chain:: channelmonitor:: write_chanmon_internal;
32
33
use crate :: chain:: channelmonitor:: {
33
- write_chanmon_internal , Balance , ChannelMonitor , ChannelMonitorUpdate , MonitorEvent ,
34
- TransactionOutputs , WithChannelMonitor ,
34
+ Balance , ChannelMonitor , ChannelMonitorUpdate , MonitorEvent , TransactionOutputs ,
35
+ WithChannelMonitor ,
35
36
} ;
36
37
use crate :: chain:: transaction:: { OutPoint , TransactionData } ;
37
38
use crate :: chain:: { ChannelMonitorUpdateStatus , Filter , WatchedOutput } ;
@@ -48,7 +49,7 @@ use crate::types::features::{InitFeatures, NodeFeatures};
48
49
use crate :: util:: errors:: APIError ;
49
50
use crate :: util:: logger:: { Logger , WithContext } ;
50
51
use crate :: util:: persist:: MonitorName ;
51
- #[ allow ( unused_imports ) ]
52
+ #[ cfg ( peer_storage ) ]
52
53
use crate :: util:: ser:: { VecWriter , Writeable } ;
53
54
use crate :: util:: wakers:: { Future , Notifier } ;
54
55
use bitcoin:: secp256k1:: PublicKey ;
@@ -812,50 +813,54 @@ where
812
813
mon. values ( ) . map ( |monitor| monitor. monitor . get_counterparty_node_id ( ) ) . collect ( )
813
814
}
814
815
816
+ #[ cfg( peer_storage) ]
815
817
fn send_peer_storage ( & self , their_node_id : PublicKey ) {
816
818
#[ allow( unused_mut) ]
817
819
let mut monitors_list: Vec < PeerStorageMonitorHolder > = Vec :: new ( ) ;
818
820
let random_bytes = self . entropy_source . get_secure_random_bytes ( ) ;
819
821
820
- #[ cfg( peer_storage) ]
822
+ const MAX_PEER_STORAGE_SIZE : usize = 65531 ;
823
+ const USIZE_LEN : usize = core:: mem:: size_of :: < usize > ( ) ;
824
+ let mut usize_bytes = [ 0u8 ; USIZE_LEN ] ;
825
+ usize_bytes. copy_from_slice ( & random_bytes[ 0 ..USIZE_LEN ] ) ;
826
+ let random_usize = usize:: from_le_bytes ( usize_bytes) ;
827
+
828
+ let mut curr_size = 0 ;
829
+ let monitors = self . monitors . read ( ) . unwrap ( ) ;
830
+ let mut stored_chanmon_idx = alloc:: collections:: BTreeSet :: < usize > :: new ( ) ;
831
+ // Used as a fallback reference if the set is empty
832
+ let zero = 0 ;
833
+
834
+ while curr_size < MAX_PEER_STORAGE_SIZE
835
+ && * stored_chanmon_idx. last ( ) . unwrap_or ( & zero) < monitors. len ( )
821
836
{
822
- const MAX_PEER_STORAGE_SIZE : usize = 65531 ;
823
- const USIZE_LEN : usize = core:: mem:: size_of :: < usize > ( ) ;
824
- let random_usize = usize:: from_le_bytes ( random_bytes[ 0 ..USIZE_LEN ] . try_into ( ) . unwrap ( ) ) ;
825
- let mut curr_size = 0 ;
826
- let monitors = self . monitors . read ( ) . unwrap ( ) ;
827
- // Randomising Keys in the HashMap to fetch monitors without repetition.
828
- let mut keys: Vec < & ChannelId > = monitors. keys ( ) . collect ( ) ;
829
- for i in ( 1 ..keys. len ( ) ) . rev ( ) {
830
- let j = random_usize % ( i + 1 ) ;
831
- keys. swap ( i, j) ;
832
- }
837
+ let idx = random_usize % monitors. len ( ) ;
838
+ stored_chanmon_idx. insert ( idx + 1 ) ;
839
+ let ( cid, mon) = monitors. iter ( ) . skip ( idx) . next ( ) . unwrap ( ) ;
833
840
834
- for chan_id in keys {
835
- let mon = & monitors[ chan_id] ;
836
- let mut ser_chan = VecWriter ( Vec :: new ( ) ) ;
837
- let min_seen_secret = mon. monitor . get_min_seen_secret ( ) ;
838
- let counterparty_node_id = mon. monitor . get_counterparty_node_id ( ) ;
841
+ let mut ser_chan = VecWriter ( Vec :: new ( ) ) ;
842
+ let min_seen_secret = mon. monitor . get_min_seen_secret ( ) ;
843
+ let counterparty_node_id = mon. monitor . get_counterparty_node_id ( ) ;
844
+ {
839
845
let chan_mon = mon. monitor . inner . lock ( ) . unwrap ( ) ;
840
846
841
847
write_chanmon_internal ( & chan_mon, true , & mut ser_chan)
842
848
. expect ( "can not write Channel Monitor for peer storage message" ) ;
843
-
844
- let peer_storage_monitor = PeerStorageMonitorHolder {
845
- channel_id : * chan_id,
846
- min_seen_secret,
847
- counterparty_node_id,
848
- monitor_bytes : ser_chan. 0 ,
849
- } ;
850
-
851
- // Adding size of peer_storage_monitor.
852
- curr_size += peer_storage_monitor. serialized_length ( ) ;
853
-
854
- if curr_size > MAX_PEER_STORAGE_SIZE {
855
- break ;
856
- }
857
- monitors_list. push ( peer_storage_monitor) ;
858
849
}
850
+ let peer_storage_monitor = PeerStorageMonitorHolder {
851
+ channel_id : * cid,
852
+ min_seen_secret,
853
+ counterparty_node_id,
854
+ monitor_bytes : ser_chan. 0 ,
855
+ } ;
856
+
857
+ // Adding size of peer_storage_monitor.
858
+ curr_size += peer_storage_monitor. serialized_length ( ) ;
859
+
860
+ if curr_size > MAX_PEER_STORAGE_SIZE {
861
+ break ;
862
+ }
863
+ monitors_list. push ( peer_storage_monitor) ;
859
864
}
860
865
861
866
let serialised_channels = monitors_list. encode ( ) ;
@@ -965,6 +970,7 @@ where
965
970
)
966
971
} ) ;
967
972
973
+ #[ cfg( peer_storage) ]
968
974
// Send peer storage everytime a new block arrives.
969
975
for node_id in self . all_counterparty_node_ids ( ) {
970
976
self . send_peer_storage ( node_id) ;
@@ -1066,6 +1072,7 @@ where
1066
1072
)
1067
1073
} ) ;
1068
1074
1075
+ #[ cfg( peer_storage) ]
1069
1076
// Send peer storage everytime a new block arrives.
1070
1077
for node_id in self . all_counterparty_node_ids ( ) {
1071
1078
self . send_peer_storage ( node_id) ;
0 commit comments