Skip to content

Commit

Permalink
Renaming to byteSize
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed Sep 27, 2023
1 parent af51fbc commit 092a3b0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
4 changes: 2 additions & 2 deletions libp2p/protocols/pubsub/gossipsub.nim
Original file line number Diff line number Diff line change
Expand Up @@ -400,13 +400,13 @@ proc rateLimit*(g: GossipSub, peer: PubSubPeer, rpcMsgOpt: Opt[RPCMsg], msgSize:

let usefulMsgBytesNum =
if g.verifySignature:
len(rmsg.messages)
byteSize(rmsg.messages)
else:
dataAndTopicsIdSize(rmsg.messages)

var uselessAppBytesNum = msgSize - usefulMsgBytesNum
rmsg.control.withValue(control):
uselessAppBytesNum -= (len(control.ihave) + len(control.iwant))
uselessAppBytesNum -= (byteSize(control.ihave) + byteSize(control.iwant))

peer.overheadRateLimitOpt.withValue(overheadRateLimit):
if not overheadRateLimit.tryConsume(uselessAppBytesNum):
Expand Down
4 changes: 2 additions & 2 deletions libp2p/protocols/pubsub/pubsubpeer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,10 @@ iterator splitRPCMsg(peer: PubSubPeer, rpcMsg: RPCMsg, maxSize: int, anonymize:
var currentRPCMsg = rpcMsg
currentRPCMsg.messages = newSeq[Message]()

var currentSize = len(currentRPCMsg)
var currentSize = byteSize(currentRPCMsg)

for msg in rpcMsg.messages:
let msgSize = len(msg)
let msgSize = byteSize(msg)

# Check if adding the next message will exceed maxSize
if float(currentSize + msgSize) * 1.1 > float(maxSize): # Guessing 10% protobuf overhead
Expand Down
39 changes: 24 additions & 15 deletions libp2p/protocols/pubsub/rpc/messages.nim
Original file line number Diff line number Diff line change
Expand Up @@ -126,43 +126,52 @@ func shortLog*(m: RPCMsg): auto =
)

static: expectedFields(PeerInfoMsg, @["peerId", "signedPeerRecord"])
proc len(peerInfo: PeerInfoMsg): int =
proc byteSize(peerInfo: PeerInfoMsg): int =
peerInfo.peerId.len + peerInfo.signedPeerRecord.len

static: expectedFields(SubOpts, @["subscribe", "topic"])
proc len(subOpts: SubOpts): int =
proc byteSize(subOpts: SubOpts): int =
1 + subOpts.topic.len # 1 byte for the bool

static: expectedFields(Message, @["fromPeer", "data", "seqno", "topicIds", "signature", "key"])
proc len*(msg: Message): int =
proc byteSize*(msg: Message): int =
msg.fromPeer.len + msg.data.len + msg.seqno.len +
msg.signature.len + msg.key.len + msg.topicIds.foldl(a + b.len, 0)

proc byteSize*(msgs: seq[Message]): int =
msgs.foldl(a + b.byteSize, 0)

static: expectedFields(ControlIHave, @["topicId", "messageIds"])
proc len(controlIHave: ControlIHave): int =
proc byteSize(controlIHave: ControlIHave): int =
controlIHave.topicId.len + controlIHave.messageIds.foldl(a + b.len, 0)

proc byteSize*(ihaves: seq[ControlIHave]): int =
ihaves.foldl(a + b.byteSize, 0)

static: expectedFields(ControlIWant, @["messageIds"])
proc len(controlIWant: ControlIWant): int =
proc byteSize(controlIWant: ControlIWant): int =
controlIWant.messageIds.foldl(a + b.len, 0)

proc byteSize*(iwants: seq[ControlIWant]): int =
iwants.foldl(a + b.byteSize, 0)

static: expectedFields(ControlGraft, @["topicId"])
proc len(controlGraft: ControlGraft): int =
proc byteSize(controlGraft: ControlGraft): int =
controlGraft.topicId.len

static: expectedFields(ControlPrune, @["topicId", "peers", "backoff"])
proc len(controlPrune: ControlPrune): int =
controlPrune.topicId.len + controlPrune.peers.foldl(a + b.len, 0) + 8 # 8 bytes for uint64
proc byteSize(controlPrune: ControlPrune): int =
controlPrune.topicId.len + controlPrune.peers.foldl(a + b.byteSize, 0) + 8 # 8 bytes for uint64

static: expectedFields(ControlMessage, @["ihave", "iwant", "graft", "prune", "idontwant"])
proc len(control: ControlMessage): int =
control.ihave.foldl(a + b.len, 0) + control.iwant.foldl(a + b.len, 0) +
control.graft.foldl(a + b.len, 0) + control.prune.foldl(a + b.len, 0) +
control.idontwant.foldl(a + b.len, 0)
proc byteSize(control: ControlMessage): int =
control.ihave.foldl(a + b.byteSize, 0) + control.iwant.foldl(a + b.byteSize, 0) +
control.graft.foldl(a + b.byteSize, 0) + control.prune.foldl(a + b.byteSize, 0) +
control.idontwant.foldl(a + b.byteSize, 0)

static: expectedFields(RPCMsg, @["subscriptions", "messages", "control", "ping", "pong"])
proc len*(rpc: RPCMsg): int =
result = rpc.subscriptions.foldl(a + b.len, 0) + rpc.messages.foldl(a + b.len, 0) +
proc byteSize*(rpc: RPCMsg): int =
result = rpc.subscriptions.foldl(a + b.byteSize, 0) + rpc.messages.foldl(a + b.byteSize, 0) +
rpc.ping.len + rpc.pong.len
rpc.control.withValue(ctrl):
result += ctrl.len
result += ctrl.byteSize

0 comments on commit 092a3b0

Please sign in to comment.