Skip to content

Commit

Permalink
fix metrics and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed Jul 4, 2023
1 parent 707b60f commit f2ff1e3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
13 changes: 7 additions & 6 deletions libp2p/protocols/pubsub/gossipsub/scoring.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ declareGauge(libp2p_gossipsub_peers_score_invalidMessageDeliveries, "Detailed go
declareGauge(libp2p_gossipsub_peers_score_appScore, "Detailed gossipsub scoring metric", labels = ["agent"])
declareGauge(libp2p_gossipsub_peers_score_behaviourPenalty, "Detailed gossipsub scoring metric", labels = ["agent"])
declareGauge(libp2p_gossipsub_peers_score_colocationFactor, "Detailed gossipsub scoring metric", labels = ["agent"])
declareGauge(libp2p_gossipsub_peers_score_averageInvalidIgnoredTrafficRatio, "Average Invalid Ignored Traffic Ratio", labels = ["agent"])
declareGauge(libp2p_gossipsub_peers_score_averageInvalidTrafficRatio, "Average Invalid Traffic Ratio", labels = ["agent"])
declareGauge(libp2p_gossipsub_peers_score_invalidIgnoredTrafficMB, "Invalid Ignored Traffic (MB)", labels = ["agent"])
declareGauge(libp2p_gossipsub_peers_score_invalidTrafficMB, "Invalid Traffic (MB)", labels = ["agent"])
declareGauge(libp2p_gossipsub_peers_score_totalTrafficMB, "Total Traffic (MB)", labels = ["agent"])

proc init*(_: type[TopicParams]): TopicParams =
TopicParams(
Expand Down Expand Up @@ -132,10 +133,10 @@ proc disconnectIfBadTrafficPeer(g: GossipSub, peer: PubSubPeer) =
let invalidIgnoredTrafficRatio = float64(peer.invalidIgnoredTraffic) / float64(peer.totalTraffic)
let totalInvalidTrafficRatio = invalidTrafficRatio + invalidIgnoredTrafficRatio
let numberOfPeersForAgent = float64(numberOfPeersForAgent(g, agent))
libp2p_gossipsub_peers_score_averageInvalidTrafficRatio
.inc(invalidTrafficRatio / (if numberOfPeersForAgent != 0: numberOfPeersForAgent else: 1), labelValues = [agent])
libp2p_gossipsub_peers_score_averageInvalidIgnoredTrafficRatio
.inc(invalidIgnoredTrafficRatio / (if numberOfPeersForAgent != 0: numberOfPeersForAgent else: 1), labelValues = [agent])
libp2p_gossipsub_peers_score_invalidTrafficMB.inc(float64(peer.invalidTraffic) / 1_000_000, labelValues = [agent])
libp2p_gossipsub_peers_score_invalidIgnoredTrafficMB.inc(float64(peer.invalidIgnoredTraffic) / 1_000_000, labelValues = [agent])
libp2p_gossipsub_peers_score_totalTrafficMB.inc(float64(peer.totalTraffic) / 1_000_000, labelValues = [agent])

discard g.disconnectIfBadPeer(peer, -totalInvalidTrafficRatio, -0.30'f64) #g.parameters.maxInvalidTrafficRatio)

proc updateScores*(g: GossipSub) = # avoid async
Expand Down
6 changes: 4 additions & 2 deletions libp2p/protocols/pubsub/rpc/messages.nim
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,12 @@ proc byteSize*(msg: RPCMsg): int =
let ctrl = msg.control.get()
for item in ctrl.ihave:
total += item.topicId.len
total += item.messageIds.len * sizeof(byte) # Assuming MessageId is seq[byte]
for msgId in item.messageIds:
total += msgId.len

for item in ctrl.iwant:
total += item.messageIds.len * sizeof(byte) # Assuming MessageId is seq[byte]
for msgId in item.messageIds:
total += msgId.len

for item in ctrl.graft:
total += item.topicId.len
Expand Down
44 changes: 44 additions & 0 deletions tests/pubsub/testmessage.nim
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,47 @@ suite "Message":
check:
msgIdResult.isErr
msgIdResult.error == ValidationResult.Reject

test "byteSize for Message":
var msg = Message(
fromPeer: PeerId(data: @[]), # Empty seq[byte]
data: @[1'u8, 2, 3], # 3 bytes
seqno: @[1'u8], # 1 byte
signature: @[], # Empty seq[byte]
key: @[1'u8], # 1 byte
topicIds: @["abc", "defgh"] # 3 + 5 = 8 bytes
)

check byteSize(msg) == 3 + 1 + 1 + 8 # Total: 13 bytes

test "byteSize for RPCMsg":
var msg = RPCMsg(
subscriptions: @[
SubOpts(topic: "abc", subscribe: true),
SubOpts(topic: "def", subscribe: false)
], # 3 + 3 + 2 * sizeof(bool) bytes
messages: @[
Message(fromPeer: PeerId(data: @[]), data: @[1'u8, 2, 3], seqno: @[1'u8], signature: @[], key: @[1'u8], topicIds: @["abc", "defgh"]),
Message(fromPeer: PeerId(data: @[]), data: @[], seqno: @[], signature: @[], key: @[], topicIds: @["abc"])
], # byteSize: 13 + 3 = 16 bytes
control: some(ControlMessage(
ihave: @[
ControlIHave(topicId: "ghi", messageIds: @[@[1'u8, 2, 3]])
], # 3 + 3 bytes
iwant: @[
ControlIWant(messageIds: @[@[1'u8, 2]])
], # 2 bytes
graft: @[
ControlGraft(topicId: "jkl")
], # 3 bytes
prune: @[
ControlPrune(topicId: "mno", peers: @[PeerInfoMsg(peerId: PeerId(data: @[]), signedPeerRecord: @[])], backoff: 1)
] # 3 + sizeof(uint64) bytes
)),
ping: @[], # Empty seq[byte]
pong: @[] # Empty seq[byte]
)

let boolSize = sizeof(bool)
let uint64Size = sizeof(uint64)
check byteSize(msg) == (3 + 3 + 2 * boolSize) + 16 + (3 + 3 + 2 + 3 + 3 + uint64Size)

0 comments on commit f2ff1e3

Please sign in to comment.