2021-03-25 08:37:11 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2023-11-27 13:08:58 +00:00
|
|
|
std/options,
|
2022-11-04 09:52:08 +00:00
|
|
|
testutils/unittests,
|
2023-11-27 13:08:58 +00:00
|
|
|
eth/p2p/discoveryv5/enr,
|
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-11-27 13:08:58 +00:00
|
|
|
../../waku/waku_enr,
|
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-11-27 13:08:58 +00:00
|
|
|
peerKey = generateSecp256k1Key()
|
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-11-27 13:08:58 +00:00
|
|
|
topics = @["/waku/2/rs/2/0", "/waku/2/rs/2/1"]
|
|
|
|
|
|
|
|
# Create ENR
|
|
|
|
var enrBuilder = EnrBuilder.init(peerKey)
|
|
|
|
enrBuilder.withShardedTopics(topics).expect("Valid topics")
|
|
|
|
let record = enrBuilder.build().expect("Valid record")
|
|
|
|
|
|
|
|
let stored = RemotePeerInfo(
|
2023-03-09 18:05:50 +00:00
|
|
|
peerId: peer.peerId,
|
|
|
|
addrs: @[peerLoc],
|
2023-11-27 13:08:58 +00:00
|
|
|
enr: some(record),
|
2023-03-09 18:05:50 +00:00
|
|
|
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-11-27 13:08:58 +00:00
|
|
|
require storage.put(stored).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-11-27 13:08:58 +00:00
|
|
|
# Fetched variable from callback
|
2023-03-09 18:05:50 +00:00
|
|
|
var resStoredInfo: RemotePeerInfo
|
|
|
|
|
2023-11-27 13:08:58 +00:00
|
|
|
proc data(storedInfo: RemotePeerInfo) =
|
2021-03-25 08:37:11 +00:00
|
|
|
responseCount += 1
|
2023-03-09 18:05:50 +00:00
|
|
|
resStoredInfo = storedInfo
|
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
|
|
|
resStoredInfo.peerId == peer.peerId
|
|
|
|
resStoredInfo.addrs == @[peerLoc]
|
|
|
|
resStoredInfo.protocols == @[peerProto]
|
|
|
|
resStoredInfo.publicKey == peerKey.getPublicKey().tryGet()
|
2023-11-27 13:08:58 +00:00
|
|
|
resStoredInfo.connectedness == connectedness
|
|
|
|
resStoredInfo.disconnectTime == disconn
|
|
|
|
|
|
|
|
assert resStoredInfo.enr.isSome(), "The ENR info wasn't properly stored"
|
|
|
|
check: resStoredInfo.enr.get() == record
|
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-11-27 13:08:58 +00:00
|
|
|
stored.connectedness = CannotConnect
|
|
|
|
stored.disconnectTime = disconn + 10
|
|
|
|
stored.enr = none(Record)
|
|
|
|
require storage.put(stored).isOk
|
2023-02-13 10:43:49 +00:00
|
|
|
|
2021-03-26 08:49:51 +00:00
|
|
|
responseCount = 0
|
2023-11-27 13:08:58 +00:00
|
|
|
proc replacedData(storedInfo: RemotePeerInfo) =
|
2021-03-26 08:49:51 +00:00
|
|
|
responseCount += 1
|
2023-03-09 18:05:50 +00:00
|
|
|
resStoredInfo = storedInfo
|
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
|
|
|
resStoredInfo.peerId == peer.peerId
|
|
|
|
resStoredInfo.addrs == @[peerLoc]
|
|
|
|
resStoredInfo.protocols == @[peerProto]
|
|
|
|
resStoredInfo.publicKey == peerKey.getPublicKey().tryGet()
|
2023-11-27 13:08:58 +00:00
|
|
|
resStoredInfo.connectedness == Connectedness.CannotConnect
|
|
|
|
resStoredInfo.disconnectTime == disconn + 10
|
|
|
|
resStoredInfo.enr.isNone()
|