2022-07-01 18:19:57 +00:00
|
|
|
# Nim-LibP2P
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2022-07-01 18:19:57 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
|
|
|
## Stores generic informations about peers.
|
|
|
|
runnableExamples:
|
|
|
|
# Will keep info of all connected peers +
|
|
|
|
# last 50 disconnected peers
|
|
|
|
let peerStore = PeerStore.new(capacity = 50)
|
|
|
|
|
|
|
|
# Create a custom book type
|
|
|
|
type MoodBook = ref object of PeerBook[string]
|
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
var somePeerId = PeerId.random().expect("get random key")
|
2022-07-01 18:19:57 +00:00
|
|
|
|
|
|
|
peerStore[MoodBook][somePeerId] = "Happy"
|
|
|
|
doAssert peerStore[MoodBook][somePeerId] == "Happy"
|
2021-02-08 21:16:23 +00:00
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2021-02-08 21:16:23 +00:00
|
|
|
|
|
|
|
import
|
2022-05-25 10:12:57 +00:00
|
|
|
std/[tables, sets, options, macros],
|
2023-05-18 08:24:17 +00:00
|
|
|
chronos,
|
2021-02-08 21:16:23 +00:00
|
|
|
./crypto/crypto,
|
2021-09-08 09:07:46 +00:00
|
|
|
./protocols/identify,
|
2023-03-08 11:30:19 +00:00
|
|
|
./protocols/protocol,
|
2021-06-08 16:55:24 +00:00
|
|
|
./peerid, ./peerinfo,
|
2022-03-14 08:39:30 +00:00
|
|
|
./routing_record,
|
2022-07-01 18:19:57 +00:00
|
|
|
./multiaddress,
|
2023-03-08 11:30:19 +00:00
|
|
|
./stream/connection,
|
|
|
|
./multistream,
|
|
|
|
./muxers/muxer,
|
2022-07-01 18:19:57 +00:00
|
|
|
utility
|
2021-02-08 21:16:23 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
#################
|
|
|
|
# Handler types #
|
|
|
|
#################
|
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
PeerBookChangeHandler* = proc(peerId: PeerId) {.gcsafe, raises: [].}
|
2022-07-01 18:19:57 +00:00
|
|
|
|
2021-02-08 21:16:23 +00:00
|
|
|
#########
|
|
|
|
# Books #
|
|
|
|
#########
|
|
|
|
|
|
|
|
# Each book contains a book (map) and event handler(s)
|
2022-05-25 10:12:57 +00:00
|
|
|
BasePeerBook = ref object of RootObj
|
|
|
|
changeHandlers: seq[PeerBookChangeHandler]
|
|
|
|
deletor: PeerBookChangeHandler
|
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
PeerBook*[T] {.public.} = ref object of BasePeerBook
|
2021-12-16 10:05:20 +00:00
|
|
|
book*: Table[PeerId, T]
|
2021-06-08 16:55:24 +00:00
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
SeqPeerBook*[T] = ref object of PeerBook[seq[T]]
|
2022-07-01 18:19:57 +00:00
|
|
|
|
|
|
|
AddressBook* {.public.} = ref object of SeqPeerBook[MultiAddress]
|
|
|
|
ProtoBook* {.public.} = ref object of SeqPeerBook[string]
|
|
|
|
KeyBook* {.public.} = ref object of PeerBook[PublicKey]
|
|
|
|
|
|
|
|
AgentBook* {.public.} = ref object of PeerBook[string]
|
|
|
|
ProtoVersionBook* {.public.} = ref object of PeerBook[string]
|
|
|
|
SPRBook* {.public.} = ref object of PeerBook[Envelope]
|
|
|
|
|
2021-02-08 21:16:23 +00:00
|
|
|
####################
|
|
|
|
# Peer store types #
|
|
|
|
####################
|
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
PeerStore* {.public.} = ref object
|
2022-05-25 10:12:57 +00:00
|
|
|
books: Table[string, BasePeerBook]
|
2023-03-08 11:30:19 +00:00
|
|
|
identify: Identify
|
2022-05-25 10:12:57 +00:00
|
|
|
capacity*: int
|
|
|
|
toClean*: seq[PeerId]
|
2022-07-01 18:19:57 +00:00
|
|
|
|
2023-03-08 11:30:19 +00:00
|
|
|
proc new*(T: type PeerStore, identify: Identify, capacity = 1000): PeerStore {.public.} =
|
|
|
|
T(
|
|
|
|
identify: identify,
|
|
|
|
capacity: capacity
|
|
|
|
)
|
2021-02-08 21:16:23 +00:00
|
|
|
|
|
|
|
#########################
|
|
|
|
# Generic Peer Book API #
|
|
|
|
#########################
|
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
proc `[]`*[T](peerBook: PeerBook[T],
|
2022-07-01 18:19:57 +00:00
|
|
|
peerId: PeerId): T {.public.} =
|
|
|
|
## Get all known metadata of a provided peer, or default(T) if missing
|
2021-02-08 21:16:23 +00:00
|
|
|
peerBook.book.getOrDefault(peerId)
|
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
proc `[]=`*[T](peerBook: PeerBook[T],
|
2021-12-16 10:05:20 +00:00
|
|
|
peerId: PeerId,
|
2022-07-01 18:19:57 +00:00
|
|
|
entry: T) {.public.} =
|
|
|
|
## Set metadata for a given peerId.
|
|
|
|
|
2021-02-08 21:16:23 +00:00
|
|
|
peerBook.book[peerId] = entry
|
|
|
|
|
|
|
|
# Notify clients
|
|
|
|
for handler in peerBook.changeHandlers:
|
2022-05-25 10:12:57 +00:00
|
|
|
handler(peerId)
|
2021-02-08 21:16:23 +00:00
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
proc del*[T](peerBook: PeerBook[T],
|
2022-07-01 18:19:57 +00:00
|
|
|
peerId: PeerId): bool {.public.} =
|
|
|
|
## Delete the provided peer from the book. Returns whether the peer was in the book
|
|
|
|
|
2022-05-25 10:12:57 +00:00
|
|
|
if peerId notin peerBook.book:
|
2021-02-08 21:16:23 +00:00
|
|
|
return false
|
|
|
|
else:
|
|
|
|
peerBook.book.del(peerId)
|
2022-05-25 10:12:57 +00:00
|
|
|
# Notify clients
|
|
|
|
for handler in peerBook.changeHandlers:
|
|
|
|
handler(peerId)
|
2021-02-08 21:16:23 +00:00
|
|
|
return true
|
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
proc contains*[T](peerBook: PeerBook[T], peerId: PeerId): bool {.public.} =
|
2021-09-08 09:07:46 +00:00
|
|
|
peerId in peerBook.book
|
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
proc addHandler*[T](peerBook: PeerBook[T], handler: PeerBookChangeHandler) {.public.} =
|
|
|
|
## Adds a callback that will be called everytime the book changes
|
2022-05-25 10:12:57 +00:00
|
|
|
peerBook.changeHandlers.add(handler)
|
2021-02-08 21:16:23 +00:00
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
proc len*[T](peerBook: PeerBook[T]): int {.public.} = peerBook.book.len
|
2021-09-08 09:07:46 +00:00
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
##################
|
2021-02-08 21:16:23 +00:00
|
|
|
# Peer Store API #
|
|
|
|
##################
|
2022-05-25 10:12:57 +00:00
|
|
|
macro getTypeName(t: type): untyped =
|
|
|
|
# Generate unique name in form of Module.Type
|
|
|
|
let typ = getTypeImpl(t)[1]
|
|
|
|
newLit(repr(typ.owner()) & "." & repr(typ))
|
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
proc `[]`*[T](p: PeerStore, typ: type[T]): T {.public.} =
|
|
|
|
## Get a book from the PeerStore (ex: peerStore[AddressBook])
|
2022-05-25 10:12:57 +00:00
|
|
|
let name = getTypeName(T)
|
|
|
|
result = T(p.books.getOrDefault(name))
|
|
|
|
if result.isNil:
|
|
|
|
result = T.new()
|
|
|
|
result.deletor = proc(pid: PeerId) =
|
|
|
|
# Manual method because generic method
|
|
|
|
# don't work
|
|
|
|
discard T(p.books.getOrDefault(name)).del(pid)
|
|
|
|
p.books[name] = result
|
|
|
|
return result
|
|
|
|
|
|
|
|
proc del*(peerStore: PeerStore,
|
2022-07-01 18:19:57 +00:00
|
|
|
peerId: PeerId) {.public.} =
|
2021-02-08 21:16:23 +00:00
|
|
|
## Delete the provided peer from every book.
|
2022-05-25 10:12:57 +00:00
|
|
|
for _, book in peerStore.books:
|
|
|
|
book.deletor(peerId)
|
2021-02-08 21:16:23 +00:00
|
|
|
|
2021-09-08 09:07:46 +00:00
|
|
|
proc updatePeerInfo*(
|
|
|
|
peerStore: PeerStore,
|
|
|
|
info: IdentifyInfo) =
|
|
|
|
|
|
|
|
if info.addrs.len > 0:
|
2022-05-25 10:12:57 +00:00
|
|
|
peerStore[AddressBook][info.peerId] = info.addrs
|
2021-09-08 09:07:46 +00:00
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
info.pubkey.withValue(pubkey):
|
|
|
|
peerStore[KeyBook][info.peerId] = pubkey
|
2022-11-21 09:55:25 +00:00
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
info.agentVersion.withValue(agentVersion):
|
|
|
|
peerStore[AgentBook][info.peerId] = agentVersion.string
|
2021-09-08 09:07:46 +00:00
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
info.protoVersion.withValue(protoVersion):
|
|
|
|
peerStore[ProtoVersionBook][info.peerId] = protoVersion.string
|
2021-02-08 21:16:23 +00:00
|
|
|
|
2021-09-08 09:07:46 +00:00
|
|
|
if info.protos.len > 0:
|
2022-05-25 10:12:57 +00:00
|
|
|
peerStore[ProtoBook][info.peerId] = info.protos
|
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
info.signedPeerRecord.withValue(signedPeerRecord):
|
|
|
|
peerStore[SPRBook][info.peerId] = signedPeerRecord
|
2022-05-25 10:12:57 +00:00
|
|
|
|
|
|
|
let cleanupPos = peerStore.toClean.find(info.peerId)
|
|
|
|
if cleanupPos >= 0:
|
|
|
|
peerStore.toClean.delete(cleanupPos)
|
|
|
|
|
|
|
|
proc cleanup*(
|
|
|
|
peerStore: PeerStore,
|
|
|
|
peerId: PeerId) =
|
|
|
|
|
|
|
|
if peerStore.capacity == 0:
|
|
|
|
peerStore.del(peerId)
|
|
|
|
return
|
|
|
|
elif peerStore.capacity < 0:
|
|
|
|
#infinite capacity
|
|
|
|
return
|
|
|
|
|
|
|
|
peerStore.toClean.add(peerId)
|
|
|
|
while peerStore.toClean.len > peerStore.capacity:
|
|
|
|
peerStore.del(peerStore.toClean[0])
|
|
|
|
peerStore.toClean.delete(0)
|
2023-03-08 11:30:19 +00:00
|
|
|
|
|
|
|
proc identify*(
|
2024-03-06 12:12:05 +00:00
|
|
|
peerStore: PeerStore,
|
|
|
|
muxer: Muxer) {.async: (raises: [CancelledError, LPError]).} =
|
2023-03-08 11:30:19 +00:00
|
|
|
|
|
|
|
# new stream for identify
|
|
|
|
var stream = await muxer.newStream()
|
|
|
|
if stream == nil:
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
if (await MultistreamSelect.select(stream, peerStore.identify.codec())):
|
|
|
|
let info = await peerStore.identify.identify(stream, stream.peerId)
|
|
|
|
|
|
|
|
when defined(libp2p_agents_metrics):
|
2023-06-28 14:44:58 +00:00
|
|
|
var
|
|
|
|
knownAgent = "unknown"
|
|
|
|
shortAgent = info.agentVersion.get("").split("/")[0].safeToLowerAscii().get("")
|
|
|
|
if KnownLibP2PAgentsSeq.contains(shortAgent):
|
|
|
|
knownAgent = shortAgent
|
2023-03-08 11:30:19 +00:00
|
|
|
muxer.connection.setShortAgent(knownAgent)
|
|
|
|
|
|
|
|
peerStore.updatePeerInfo(info)
|
|
|
|
finally:
|
|
|
|
await stream.closeWithEOF()
|
2023-03-24 15:42:49 +00:00
|
|
|
|
2023-04-14 14:23:19 +00:00
|
|
|
proc getMostObservedProtosAndPorts*(self: PeerStore): seq[MultiAddress] =
|
|
|
|
return self.identify.observedAddrManager.getMostObservedProtosAndPorts()
|
|
|
|
|
2023-03-24 15:42:49 +00:00
|
|
|
proc guessDialableAddr*(self: PeerStore, ma: MultiAddress): MultiAddress =
|
|
|
|
return self.identify.observedAddrManager.guessDialableAddr(ma)
|