Skip to content

Commit 1b28c03

Browse files
committed
fixes some basic decoding and padding mistakes
1 parent e0abd40 commit 1b28c03

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

codex/bittorrent/manifest/decoding.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import pkg/questionable/results
66
import ../../blocktype
77
import ./manifest
88

9-
proc decode(_: type BitTorrentManifest, data: openArray[byte]): ?!BitTorrentManifest =
9+
func decode(_: type BitTorrentManifest, data: openArray[byte]): ?!BitTorrentManifest =
1010
# ```protobuf
1111
# Message BitTorrentManifest {
1212
# Message Piece {
@@ -25,7 +25,7 @@ proc decode(_: type BitTorrentManifest, data: openArray[byte]): ?!BitTorrentMani
2525
# ```
2626

2727
var
28-
pbNode = initProtoBuffer()
28+
pbNode = initProtoBuffer(data)
2929
pbInfo: ProtoBuffer
3030
length: uint64
3131
pieceLength: uint32
@@ -50,7 +50,7 @@ proc decode(_: type BitTorrentManifest, data: openArray[byte]): ?!BitTorrentMani
5050
var dataBuf = newSeq[byte]()
5151
if pbPiece.getField(1, dataBuf).isErr:
5252
return failure("Unable to decode `data` from BitTorrentPiece")
53-
without mhash =? MultiHash.init("sha1", dataBuf).mapFailure, err:
53+
without mhash =? MultiHash.init(dataBuf).mapFailure, err:
5454
return failure(err.msg)
5555
pieces.add(mhash)
5656
discard ?pbInfo.getField(4, name).mapFailure

codex/bittorrent/manifest/manifest.nim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ type
2121
info*: BitTorrentInfo
2222
codexManifestCid*: Cid
2323

24+
proc `$`*(self: BitTorrentInfo): string =
25+
"BitTorrentInfo(length: " & $self.length & ", pieceLength: " & $self.pieceLength &
26+
", pieces: " & $self.pieces & ", name: " & $self.name & ")"
27+
28+
proc `$`*(self: BitTorrentManifest): string =
29+
"BitTorrentManifest(info: " & $self.info & ", codexManifestCid: " &
30+
$self.codexManifestCid & ")"
31+
2432
proc newBitTorrentManifest*(
2533
info: BitTorrentInfo, codexManifestCid: Cid
2634
): BitTorrentManifest =

codex/node.nim

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,32 +159,34 @@ proc fetchManifest*(self: CodexNodeRef, cid: Cid): Future[?!Manifest] {.async.}
159159
manifest.success
160160

161161
proc fetchTorrentManifest*(
162-
self: CodexNodeRef, cid: Cid
162+
self: CodexNodeRef, infoHashCid: Cid
163163
): Future[?!BitTorrentManifest] {.async.} =
164-
if err =? cid.isTorrentInfoHash.errorOption:
164+
if err =? infoHashCid.isTorrentInfoHash.errorOption:
165165
return failure "CID has invalid content type for torrent info hash {$cid}"
166166

167-
trace "Retrieving torrent manifest for cid", cid
167+
trace "Retrieving torrent manifest for infoHashCid", infoHashCid
168168

169-
without blk =? await self.networkStore.getBlock(BlockAddress.init(cid)), err:
170-
trace "Error retrieve manifest block", cid, err = err.msg
169+
without blk =? await self.networkStore.getBlock(BlockAddress.init(infoHashCid)), err:
170+
trace "Error retrieve manifest block", infoHashCid, err = err.msg
171171
return failure err
172172

173-
trace "Decoding torrent manifest for cid", cid
173+
trace "Successfully retrieved torrent manifest with given block cid",
174+
cid = blk.cid, infoHashCid
175+
trace "Decoding torrent manifest"
174176

175177
without torrentManifest =? BitTorrentManifest.decode(blk), err:
176178
trace "Unable to decode torrent manifest", err = err.msg
177179
return failure("Unable to decode torrent manifest")
178180

179-
trace "Decoded torrent manifest", cid
181+
trace "Decoded torrent manifest", infoHashCid, torrentManifest = $torrentManifest
180182

