From 1f2a9f90d54fdcc9c17f65d11c1b59be1e5b9cf4 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Wed, 7 Apr 2021 12:19:00 +0200 Subject: [PATCH] protobuf message for bandwidth pricing --- dagger/bitswap/protobuf/payments.nim | 43 ++++++++++++++++++ dagger/bitswap/protobuf/payments.proto | 9 ++++ .../dagger/bitswap/protobuf/testpayments.nim | 45 +++++++++++++++++++ tests/dagger/examples.nim | 11 +++++ tests/testAll.nim | 1 + 5 files changed, 109 insertions(+) create mode 100644 dagger/bitswap/protobuf/payments.nim create mode 100644 dagger/bitswap/protobuf/payments.proto create mode 100644 tests/dagger/bitswap/protobuf/testpayments.nim create mode 100644 tests/dagger/examples.nim diff --git a/dagger/bitswap/protobuf/payments.nim b/dagger/bitswap/protobuf/payments.nim new file mode 100644 index 00000000..84d55618 --- /dev/null +++ b/dagger/bitswap/protobuf/payments.nim @@ -0,0 +1,43 @@ +import pkg/protobuf_serialization +import pkg/nitro +import pkg/questionable + +import_proto3 "payments.proto" + +export PricingMessage + +export nitro + +type + Pricing* = object + address*: EthAddress + asset*: EthAddress + price*: UInt256 + +func init*(_: type PricingMessage, pricing: Pricing): PricingMessage = + PricingMessage( + address: @(pricing.address.toArray), + asset: @(pricing.asset.toArray), + price: @(pricing.price.toBytesBE) + ) + +func parse(_: type EthAddress, bytes: seq[byte]): ?EthAddress = + var address: array[20, byte] + if bytes.len != address.len: + return EthAddress.none + for i in 0.. 32: + return UInt256.none + UInt256.fromBytesBE(bytes).some + +func init*(_: type Pricing, message: PricingMessage): ?Pricing = + let address = EthAddress.parse(message.address) + let asset = EThAddress.parse(message.asset) + let price = UInt256.parse(message.price) + if address.isNone or asset.isNone or price.isNone: + return Pricing.none + Pricing(address: address.get, asset: asset.get, price: price.get).some diff --git a/dagger/bitswap/protobuf/payments.proto b/dagger/bitswap/protobuf/payments.proto new file mode 100644 index 00000000..33085d37 --- /dev/null +++ b/dagger/bitswap/protobuf/payments.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; + +package bitswap.payments.pb; + +message PricingMessage { + bytes address = 1; // Ethereum address to which payments should be made + bytes asset = 2; // Asset (coin) with which to pay + bytes price = 3; // Amount of assets to pay per byte (UInt256) +} diff --git a/tests/dagger/bitswap/protobuf/testpayments.nim b/tests/dagger/bitswap/protobuf/testpayments.nim new file mode 100644 index 00000000..a07b61c4 --- /dev/null +++ b/tests/dagger/bitswap/protobuf/testpayments.nim @@ -0,0 +1,45 @@ +import pkg/asynctest +import pkg/chronos +import ../../examples +import ../../../../dagger/bitswap/protobuf/payments + +suite "pricing protobuf messages": + + let address = EthAddress.example + let asset = EthAddress.example + let price = UInt256.example + let pricing = Pricing(address: address, asset: asset, price: price) + let message = PricingMessage.init(pricing) + + test "encodes recipient of payments": + check message.address == @(address.toArray) + + test "encodes address of asset": + check message.asset == @(asset.toArray) + + test "encodes price per byte": + check message.price == @(price.toBytesBE) + + test "decodes recipient of payments": + check Pricing.init(message)?.address == address.some + + test "decodes address of asset": + check Pricing.init(message)?.asset == asset.some + + test "decodes price": + check Pricing.init(message)?.price == price.some + + test "fails to decode when address has incorrect number of bytes": + var incorrect = message + incorrect.address.del(0) + check Pricing.init(incorrect).isNone + + test "fails to decode when asset has incorrect number of bytes": + var incorrect = message + incorrect.asset.del(0) + check Pricing.init(incorrect).isNone + + test "fails to decode when price has too many bytes": + var incorrect = message + incorrect.price = newSeq[byte](33) + check Pricing.init(incorrect).isNone diff --git a/tests/dagger/examples.nim b/tests/dagger/examples.nim new file mode 100644 index 00000000..b1b3776d --- /dev/null +++ b/tests/dagger/examples.nim @@ -0,0 +1,11 @@ +import std/random +import pkg/nitro + +proc example*(_: type EthAddress): EthAddress = + EthPrivateKey.random().toPublicKey.toAddress + +proc example*(_: type UInt256): UInt256 = + var bytes: array[32, byte] + for b in bytes.mitems: + b = rand(byte) + UInt256.fromBytes(bytes) diff --git a/tests/testAll.nim b/tests/testAll.nim index 0fcd505b..4e007243 100644 --- a/tests/testAll.nim +++ b/tests/testAll.nim @@ -1,6 +1,7 @@ import ./dagger/bitswap/testbitswap import ./dagger/bitswap/testengine import ./dagger/bitswap/testnetwork +import ./dagger/bitswap/protobuf/testpayments import ./dagger/testasyncheapqueue import ./dagger/testblockstore import ./dagger/testchunking