From c819ba1be18162d82dc931b2ccf9524b173d8e32 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Wed, 7 Apr 2021 14:30:33 +0200 Subject: [PATCH] protobuf message for state channel updates --- dagger/bitswap/protobuf/payments.nim | 7 +++++++ dagger/bitswap/protobuf/payments.proto | 4 ++++ tests/dagger/bitswap/protobuf/testpayments.nim | 17 +++++++++++++++++ tests/dagger/examples.nim | 8 ++++++++ 4 files changed, 36 insertions(+) diff --git a/dagger/bitswap/protobuf/payments.nim b/dagger/bitswap/protobuf/payments.nim index ac180ab6..2a8f8f08 100644 --- a/dagger/bitswap/protobuf/payments.nim +++ b/dagger/bitswap/protobuf/payments.nim @@ -6,6 +6,7 @@ import pkg/upraises import_proto3 "payments.proto" export PricingMessage +export StateChannelUpdate export nitro @@ -44,3 +45,9 @@ func init*(_: type Pricing, message: PricingMessage): ?Pricing = if address.isNone or asset.isNone or price.isNone: return Pricing.none Pricing(address: address.get, asset: asset.get, price: price.get).some + +func init*(_: type StateChannelUpdate, state: SignedState): StateChannelUpdate = + StateChannelUpdate(update: state.toJson.toBytes) + +proc init*(_: type SignedState, update: StateChannelUpdate): ?SignedState = + SignedState.fromJson(string.fromBytes(update.update)) diff --git a/dagger/bitswap/protobuf/payments.proto b/dagger/bitswap/protobuf/payments.proto index 33085d37..1ef06ee6 100644 --- a/dagger/bitswap/protobuf/payments.proto +++ b/dagger/bitswap/protobuf/payments.proto @@ -7,3 +7,7 @@ message PricingMessage { bytes asset = 2; // Asset (coin) with which to pay bytes price = 3; // Amount of assets to pay per byte (UInt256) } + +message StateChannelUpdate { + bytes update = 1; // Signed Nitro state, serialized as JSON +} diff --git a/tests/dagger/bitswap/protobuf/testpayments.nim b/tests/dagger/bitswap/protobuf/testpayments.nim index a07b61c4..c8b5fde4 100644 --- a/tests/dagger/bitswap/protobuf/testpayments.nim +++ b/tests/dagger/bitswap/protobuf/testpayments.nim @@ -1,5 +1,6 @@ import pkg/asynctest import pkg/chronos +import pkg/stew/byteutils import ../../examples import ../../../../dagger/bitswap/protobuf/payments @@ -43,3 +44,19 @@ suite "pricing protobuf messages": var incorrect = message incorrect.price = newSeq[byte](33) check Pricing.init(incorrect).isNone + +suite "channel update messages": + + let state = SignedState.example + let update = StateChannelUpdate.init(state) + + test "encodes a nitro signed state": + check update.update == state.toJson.toBytes + + test "decodes a channel update": + check SignedState.init(update) == state.some + + test "fails to decode incorrect channel update": + var incorrect = update + incorrect.update.del(0) + check SignedState.init(incorrect).isNone diff --git a/tests/dagger/examples.nim b/tests/dagger/examples.nim index b1b3776d..51ac75a4 100644 --- a/tests/dagger/examples.nim +++ b/tests/dagger/examples.nim @@ -9,3 +9,11 @@ proc example*(_: type UInt256): UInt256 = for b in bytes.mitems: b = rand(byte) UInt256.fromBytes(bytes) + +proc example*(_: type SignedState): SignedState = + var wallet = Wallet.init(EthPrivateKey.random()) + let hub, asset, receiver = EthAddress.example + let chainId, amount = UInt256.example + let nonce = UInt48.rand() + let channel = wallet.openLedgerChannel(hub, chainId, nonce, asset, amount).get + wallet.pay(channel, asset, receiver, amount).get