2021-04-14 14:07:49 +00:00
|
|
|
import std/unittest
|
|
|
|
import pkg/dagger/bitswap/engine/payments
|
|
|
|
import ../../examples
|
|
|
|
|
|
|
|
suite "engine payments":
|
|
|
|
|
|
|
|
let amountOfBytes = 42
|
|
|
|
|
2021-04-19 14:37:38 +00:00
|
|
|
var wallet: WalletRef
|
2021-04-14 14:07:49 +00:00
|
|
|
var peer: BitswapPeerCtx
|
|
|
|
|
|
|
|
setup:
|
2021-04-19 14:37:38 +00:00
|
|
|
wallet = WalletRef.example
|
2021-04-14 14:07:49 +00:00
|
|
|
peer = BitswapPeerCtx.example
|
|
|
|
peer.pricing = Pricing.example.some
|
|
|
|
|
|
|
|
test "pays for received bytes":
|
2021-04-15 09:23:49 +00:00
|
|
|
let payment = !wallet.pay(peer, amountOfBytes)
|
|
|
|
let pricing = !peer.pricing
|
2021-04-14 14:07:49 +00:00
|
|
|
let balances = payment.state.outcome.balances(pricing.asset)
|
|
|
|
let destination = pricing.address.toDestination
|
2021-04-15 09:23:49 +00:00
|
|
|
check !balances[destination] == amountOfBytes.u256 * pricing.price
|
2021-04-14 14:07:49 +00:00
|
|
|
|
|
|
|
test "no payment when no price is set":
|
|
|
|
peer.pricing = Pricing.none
|
2021-04-15 09:23:49 +00:00
|
|
|
check wallet.pay(peer, amountOfBytes).isFailure
|
2021-04-14 14:07:49 +00:00
|
|
|
|
|
|
|
test "uses same channel for consecutive payments":
|
|
|
|
let payment1, payment2 = wallet.pay(peer, amountOfBytes)
|
|
|
|
let channel1 = payment1.?state.?channel.?getChannelId
|
|
|
|
let channel2 = payment2.?state.?channel.?getChannelId
|
|
|
|
check channel1 == channel2
|