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()
|
|
|
|
peer = PeerInfo.init(peerKey, @[peerLoc])
|
|
|
|
peerProto = "/waku/2/default-waku/codec"
|
|
|
|
stored = StoredInfo(peerId: peer.peerId, addrs: toHashSet([peerLoc]), protos: toHashSet([peerProto]), publicKey: peerKey.getKey().tryGet())
|
|
|
|
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
|
|
|
|
proc data(peerId: PeerID, storedInfo: StoredInfo,
|
2021-04-21 09:36:56 +00:00
|
|
|
connectedness: Connectedness, disconnectTime: int64) =
|
2021-03-25 08:37:11 +00:00
|
|
|
responseCount += 1
|
|
|
|
check:
|
|
|
|
peerId == peer.peerId
|
|
|
|
storedInfo == stored
|
|
|
|
connectedness == conn
|
2021-04-21 09:36:56 +00:00
|
|
|
disconnectTime == disconn
|
2021-03-25 08:37:11 +00:00
|
|
|
|
|
|
|
let res = storage.getAll(data)
|
|
|
|
|
|
|
|
check:
|
|
|
|
res.isErr == false
|
|
|
|
responseCount == 1
|
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-04-21 09:36:56 +00:00
|
|
|
connectedness: Connectedness, disconnectTime: int64) =
|
2021-03-26 08:49:51 +00:00
|
|
|
responseCount += 1
|
|
|
|
check:
|
|
|
|
peerId == peer.peerId
|
|
|
|
storedInfo == stored
|
|
|
|
connectedness == CannotConnect
|
2021-04-21 09:36:56 +00:00
|
|
|
disconnectTime == disconn + 10
|
2021-03-26 08:49:51 +00:00
|
|
|
|
|
|
|
let repRes = storage.getAll(replacedData)
|
|
|
|
|
|
|
|
check:
|
|
|
|
repRes.isErr == false
|
|
|
|
responseCount == 1
|