2021-03-25 08:37:11 +00:00
|
|
|
{.used.}
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
import std/options, testutils/unittests, eth/p2p/discoveryv5/enr, libp2p/crypto/crypto
|
2022-10-27 22:05:02 +00:00
|
|
|
import
|
2024-07-05 22:03:38 +00:00
|
|
|
waku/[
|
|
|
|
common/databases/db_sqlite,
|
|
|
|
node/peer_manager/peer_manager,
|
|
|
|
node/peer_manager/peer_store/waku_peer_storage,
|
|
|
|
waku_enr,
|
|
|
|
],
|
2023-04-05 14:01:51 +00:00
|
|
|
./testlib/wakucore
|
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
|
2024-03-26 18:44:55 +00:00
|
|
|
pubsubTopics = @["/waku/2/rs/2/0", "/waku/2/rs/2/1"]
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2023-11-27 13:08:58 +00:00
|
|
|
# Create ENR
|
|
|
|
var enrBuilder = EnrBuilder.init(peerKey)
|
2024-03-26 18:44:55 +00:00
|
|
|
enrBuilder.withShardedTopics(pubsubTopics).expect("Valid topics")
|
2023-11-27 13:08:58 +00:00
|
|
|
let record = enrBuilder.build().expect("Valid record")
|
|
|
|
|
|
|
|
let stored = RemotePeerInfo(
|
2024-03-15 23:08:47 +00:00
|
|
|
peerId: peer.peerId,
|
|
|
|
addrs: @[peerLoc],
|
|
|
|
enr: some(record),
|
|
|
|
protocols: @[peerProto],
|
|
|
|
publicKey: peerKey.getPublicKey().tryGet(),
|
|
|
|
connectedness: connectedness,
|
|
|
|
disconnectTime: disconn,
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2023-11-27 13:08:58 +00:00
|
|
|
assert resStoredInfo.enr.isSome(), "The ENR info wasn't properly stored"
|
2024-03-15 23:08:47 +00:00
|
|
|
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()
|