-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/peerstore impl #505
Feat/peerstore impl #505
Changes from 1 commit
c506272
48be0a7
ff7d081
926d340
12e0716
218ca64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,21 +14,14 @@ import | |
./multiaddress | ||
|
||
type | ||
############################## | ||
# Listener and handler types # | ||
############################## | ||
################# | ||
# Handler types # | ||
################# | ||
|
||
AddrChangeHandler* = proc(peerId: PeerID, multiaddrs: HashSet[MultiAddress]) | ||
ProtoChangeHandler* = proc(peerId: PeerID, protos: HashSet[string]) | ||
KeyChangeHandler* = proc(peerId: PeerID, publicKey: PublicKey) | ||
MetadataChangeHandler* = proc(peerId: PeerID, metadata: Table[string, seq[byte]]) | ||
|
||
EventListener* = object | ||
# Listener object with client-defined handlers for peer store events | ||
addrChange*: AddrChangeHandler | ||
protoChange*: ProtoChangeHandler | ||
keyChange*: KeyChangeHandler | ||
metadataChange*: MetadataChangeHandler | ||
|
||
######### | ||
# Books # | ||
|
@@ -60,7 +53,10 @@ type | |
protoBook*: ProtoBook | ||
keyBook*: KeyBook | ||
metadataBook*: MetadataBook | ||
listeners: seq[EventListener] | ||
addrChangeHandlers*: seq[AddrChangeHandler] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, wouldn't it make more sense to move the change handlers into the specific There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. :) Improvement in ff7d081 The idea was to create some level of indirection between the multiple public handlers that different clients can register on the Peer Store and the single, private handler that the peer store itself registers per *Book when it initialises those books. Each book then only notifies its encompassing Peer Store of changes and the Peer Store in turn notifies its multiple registered clients (perhaps enforcing optional policy). Currently this serves no more than a conceptual purpose, so it will be cleaner to just move the handlers to the individual books. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I'm not sure if the "proxy" handler makes sense there - but I understand the idea behind it now. My perspective on the |
||
protoChangeHandlers*: seq[ProtoChangeHandler] | ||
keyChangeHandlers*: seq[KeyChangeHandler] | ||
metadataChangeHandlers*: seq[MetadataChangeHandler] | ||
|
||
StoredInfo* = object | ||
# Collates stored info about a peer | ||
|
@@ -87,27 +83,30 @@ proc init(T: type MetadataBook, metadataChange: MetadataChangeHandler): Metadata | |
metadataChange: metadataChange) | ||
|
||
proc init*(p: PeerStore) = | ||
p.listeners = newSeq[EventListener]() | ||
p.addrChangeHandlers = newSeq[AddrChangeHandler]() | ||
p.protoChangeHandlers = newSeq[ProtoChangeHandler]() | ||
p.keyChangeHandlers = newSeq[KeyChangeHandler]() | ||
p.metadataChangeHandlers = newSeq[MetadataChangeHandler]() | ||
|
||
proc addrChange(peerId: PeerID, multiaddrs: HashSet[MultiAddress]) = | ||
# Notify all listeners of change in multiaddr | ||
for listener in p.listeners: | ||
listener.addrChange(peerId, multiaddrs) | ||
for handler in p.addrChangeHandlers: | ||
handler(peerId, multiaddrs) | ||
|
||
proc protoChange(peerId: PeerID, protos: HashSet[string]) = | ||
# Notify all listeners of change in proto | ||
for listener in p.listeners: | ||
listener.protoChange(peerId, protos) | ||
for handler in p.protoChangeHandlers: | ||
handler(peerId, protos) | ||
|
||
proc keyChange(peerId: PeerID, publicKey: PublicKey) = | ||
# Notify all listeners of change in public key | ||
for listener in p.listeners: | ||
listener.keyChange(peerId, publicKey) | ||
for handler in p.keyChangeHandlers: | ||
handler(peerId, publicKey) | ||
|
||
proc metadataChange(peerId: PeerID, metadata: Table[string, seq[byte]]) = | ||
# Notify all listeners of change in public key | ||
for listener in p.listeners: | ||
listener.metadataChange(peerId, metadata) | ||
for handler in p.metadataChangeHandlers: | ||
handler(peerId, metadata) | ||
|
||
p.addressBook = AddressBook.init(addrChange) | ||
p.protoBook = ProtoBook.init(protoChange) | ||
|
@@ -299,11 +298,17 @@ proc deleteValue*(metadataBook: var MetadataBook, | |
# Peer Store API # | ||
################## | ||
|
||
proc addListener*(peerStore: PeerStore, | ||
listener: EventListener) = | ||
## Register event listener to notify clients of changes in the peer store | ||
|
||
peerStore.listeners.add(listener) | ||
proc addHandlers*(peerStore: PeerStore, | ||
addrChangeHandler: AddrChangeHandler, | ||
protoChangeHandler: ProtoChangeHandler, | ||
keyChangeHandler: KeyChangeHandler, | ||
metadataChangeHandler: MetadataChangeHandler) = | ||
## Register event handlers to notify clients of changes in the peer store | ||
|
||
peerStore.addrChangeHandlers.add(addrChangeHandler) | ||
peerStore.protoChangeHandlers.add(protoChangeHandler) | ||
peerStore.keyChangeHandlers.add(keyChangeHandler) | ||
peerStore.metadataChangeHandlers.add(metadataChangeHandler) | ||
|
||
proc delete*(peerStore: PeerStore, | ||
peerId: PeerID): bool = | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is FooChangeHandler the common name here? Nitpick, but something like FooUpdateHandler might be a tiny bit more descriptive, "Change" to me suggests something could be breaking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree.
change
was selected to stay as close as possible to similar events and naming in the js-implementation, but I am happy to update naming toupdate
:)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the naming convention makes sense here.