uses MultiHash as a type for piece hashes (instead alias)

This commit is contained in:
Marcin Czenko 2025-03-24 02:13:07 +01:00
parent cbaace39a9
commit 42b74cc11f
No known key found for this signature in database
GPG Key ID: 33DEA0C8E30937C0
3 changed files with 6 additions and 5 deletions

View File

@ -1,4 +1,5 @@
import pkg/libp2p/cid
import pkg/libp2p/multihash
import pkg/libp2p/protobuf/minprotobuf
import pkg/questionable/results
@ -29,7 +30,7 @@ func decode*(_: type BitTorrentManifest, data: openArray[byte]): ?!BitTorrentMan
pbInfo: ProtoBuffer
length: uint64
pieceLength: uint32
pieces: seq[BitTorrentPiece]
pieces: seq[MultiHash]
piecesBytes: seq[seq[byte]]
name: string
cidBuf = newSeq[byte]()
@ -49,7 +50,7 @@ func decode*(_: type BitTorrentManifest, data: openArray[byte]): ?!BitTorrentMan
var pbPiece = initProtoBuffer(piece)
var dataBuf = newSeq[byte]()
if pbPiece.getField(1, dataBuf).isErr:
return failure("Unable to decode `data` from BitTorrentPiece")
return failure("Unable to decode piece `data` to MultiHash")
without mhash =? MultiHash.init(dataBuf).mapFailure, err:
return failure(err.msg)
pieces.add(mhash)

View File

@ -1,11 +1,12 @@
import pkg/libp2p/cid
import pkg/libp2p/multihash
import pkg/libp2p/protobuf/minprotobuf
import pkg/questionable/results
import ./manifest
proc write(pb: var ProtoBuffer, field: int, value: BitTorrentPiece) =
proc write(pb: var ProtoBuffer, field: int, value: MultiHash) =
var ipb = initProtoBuffer()
ipb.write(1, value.data.buffer)
ipb.finish()

View File

@ -11,11 +11,10 @@ import ../../codextypes
import ../bencoding
type
BitTorrentPiece* = MultiHash
BitTorrentInfo* = ref object
length* {.serialize.}: uint64
pieceLength* {.serialize.}: uint32
pieces* {.serialize.}: seq[BitTorrentPiece]
pieces* {.serialize.}: seq[MultiHash]
name* {.serialize.}: ?string
BitTorrentManifest* = ref object