Add initializers for bitswap messages
This commit is contained in:
parent
fa15c92158
commit
e6d84b9c0d
|
@ -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)
|
|
@ -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)
|
||||
])
|
|
@ -3,6 +3,7 @@ import ./ipfs/testChunking
|
|||
import ./ipfs/testRepo
|
||||
import ./ipfs/testDhtRouting
|
||||
import ./ipfs/testProtobuf
|
||||
import ./ipfs/testBitswapMessages
|
||||
import ./ipfs/testIpfs
|
||||
|
||||
{.warning[UnusedImport]: off.}
|
||||
|
|
Loading…
Reference in New Issue