Add initializers for bitswap messages

This commit is contained in:
Mark Spanbroek 2021-01-21 17:10:20 +01:00 committed by markspanbroek
parent fa15c92158
commit e6d84b9c0d
3 changed files with 39 additions and 0 deletions

15
ipfs/bitswap/messages.nim Normal file
View File

@ -0,0 +1,15 @@
import pkg/libp2p
import ../protobuf/bitswap
export Cid
export Message
proc want*(t: type Message, cids: varargs[Cid]): Message =
for cid in cids:
let entry = Entry(`block`: cid.data.buffer)
result.wantlist.entries.add(entry)
proc send*(t: type Message, blocks: varargs[seq[byte]]): Message =
for data in blocks:
let bloc = Block(data: data)
result.payload.add(bloc)

View File

@ -0,0 +1,23 @@
import std/unittest
import pkg/libp2p
import pkg/ipfs/protobuf/bitswap
import pkg/ipfs/bitswap/messages
import ../helpers/examples
suite "bitswap messages":
test "creates message with want list":
let cid1, cid2 = Cid.example
let message = Message.want(cid1, cid2)
check message == Message(wantlist: WantList(entries: @[
Entry(`block`: cid1.data.buffer),
Entry(`block`: cid2.data.buffer)
]))
test "creates message that sends blocks":
let block1, block2 = seq[byte].example
let message = Message.send(block1, block2)
check message == Message(payload: @[
Block(data: block1),
Block(data: block2)
])

View File

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