From 094a68e41d40cd3f863e2748691bb3fce99d8fb6 Mon Sep 17 00:00:00 2001 From: NagyZoltanPeter <113987313+NagyZoltanPeter@users.noreply.github.com> Date: Mon, 12 May 2025 15:23:19 +0200 Subject: [PATCH] fix: addPeer could unintentionally override metadata of previously stored peer with defaults and empty (#3403) * fix: addPeer could unintentionally override metadata of previously stored peer with defaults and empty * Add explanation why we discard updates of different peerStore books. Co-authored-by: Ivan FB <128452529+Ivansete-status@users.noreply.github.com> --------- Co-authored-by: Ivan FB <128452529+Ivansete-status@users.noreply.github.com> --- waku/node/peer_manager/waku_peer_store.nim | 27 ++++++++++++++-------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/waku/node/peer_manager/waku_peer_store.nim b/waku/node/peer_manager/waku_peer_store.nim index 777e4f2be..ee339e858 100644 --- a/waku/node/peer_manager/waku_peer_store.nim +++ b/waku/node/peer_manager/waku_peer_store.nim @@ -100,16 +100,23 @@ proc addPeer*(peerStore: PeerStore, peer: RemotePeerInfo, origin = UnknownOrigin protos.add($new_proto) peerStore[ProtoBook][peer.peerId] = protos - peerStore[AgentBook][peer.peerId] = peer.agent - peerStore[ProtoVersionBook][peer.peerId] = peer.protoVersion - peerStore[KeyBook][peer.peerId] = peer.publicKey - peerStore[ConnectionBook][peer.peerId] = peer.connectedness - peerStore[DisconnectBook][peer.peerId] = peer.disconnectTime - peerStore[SourceBook][peer.peerId] = - if origin != UnknownOrigin: origin else: peer.origin - peerStore[DirectionBook][peer.peerId] = peer.direction - peerStore[LastFailedConnBook][peer.peerId] = peer.lastFailedConn - peerStore[NumberFailedConnBook][peer.peerId] = peer.numberFailedConn + ## We don't care whether the item was already present in the table or not. Hence, we always discard the hasKeyOrPut's bool returned value + discard peerStore[AgentBook].book.hasKeyOrPut(peer.peerId, peer.agent) + discard peerStore[ProtoVersionBook].book.hasKeyOrPut(peer.peerId, peer.protoVersion) + discard peerStore[KeyBook].book.hasKeyOrPut(peer.peerId, peer.publicKey) + + discard peerStore[ConnectionBook].book.hasKeyOrPut(peer.peerId, peer.connectedness) + discard peerStore[DisconnectBook].book.hasKeyOrPut(peer.peerId, peer.disconnectTime) + if origin != UnknownOrigin: + peerStore[SourceBook][peer.peerId] = origin + else: + discard peerStore[SourceBook].book.hasKeyOrPut(peer.peerId, peer.origin) + + discard peerStore[DirectionBook].book.hasKeyOrPut(peer.peerId, peer.direction) + discard + peerStore[LastFailedConnBook].book.hasKeyOrPut(peer.peerId, peer.lastFailedConn) + discard + peerStore[NumberFailedConnBook].book.hasKeyOrPut(peer.peerId, peer.numberFailedConn) if peer.enr.isSome(): peerStore[ENRBook][peer.peerId] = peer.enr.get()