Hard-code asset address

We're only going to support a single asset for now.
This commit is contained in:
Mark Spanbroek 2021-04-22 10:11:24 +02:00 committed by markspanbroek
parent 811b29fb5e
commit 11da2339de
9 changed files with 13 additions and 28 deletions

View File

@ -276,9 +276,8 @@ proc paymentHandler*(engine: BitswapEngine, peer: PeerId, payment: SignedState)
return return
if channel =? context.paymentChannel: if channel =? context.paymentChannel:
let asset = enginePricing.asset
let sender = contextPricing.address let sender = contextPricing.address
discard engine.wallet.acceptPayment(channel, asset, sender, payment) discard engine.wallet.acceptPayment(channel, Asset, sender, payment)
else: else:
context.paymentChannel = engine.wallet.acceptChannel(payment).option context.paymentChannel = engine.wallet.acceptChannel(payment).option

View File

@ -9,7 +9,8 @@ export peercontext
push: {.upraises: [].} 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 const AmountPerChannel = (10'u64^18).u256 # 1 asset, ERC20 default is 18 decimals
func openLedgerChannel*(wallet: WalletRef, func openLedgerChannel*(wallet: WalletRef,
@ -21,7 +22,7 @@ func getOrOpenChannel(wallet: WalletRef, peer: BitswapPeerCtx): ?!ChannelId =
if channel =? peer.paymentChannel: if channel =? peer.paymentChannel:
success channel success channel
elif pricing =? peer.pricing: elif pricing =? peer.pricing:
let channel = ?wallet.openLedgerChannel(pricing.address, pricing.asset) let channel = ?wallet.openLedgerChannel(pricing.address, Asset)
peer.paymentChannel = channel.some peer.paymentChannel = channel.some
success channel success channel
else: else:
@ -32,7 +33,7 @@ func pay*(wallet: WalletRef,
amountOfBytes: int): ?!SignedState = amountOfBytes: int): ?!SignedState =
if pricing =? peer.pricing: if pricing =? peer.pricing:
let amount = amountOfBytes.u256 * pricing.price let amount = amountOfBytes.u256 * pricing.price
let asset = pricing.asset let asset = Asset
let receiver = pricing.address let receiver = pricing.address
let channel = ?wallet.getOrOpenChannel(peer) let channel = ?wallet.getOrOpenChannel(peer)
wallet.pay(channel, asset, receiver, amount) wallet.pay(channel, asset, receiver, amount)

View File

@ -39,7 +39,6 @@ message Message {
message PricingMessage { message PricingMessage {
bytes address = 1; // Ethereum address to which payments should be made 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) bytes price = 3; // Amount of assets to pay per byte (UInt256)
} }

View File

@ -17,13 +17,11 @@ push: {.upraises: [].}
type type
Pricing* = object Pricing* = object
address*: EthAddress address*: EthAddress
asset*: EthAddress
price*: UInt256 price*: UInt256
func init*(_: type PricingMessage, pricing: Pricing): PricingMessage = func init*(_: type PricingMessage, pricing: Pricing): PricingMessage =
PricingMessage( PricingMessage(
address: @(pricing.address.toArray), address: @(pricing.address.toArray),
asset: @(pricing.asset.toArray),
price: @(pricing.price.toBytesBE) price: @(pricing.price.toBytesBE)
) )
@ -42,11 +40,10 @@ func parse(_: type UInt256, bytes: seq[byte]): ?UInt256 =
func init*(_: type Pricing, message: PricingMessage): ?Pricing = func init*(_: type Pricing, message: PricingMessage): ?Pricing =
let address = EthAddress.parse(message.address) let address = EthAddress.parse(message.address)
let asset = EThAddress.parse(message.asset)
let price = UInt256.parse(message.price) let price = UInt256.parse(message.price)
if address.isNone or asset.isNone or price.isNone: if address.isNone or price.isNone:
return Pricing.none 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 = func init*(_: type StateChannelUpdate, state: SignedState): StateChannelUpdate =
StateChannelUpdate(update: state.toJson.toBytes) StateChannelUpdate(update: state.toJson.toBytes)

View File

@ -17,7 +17,7 @@ suite "engine payments":
test "pays for received bytes": test "pays for received bytes":
let payment = !wallet.pay(peer, amountOfBytes) let payment = !wallet.pay(peer, amountOfBytes)
let pricing = !peer.pricing let pricing = !peer.pricing
let balances = payment.state.outcome.balances(pricing.asset) let balances = payment.state.outcome.balances(Asset)
let destination = pricing.address.toDestination let destination = pricing.address.toDestination
check !balances[destination] == amountOfBytes.u256 * pricing.price check !balances[destination] == amountOfBytes.u256 * pricing.price

View File

@ -7,26 +7,19 @@ import ../../../../dagger/bitswap/protobuf/payments
suite "pricing protobuf messages": suite "pricing protobuf messages":
let address = EthAddress.example let address = EthAddress.example
let asset = EthAddress.example
let price = UInt256.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) let message = PricingMessage.init(pricing)
test "encodes recipient of payments": test "encodes recipient of payments":
check message.address == @(address.toArray) check message.address == @(address.toArray)
test "encodes address of asset":
check message.asset == @(asset.toArray)
test "encodes price per byte": test "encodes price per byte":
check message.price == @(price.toBytesBE) check message.price == @(price.toBytesBE)
test "decodes recipient of payments": test "decodes recipient of payments":
check Pricing.init(message).?address == address.some check Pricing.init(message).?address == address.some
test "decodes address of asset":
check Pricing.init(message).?asset == asset.some
test "decodes price": test "decodes price":
check Pricing.init(message).?price == price.some check Pricing.init(message).?price == price.some
@ -35,11 +28,6 @@ suite "pricing protobuf messages":
incorrect.address.del(0) incorrect.address.del(0)
check Pricing.init(incorrect).isNone 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": test "fails to decode when price has too many bytes":
var incorrect = message var incorrect = message
incorrect.price = newSeq[byte](33) incorrect.price = newSeq[byte](33)

View File

@ -9,6 +9,7 @@ import pkg/libp2p/errors
import pkg/dagger/p2p/rng import pkg/dagger/p2p/rng
import pkg/dagger/bitswap import pkg/dagger/bitswap
import pkg/dagger/bitswap/engine/payments
import pkg/dagger/stores/memorystore import pkg/dagger/stores/memorystore
import pkg/dagger/chunker import pkg/dagger/chunker
import pkg/dagger/blocktype as bt import pkg/dagger/blocktype as bt
@ -154,7 +155,7 @@ suite "Bitswap engine - 2 nodes":
let pricing = !bitswap2.engine.pricing let pricing = !bitswap2.engine.pricing
await sleepAsync(100.millis) await sleepAsync(100.millis)
let channel = !peerCtx1.paymentChannel let channel = !peerCtx1.paymentChannel
check wallet2.balance(channel, pricing.asset) > 0 check wallet2.balance(channel, Asset) > 0
suite "Bitswap - multiple nodes": suite "Bitswap - multiple nodes":
let let

View File

@ -9,6 +9,7 @@ import pkg/libp2p/errors
import pkg/dagger/p2p/rng import pkg/dagger/p2p/rng
import pkg/dagger/bitswap import pkg/dagger/bitswap
import pkg/dagger/bitswap/pendingblocks import pkg/dagger/bitswap/pendingblocks
import pkg/dagger/bitswap/engine/payments
import pkg/dagger/stores/memorystore import pkg/dagger/stores/memorystore
import pkg/dagger/chunker import pkg/dagger/chunker
import pkg/dagger/blocktype as bt import pkg/dagger/blocktype as bt
@ -155,7 +156,7 @@ suite "Bitswap engine handlers":
engine.request.sendPayment = proc(receiver: PeerID, payment: SignedState) = engine.request.sendPayment = proc(receiver: PeerID, payment: SignedState) =
let amount = blocks.mapIt(it.data.len).foldl(a+b).u256 * pricing.price 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 receiver == peerId
check balances[pricing.address.toDestination] == amount check balances[pricing.address.toDestination] == amount
done.complete() done.complete()

View File

@ -37,7 +37,6 @@ proc example*(_: type SignedState): SignedState =
proc example*(_: type Pricing): Pricing = proc example*(_: type Pricing): Pricing =
Pricing( Pricing(
address: EthAddress.example, address: EthAddress.example,
asset: EthAddress.example,
price: uint32.example.u256 price: uint32.example.u256
) )