Serialize bitswap messages using protobuf

This commit is contained in:
Mark Spanbroek 2021-01-21 14:34:07 +01:00 committed by markspanbroek
parent 216317d4f5
commit fa15c92158
5 changed files with 47 additions and 0 deletions

View File

@ -6,4 +6,5 @@ license = "MIT"
requires "nim >= 1.4.2 & < 2.0.0" requires "nim >= 1.4.2 & < 2.0.0"
requires "libp2p >= 0.0.2 & < 0.1.0" requires "libp2p >= 0.0.2 & < 0.1.0"
requires "chronos >= 2.5.2 & < 3.0.0" requires "chronos >= 2.5.2 & < 3.0.0"
requires "protobufserialization >= 0.2.0 & < 0.3.0"
requires "asynctest >= 0.2.1 & < 0.3.0" requires "asynctest >= 0.2.1 & < 0.3.0"

View File

@ -0,0 +1,7 @@
import pkg/protobuf_serialization
import_proto3 "message.proto"
export Message
export Wantlist, WantType, Entry
export Block, BlockPresenceType, BlockPresence

View File

@ -0,0 +1,13 @@
import std/sequtils
import std/random
import pkg/libp2p
import pkg/ipfs/ipfsobject
proc example*(t: type seq[byte]): seq[byte] =
newSeqWith(10, rand(byte))
proc example*(t: type IpfsObject): IpfsObject =
IpfsObject(data: seq[byte].example)
proc example*(t: type Cid): Cid =
IpfsObject.example.cid

View File

@ -0,0 +1,25 @@
import std/unittest
import pkg/libp2p
import pkg/protobuf_serialization
import pkg/ipfs/protobuf/bitswap
import ../helpers/examples
suite "protobuf messages":
test "serializes bitswap want lists":
let cid = Cid.example
let entry = Entry(`block`: cid.data.buffer)
let wantlist = WantList(entries: @[entry])
let message = Message(wantlist: wantlist)
let encoded = Protobuf.encode(message)
check Protobuf.decode(encoded, Message) == message
test "serializes bitswap blocks":
let bloc = Block(data: seq[byte].example)
let message = Message(payload: @[bloc])
let encoded = Protobuf.encode(message)
check Protobuf.decode(encoded, Message) == message

View File

@ -2,6 +2,7 @@ import ./ipfs/testObject
import ./ipfs/testChunking import ./ipfs/testChunking
import ./ipfs/testRepo import ./ipfs/testRepo
import ./ipfs/testDhtRouting import ./ipfs/testDhtRouting
import ./ipfs/testProtobuf
import ./ipfs/testIpfs import ./ipfs/testIpfs
{.warning[UnusedImport]: off.} {.warning[UnusedImport]: off.}