diff --git a/tests/v2/test_peer_storage.nim b/tests/v2/test_peer_storage.nim index 65fac6e34..38ae5c002 100644 --- a/tests/v2/test_peer_storage.nim +++ b/tests/v2/test_peer_storage.nim @@ -19,7 +19,7 @@ suite "Peer Storage": peerKey = crypto.PrivateKey.random(ECDSA, rng[]).get() peer = PeerInfo.new(peerKey, @[peerLoc]) peerProto = "/waku/2/default-waku/codec" - stored = StoredInfo(peerId: peer.peerId, addrs: toHashSet([peerLoc]), protos: toHashSet([peerProto]), publicKey: peerKey.getPublicKey().tryGet()) + stored = StoredInfo(peerId: peer.peerId, addrs: @[peerLoc], protos: @[peerProto], publicKey: peerKey.getPublicKey().tryGet()) conn = Connectedness.CanConnect disconn = 999999 diff --git a/vendor/nim-chronos b/vendor/nim-chronos index 875d7d8e6..b3548583f 160000 --- a/vendor/nim-chronos +++ b/vendor/nim-chronos @@ -1 +1 @@ -Subproject commit 875d7d8e6ef0803ae1c331dbf76b1981b0caeb15 +Subproject commit b3548583fcc768d93654685e7ea55126c1752c29 diff --git a/vendor/nim-libp2p b/vendor/nim-libp2p index 13503f379..36f3132d9 160000 --- a/vendor/nim-libp2p +++ b/vendor/nim-libp2p @@ -1 +1 @@ -Subproject commit 13503f3799aa7c746e17db0a4af38f8aec4115b8 +Subproject commit 36f3132d9a9346539d8b6969921990c82cb6db2f diff --git a/vendor/nim-sqlite3-abi b/vendor/nim-sqlite3-abi index 07039dd88..2f040a5bf 160000 --- a/vendor/nim-sqlite3-abi +++ b/vendor/nim-sqlite3-abi @@ -1 +1 @@ -Subproject commit 07039dd887c4e5b57367a16f4be3c18763be1d7b +Subproject commit 2f040a5bfcef78f29b72016dfef98706a0f6dc9f diff --git a/vendor/nim-stew b/vendor/nim-stew index cdb1f213d..412a691f5 160000 --- a/vendor/nim-stew +++ b/vendor/nim-stew @@ -1 +1 @@ -Subproject commit cdb1f213d073fd2ecbdaf35a866417657da9294c +Subproject commit 412a691f5d29c93bee8f083d213ee8f2c6578bed diff --git a/vendor/nim-web3 b/vendor/nim-web3 index d260915a0..755b6dc92 160000 --- a/vendor/nim-web3 +++ b/vendor/nim-web3 @@ -1 +1 @@ -Subproject commit d260915a0c2111e64ac49b9f75083734c58e5f03 +Subproject commit 755b6dc92b1545d6c9eb242c551e8188e35ffe5d diff --git a/vendor/nim-websock b/vendor/nim-websock index 8927db93f..47b486b52 160000 --- a/vendor/nim-websock +++ b/vendor/nim-websock @@ -1 +1 @@ -Subproject commit 8927db93f6ca96abaacfea39f8ca50ce9d41bcdb +Subproject commit 47b486b52f850d3534b8a1e778fcf9cf40ffe7f6 diff --git a/waku/v2/node/peer_manager/peer_manager.nim b/waku/v2/node/peer_manager/peer_manager.nim index 9bffd79fb..066ced644 100644 --- a/waku/v2/node/peer_manager/peer_manager.nim +++ b/waku/v2/node/peer_manager/peer_manager.nim @@ -63,7 +63,7 @@ proc dialPeer(pm: PeerManager, peerId: PeerID, debug "Dialing remote peer timed out" waku_peers_dials.inc(labelValues = ["timeout"]) - pm.peerStore.connectionBook.set(peerId, CannotConnect) + pm.peerStore.connectionBook[peerId] = CannotConnect if not pm.storage.isNil: pm.storage.insertOrReplace(peerId, pm.peerStore.get(peerId), CannotConnect) @@ -73,7 +73,7 @@ proc dialPeer(pm: PeerManager, peerId: PeerID, debug "Dialing remote peer failed", msg = e.msg waku_peers_dials.inc(labelValues = ["failed"]) - pm.peerStore.connectionBook.set(peerId, CannotConnect) + pm.peerStore.connectionBook[peerId] = CannotConnect if not pm.storage.isNil: pm.storage.insertOrReplace(peerId, pm.peerStore.get(peerId), CannotConnect) @@ -89,11 +89,11 @@ proc loadFromStorage(pm: PeerManager) = # Do not manage self return - pm.peerStore.addressBook.set(peerId, storedInfo.addrs) - pm.peerStore.protoBook.set(peerId, storedInfo.protos) - pm.peerStore.keyBook.set(peerId, storedInfo.publicKey) - pm.peerStore.connectionBook.set(peerId, NotConnected) # Reset connectedness state - pm.peerStore.disconnectBook.set(peerId, disconnectTime) + pm.peerStore.addressBook[peerId] = storedInfo.addrs + pm.peerStore.protoBook[peerId] = storedInfo.protos + pm.peerStore.keyBook[peerId] = storedInfo.publicKey + pm.peerStore.connectionBook[peerId] = NotConnected # Reset connectedness state + pm.peerStore.disconnectBook[peerId] = disconnectTime let res = pm.storage.getAll(onData) if res.isErr: @@ -109,12 +109,12 @@ proc loadFromStorage(pm: PeerManager) = proc onConnEvent(pm: PeerManager, peerId: PeerID, event: ConnEvent) {.async.} = case event.kind of ConnEventKind.Connected: - pm.peerStore.connectionBook.set(peerId, Connected) + pm.peerStore.connectionBook[peerId] = Connected if not pm.storage.isNil: pm.storage.insertOrReplace(peerId, pm.peerStore.get(peerId), Connected) return of ConnEventKind.Disconnected: - pm.peerStore.connectionBook.set(peerId, CanConnect) + pm.peerStore.connectionBook[peerId] = CanConnect if not pm.storage.isNil: pm.storage.insertOrReplace(peerId, pm.peerStore.get(peerId), CanConnect, getTime().toUnix) return @@ -167,7 +167,7 @@ proc connectedness*(pm: PeerManager, peerId: PeerID): Connectedness = # Peer is not managed, therefore not connected return NotConnected else: - pm.peerStore.connectionBook.get(peerId) + pm.peerStore.connectionBook[peerId] proc hasPeer*(pm: PeerManager, peerId: PeerID, proto: string): bool = # Returns `true` if peer is included in manager for the specified protocol @@ -199,7 +199,7 @@ proc addPeer*(pm: PeerManager, remotePeerInfo: RemotePeerInfo, proto: string) = var publicKey: PublicKey discard remotePeerInfo.peerId.extractPublicKey(publicKey) - pm.peerStore.keyBook.set(remotePeerInfo.peerId, publicKey) + pm.peerStore.keyBook[remotePeerInfo.peerId] = publicKey # ...associated protocols pm.peerStore.protoBook.add(remotePeerInfo.peerId, proto) @@ -231,13 +231,13 @@ proc reconnectPeers*(pm: PeerManager, for storedInfo in pm.peers(protocolMatcher): # Check if peer is reachable. - if pm.peerStore.connectionBook.get(storedInfo.peerId) == CannotConnect: + if pm.peerStore.connectionBook[storedInfo.peerId] == CannotConnect: debug "Not reconnecting to unreachable peer", peerId=storedInfo.peerId continue # Respect optional backoff period where applicable. let - disconnectTime = Moment.init(pm.peerStore.disconnectBook.get(storedInfo.peerId), Second) # Convert + disconnectTime = Moment.init(pm.peerStore.disconnectBook[storedInfo.peerId], Second) # Convert currentTime = Moment.init(getTime().toUnix, Second) # Current time comparable to persisted value backoffTime = disconnectTime + backoff - currentTime # Consider time elapsed since last disconnect @@ -284,7 +284,7 @@ proc dialPeer*(pm: PeerManager, peerId: PeerID, proto: string, dialTimeout = def # Do not attempt to dial self return none(Connection) - let addrs = toSeq(pm.switch.peerStore.addressBook.get(peerId)) + let addrs = pm.switch.peerStore[AddressBook][peerId] if addrs.len == 0: return none(Connection) diff --git a/waku/v2/node/peer_manager/waku_peer_store.nim b/waku/v2/node/peer_manager/waku_peer_store.nim index a73de5ee4..8cbe0591a 100644 --- a/waku/v2/node/peer_manager/waku_peer_store.nim +++ b/waku/v2/node/peer_manager/waku_peer_store.nim @@ -18,9 +18,9 @@ type # Connected: actively connected to peer. Connected - ConnectionBook* = object of PeerBook[Connectedness] + ConnectionBook* = ref object of PeerBook[Connectedness] - DisconnectBook* = object of PeerBook[int64] # Keeps track of when peers were disconnected in Unix timestamps + DisconnectBook* = ref object of PeerBook[int64] # Keeps track of when peers were disconnected in Unix timestamps WakuPeerStore* = ref object addressBook*: AddressBook @@ -32,14 +32,38 @@ type StoredInfo* = object # Collates stored info about a peer peerId*: PeerID - addrs*: HashSet[MultiAddress] - protos*: HashSet[string] + addrs*: seq[MultiAddress] + protos*: seq[string] publicKey*: PublicKey proc new*(T: type WakuPeerStore): WakuPeerStore = - var p: WakuPeerStore - new(p) - return p + let + addressBook = AddressBook(book: initTable[PeerID, seq[MultiAddress]]()) + protoBook = ProtoBook(book: initTable[PeerID, seq[string]]()) + keyBook = KeyBook(book: initTable[PeerID, PublicKey]()) + connectionBook = ConnectionBook(book: initTable[PeerID, Connectedness]()) + disconnectBook = DisconnectBook(book: initTable[PeerID, int64]()) + + T(addressBook: addressBook, + protoBook: protoBook, + keyBook: keyBook, + connectionBook: connectionBook, + disconnectBook: disconnectBook) + +##################### +# Utility functions # +##################### + +proc add*[T](peerBook: SeqPeerBook[T], + peerId: PeerId, + entry: T) = + ## Add entry to a given peer. If the peer is not known, + ## it will be set with the provided entry. + + peerBook.book.mgetOrPut(peerId, + newSeq[T]()).add(entry) + + # TODO: Notify clients? ################## # Peer Store API # @@ -51,9 +75,9 @@ proc get*(peerStore: WakuPeerStore, StoredInfo( peerId: peerId, - addrs: peerStore.addressBook.get(peerId), - protos: peerStore.protoBook.get(peerId), - publicKey: peerStore.keyBook.get(peerId) + addrs: peerStore.addressBook[peerId], + protos: peerStore.protoBook[peerId], + publicKey: peerStore.keyBook[peerId] ) proc peers*(peerStore: WakuPeerStore): seq[StoredInfo] = diff --git a/waku/v2/node/storage/peer/waku_peer_storage.nim b/waku/v2/node/storage/peer/waku_peer_storage.nim index f096e17da..3d351a8f0 100644 --- a/waku/v2/node/storage/peer/waku_peer_storage.nim +++ b/waku/v2/node/storage/peer/waku_peer_storage.nim @@ -33,8 +33,8 @@ proc init*(T: type StoredInfo, buffer: seq[byte]): ProtoResult[T] = discard ? pb.getRepeatedField(3, protoSeq) discard ? pb.getField(4, storedInfo.publicKey) - storedInfo.addrs = toHashSet(multiaddrSeq) - storedInfo.protos = toHashSet(protoSeq) + storedInfo.addrs = multiaddrSeq + storedInfo.protos = protoSeq ok(storedInfo) diff --git a/waku/v2/node/wakunode2.nim b/waku/v2/node/wakunode2.nim index 818af937a..d6169a220 100644 --- a/waku/v2/node/wakunode2.nim +++ b/waku/v2/node/wakunode2.nim @@ -873,7 +873,7 @@ proc runDiscv5Loop(node: WakuNode) {.async.} = trace "Discovered peers", count=discoveredPeers.get().len() let newPeers = discoveredPeers.get().filterIt( - not node.switch.peerStore.addressBook.contains(it.peerId)) + not node.switch.peerStore[AddressBook].contains(it.peerId)) if newPeers.len > 0: debug "Connecting to newly discovered peers", count=newPeers.len()