@@ -159,32 +159,34 @@ proc fetchManifest*(self: CodexNodeRef, cid: Cid): Future[?!Manifest] {.async.}
159
159
manifest.success
160
160
161
161
proc fetchTorrentManifest* (
162
- self: CodexNodeRef, cid : Cid
162
+ self: CodexNodeRef, infoHashCid : Cid
163
163
): Future[?! BitTorrentManifest] {.async.} =
164
- if err =? cid .isTorrentInfoHash.errorOption:
164
+ if err =? infoHashCid .isTorrentInfoHash.errorOption:
165
165
return failure " CID has invalid content type for torrent info hash {$cid}"
166
166
167
- trace " Retrieving torrent manifest for cid " , cid
167
+ trace " Retrieving torrent manifest for infoHashCid " , infoHashCid
168
168
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
171
171
return failure err
172
172
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"
174
176
175
177
without torrentManifest =? BitTorrentManifest.decode(blk), err:
176
178
trace " Unable to decode torrent manifest" , err = err.msg
177
179
return failure(" Unable to decode torrent manifest" )
178
180
179
- trace " Decoded torrent manifest" , cid
181
+ trace " Decoded torrent manifest" , infoHashCid, torrentManifest = $ torrentManifest
180
182
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
183
185
return failure(err.msg)
184
186
185
187
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 }"
188
190
189
191
return torrentManifest.success
190
192
@@ -431,6 +433,7 @@ proc streamTorrent(
431
433
error " Piece verification failed" , pieceIndex = pieceIndex
432
434
return failure(" Piece verification failed" )
433
435
436
+ trace " Piece verified" , pieceIndex, pieceHash
434
437
# great success
435
438
success()
436
439
@@ -629,12 +632,12 @@ proc storePieces*(
629
632
# # Save stream contents as dataset with given blockSize
630
633
# # to nodes's BlockStore, and return Cid of its manifest
631
634
# #
632
- info " Storing data "
635
+ info " Storing pieces "
633
636
634
637
let
635
638
hcodec = Sha256HashCodec
636
639
dataCodec = BlockCodec
637
- chunker = LPStreamChunker.new(stream, chunkSize = blockSize)
640
+ chunker = LPStreamChunker.new(stream, chunkSize = blockSize, pad = false )
638
641
numOfBlocksPerPiece = pieceLength.int div blockSize.int
639
642
640
643
var
@@ -645,13 +648,18 @@ proc storePieces*(
645
648
646
649
pieceHashCtx.init()
647
650
651
+ trace " number of blocks per piece: " , numOfBlocksPerPiece
652
+
648
653
try :
649
654
while (let chunk = await chunker.getBytes(); chunk.len > 0 ):
655
+ trace " storing block..." , chunkLength = chunk.len
650
656
if pieceIter.finished:
657
+ trace " finishing piece..."
651
658
without mh =? MultiHash.init($ Sha1HashCodec, pieceHashCtx.finish()).mapFailure,
652
659
err:
653
660
return failure(err)
654
661
pieces.add(mh)
662
+ trace " successfully computed piece multihash" , pieces = $ pieces
655
663
pieceIter = Iter[int ].new(0 ..< numOfBlocksPerPiece)
656
664
pieceHashCtx.init()
657
665
without mhash =? MultiHash.digest($ hcodec, chunk).mapFailure, err:
@@ -669,7 +677,11 @@ proc storePieces*(
669
677
error " Unable to store block" , cid = blk.cid, err = err.msg
670
678
return failure(& " Unable to store block { blk.cid} " )
671
679
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
673
685
except CancelledError as exc:
674
686
raise exc
675
687
except CatchableError as exc:
@@ -681,6 +693,8 @@ proc storePieces*(
681
693
return failure(err)
682
694
pieces.add(mh)
683
695
696
+ trace " finished processing blocks" , pieces = $ pieces
697
+
684
698
without tree =? CodexTree.init(cids), err:
685
699
return failure(err)
686
700
@@ -744,11 +758,17 @@ proc storeTorrent*(
744
758
):
745
759
return failure(" Unable to store BitTorrent data" )
746
760
761
+ trace " Created BitTorrent manifest" , bitTorrentManifest = $ bitTorrentManifest
762
+
747
763
let infoBencoded = bencode(bitTorrentManifest.info)
748
764
765
+ trace " BitTorrent Info successfully bencoded"
766
+
749
767
without infoHash =? MultiHash.digest($ Sha1HashCodec, infoBencoded).mapFailure, err:
750
768
return failure(err)
751
769
770
+ trace " computed info hash" , infoHash = $ infoHash
771
+
752
772
without manifestBlk =? await self.storeBitTorrentManifest(
753
773
bitTorrentManifest, infoHash
754
774
), err:
0 commit comments