@@ -15,7 +15,7 @@ use crate::{
1515} ;
1616use bytes:: BytesMut ;
1717use ethrex_common:: { H256 , H512 } ;
18- use ethrex_storage:: Store ;
18+ use ethrex_storage:: { Store , error :: StoreError } ;
1919use futures:: StreamExt ;
2020use rand:: rngs:: OsRng ;
2121use secp256k1:: SecretKey ;
@@ -44,6 +44,7 @@ pub const INITIAL_LOOKUP_INTERVAL: Duration = Duration::from_millis(100); // 10
4444pub const LOOKUP_INTERVAL : Duration = Duration :: from_millis ( 600 ) ; // 100 per minute
4545const CHANGE_FIND_NODE_MESSAGE_INTERVAL : Duration = Duration :: from_secs ( 5 ) ;
4646const PRUNE_INTERVAL : Duration = Duration :: from_secs ( 5 ) ;
47+ const UPDATE_FORKID_INTERVAL : Duration = Duration :: from_secs ( 5 ) ;
4748
4849#[ derive( Debug , thiserror:: Error ) ]
4950pub enum DiscoveryServerError {
@@ -59,6 +60,8 @@ pub enum DiscoveryServerError {
5960 InvalidContact ,
6061 #[ error( transparent) ]
6162 PeerTable ( #[ from] PeerTableError ) ,
63+ #[ error( transparent) ]
64+ Store ( #[ from] StoreError ) ,
6265}
6366
6467#[ derive( Debug , Clone ) ]
@@ -69,6 +72,7 @@ pub enum InMessage {
6972 EnrLookup ,
7073 Prune ,
7174 ChangeFindNodeMessage ,
75+ UpdateForkId ,
7276 Shutdown ,
7377}
7478
@@ -79,6 +83,7 @@ pub enum OutMessage {
7983
8084#[ derive( Debug ) ]
8185pub struct DiscoveryServer {
86+ storage : Store ,
8287 local_node : Node ,
8388 local_node_record : NodeRecord ,
8489 signer : SecretKey ,
@@ -103,12 +108,14 @@ impl DiscoveryServer {
103108 let mut local_node_record = NodeRecord :: from_node ( & local_node, 1 , & signer)
104109 . expect ( "Failed to create local node record" ) ;
105110 if let Ok ( fork_id) = storage. get_fork_id ( ) . await {
111+ trace ! ( fork_id=?fork_id, "Creating Initial ForkId" ) ;
106112 local_node_record
107113 . set_fork_id ( fork_id, & signer)
108114 . expect ( "Failed to set fork_id on local node record" ) ;
109115 }
110116
111117 let mut discovery_server = Self {
118+ storage,
112119 local_node : local_node. clone ( ) ,
113120 local_node_record,
114121 signer,
@@ -508,6 +515,28 @@ impl DiscoveryServer {
508515 Ok ( ( ) )
509516 }
510517
518+ async fn update_forkid ( & mut self ) -> Result < ( ) , DiscoveryServerError > {
519+ let latest_block_number = self . storage . get_latest_block_number ( ) . await ?;
520+ let block_header = self
521+ . storage
522+ . get_block_header ( latest_block_number) ?
523+ . ok_or ( StoreError :: MissingLatestBlockNumber ) ?;
524+
525+ let mut pairs = self . local_node_record . decode_pairs ( ) ;
526+
527+ trace ! ( fork_id=?& pairs. eth, latest_block_number=latest_block_number, latest_block_timestamp=block_header. timestamp, "Updating ForkId" ) ;
528+
529+ if let Some ( fork_id) = pairs. eth
530+ && fork_id. fork_next <= block_header. timestamp
531+ {
532+ let latest_fork_id = self . storage . get_fork_id ( ) . await ?;
533+ pairs. eth = Some ( latest_fork_id) ;
534+
535+ self . local_node_record . pairs = pairs. into ( ) ;
536+ }
537+ Ok ( ( ) )
538+ }
539+
511540 async fn validate_contact (
512541 & mut self ,
513542 sender_public_key : H512 ,
@@ -592,6 +621,11 @@ impl GenServer for DiscoveryServer {
592621 handle. clone ( ) ,
593622 InMessage :: ChangeFindNodeMessage ,
594623 ) ;
624+ send_interval (
625+ UPDATE_FORKID_INTERVAL ,
626+ handle. clone ( ) ,
627+ InMessage :: UpdateForkId ,
628+ ) ;
595629 let _ = handle. clone ( ) . cast ( InMessage :: Lookup ) . await ;
596630 let _ = handle. clone ( ) . cast ( InMessage :: EnrLookup ) . await ;
597631 send_message_on ( handle. clone ( ) , tokio:: signal:: ctrl_c ( ) , InMessage :: Shutdown ) ;
@@ -638,6 +672,12 @@ impl GenServer for DiscoveryServer {
638672 let interval = self . get_lookup_interval ( ) . await ;
639673 send_after ( interval, handle. clone ( ) , Self :: CastMsg :: EnrLookup ) ;
640674 }
675+ Self :: CastMsg :: UpdateForkId => {
676+ trace ! ( received = "UpdateForkId" ) ;
677+ let _ = self . update_forkid ( ) . await . inspect_err (
678+ |e| error ! ( error=?e, "Error updating forkid of local node record" ) ,
679+ ) ;
680+ }
641681 Self :: CastMsg :: Prune => {
642682 trace ! ( received = "Prune" ) ;
643683 let _ = self
0 commit comments