Skip to content

Commit

Permalink
chore: adding onRecvAndValidated observers
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmer committed Jul 16, 2024
1 parent dc83a1e commit 10a1ef8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions libp2p/protocols/pubsub/gossipsub.nim
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,9 @@ proc validateAndRelay(

g.rewardDelivered(peer, topic, true)

# trigger hooks
peer.recvAndValidatedObservers(msg, msgId)

# The send list typically matches the idontwant list from above, but
# might differ if validation takes time
var toSendPeers = HashSet[PubSubPeer]()
Expand Down
9 changes: 9 additions & 0 deletions libp2p/protocols/pubsub/pubsubpeer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type
PubSubObserver* = ref object
onRecv*: proc(peer: PubSubPeer, msgs: var RPCMsg) {.gcsafe, raises: [].}
onSend*: proc(peer: PubSubPeer, msgs: var RPCMsg) {.gcsafe, raises: [].}
onRecvAndValidated*:
proc(peer: PubSubPeer, msg: Message, msgId: MessageId) {.gcsafe, raises: [].}

PubSubPeerEventKind* {.pure.} = enum
StreamOpened
Expand Down Expand Up @@ -172,6 +174,13 @@ proc recvObservers*(p: PubSubPeer, msg: var RPCMsg) =
if not (isNil(obs)): # TODO: should never be nil, but...
obs.onRecv(p, msg)

proc recvAndValidatedObservers*(p: PubSubPeer, msg: Message, msgId: MessageId) =
# trigger hooks
if not (isNil(p.observers)) and p.observers[].len > 0:
for obs in p.observers[]:
if not (isNil(obs)): # TODO: should never be nil, but...
obs.onRecvAndValidated(p, msg, msgId)

proc sendObservers(p: PubSubPeer, msg: var RPCMsg) =
# trigger hooks
if not (isNil(p.observers)) and p.observers[].len > 0:
Expand Down

0 comments on commit 10a1ef8

Please sign in to comment.