mirror of
https://github.com/vacp2p/nim-libp2p-experimental.git
synced 2025-01-19 14:51:23 +00:00
3b718baa97
* feat: allow msgIdProvider to fail Closes: #642. Changes the return type of the msgIdProvider to `Result[MessageID, string]` so that message id generation can fail. String error type was chosen as this `msgIdProvider` mainly because the failed message id generation drops the message and logs the error provided. Because `msgIdProvider` can be externally provided by library consumers, an enum didn’t make sense and a object seemed to be overkill. Exceptions could have been used as well, however, in this case, Result ergonomics were warranted and prevented wrapping quite a large block of code in try/except. The `defaultMsgIdProvider` function previously allowed message id generation to fail silently for use in the tests: when seqno or source peerid were not valid, the message id generated was based on a hash of the message data and topic ids. The silent failing was moved to the `defaultMsgIdProvider` used only in the tests so that it could not fail silently in applications. Unit tests were added for the `defaultMsgIdProvider`. * Change MsgIdProvider error type to ValidationResult
103 lines
3.2 KiB
Nim
103 lines
3.2 KiB
Nim
{.used.}
|
|
|
|
import unittest2, options, sets, sequtils, bearssl
|
|
import stew/byteutils
|
|
import ../../libp2p/[peerid,
|
|
crypto/crypto,
|
|
protocols/pubsub/mcache,
|
|
protocols/pubsub/rpc/messages]
|
|
import ./utils
|
|
|
|
var rng = newRng()
|
|
|
|
proc randomPeerId(): PeerId =
|
|
PeerId.init(PrivateKey.random(ECDSA, rng[]).get()).get()
|
|
|
|
const MsgIdGenFail = "msg id gen failure"
|
|
|
|
suite "MCache":
|
|
test "put/get":
|
|
var mCache = MCache.init(3, 5)
|
|
var msg = Message(fromPeer: randomPeerId(), seqno: "12345".toBytes())
|
|
let msgId = defaultMsgIdProvider(msg).expect(MsgIdGenFail)
|
|
mCache.put(msgId, msg)
|
|
check mCache.get(msgId).isSome and mCache.get(msgId).get() == msg
|
|
|
|
test "window":
|
|
var mCache = MCache.init(3, 5)
|
|
|
|
for i in 0..<3:
|
|
var msg = Message(fromPeer: randomPeerId(),
|
|
seqno: "12345".toBytes(),
|
|
topicIDs: @["foo"])
|
|
mCache.put(defaultMsgIdProvider(msg).expect(MsgIdGenFail), msg)
|
|
|
|
for i in 0..<5:
|
|
var msg = Message(fromPeer: randomPeerId(),
|
|
seqno: "12345".toBytes(),
|
|
topicIDs: @["bar"])
|
|
mCache.put(defaultMsgIdProvider(msg).expect(MsgIdGenFail), msg)
|
|
|
|
var mids = mCache.window("foo")
|
|
check mids.len == 3
|
|
|
|
var id = toSeq(mids)[0]
|
|
check mCache.get(id).get().topicIDs[0] == "foo"
|
|
|
|
test "shift - shift 1 window at a time":
|
|
var mCache = MCache.init(1, 5)
|
|
|
|
for i in 0..<3:
|
|
var msg = Message(fromPeer: randomPeerId(),
|
|
seqno: "12345".toBytes(),
|
|
topicIDs: @["foo"])
|
|
mCache.put(defaultMsgIdProvider(msg).expect(MsgIdGenFail), msg)
|
|
|
|
mCache.shift()
|
|
check mCache.window("foo").len == 0
|
|
|
|
for i in 0..<3:
|
|
var msg = Message(fromPeer: randomPeerId(),
|
|
seqno: "12345".toBytes(),
|
|
topicIDs: @["bar"])
|
|
mCache.put(defaultMsgIdProvider(msg).expect(MsgIdGenFail), msg)
|
|
|
|
mCache.shift()
|
|
check mCache.window("bar").len == 0
|
|
|
|
for i in 0..<3:
|
|
var msg = Message(fromPeer: randomPeerId(),
|
|
seqno: "12345".toBytes(),
|
|
topicIDs: @["baz"])
|
|
mCache.put(defaultMsgIdProvider(msg).expect(MsgIdGenFail), msg)
|
|
|
|
mCache.shift()
|
|
check mCache.window("baz").len == 0
|
|
|
|
test "shift - 2 windows at a time":
|
|
var mCache = MCache.init(1, 5)
|
|
|
|
for i in 0..<3:
|
|
var msg = Message(fromPeer: randomPeerId(),
|
|
seqno: "12345".toBytes(),
|
|
topicIDs: @["foo"])
|
|
mCache.put(defaultMsgIdProvider(msg).expect(MsgIdGenFail), msg)
|
|
|
|
for i in 0..<3:
|
|
var msg = Message(fromPeer: randomPeerId(),
|
|
seqno: "12345".toBytes(),
|
|
topicIDs: @["bar"])
|
|
mCache.put(defaultMsgIdProvider(msg).expect(MsgIdGenFail), msg)
|
|
|
|
for i in 0..<3:
|
|
var msg = Message(fromPeer: randomPeerId(),
|
|
seqno: "12345".toBytes(),
|
|
topicIDs: @["baz"])
|
|
mCache.put(defaultMsgIdProvider(msg).expect(MsgIdGenFail), msg)
|
|
|
|
mCache.shift()
|
|
check mCache.window("foo").len == 0
|
|
|
|
mCache.shift()
|
|
check mCache.window("bar").len == 0
|