From 3bb596073938fe19b8c53548805f537020ca3713 Mon Sep 17 00:00:00 2001 From: Tomasz Bekas Date: Wed, 19 Jul 2023 15:10:14 +0200 Subject: [PATCH] Fix for decoding large manifests (#479) --- .gitignore | 1 + codex/blockexchange/network/network.nim | 2 ++ codex/blockexchange/network/networkpeer.nim | 10 ++++------ codex/blockexchange/protobuf/message.nim | 13 +++++++++---- vendor/nim-libp2p | 2 +- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 167fcc16..05d53c20 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,4 @@ codex.nims nimbus-build-system.paths docker/hostdatadir docker/prometheus-data +.DS_Store diff --git a/codex/blockexchange/network/network.nim b/codex/blockexchange/network/network.nim index ef048e45..9c041fbf 100644 --- a/codex/blockexchange/network/network.nim +++ b/codex/blockexchange/network/network.nim @@ -84,6 +84,8 @@ proc send*(b: BlockExcNetwork, id: PeerId, msg: pb.Message) {.async.} = await b.inflightSema.acquire() trace "Sending message to peer", peer = id await peer[].send(msg) + except CatchableError as err: + error "Error sending message", peer = id, msg = err.msg finally: b.inflightSema.release() do: diff --git a/codex/blockexchange/network/networkpeer.nim b/codex/blockexchange/network/networkpeer.nim index 2c8890b0..1ed5f46b 100644 --- a/codex/blockexchange/network/networkpeer.nim +++ b/codex/blockexchange/network/networkpeer.nim @@ -15,14 +15,12 @@ import pkg/chronicles import pkg/libp2p import ../protobuf/blockexc +import ../protobuf/message import ../../errors logScope: topics = "codex blockexcnetworkpeer" -const - MaxMessageSize = 100 * 1 shl 20 # manifest files can be big - type ConnProvider* = proc(): Future[Connection] {.gcsafe, closure.} @@ -45,12 +43,12 @@ proc readLoop*(b: NetworkPeer, conn: Connection) {.async.} = try: while not conn.atEof or not conn.closed: let - data = await conn.readLp(MaxMessageSize) + data = await conn.readLp(MaxMessageSize.int) msg = Message.protobufDecode(data).mapFailure().tryGet() trace "Got message for peer", peer = b.id await b.handler(b, msg) - except CatchableError as exc: - trace "Exception in blockexc read loop", exc = exc.msg + except CatchableError as err: + warn "Exception in blockexc read loop", msg = err.msg finally: await conn.close() diff --git a/codex/blockexchange/protobuf/message.nim b/codex/blockexchange/protobuf/message.nim index 6f124a56..bbbfdf49 100644 --- a/codex/blockexchange/protobuf/message.nim +++ b/codex/blockexchange/protobuf/message.nim @@ -5,6 +5,11 @@ import pkg/libp2p/protobuf/minprotobuf +import ../../units + +const + MaxBlockSize* = 100.MiBs.uint + MaxMessageSize* = 100.MiBs.uint type WantType* = enum @@ -72,7 +77,7 @@ proc write*(pb: var ProtoBuffer, field: int, value: Wantlist) = pb.write(field, ipb) proc write*(pb: var ProtoBuffer, field: int, value: Block) = - var ipb = initProtoBuffer() + var ipb = initProtoBuffer(maxSize = MaxBlockSize) ipb.write(1, value.prefix) ipb.write(2, value.data) ipb.finish() @@ -99,7 +104,7 @@ proc write*(pb: var ProtoBuffer, field: int, value: StateChannelUpdate) = pb.write(field, ipb) proc protobufEncode*(value: Message): seq[byte] = - var ipb = initProtoBuffer() + var ipb = initProtoBuffer(maxSize = MaxMessageSize) ipb.write(1, value.wantlist) for v in value.payload: ipb.write(3, v) @@ -175,14 +180,14 @@ proc decode*(_: type StateChannelUpdate, pb: ProtoBuffer): ProtoResult[StateChan proc protobufDecode*(_: type Message, msg: seq[byte]): ProtoResult[Message] = var value = Message() - pb = initProtoBuffer(msg) + pb = initProtoBuffer(msg, maxSize = MaxMessageSize) ipb: ProtoBuffer sublist: seq[seq[byte]] if ? pb.getField(1, ipb): value.wantlist = ? Wantlist.decode(ipb) if ? pb.getRepeatedField(3, sublist): for item in sublist: - value.payload.add(? Block.decode(initProtoBuffer(item))) + value.payload.add(? Block.decode(initProtoBuffer(item, maxSize = MaxBlockSize))) if ? pb.getRepeatedField(4, sublist): for item in sublist: value.blockPresences.add(? BlockPresence.decode(initProtoBuffer(item))) diff --git a/vendor/nim-libp2p b/vendor/nim-libp2p index a3e9d1ed..8c2eca18 160000 --- a/vendor/nim-libp2p +++ b/vendor/nim-libp2p @@ -1 +1 @@ -Subproject commit a3e9d1ed80c048cd5abc839cbe0863cefcedc702 +Subproject commit 8c2eca18dcc538c57a8fbc0c53fd0b9d24d56cff