From 11da2339dea0a5cf31a75164461d351f0c329697 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 22 Apr 2021 10:11:24 +0200 Subject: [PATCH] Hard-code asset address We're only going to support a single asset for now. --- dagger/bitswap/engine.nim | 3 +-- dagger/bitswap/engine/payments.nim | 7 ++++--- dagger/bitswap/protobuf/message.proto | 1 - dagger/bitswap/protobuf/payments.nim | 7 ++----- tests/dagger/bitswap/engine/testpayments.nim | 2 +- tests/dagger/bitswap/protobuf/testpayments.nim | 14 +------------- tests/dagger/bitswap/testbitswap.nim | 3 ++- tests/dagger/bitswap/testengine.nim | 3 ++- tests/dagger/examples.nim | 1 - 9 files changed, 13 insertions(+), 28 deletions(-) diff --git a/dagger/bitswap/engine.nim b/dagger/bitswap/engine.nim index 78bd2af9..d1f6119e 100644 --- a/dagger/bitswap/engine.nim +++ b/dagger/bitswap/engine.nim @@ -276,9 +276,8 @@ proc paymentHandler*(engine: BitswapEngine, peer: PeerId, payment: SignedState) return if channel =? context.paymentChannel: - let asset = enginePricing.asset let sender = contextPricing.address - discard engine.wallet.acceptPayment(channel, asset, sender, payment) + discard engine.wallet.acceptPayment(channel, Asset, sender, payment) else: context.paymentChannel = engine.wallet.acceptChannel(payment).option diff --git a/dagger/bitswap/engine/payments.nim b/dagger/bitswap/engine/payments.nim index f4e52527..78d3aaeb 100644 --- a/dagger/bitswap/engine/payments.nim +++ b/dagger/bitswap/engine/payments.nim @@ -9,7 +9,8 @@ export peercontext push: {.upraises: [].} -const ChainId = 0.u256 # invalid chain id for now +const ChainId* = 0.u256 # invalid chain id for now +const Asset* = EthAddress.zero # invalid ERC20 asset address for now const AmountPerChannel = (10'u64^18).u256 # 1 asset, ERC20 default is 18 decimals func openLedgerChannel*(wallet: WalletRef, @@ -21,7 +22,7 @@ func getOrOpenChannel(wallet: WalletRef, peer: BitswapPeerCtx): ?!ChannelId = if channel =? peer.paymentChannel: success channel elif pricing =? peer.pricing: - let channel = ?wallet.openLedgerChannel(pricing.address, pricing.asset) + let channel = ?wallet.openLedgerChannel(pricing.address, Asset) peer.paymentChannel = channel.some success channel else: @@ -32,7 +33,7 @@ func pay*(wallet: WalletRef, amountOfBytes: int): ?!SignedState = if pricing =? peer.pricing: let amount = amountOfBytes.u256 * pricing.price - let asset = pricing.asset + let asset = Asset let receiver = pricing.address let channel = ?wallet.getOrOpenChannel(peer) wallet.pay(channel, asset, receiver, amount) diff --git a/dagger/bitswap/protobuf/message.proto b/dagger/bitswap/protobuf/message.proto index 4abb2e1b..d02810eb 100644 --- a/dagger/bitswap/protobuf/message.proto +++ b/dagger/bitswap/protobuf/message.proto @@ -39,7 +39,6 @@ message Message { 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/dagger/bitswap/protobuf/payments.nim b/dagger/bitswap/protobuf/payments.nim index 5fca5aa2..0c464eb5 100644 --- a/dagger/bitswap/protobuf/payments.nim +++ b/dagger/bitswap/protobuf/payments.nim @@ -17,13 +17,11 @@ push: {.upraises: [].} 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) ) @@ -42,11 +40,10 @@ func parse(_: type UInt256, bytes: seq[byte]): ?UInt256 = 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: + if address.isNone or price.isNone: return Pricing.none - Pricing(address: address.get, asset: asset.get, price: price.get).some + Pricing(address: address.get, price: price.get).some func init*(_: type StateChannelUpdate, state: SignedState): StateChannelUpdate = StateChannelUpdate(update: state.toJson.toBytes) diff --git a/tests/dagger/bitswap/engine/testpayments.nim b/tests/dagger/bitswap/engine/testpayments.nim index acc72a5a..e0e2c3cb 100644 --- a/tests/dagger/bitswap/engine/testpayments.nim +++ b/tests/dagger/bitswap/engine/testpayments.nim @@ -17,7 +17,7 @@ suite "engine payments": test "pays for received bytes": let payment = !wallet.pay(peer, amountOfBytes) let pricing = !peer.pricing - let balances = payment.state.outcome.balances(pricing.asset) + let balances = payment.state.outcome.balances(Asset) let destination = pricing.address.toDestination check !balances[destination] == amountOfBytes.u256 * pricing.price diff --git a/tests/dagger/bitswap/protobuf/testpayments.nim b/tests/dagger/bitswap/protobuf/testpayments.nim index f07775e9..76b35984 100644 --- a/tests/dagger/bitswap/protobuf/testpayments.nim +++ b/tests/dagger/bitswap/protobuf/testpayments.nim @@ -7,26 +7,19 @@ 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 pricing = Pricing(address: address, 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 @@ -35,11 +28,6 @@ suite "pricing protobuf messages": 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) diff --git a/tests/dagger/bitswap/testbitswap.nim b/tests/dagger/bitswap/testbitswap.nim index 52fb0c6e..74836451 100644 --- a/tests/dagger/bitswap/testbitswap.nim +++ b/tests/dagger/bitswap/testbitswap.nim @@ -9,6 +9,7 @@ import pkg/libp2p/errors import pkg/dagger/p2p/rng import pkg/dagger/bitswap +import pkg/dagger/bitswap/engine/payments import pkg/dagger/stores/memorystore import pkg/dagger/chunker import pkg/dagger/blocktype as bt @@ -154,7 +155,7 @@ suite "Bitswap engine - 2 nodes": let pricing = !bitswap2.engine.pricing await sleepAsync(100.millis) let channel = !peerCtx1.paymentChannel - check wallet2.balance(channel, pricing.asset) > 0 + check wallet2.balance(channel, Asset) > 0 suite "Bitswap - multiple nodes": let diff --git a/tests/dagger/bitswap/testengine.nim b/tests/dagger/bitswap/testengine.nim index cbdd7e75..14c3d1bf 100644 --- a/tests/dagger/bitswap/testengine.nim +++ b/tests/dagger/bitswap/testengine.nim @@ -9,6 +9,7 @@ import pkg/libp2p/errors import pkg/dagger/p2p/rng import pkg/dagger/bitswap import pkg/dagger/bitswap/pendingblocks +import pkg/dagger/bitswap/engine/payments import pkg/dagger/stores/memorystore import pkg/dagger/chunker import pkg/dagger/blocktype as bt @@ -155,7 +156,7 @@ suite "Bitswap engine handlers": engine.request.sendPayment = proc(receiver: PeerID, payment: SignedState) = let amount = blocks.mapIt(it.data.len).foldl(a+b).u256 * pricing.price - let balances = !payment.state.outcome.balances(pricing.asset) + let balances = !payment.state.outcome.balances(Asset) check receiver == peerId check balances[pricing.address.toDestination] == amount done.complete() diff --git a/tests/dagger/examples.nim b/tests/dagger/examples.nim index 9c4583f6..3f1b475f 100644 --- a/tests/dagger/examples.nim +++ b/tests/dagger/examples.nim @@ -37,7 +37,6 @@ proc example*(_: type SignedState): SignedState = proc example*(_: type Pricing): Pricing = Pricing( address: EthAddress.example, - asset: EthAddress.example, price: uint32.example.u256 )