181-
without isValid =? torrentManifest.validate(cid), err:
182-
trace "Error validating torrent manifest", cid, err = err.msg
183+
without isValid =? torrentManifest.validate(infoHashCid), err:
184+
trace "Error validating torrent manifest", infoHashCid, err = err.msg
183185
return failure(err.msg)
184186

185187
if not isValid:
186-
trace "Torrent manifest does not match torrent info hash", cid
187-
return failure "Torrent manifest does not match torrent info hash {$cid}"
188+
trace "Torrent manifest does not match torrent info hash", infoHashCid
189+
return failure "Torrent manifest does not match torrent info hash {$infoHashCid}"
188190

189191
return torrentManifest.success
190192

@@ -431,6 +433,7 @@ proc streamTorrent(
431433
error "Piece verification failed", pieceIndex = pieceIndex
432434
return failure("Piece verification failed")
433435

436+
trace "Piece verified", pieceIndex, pieceHash
434437
# great success
435438
success()
436439

@@ -629,12 +632,12 @@ proc storePieces*(
629632
## Save stream contents as dataset with given blockSize
630633
## to nodes's BlockStore, and return Cid of its manifest
631634
##
632-
info "Storing data"
635+
info "Storing pieces"
633636

634637
let
635638
hcodec = Sha256HashCodec
636639
dataCodec = BlockCodec
637-
chunker = LPStreamChunker.new(stream, chunkSize = blockSize)
640+
chunker = LPStreamChunker.new(stream, chunkSize = blockSize, pad = false)
638641
numOfBlocksPerPiece = pieceLength.int div blockSize.int
639642

640643
var
@@ -645,13 +648,18 @@ proc storePieces*(
645648

646649
pieceHashCtx.init()
647650

651+
trace "number of blocks per piece: ", numOfBlocksPerPiece
652+
648653
try:
649654
while (let chunk = await chunker.getBytes(); chunk.len > 0):
655+
trace "storing block...", chunkLength = chunk.len
650656
if pieceIter.finished:
657+
trace "finishing piece..."
651658
without mh =? MultiHash.init($Sha1HashCodec, pieceHashCtx.finish()).mapFailure,
652659
err:
653660
return failure(err)
654661
pieces.add(mh)
662+
trace "successfully computed piece multihash", pieces = $pieces
655663
pieceIter = Iter[int].new(0 ..< numOfBlocksPerPiece)
656664
pieceHashCtx.init()
657665
without mhash =? MultiHash.digest($hcodec, chunk).mapFailure, err:
@@ -669,7 +677,11 @@ proc storePieces*(
669677
error "Unable to store block", cid = blk.cid, err = err.msg
670678
return failure(&"Unable to store block {blk.cid}")
671679
pieceHashCtx.update(chunk)
672-
discard pieceIter.next()
680+
let idx = pieceIter.next()
681+
trace "stored block in piece with index=", idx
682+
if chunk.len < blockSize.int:
683+
trace "no more block to read"
684+
break
673685
except CancelledError as exc:
674686
raise exc
675687
except CatchableError as exc:
@@ -681,6 +693,8 @@ proc storePieces*(
681693
return failure(err)
682694
pieces.add(mh)
683695

696+
trace "finished processing blocks", pieces = $pieces
697+
684698
without tree =? CodexTree.init(cids), err:
685699
return failure(err)
686700

@@ -744,11 +758,17 @@ proc storeTorrent*(
744758
):
745759
return failure("Unable to store BitTorrent data")
746760

761+
trace "Created BitTorrent manifest", bitTorrentManifest = $bitTorrentManifest
762+
747763
let infoBencoded = bencode(bitTorrentManifest.info)
748764

765+
trace "BitTorrent Info successfully bencoded"
766+
749767
without infoHash =? MultiHash.digest($Sha1HashCodec, infoBencoded).mapFailure, err:
750768
return failure(err)
751769

770+
trace "computed info hash", infoHash = $infoHash
771+
752772
without manifestBlk =? await self.storeBitTorrentManifest(
753773
bitTorrentManifest, infoHash
754774
), err:

0 commit comments

Comments
 (0)