2021-03-25 08:37:11 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[unittest, sets],
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
../test_helpers,
|
2021-03-26 08:49:51 +00:00
|
|
|
../../waku/v2/node/peer_manager/peer_manager,
|
2021-03-25 08:37:11 +00:00
|
|
|
../../waku/v2/node/storage/peer/waku_peer_storage
|
|
|
|
|
|
|
|
suite "Peer Storage":
|
|
|
|
|
2021-03-26 08:49:51 +00:00
|
|
|
test "Store, replace and retrieve from persistent peer storage":
|
2021-03-25 08:37:11 +00:00
|
|
|
let
|
|
|
|
database = SqliteDatabase.init("", inMemory = true)[]
|
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()
|
|
|
|
peerKey = crypto.PrivateKey.random(ECDSA, rng[]).get()
|
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"
|
2021-11-04 14:46:38 +00:00
|
|
|
stored = StoredInfo(peerId: peer.peerId, addrs: toHashSet([peerLoc]), protos: toHashSet([peerProto]), publicKey: peerKey.getPublicKey().tryGet())
|
2021-03-25 08:37:11 +00:00
|
|
|
conn = Connectedness.CanConnect
|
2021-04-21 09:36:56 +00:00
|
|
|
disconn = 999999
|
2021-03-25 08:37:11 +00:00
|
|
|
|
|
|
|
defer: storage.close()
|
|
|
|
|
2021-03-26 08:49:51 +00:00
|
|
|
# Test insert and retrieve
|
|
|
|
|
2021-04-21 09:36:56 +00:00
|
|
|
discard storage.put(peer.peerId, stored, conn, disconn)
|
2021-03-25 08:37:11 +00:00
|
|
|
|
|
|
|
var responseCount = 0
|
2021-07-14 17:58:46 +00:00
|
|
|
# flags to check data matches what was stored (default true)
|
|
|
|
var peerIdFlag, storedInfoFlag, connectednessFlag, disconnectFlag: bool
|
|
|
|
|
2021-03-25 08:37:11 +00:00
|
|
|
proc data(peerId: PeerID, storedInfo: StoredInfo,
|
2021-07-14 17:58:46 +00:00
|
|
|
connectedness: Connectedness, disconnectTime: int64) {.raises: [Defect].} =
|
2021-03-25 08:37:11 +00:00
|
|
|
responseCount += 1
|
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.
|
|
|
|
peerIdFlag = peerId == peer.peerId
|
|
|
|
storedInfoFlag = storedInfo == stored
|
|
|
|
connectednessFlag = connectedness == conn
|
|
|
|
disconnectFlag = disconnectTime == disconn
|
2021-03-25 08:37:11 +00:00
|
|
|
|
|
|
|
let res = storage.getAll(data)
|
|
|
|
|
|
|
|
check:
|
|
|
|
res.isErr == false
|
|
|
|
responseCount == 1
|
2021-07-14 17:58:46 +00:00
|
|
|
peerIdFlag
|
|
|
|
storedInfoFlag
|
|
|
|
connectednessFlag
|
|
|
|
disconnectFlag
|
2021-03-26 08:49:51 +00:00
|
|
|
|
|
|
|
# Test replace and retrieve (update an existing entry)
|
2021-04-21 09:36:56 +00:00
|
|
|
discard storage.put(peer.peerId, stored, Connectedness.CannotConnect, disconn + 10)
|
2021-03-26 08:49:51 +00:00
|
|
|
|
|
|
|
responseCount = 0
|
|
|
|
proc replacedData(peerId: PeerID, storedInfo: StoredInfo,
|
2021-07-14 17:58:46 +00:00
|
|
|
connectedness: Connectedness, disconnectTime: int64) {.raises: [Defect].} =
|
2021-03-26 08:49:51 +00:00
|
|
|
responseCount += 1
|
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.
|
|
|
|
peerIdFlag = peerId == peer.peerId
|
|
|
|
storedInfoFlag = storedInfo == stored
|
|
|
|
connectednessFlag = connectedness == CannotConnect
|
|
|
|
disconnectFlag = disconnectTime == disconn + 10
|
|
|
|
|
2021-03-26 08:49:51 +00:00
|
|
|
let repRes = storage.getAll(replacedData)
|
|
|
|
|
|
|
|
check:
|
|
|
|
repRes.isErr == false
|
|
|
|
responseCount == 1
|
2021-07-14 17:58:46 +00:00
|
|
|
peerIdFlag
|
|
|
|
storedInfoFlag
|
|
|
|
connectednessFlag
|
|
|
|
disconnectFlag
|