Skip to content

Commit

Permalink
Simplify data size calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed Aug 2, 2023
1 parent 9403349 commit 6842b9a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 37 deletions.
10 changes: 4 additions & 6 deletions libp2p/protocols/pubsub/gossipsub.nim
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,9 @@ proc validateAndRelay(g: GossipSub,
except CatchableError as exc:
info "validateAndRelay failed", msg=exc.msg

proc dataAndTopicsIdSize(msgs: seq[Message]): int =
msgs.mapIt(it.data.len + it.topicIds.mapIt(it.len).foldl(a + b, 0)).foldl(a + b, 0)

proc rateLimit*(g: GossipSub, peer: PubSubPeer, rpcMsgOpt: Opt[RPCMsg], msgSize: int) {.raises:[PeerRateLimitError].} =
# In this way we count even ignored fields by protobuf
var rmsg = rpcMsgOpt.valueOr:
Expand All @@ -381,12 +384,7 @@ proc rateLimit*(g: GossipSub, peer: PubSubPeer, rpcMsgOpt: Opt[RPCMsg], msgSize:
if g.verifySignature:
byteSize(rmsg.messages)
else:
rmsg.messages.mapIt(it.data.len).foldl(a + b, 0) +
rmsg.messages
.mapIt(
it.topicIds.mapIt(it.len).foldl(a + b, 0)
)
.foldl(a + b, 0)
dataAndTopicsIdSize(rmsg.messages)

var uselessAppBytesNum = msgSize - usefulMsgBytesNum
rmsg.control.withValue(control):
Expand Down
31 changes: 0 additions & 31 deletions libp2p/protocols/pubsub/rpc/messages.nim
Original file line number Diff line number Diff line change
Expand Up @@ -145,34 +145,3 @@ proc byteSize*(iwant: seq[ControlIWant]): int =
for msgId in item.messageIds:
total += msgId.len
return total

proc byteSize*(msg: RPCMsg): int =
var total = 0

for sub in msg.subscriptions:
total += sub.topic.len
total += sizeof(sub.subscribe)

for msg in msg.messages:
total += byteSize(msg)

if msg.control.isSome:
let ctrl = msg.control.get()
total += byteSize(ctrl.ihave)

total += byteSize(ctrl.iwant)

for item in ctrl.graft:
total += item.topicId.len

for item in ctrl.prune:
total += item.topicId.len
total += sizeof(item.backoff)
for peer in item.peers:
total += peer.peerId.len
total += peer.signedPeerRecord.len * sizeof(byte)

total += msg.ping.len * sizeof(byte)
total += msg.pong.len * sizeof(byte)

return total

0 comments on commit 6842b9a

Please sign in to comment.