Skip to content

Commit 60f1455

Browse files
committed
fix(l1): update existing contact ENR on NODES response
1 parent 38e11d1 commit 60f1455

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

crates/networking/p2p/discv5/peer_table.rs

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -843,28 +843,45 @@ impl PeerTableServer {
843843

844844
async fn new_contact_records(&mut self, node_records: Vec<NodeRecord>, local_node_id: H256) {
845845
for node_record in node_records {
846+
if !node_record.verify_signature() {
847+
continue;
848+
}
846849
if let Ok(node) = Node::from_enr(&node_record) {
847850
let node_id = node.node_id();
848-
if let Entry::Vacant(vacant_entry) = self.contacts.entry(node_id)
849-
&& !self.discarded_contacts.contains(&node_id)
850-
&& node_id != local_node_id
851-
{
852-
let mut contact = Contact::from(node);
853-
let is_fork_id_valid =
854-
if let Some(remote_fork_id) = node_record.decode_pairs().eth {
855-
backend::is_fork_id_valid(&self.store, &remote_fork_id)
856-
.await
857-
.ok()
858-
.or(Some(false))
859-
} else {
860-
Some(false)
861-
};
862-
contact.is_fork_id_valid = is_fork_id_valid;
863-
contact.record = Some(node_record);
864-
vacant_entry.insert(contact);
865-
METRICS.record_new_discovery().await;
851+
if self.discarded_contacts.contains(&node_id) || node_id == local_node_id {
852+
continue;
853+
}
854+
let is_fork_id_valid =
855+
if let Some(remote_fork_id) = node_record.decode_pairs().eth {
856+
backend::is_fork_id_valid(&self.store, &remote_fork_id)
857+
.await
858+
.ok()
859+
.or(Some(false))
860+
} else {
861+
Some(false)
862+
};
863+
match self.contacts.entry(node_id) {
864+
Entry::Vacant(vacant_entry) => {
865+
let mut contact = Contact::from(node);
866+
contact.is_fork_id_valid = is_fork_id_valid;
867+
contact.record = Some(node_record);
868+
vacant_entry.insert(contact);
869+
METRICS.record_new_discovery().await;
870+
}
871+
Entry::Occupied(mut occupied_entry) => {
872+
let existing_seq = occupied_entry
873+
.get()
874+
.record
875+
.as_ref()
876+
.map_or(0, |r| r.seq);
877+
if node_record.seq > existing_seq {
878+
let contact = occupied_entry.get_mut();
879+
contact.node = node;
880+
contact.record = Some(node_record);
881+
contact.is_fork_id_valid = is_fork_id_valid;
882+
}
883+
}
866884
}
867-
// TODO Handle the case the contact is already present
868885
}
869886
}
870887
}

0 commit comments

Comments
 (0)