2021-03-25 08:37:11 +00:00
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
|
|
import
|
2022-11-04 09:52:08 +00:00
|
|
|
|
testutils/unittests,
|
2022-10-27 22:05:02 +00:00
|
|
|
|
libp2p/crypto/crypto
|
|
|
|
|
import
|
2023-06-22 09:27:40 +00:00
|
|
|
|
../../waku/common/databases/db_sqlite,
|
2023-08-09 17:11:50 +00:00
|
|
|
|
../../waku/node/peer_manager/peer_manager,
|
|
|
|
|
../../waku/node/peer_manager/peer_store/waku_peer_storage,
|
2023-04-05 14:01:51 +00:00
|
|
|
|
./testlib/wakucore
|
2021-03-25 08:37:11 +00:00
|
|
|
|
|
2022-11-04 09:52:08 +00:00
|
|
|
|
|
2021-03-25 08:37:11 +00:00
|
|
|
|
suite "Peer Storage":
|
|
|
|
|
|
2021-03-26 08:49:51 +00:00
|
|
|
|
test "Store, replace and retrieve from persistent peer storage":
|
2023-02-13 10:43:49 +00:00
|
|
|
|
let
|
2022-10-27 22:05:02 +00:00
|
|
|
|
database = SqliteDatabase.new(":memory:").tryGet()
|
2021-03-26 08:49:51 +00:00
|
|
|
|
storage = WakuPeerStorage.new(database)[]
|
2021-03-25 08:37:11 +00:00
|
|
|
|
|
|
|
|
|
# Test Peer
|
|
|
|
|
peerLoc = MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet()
|
2023-02-13 10:43:49 +00:00
|
|
|
|
peerKey = generateEcdsaKey()
|
2021-11-04 14:46:38 +00:00
|
|
|
|
peer = PeerInfo.new(peerKey, @[peerLoc])
|
2021-03-25 08:37:11 +00:00
|
|
|
|
peerProto = "/waku/2/default-waku/codec"
|
2023-03-09 18:05:50 +00:00
|
|
|
|
connectedness = Connectedness.CanConnect
|
2021-04-21 09:36:56 +00:00
|
|
|
|
disconn = 999999
|
2023-03-09 18:05:50 +00:00
|
|
|
|
stored = RemotePeerInfo(
|
|
|
|
|
peerId: peer.peerId,
|
|
|
|
|
addrs: @[peerLoc],
|
|
|
|
|
protocols: @[peerProto],
|
|
|
|
|
publicKey: peerKey.getPublicKey().tryGet(),
|
|
|
|
|
connectedness: connectedness,
|
|
|
|
|
disconnectTime: disconn)
|
2023-02-13 10:43:49 +00:00
|
|
|
|
|
2021-03-25 08:37:11 +00:00
|
|
|
|
defer: storage.close()
|
2023-02-13 10:43:49 +00:00
|
|
|
|
|
2021-03-26 08:49:51 +00:00
|
|
|
|
# Test insert and retrieve
|
|
|
|
|
|
2023-03-09 18:05:50 +00:00
|
|
|
|
require storage.put(peer.peerId, stored, connectedness, disconn).isOk
|
2023-02-13 10:43:49 +00:00
|
|
|
|
|
2021-03-25 08:37:11 +00:00
|
|
|
|
var responseCount = 0
|
2021-07-14 17:58:46 +00:00
|
|
|
|
|
2023-03-09 18:05:50 +00:00
|
|
|
|
# Fetched variables from callback
|
|
|
|
|
var resPeerId: PeerId
|
|
|
|
|
var resStoredInfo: RemotePeerInfo
|
|
|
|
|
var resConnectedness: Connectedness
|
|
|
|
|
var resDisconnect: int64
|
|
|
|
|
|
|
|
|
|
proc data(peerId: PeerID, storedInfo: RemotePeerInfo,
|
2021-07-14 17:58:46 +00:00
|
|
|
|
connectedness: Connectedness, disconnectTime: int64) {.raises: [Defect].} =
|
2021-03-25 08:37:11 +00:00
|
|
|
|
responseCount += 1
|
2023-02-13 10:43:49 +00:00
|
|
|
|
|
2021-07-14 17:58:46 +00:00
|
|
|
|
# Note: cannot use `check` within `{.raises: [Defect].}` block
|
|
|
|
|
# @TODO: /Nim/lib/pure/unittest.nim(577, 16) Error: can raise an unlisted exception: Exception
|
|
|
|
|
# These flags are checked outside this block.
|
2023-03-09 18:05:50 +00:00
|
|
|
|
resPeerId = peerId
|
|
|
|
|
resStoredInfo = storedInfo
|
|
|
|
|
resConnectedness = connectedness
|
|
|
|
|
resDisconnect = disconnectTime
|
2023-02-13 10:43:49 +00:00
|
|
|
|
|
2021-03-25 08:37:11 +00:00
|
|
|
|
let res = storage.getAll(data)
|
2023-02-13 10:43:49 +00:00
|
|
|
|
|
2021-03-25 08:37:11 +00:00
|
|
|
|
check:
|
|
|
|
|
res.isErr == false
|
|
|
|
|
responseCount == 1
|
2023-03-09 18:05:50 +00:00
|
|
|
|
resPeerId == peer.peerId
|
|
|
|
|
resStoredInfo.peerId == peer.peerId
|
|
|
|
|
resStoredInfo.addrs == @[peerLoc]
|
|
|
|
|
resStoredInfo.protocols == @[peerProto]
|
|
|
|
|
resStoredInfo.publicKey == peerKey.getPublicKey().tryGet()
|
|
|
|
|
# TODO: For compatibility, we don't store connectedness and disconnectTime
|
|
|
|
|
#resStoredInfo.connectedness == connectedness
|
|
|
|
|
#resStoredInfo.disconnectTime == disconn
|
|
|
|
|
resConnectedness == Connectedness.CanConnect
|
|
|
|
|
resDisconnect == disconn
|
2023-02-13 10:43:49 +00:00
|
|
|
|
|
2021-03-26 08:49:51 +00:00
|
|
|
|
# Test replace and retrieve (update an existing entry)
|
2023-03-09 18:05:50 +00:00
|
|
|
|
require storage.put(peer.peerId, stored, Connectedness.CannotConnect, disconn + 10).isOk
|
2023-02-13 10:43:49 +00:00
|
|
|
|
|
2021-03-26 08:49:51 +00:00
|
|
|
|
responseCount = 0
|
2023-03-09 18:05:50 +00:00
|
|
|
|
proc replacedData(peerId: PeerID, storedInfo: RemotePeerInfo,
|
2021-07-14 17:58:46 +00:00
|
|
|
|
connectedness: Connectedness, disconnectTime: int64) {.raises: [Defect].} =
|
2021-03-26 08:49:51 +00:00
|
|
|
|
responseCount += 1
|
2023-02-13 10:43:49 +00:00
|
|
|
|
|
2021-07-14 17:58:46 +00:00
|
|
|
|
# Note: cannot use `check` within `{.raises: [Defect].}` block
|
|
|
|
|
# @TODO: /Nim/lib/pure/unittest.nim(577, 16) Error: can raise an unlisted exception: Exception
|
|
|
|
|
# These flags are checked outside this block.
|
2023-03-09 18:05:50 +00:00
|
|
|
|
resPeerId = peerId
|
|
|
|
|
resStoredInfo = storedInfo
|
|
|
|
|
resConnectedness = connectedness
|
|
|
|
|
resDisconnect = disconnectTime
|
2021-07-14 17:58:46 +00:00
|
|
|
|
|
2021-03-26 08:49:51 +00:00
|
|
|
|
let repRes = storage.getAll(replacedData)
|
2023-02-13 10:43:49 +00:00
|
|
|
|
|
2021-03-26 08:49:51 +00:00
|
|
|
|
check:
|
|
|
|
|
repRes.isErr == false
|
|
|
|
|
responseCount == 1
|
2023-03-09 18:05:50 +00:00
|
|
|
|
resPeerId == peer.peerId
|
|
|
|
|
resStoredInfo.peerId == peer.peerId
|
|
|
|
|
resStoredInfo.addrs == @[peerLoc]
|
|
|
|
|
resStoredInfo.protocols == @[peerProto]
|
|
|
|
|
resStoredInfo.publicKey == peerKey.getPublicKey().tryGet()
|
|
|
|
|
# TODO: For compatibility, we don't store connectedness and disconnectTime
|
|
|
|
|
#resStoredInfo.connectedness == connectedness
|
|
|
|
|
#resStoredInfo.disconnectTime == disconn
|
|
|
|
|
resConnectedness == Connectedness.CannotConnect
|
|
|
|
|
resDisconnect == disconn + 10
|