Add pricing to block presence messages

This commit is contained in:
Mark Spanbroek 2021-04-26 16:34:04 +02:00 committed by markspanbroek
parent 11da2339de
commit 0ac876dbc3
5 changed files with 100 additions and 0 deletions

View File

@ -35,6 +35,7 @@ message Message {
message BlockPresence {
bytes cid = 1;
BlockPresenceType type = 2;
bytes price = 3; // Amount of assets to pay per byte (UInt256)
}
message PricingMessage {

View File

@ -0,0 +1,46 @@
import libp2p
import pkg/stint
import pkg/questionable
import pkg/questionable/results
import pkg/upraises
import ./bitswap
export questionable
export stint
export BlockPresenceType
upraises.push: {.upraises: [].}
type
PresenceMessage* = bitswap.BlockPresence
Presence* = object
cid*: Cid
have*: bool
price*: UInt256
func init*(_: type PresenceMessage, presence: Presence): PresenceMessage =
PresenceMessage(
cid: presence.cid.data.buffer,
`type`: if presence.have: presenceHave else: presenceDontHave,
price: @(presence.price.toBytesBE)
)
func parse(_: type UInt256, bytes: seq[byte]): ?UInt256 =
if bytes.len > 32:
return UInt256.none
UInt256.fromBytesBE(bytes).some
func init*(_: type Presence, message: PresenceMessage): ?Presence =
mixin parsedCid, parsedPrice
without parsedCid =? Cid.init(message.cid):
return none Presence
without parsedPrice =? UInt256.parse(message.price):
return none Presence
some Presence(
cid: parsedCid,
have: message.`type` == presenceHave,
price: parsedPrice
)

View File

@ -0,0 +1,49 @@
import std/sequtils
import pkg/asynctest
import pkg/chronos
import pkg/libp2p
import ../../../../dagger/bitswap/protobuf/presence
import ../../examples
suite "block presence protobuf messages":
let cid = Cid.example
let price = UInt256.example
let presence = Presence(cid: cid, have: true, price: price)
let message = PresenceMessage.init(presence)
test "encodes CID":
check message.cid == cid.data.buffer
test "encodes have/donthave":
var presence = presence
presence.have = true
check PresenceMessage.init(presence).`type` == presenceHave
presence.have = false
check PresenceMessage.init(presence).`type` == presenceDontHave
test "encodes price":
check message.price == @(price.toBytesBE)
test "decodes CID":
check Presence.init(message).?cid == cid.some
test "fails to decode when CID is invalid":
var incorrect = message
incorrect.cid.del(0)
check Presence.init(incorrect).isNone
test "decodes have/donthave":
var message = message
message.`type` = presenceHave
check Presence.init(message).?have == true.some
message.`type` = presenceDontHave
check Presence.init(message).?have == false.some
test "decodes price":
check Presence.init(message).?price == price.some
test "fails to decode when price is invalid":
var incorrect = message
incorrect.price.add(0)
check Presence.init(incorrect).isNone

View File

@ -51,3 +51,6 @@ proc example*(_: type PeerId): PeerID =
proc example*(_: type BitswapPeerCtx): BitswapPeerCtx =
BitswapPeerCtx(id: PeerID.example)
proc example*(_: type Cid): Cid =
Block.example.cid

View File

@ -2,6 +2,7 @@ import ./dagger/bitswap/testbitswap
import ./dagger/bitswap/testengine
import ./dagger/bitswap/testnetwork
import ./dagger/bitswap/protobuf/testpayments as testprotobufpayments
import ./dagger/bitswap/protobuf/testpresence
import ./dagger/bitswap/engine/testpayments as testenginepayments
import ./dagger/testasyncheapqueue
import ./dagger/testblockstore