2021-03-23 08:04:51 +00:00
|
|
|
{.used.}
|
|
|
|
|
2020-11-13 06:02:17 +00:00
|
|
|
import
|
2022-10-28 18:11:28 +00:00
|
|
|
std/[options, tables, sets],
|
2022-09-21 16:27:40 +00:00
|
|
|
stew/byteutils,
|
|
|
|
stew/shims/net as stewNet,
|
2021-03-26 09:52:04 +00:00
|
|
|
testutils/unittests,
|
2022-09-21 16:27:40 +00:00
|
|
|
chronos,
|
|
|
|
chronicles,
|
2020-11-21 05:31:48 +00:00
|
|
|
libp2p/switch,
|
|
|
|
libp2p/protobuf/minprotobuf,
|
|
|
|
libp2p/stream/[bufferstream, connection],
|
|
|
|
libp2p/crypto/[crypto, secp],
|
|
|
|
libp2p/switch,
|
2022-09-20 09:39:52 +00:00
|
|
|
eth/keys
|
|
|
|
import
|
2021-07-13 07:18:51 +00:00
|
|
|
../../waku/v2/protocol/waku_message,
|
2022-07-25 11:01:37 +00:00
|
|
|
../../waku/v2/protocol/waku_store,
|
2020-11-23 02:27:45 +00:00
|
|
|
../../waku/v2/protocol/waku_swap/waku_swap,
|
2022-09-20 09:39:52 +00:00
|
|
|
../../waku/v2/node/storage/message/waku_store_queue,
|
2022-10-18 14:05:53 +00:00
|
|
|
../../waku/v2/node/waku_node,
|
2021-10-06 12:29:08 +00:00
|
|
|
../../waku/v2/utils/peers,
|
2022-09-21 16:27:40 +00:00
|
|
|
../test_helpers,
|
2022-10-21 13:01:39 +00:00
|
|
|
./utils,
|
|
|
|
./testlib/common
|
2022-09-21 16:27:40 +00:00
|
|
|
|
2020-11-13 06:02:17 +00:00
|
|
|
|
|
|
|
procSuite "Waku SWAP Accounting":
|
|
|
|
test "Handshake Encode/Decode":
|
|
|
|
let
|
|
|
|
beneficiary = @[byte 0, 1, 2]
|
|
|
|
handshake = Handshake(beneficiary: beneficiary)
|
|
|
|
pb = handshake.encode()
|
|
|
|
|
|
|
|
let decodedHandshake = Handshake.init(pb.buffer)
|
|
|
|
|
|
|
|
check:
|
|
|
|
decodedHandshake.isErr == false
|
|
|
|
decodedHandshake.get().beneficiary == beneficiary
|
|
|
|
|
|
|
|
test "Cheque Encode/Decode":
|
|
|
|
let
|
|
|
|
amount = 1'u32
|
|
|
|
date = 9000'u32
|
|
|
|
beneficiary = @[byte 0, 1, 2]
|
|
|
|
cheque = Cheque(beneficiary: beneficiary, amount: amount, date: date)
|
|
|
|
pb = cheque.encode()
|
|
|
|
|
|
|
|
let decodedCheque = Cheque.init(pb.buffer)
|
|
|
|
|
|
|
|
check:
|
|
|
|
decodedCheque.isErr == false
|
|
|
|
decodedCheque.get() == cheque
|
2020-11-21 05:31:48 +00:00
|
|
|
|
2022-10-28 18:11:28 +00:00
|
|
|
# TODO: To do this reliably we need access to contract node
|
2021-03-01 05:55:20 +00:00
|
|
|
# With current logic state isn't updated because of bad cheque
|
|
|
|
# Consider moving this test to e2e test, and/or move swap module to be on by default
|
2020-11-21 05:31:48 +00:00
|
|
|
asyncTest "Update accounting state after store operations":
|
2022-09-21 16:27:40 +00:00
|
|
|
## Setup
|
2020-11-21 05:31:48 +00:00
|
|
|
let
|
2022-09-21 16:27:40 +00:00
|
|
|
serverKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-10-28 18:11:28 +00:00
|
|
|
server = WakuNode.new(serverKey, ValidIpAddress.init("0.0.0.0"), Port(60102))
|
2022-09-21 16:27:40 +00:00
|
|
|
clientKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-10-28 18:11:28 +00:00
|
|
|
client = WakuNode.new(clientKey, ValidIpAddress.init("0.0.0.0"), Port(60100))
|
2020-11-21 05:31:48 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
await allFutures(client.start(), server.start())
|
2022-10-28 18:11:28 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
await server.mountSwap()
|
|
|
|
await server.mountStore(store=StoreQueueRef.new())
|
|
|
|
await client.mountSwap()
|
|
|
|
await client.mountStore()
|
2022-10-28 18:11:28 +00:00
|
|
|
client.mountStoreClient()
|
2022-09-21 16:27:40 +00:00
|
|
|
|
|
|
|
client.wakuSwap.setPeer(server.peerInfo.toRemotePeerInfo())
|
|
|
|
server.wakuSwap.setPeer(client.peerInfo.toRemotePeerInfo())
|
|
|
|
|
2022-10-28 18:11:28 +00:00
|
|
|
client.setStorePeer(server.peerInfo.toRemotePeerInfo())
|
|
|
|
server.setStorePeer(client.peerInfo.toRemotePeerInfo())
|
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
## Given
|
|
|
|
let message = fakeWakuMessage()
|
2022-10-28 18:11:28 +00:00
|
|
|
require server.wakuStore.store.put(DefaultPubsubTopic, message).isOk()
|
2020-11-21 05:31:48 +00:00
|
|
|
|
2022-10-28 18:11:28 +00:00
|
|
|
let serverPeer = server.peerInfo.toRemotePeerInfo()
|
|
|
|
let rpc = HistoryQuery(contentFilters: @[HistoryContentFilter(contentTopic: DefaultContentTopic)])
|
2020-11-21 05:31:48 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
## When
|
2022-10-28 18:11:28 +00:00
|
|
|
let queryRes = await client.query(rpc, peer=serverPeer)
|
2020-11-21 05:31:48 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
## Then
|
|
|
|
check queryRes.isOk()
|
2020-11-21 05:31:48 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
let response = queryRes.get()
|
|
|
|
check:
|
|
|
|
response.messages == @[message]
|
2020-11-21 05:31:48 +00:00
|
|
|
|
|
|
|
check:
|
2022-09-21 16:27:40 +00:00
|
|
|
client.wakuSwap.accounting[server.peerInfo.peerId] == 1
|
|
|
|
server.wakuSwap.accounting[client.peerInfo.peerId] == -1
|
|
|
|
|
|
|
|
## Cleanup
|
|
|
|
await allFutures(client.stop(), server.stop())
|
|
|
|
|
2020-11-26 10:02:10 +00:00
|
|
|
|
2021-06-15 02:06:36 +00:00
|
|
|
# This test will only Be checked if in Mock mode
|
2022-09-21 16:27:40 +00:00
|
|
|
# TODO: Add cheque here
|
2021-06-15 02:06:36 +00:00
|
|
|
asyncTest "Update accounting state after sending cheque":
|
2022-09-21 16:27:40 +00:00
|
|
|
## Setup
|
2021-06-15 02:06:36 +00:00
|
|
|
let
|
2022-09-21 16:27:40 +00:00
|
|
|
serverKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-10-28 18:11:28 +00:00
|
|
|
server = WakuNode.new(serverKey, ValidIpAddress.init("0.0.0.0"), Port(60202))
|
2022-09-21 16:27:40 +00:00
|
|
|
clientKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-10-28 18:11:28 +00:00
|
|
|
client = WakuNode.new(clientKey, ValidIpAddress.init("0.0.0.0"), Port(60200))
|
2022-09-21 16:27:40 +00:00
|
|
|
|
2021-06-15 02:06:36 +00:00
|
|
|
# Define the waku swap Config for this test
|
|
|
|
let swapConfig = SwapConfig(mode: SwapMode.Mock, paymentThreshold: 1, disconnectThreshold: -1)
|
|
|
|
|
|
|
|
# Start nodes and mount protocols
|
2022-09-21 16:27:40 +00:00
|
|
|
await allFutures(client.start(), server.start())
|
|
|
|
await server.mountSwap(swapConfig)
|
|
|
|
await server.mountStore(store=StoreQueueRef.new())
|
|
|
|
await client.mountSwap(swapConfig)
|
|
|
|
await client.mountStore()
|
2022-10-28 18:11:28 +00:00
|
|
|
client.mountStoreClient()
|
2022-09-21 16:27:40 +00:00
|
|
|
|
|
|
|
client.wakuSwap.setPeer(server.peerInfo.toRemotePeerInfo())
|
|
|
|
server.wakuSwap.setPeer(client.peerInfo.toRemotePeerInfo())
|
2022-10-28 18:11:28 +00:00
|
|
|
|
|
|
|
client.setStorePeer(server.peerInfo.toRemotePeerInfo())
|
|
|
|
server.setStorePeer(client.peerInfo.toRemotePeerInfo())
|
2022-09-21 16:27:40 +00:00
|
|
|
|
|
|
|
## Given
|
|
|
|
let message = fakeWakuMessage()
|
2022-10-28 18:11:28 +00:00
|
|
|
require server.wakuStore.store.put(DefaultPubsubTopic, message).isOk()
|
|
|
|
|
|
|
|
let serverPeer = server.peerInfo.toRemotePeerInfo()
|
|
|
|
let rpc = HistoryQuery(contentFilters: @[HistoryContentFilter(contentTopic: DefaultContentTopic)])
|
2021-06-15 02:06:36 +00:00
|
|
|
|
2022-09-21 16:27:40 +00:00
|
|
|
## When
|
|
|
|
# TODO: Handshakes - for now we assume implicit, e2e still works for PoC
|
2022-10-28 18:11:28 +00:00
|
|
|
let res1 = await client.query(rpc, peer=serverPeer)
|
|
|
|
let res2 = await client.query(rpc, peer=serverPeer)
|
2021-06-15 02:06:36 +00:00
|
|
|
|
2022-10-28 18:11:28 +00:00
|
|
|
require:
|
2022-09-21 16:27:40 +00:00
|
|
|
res1.isOk()
|
|
|
|
res2.isOk()
|
2021-06-15 02:06:36 +00:00
|
|
|
|
2022-10-28 18:11:28 +00:00
|
|
|
## Then
|
2021-06-15 02:06:36 +00:00
|
|
|
check:
|
|
|
|
# Accounting table updated with credit and debit, respectively
|
|
|
|
# After sending a cheque the balance is partially adjusted
|
2022-09-21 16:27:40 +00:00
|
|
|
client.wakuSwap.accounting[server.peerInfo.peerId] == 1
|
|
|
|
server.wakuSwap.accounting[client.peerInfo.peerId] == -1
|
|
|
|
|
|
|
|
## Cleanup
|
|
|
|
await allFutures(client.stop(), server.stop())
|