fix: handle rln-relay-message-limit (#2867)

* fix: enforcing rln-contract max message limit and resolve early
This commit is contained in:
Darshan K 2024-07-22 22:28:45 +05:30 committed by GitHub
parent 6d385cefef
commit 8d107b0ded
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 32 additions and 2 deletions

View File

@ -15,6 +15,9 @@ import
eth/keys eth/keys
import import
waku/[ waku/[
waku_node,
node/waku_node,
waku_rln_relay,
waku_rln_relay/protocol_types, waku_rln_relay/protocol_types,
waku_rln_relay/constants, waku_rln_relay/constants,
waku_rln_relay/contract, waku_rln_relay/contract,
@ -22,7 +25,7 @@ import
waku_rln_relay/conversion_utils, waku_rln_relay/conversion_utils,
waku_rln_relay/group_manager/on_chain/group_manager, waku_rln_relay/group_manager/on_chain/group_manager,
], ],
../testlib/common, ../testlib/[wakucore, wakunode, common],
./utils ./utils
const CHAIN_ID = 1337 const CHAIN_ID = 1337
@ -232,6 +235,7 @@ suite "Onchain group manager":
manager.wakuRlnContract.isSome() manager.wakuRlnContract.isSome()
manager.initialized manager.initialized
manager.rlnContractDeployedBlockNumber > 0 manager.rlnContractDeployedBlockNumber > 0
manager.rlnRelayMaxMessageLimit == 100
await manager.stop() await manager.stop()
@ -776,6 +780,26 @@ suite "Onchain group manager":
await manager.stop() await manager.stop()
asyncTest "rln-relay-max-message-limit testing":
let
nodekey = generateSecp256k1Key()
node = newTestWakuNode(nodekey, parseIpAddress("0.0.0.0"), Port(0))
await node.mountRelay(@[DefaultPubsubTopic])
let wakuRlnConfig = WakuRlnConfig(
rlnRelayDynamic: false,
rlnRelayCredIndex: some(0.uint),
rlnRelayUserMessageLimit: 111,
rlnRelayTreepath: genTempPath("rln_tree", "wakunode_0"),
)
try:
await node.mountRlnRelay(wakuRlnConfig)
except CatchableError as e:
check e.msg == "failed to mount WakuRlnRelay: rln-relay-user-message-limit can't be exceed then MAX_MESSAGE_LIMIT set by rln contract"
################################ ################################
## Terminating/removing Anvil ## Terminating/removing Anvil
################################ ################################

View File

@ -1089,6 +1089,8 @@ proc mountRlnRelay*(
raise raise
newException(CatchableError, "failed to mount WakuRlnRelay: " & rlnRelayRes.error) newException(CatchableError, "failed to mount WakuRlnRelay: " & rlnRelayRes.error)
let rlnRelay = rlnRelayRes.get() let rlnRelay = rlnRelayRes.get()
if (rlnConf.rlnRelayUserMessageLimit > rlnRelay.groupManager.rlnRelayMaxMessageLimit):
error "rln-relay-user-message-limit can't be exceed then MAX_MESSAGE_LIMIT set by rln contract"
let validator = generateRlnValidator(rlnRelay, spamHandler) let validator = generateRlnValidator(rlnRelay, spamHandler)
# register rln validator as default validator # register rln validator as default validator

File diff suppressed because one or more lines are too long

View File

@ -33,6 +33,7 @@ type GroupManager* = ref object of RootObj
validRoots*: Deque[MerkleNode] validRoots*: Deque[MerkleNode]
onFatalErrorAction*: OnFatalErrorHandler onFatalErrorAction*: OnFatalErrorHandler
userMessageLimit*: Option[UserMessageLimit] userMessageLimit*: Option[UserMessageLimit]
rlnRelayMaxMessageLimit*: uint64
# This proc is used to initialize the group manager # This proc is used to initialize the group manager
# Any initialization logic should be implemented here # Any initialization logic should be implemented here

View File

@ -46,6 +46,8 @@ contract(WakuRlnContract):
proc commitmentIndex(): UInt256 {.view.} proc commitmentIndex(): UInt256 {.view.}
# this constant describes the block number this contract was deployed on # this constant describes the block number this contract was deployed on
proc deployedBlockNumber(): UInt256 {.view.} proc deployedBlockNumber(): UInt256 {.view.}
# this constant describes max message limit of rln contract
proc MAX_MESSAGE_LIMIT(): UInt256 {.view.}
type type
WakuRlnContractWithSender = Sender[WakuRlnContract] WakuRlnContractWithSender = Sender[WakuRlnContract]
@ -617,6 +619,7 @@ method init*(g: OnchainGroupManager): Future[GroupManagerResult[void]] {.async.}
debug "using rln contract", deployedBlockNumber, rlnContractAddress = contractAddress debug "using rln contract", deployedBlockNumber, rlnContractAddress = contractAddress
g.rlnContractDeployedBlockNumber = cast[BlockNumber](deployedBlockNumber) g.rlnContractDeployedBlockNumber = cast[BlockNumber](deployedBlockNumber)
g.latestProcessedBlock = max(g.latestProcessedBlock, g.rlnContractDeployedBlockNumber) g.latestProcessedBlock = max(g.latestProcessedBlock, g.rlnContractDeployedBlockNumber)
g.rlnRelayMaxMessageLimit = cast[uint64](await wakuRlnContract.MAX_MESSAGE_LIMIT().call())
proc onDisconnect() {.async.} = proc onDisconnect() {.async.} =
error "Ethereum client disconnected" error "Ethereum client disconnected"