pub fn use_or_load_enr(
    enr_key: &CombinedKey,
    local_enr: &mut Enr,
    config: &NetworkConfig,
    log: &slog::Logger,
) -> Result<(), String> {
    let enr_f = config.network_dir.join(ENR_FILENAME);
    if let Ok(mut enr_file) = File::open(enr_f.clone()) {
        let mut enr_string = String::new();
        match enr_file.read_to_string(&mut enr_string) {
            Err(_) => debug!(log, "Could not read ENR from file"),
            Ok(_) => {
                match Enr::from_str(&enr_string) {
                    Ok(disk_enr) => {
                        // if the same node id, then we may need to update our sequence number
                        if local_enr.node_id() == disk_enr.node_id() {
                            if compare_enr(&local_enr, &disk_enr) {
                                debug!(log, "ENR loaded from disk"; "file" => ?enr_f);
                                // the stored ENR has the same configuration, use it
                                *local_enr = disk_enr;
                                return Ok(());
                            }

                            // same node id, different configuration - update the sequence number
                            // Note: local_enr is generated with default(0) attnets value,
                            // so a non default value in persisted enr will also update sequence number.
                            let new_seq_no = disk_enr.seq().checked_add(1).ok_or("ENR sequence number on file is too large. Remove it to generate a new NodeId")?;
                            local_enr.set_seq(new_seq_no, enr_key).map_err(|e| {
                                format!("Could not update ENR sequence number: {:?}", e)
                            })?;
                            debug!(log, "ENR sequence number increased"; "seq" =>  new_seq_no);
                        }
                    }
                    Err(e) => {
                        warn!(log, "ENR from file could not be decoded"; "error" => ?e);
                    }
                }
            }
        }
    }

    save_enr_to_disk(&config.network_dir, &local_enr, log);

    Ok(())
}
/// Either use the given ENR or load an ENR from file if it exists and matches the current NodeId
/// and sequence number.
/// If an ENR exists, with the same NodeId, this function checks to see if the loaded ENR from
/// disk is suitable to use, otherwise we increment the given ENR's sequence number.
Either use the given ENR or load an ENR from file if it exists and matches the current NodeId
and sequence number.
If an ENR exists, with the same NodeId, this function checks to see if the loaded ENR from
disk is suitable to use, otherwise we increment the given ENR's sequence number.
Either use the given ENR or load an ENR from file if it exists and matches the current NodeId and sequence number . If an ENR exists with the same NodeId this function checks to see if the loaded ENR from disk is suitable to use otherwise we increment the given ENR ' s sequence number .
pub fn use_or_load_enr(
    enr_key: &CombinedKey,
    local_enr: &mut Enr,
    config: &NetworkConfig,
    log: &slog::Logger,
) -> Result<(), String> {
    let enr_f = config.network_dir.join(ENR_FILENAME);
    if let Ok(mut enr_file) = File::open(enr_f.clone()) {
        let mut enr_string = String::new();
        match enr_file.read_to_string(&mut enr_string) {
            Err(_) => debug!(log, "Could not read ENR from file"),
            Ok(_) => {
                match Enr::from_str(&enr_string) {
                    Ok(disk_enr) => {
                        // if the same node id, then we may need to update our sequence number
                        if local_enr.node_id() == disk_enr.node_id() {
                            if compare_enr(&local_enr, &disk_enr) {
                                debug!(log, "ENR loaded from disk"; "file" => ?enr_f);
                                // the stored ENR has the same configuration, use it
                                *local_enr = disk_enr;
                                return Ok(());
                            }
                            // same node id, different configuration - update the sequence number
                            // Note: local_enr is generated with default(0) attnets value,
                            // so a non default value in persisted enr will also update sequence number.
                            let new_seq_no = disk_enr.seq().checked_add(1).ok_or("ENR sequence number on file is too large. Remove it to generate a new NodeId")?;
                            local_enr.set_seq(new_seq_no, enr_key).map_err(|e| {
                                format!("Could not update ENR sequence number: {:?}", e)
                            })?;
                            debug!(log, "ENR sequence number increased"; "seq" =>  new_seq_no);
                        }
                    }
                    Err(e) => {
                        warn!(log, "ENR from file could not be decoded"; "error" => ?e);
                    }
                }
            }
        }
    }
    save_enr_to_disk(&config.network_dir, &local_enr, log);
    Ok(())
}
pub fn use_or_load_enr ( enr_key : & CombinedKey , local_enr : & mut Enr , config : & NetworkConfig , log : & slog :: Logger , ) -> Result < ( ) , String > { let enr_f = config . network_dir . join ( ENR_FILENAME ) ; if let Ok ( mut enr_file ) = File :: open ( enr_f . clone ( ) ) { let mut enr_string = String :: new ( ) ; match enr_file . read_to_string ( & mut enr_string ) { Err ( _ ) => debug ! ( log " " ) , Ok ( _ ) => { match Enr :: from_str ( & enr_string ) { Ok ( disk_enr ) => { // if the same node id, then we may need to update our sequence number if local_enr . node_id ( ) == disk_enr . node_id ( ) { if compare_enr ( & local_enr , & disk_enr ) { debug ! ( log " " " " enr_f ) ; // the stored ENR has the same configuration, use it * local_enr = disk_enr ; return Ok ( ( ) ) ; } // same node id, different configuration - update the sequence number // Note: local_enr is generated with default(0) attnets value, // so a non default value in persisted enr will also update sequence number. let new_seq_no = disk_enr . seq ( ) . checked_add ( 1 ) . ok_or ( " " ) ? ; local_enr . set_seq ( new_seq_no , enr_key ) . map_err ( | e | { format ! ( " " e ) } ) ? ; debug ! ( log " " " " new_seq_no ) ; } } Err ( e ) => { warn ! ( log " " " " e ) ; } } } } } save_enr_to_disk ( & config . network_dir , & local_enr , log ) ; Ok ( ( ) ) }
Either use the given ENR or load an ENR from file if it exists and matches the current NodeId
and sequence number.
Either use the given ENR or load an ENR from file if it exists and matches the current NodeId and sequence number .
enr_key
&CombinedKey
local_enr
&mut Enr
config
&NetworkConfig
log
&slog::Logger