mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-05 23:43:07 +00:00
Merge 5ca64d60812157d65aa5eab77b31fd33d781452c into 96196ab8bc05f31b09dac2403f9d5de3bc05f31b
This commit is contained in:
commit
405b45a4cf
@ -7,7 +7,9 @@ import
|
|||||||
chronos,
|
chronos,
|
||||||
chronicles,
|
chronicles,
|
||||||
stint,
|
stint,
|
||||||
libp2p/crypto/crypto
|
libp2p/crypto/crypto,
|
||||||
|
random
|
||||||
|
|
||||||
import
|
import
|
||||||
waku/[
|
waku/[
|
||||||
waku_core,
|
waku_core,
|
||||||
@ -514,3 +516,34 @@ suite "Waku rln relay":
|
|||||||
var testEpochSizes: seq[uint] = @[1, 5, 10, 30, 60, 600]
|
var testEpochSizes: seq[uint] = @[1, 5, 10, 30, 60, 600]
|
||||||
for i in testEpochSizes:
|
for i in testEpochSizes:
|
||||||
await runTestForEpochSizeSec(i)
|
await runTestForEpochSizeSec(i)
|
||||||
|
|
||||||
|
asyncTest "testing rln-relay rate limit testing ":
|
||||||
|
let index = MembershipIndex(7)
|
||||||
|
|
||||||
|
# Generate a random rate limit between 20 and 600
|
||||||
|
randomize()
|
||||||
|
let randomRateLimit = rand(20 .. 600).uint
|
||||||
|
|
||||||
|
let wakuRlnConfig = getWakuRlnConfig(manager = manager, index = index)
|
||||||
|
let wakuRlnRelay = (await WakuRlnRelay.new(wakuRlnConfig)).valueOr:
|
||||||
|
raiseAssert $error
|
||||||
|
|
||||||
|
let manager = cast[OnchainGroupManager](wakuRlnRelay.groupManager)
|
||||||
|
let idCredentials = generateCredentials(manager.rlnInstance)
|
||||||
|
|
||||||
|
# Register the membership with the random rate limit
|
||||||
|
try:
|
||||||
|
waitFor manager.register(idCredentials, UserMessageLimit(randomRateLimit))
|
||||||
|
except Exception, CatchableError:
|
||||||
|
assert false,
|
||||||
|
"exception raised when calling register: " & getCurrentExceptionMsg()
|
||||||
|
|
||||||
|
# Fetch the rate limit for the registered membership
|
||||||
|
let rateLimitRes =
|
||||||
|
await manager.fetchMembershipRateLimit(idCredentials.idCommitment)
|
||||||
|
if rateLimitRes.isErr():
|
||||||
|
raiseAssert "Failed to fetch membership rate limit: " & rateLimitRes.error
|
||||||
|
|
||||||
|
let rateLimit = rateLimitRes.get()
|
||||||
|
check:
|
||||||
|
rateLimit == randomRateLimit.u256 # Check against the random rate limit
|
||||||
|
|||||||
@ -128,6 +128,32 @@ proc fetchMembershipStatus*(
|
|||||||
error "Failed to fetch membership set membership", error = getCurrentExceptionMsg()
|
error "Failed to fetch membership set membership", error = getCurrentExceptionMsg()
|
||||||
return err("Failed to fetch membership set membership: " & getCurrentExceptionMsg())
|
return err("Failed to fetch membership set membership: " & getCurrentExceptionMsg())
|
||||||
|
|
||||||
|
proc fetchMembershipRateLimit*(
|
||||||
|
g: OnchainGroupManager, idCommitment: IDCommitment
|
||||||
|
): Future[Result[UInt256, string]] {.async.} =
|
||||||
|
## Fetch the `rateLimit` of a membership by its idCommitment.
|
||||||
|
## Decodes the first 32-byte word returned by `getMembershipInfo`.
|
||||||
|
try:
|
||||||
|
let params = idCommitment.reversed()
|
||||||
|
let resultBytes = await sendEthCallWithParams(
|
||||||
|
ethRpc = g.ethRpc.get(),
|
||||||
|
functionSignature = "getMembershipInfo(uint256)",
|
||||||
|
params = params,
|
||||||
|
fromAddress = g.ethRpc.get().defaultAccount,
|
||||||
|
toAddress = fromHex(Address, g.ethContractAddress),
|
||||||
|
chainId = g.chainId,
|
||||||
|
)
|
||||||
|
if resultBytes.isErr():
|
||||||
|
return err(resultBytes.error)
|
||||||
|
let bytes = resultBytes.get()
|
||||||
|
if bytes.len < 32:
|
||||||
|
return err("unexpected ABI response length " & $bytes.len)
|
||||||
|
let rateLimit = UInt256.fromBytesBE(bytes[0 .. 31])
|
||||||
|
return ok(rateLimit)
|
||||||
|
except CatchableError:
|
||||||
|
error "Failed to fetch membership rate limit", error = getCurrentExceptionMsg()
|
||||||
|
return err("Failed to fetch membership rate limit: " & getCurrentExceptionMsg())
|
||||||
|
|
||||||
proc fetchMaxMembershipRateLimit*(
|
proc fetchMaxMembershipRateLimit*(
|
||||||
g: OnchainGroupManager
|
g: OnchainGroupManager
|
||||||
): Future[Result[UInt256, string]] {.async.} =
|
): Future[Result[UInt256, string]] {.async.} =
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user