From 98e3378900f280fc95e3be45d180ac72f9d21e99 Mon Sep 17 00:00:00 2001 From: Marcin Czenko Date: Tue, 22 Oct 2024 16:03:55 +0200 Subject: [PATCH] allows to choose a different RPC provider for a given integration test suite --- tests/integration/marketplacesuite.nim | 12 ++++++++-- tests/integration/multinodes.nim | 33 ++++++++++++++++++++++---- tests/integration/testvalidator.nim | 15 +++++++----- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/tests/integration/marketplacesuite.nim b/tests/integration/marketplacesuite.nim index 2b81bdd8..7e7ea240 100644 --- a/tests/integration/marketplacesuite.nim +++ b/tests/integration/marketplacesuite.nim @@ -11,9 +11,17 @@ import ../contracts/deployment export mp export multinodes -template marketplacesuite*(name: string, body: untyped) = +template marketplacesuite*(name: string, + body: untyped) = + marketplacesuiteWithProviderUrl name, "http://localhost:8545": + body - multinodesuite name: +# we can't just overload the name and use marketplacesuite here +# see: https://github.com/nim-lang/Nim/issues/14827 +template marketplacesuiteWithProviderUrl*(name: string, + jsonRpcProviderUrl: string, body: untyped) = + + multinodesuiteWithProviderUrl name, jsonRpcProviderUrl: var marketplace {.inject, used.}: Marketplace var period: uint64 diff --git a/tests/integration/multinodes.nim b/tests/integration/multinodes.nim index 14b48019..e907c43f 100644 --- a/tests/integration/multinodes.nim +++ b/tests/integration/multinodes.nim @@ -58,7 +58,35 @@ proc nextFreePort(startPort: int): Future[int] {.async.} = trace "port is not free", port inc port +# Following the problem described here: +# https://github.com/NomicFoundation/hardhat/issues/2053 +# It may be desireable to use http RPC provider. +# This turns out to be equally important in tests where +# subscriptions get wiped out after 5mins even when +# a new block is mined. +# For this reason, we are using http provider here as the default. +# To use a different provider in your test, you may use +# multinodesuiteWithProviderUrl template in your tests. +# The nodes are still using the default provider (which is ws://localhost:8545). +# If you want to use http provider url in the nodes, you can +# use withEthProvider config modifiers in the node configs +# to set the desired provider url. E.g.: +# NodeConfigs( +# hardhat: +# HardhatConfig.none, +# clients: +# CodexConfigs.init(nodes=1) +# .withEthProvider("http://localhost:8545") +# .some, +# ... template multinodesuite*(name: string, body: untyped) = + multinodesuiteWithProviderUrl name, "http://localhost:8545": + body + +# we can't just overload the name and use multinodesuite here +# see: https://github.com/nim-lang/Nim/issues/14827 +template multinodesuiteWithProviderUrl*(name: string, jsonRpcProviderUrl: string, + body: untyped) = asyncchecksuite name: @@ -261,10 +289,7 @@ template multinodesuite*(name: string, body: untyped) = quit(1) try: - # Workaround for https://github.com/NomicFoundation/hardhat/issues/2053 - # Do not use websockets, but use http and polling to stop subscriptions - # from being removed after 5 minutes - ethProvider = JsonRpcProvider.new("http://localhost:8545") + ethProvider = JsonRpcProvider.new(jsonRpcProviderUrl) # if hardhat was NOT started by the test, take a snapshot so it can be # reverted in the test teardown if nodeConfigs.hardhat.isNone: diff --git a/tests/integration/testvalidator.nim b/tests/integration/testvalidator.nim index a7f59375..6d326880 100644 --- a/tests/integration/testvalidator.nim +++ b/tests/integration/testvalidator.nim @@ -15,16 +15,21 @@ export logutils logScope: topics = "integration test validation" -template eventuallyS*(expression: untyped, timeout=10, step = 5, +template eventuallyS(expression: untyped, timeout=10, step = 5, cancelExpression: untyped = false): bool = bind Moment, now, seconds proc eventuallyS: Future[bool] {.async.} = let endTime = Moment.now() + timeout.seconds var i = 0 + var secondsElapsed = 0 while not expression: inc i - # echo (i*step).seconds + secondsElapsed = i*step + # echo secondsElapsed.seconds + if secondsElapsed mod 180 == 0: + await stopTrackingEvents() + await marketplace.startTrackingEvents() if endTime < Moment.now(): return false if cancelExpression: @@ -34,13 +39,11 @@ template eventuallyS*(expression: untyped, timeout=10, step = 5, await eventuallyS() -marketplacesuite "Validation": +marketplacesuiteWithProviderUrl "Validation", "ws://localhost:8545": let nodes = 3 let tolerance = 1 let proofProbability = 1 - # var slotsAndRequests = initTable[string, seq[UInt256]]() - # var events = initTable[string, seq[ref MarketplaceEvent]]() var events = { $SlotFilled: newSeq[ref MarketplaceEvent](), $SlotFreed: newSeq[ref MarketplaceEvent](), @@ -107,7 +110,7 @@ marketplacesuite "Validation": slotsFailed.incl(slotId) debug "slots failed", slotsFailed = slotsFailed, slotsNotFreed = slotsNotFreed - check slotsNotFreed == slotsFailed + check slotsNotFreed == slotsFailed test "validator marks proofs as missing when using validation groups", NodeConfigs( # Uncomment to start Hardhat automatically, typically so logs can be inspected locally