Rename provider to ethProvider in tests (#657)

This commit is contained in:
Eric 2023-12-18 09:34:04 +00:00 committed by GitHub
parent f1b1fe9152
commit 54c6258e8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 78 additions and 78 deletions

View File

@ -8,21 +8,21 @@ ethersuite "On-Chain Clock":
var clock: OnChainClock
setup:
clock = OnChainClock.new(provider)
clock = OnChainClock.new(ethProvider)
await clock.start()
teardown:
await clock.stop()
test "returns the current time of the EVM":
let latestBlock = (!await provider.getBlock(BlockTag.latest))
let latestBlock = (!await ethProvider.getBlock(BlockTag.latest))
let timestamp = latestBlock.timestamp.truncate(int64)
check clock.now() == timestamp
test "updates time with timestamp of new blocks":
let future = (getTime() + 42.years).toUnix
discard await provider.send("evm_setNextBlockTimestamp", @[%future])
discard await provider.send("evm_mine")
discard await ethProvider.send("evm_setNextBlockTimestamp", @[%future])
discard await ethProvider.send("evm_mine")
check clock.now() == future
test "updates time using wall-clock in-between blocks":
@ -33,8 +33,8 @@ ethersuite "On-Chain Clock":
test "can wait until a certain time is reached by the chain":
let future = clock.now() + 42 # seconds
let waiting = clock.waitUntil(future)
discard await provider.send("evm_setNextBlockTimestamp", @[%future])
discard await provider.send("evm_mine")
discard await ethProvider.send("evm_setNextBlockTimestamp", @[%future])
discard await ethProvider.send("evm_mine")
check await waiting.withTimeout(chronos.milliseconds(100))
test "can wait until a certain time is reached by the wall-clock":
@ -44,7 +44,7 @@ ethersuite "On-Chain Clock":
test "raises when not started":
expect AssertionDefect:
discard OnChainClock.new(provider).now()
discard OnChainClock.new(ethProvider).now()
test "raises when stopped":
await clock.stop()

View File

@ -23,13 +23,13 @@ ethersuite "Marketplace contracts":
token = token.connect(account)
setup:
client = provider.getSigner(accounts[0])
host = provider.getSigner(accounts[1])
client = ethProvider.getSigner(accounts[0])
host = ethProvider.getSigner(accounts[1])
marketplace = Marketplace.new(Marketplace.address, provider.getSigner())
marketplace = Marketplace.new(Marketplace.address, ethProvider.getSigner())
let tokenAddress = await marketplace.token()
token = Erc20Token.new(tokenAddress, provider.getSigner())
token = Erc20Token.new(tokenAddress, ethProvider.getSigner())
let config = await marketplace.config()
periodicity = Periodicity(seconds: config.proofs.period)
@ -46,13 +46,13 @@ ethersuite "Marketplace contracts":
slotId = request.slotId(0.u256)
proc waitUntilProofRequired(slotId: SlotId) {.async.} =
let currentPeriod = periodicity.periodOf(await provider.currentTime())
await provider.advanceTimeTo(periodicity.periodEnd(currentPeriod))
let currentPeriod = periodicity.periodOf(await ethProvider.currentTime())
await ethProvider.advanceTimeTo(periodicity.periodEnd(currentPeriod))
while not (
(await marketplace.isProofRequired(slotId)) and
(await marketplace.getPointer(slotId)) < 250
):
await provider.advanceTime(periodicity.seconds)
await ethProvider.advanceTime(periodicity.seconds)
proc startContract() {.async.} =
for slotIndex in 1..<request.ask.slots:
@ -67,9 +67,9 @@ ethersuite "Marketplace contracts":
test "can mark missing proofs":
switchAccount(host)
await waitUntilProofRequired(slotId)
let missingPeriod = periodicity.periodOf(await provider.currentTime())
let missingPeriod = periodicity.periodOf(await ethProvider.currentTime())
let endOfPeriod = periodicity.periodEnd(missingPeriod)
await provider.advanceTimeTo(endOfPeriod + 1)
await ethProvider.advanceTimeTo(endOfPeriod + 1)
switchAccount(client)
await marketplace.markProofAsMissing(slotId, missingPeriod)
@ -78,17 +78,17 @@ ethersuite "Marketplace contracts":
let address = await host.getAddress()
await startContract()
let requestEnd = await marketplace.requestEnd(request.id)
await provider.advanceTimeTo(requestEnd.u256 + 1)
await ethProvider.advanceTimeTo(requestEnd.u256 + 1)
let startBalance = await token.balanceOf(address)
await marketplace.freeSlot(slotId)
let endBalance = await token.balanceOf(address)
check endBalance == (startBalance + request.ask.duration * request.ask.reward + request.ask.collateral)
test "cannot mark proofs missing for cancelled request":
await provider.advanceTimeTo(request.expiry + 1)
await ethProvider.advanceTimeTo(request.expiry + 1)
switchAccount(client)
let missingPeriod = periodicity.periodOf(await provider.currentTime())
await provider.advanceTime(periodicity.seconds)
let missingPeriod = periodicity.periodOf(await ethProvider.currentTime())
await ethProvider.advanceTime(periodicity.seconds)
check await marketplace
.markProofAsMissing(slotId, missingPeriod)
.reverts("Slot not accepting proofs")

View File

@ -17,7 +17,7 @@ ethersuite "On-Chain Market":
var periodicity: Periodicity
setup:
marketplace = Marketplace.new(Marketplace.address, provider.getSigner())
marketplace = Marketplace.new(Marketplace.address, ethProvider.getSigner())
let config = await marketplace.config()
market = OnChainMarket.new(marketplace)
@ -29,8 +29,8 @@ ethersuite "On-Chain Market":
slotIndex = (request.ask.slots div 2).u256
proc advanceToNextPeriod() {.async.} =
let currentPeriod = periodicity.periodOf(await provider.currentTime())
await provider.advanceTimeTo(periodicity.periodEnd(currentPeriod) + 1)
let currentPeriod = periodicity.periodOf(await ethProvider.currentTime())
await ethProvider.advanceTimeTo(periodicity.periodEnd(currentPeriod) + 1)
proc waitUntilProofRequired(slotId: SlotId) {.async.} =
await advanceToNextPeriod()
@ -41,12 +41,12 @@ ethersuite "On-Chain Market":
await advanceToNextPeriod()
test "fails to instantiate when contract does not have a signer":
let storageWithoutSigner = marketplace.connect(provider)
let storageWithoutSigner = marketplace.connect(ethProvider)
expect AssertionDefect:
discard OnChainMarket.new(storageWithoutSigner)
test "knows signer address":
check (await market.getSigner()) == (await provider.getSigner().getAddress())
check (await market.getSigner()) == (await ethProvider.getSigner().getAddress())
test "can retrieve proof periodicity":
let periodicity = await market.periodicity()
@ -70,7 +70,7 @@ ethersuite "On-Chain Market":
test "supports withdrawing of funds":
await market.requestStorage(request)
await provider.advanceTimeTo(request.expiry + 1)
await ethProvider.advanceTimeTo(request.expiry + 1)
await market.withdrawFunds(request.id)
test "supports request subscriptions":
@ -118,7 +118,7 @@ ethersuite "On-Chain Market":
await market.requestStorage(request)
await market.fillSlot(request.id, slotIndex, proof, request.ask.collateral)
await waitUntilProofRequired(slotId)
let missingPeriod = periodicity.periodOf(await provider.currentTime())
let missingPeriod = periodicity.periodOf(await ethProvider.currentTime())
await advanceToNextPeriod()
await market.markProofAsMissing(slotId, missingPeriod)
check (await marketplace.missingProofs(slotId)) == 1
@ -128,7 +128,7 @@ ethersuite "On-Chain Market":
await market.requestStorage(request)
await market.fillSlot(request.id, slotIndex, proof, request.ask.collateral)
await waitUntilProofRequired(slotId)
let missingPeriod = periodicity.periodOf(await provider.currentTime())
let missingPeriod = periodicity.periodOf(await ethProvider.currentTime())
await advanceToNextPeriod()
check (await market.canProofBeMarkedAsMissing(slotId, missingPeriod)) == true
@ -213,7 +213,7 @@ ethersuite "On-Chain Market":
receivedIds.add(id)
let subscription = await market.subscribeRequestCancelled(request.id, onRequestCancelled)
await provider.advanceTimeTo(request.expiry + 1)
await ethProvider.advanceTimeTo(request.expiry + 1)
await market.withdrawFunds(request.id)
check receivedIds == @[request.id]
await subscription.unsubscribe()
@ -235,7 +235,7 @@ ethersuite "On-Chain Market":
if slotState == SlotState.Free:
break
await waitUntilProofRequired(slotId)
let missingPeriod = periodicity.periodOf(await provider.currentTime())
let missingPeriod = periodicity.periodOf(await ethProvider.currentTime())
await advanceToNextPeriod()
await marketplace.markProofAsMissing(slotId, missingPeriod)
check receivedIds == @[request.id]
@ -252,7 +252,7 @@ ethersuite "On-Chain Market":
receivedIds.add(requestId)
let subscription = await market.subscribeRequestCancelled(request.id, onRequestCancelled)
await provider.advanceTimeTo(request.expiry + 1) # shares expiry with otherRequest
await ethProvider.advanceTimeTo(request.expiry + 1) # shares expiry with otherRequest
await market.withdrawFunds(otherRequest.id)
check receivedIds.len == 0
await market.withdrawFunds(request.id)

View File

@ -5,23 +5,23 @@ import pkg/ethers
import ./checktest
## Unit testing suite that sets up an Ethereum testing environment.
## Injects a `provider` instance, and a list of `accounts`.
## Injects a `ethProvider` instance, and a list of `accounts`.
## Calls the `evm_snapshot` and `evm_revert` methods to ensure that any
## changes to the blockchain do not persist.
template ethersuite*(name, body) =
asyncchecksuite name:
var provider {.inject, used.}: JsonRpcProvider
var ethProvider {.inject, used.}: JsonRpcProvider
var accounts {.inject, used.}: seq[Address]
var snapshot: JsonNode
setup:
provider = JsonRpcProvider.new("ws://localhost:8545")
snapshot = await send(provider, "evm_snapshot")
accounts = await provider.listAccounts()
ethProvider = JsonRpcProvider.new("ws://localhost:8545")
snapshot = await send(ethProvider, "evm_snapshot")
accounts = await ethProvider.listAccounts()
teardown:
discard await send(provider, "evm_revert", @[snapshot])
discard await send(ethProvider, "evm_revert", @[snapshot])
body

View File

@ -24,7 +24,7 @@ type
validators*: uint
DebugNodes* = object
client*: bool
provider*: bool
ethProvider*: bool
validator*: bool
topics*: string
Role* {.pure.} = enum
@ -49,15 +49,15 @@ proc init*(_: type StartNodes,
StartNodes(clients: clients, providers: providers, validators: validators)
proc init*(_: type DebugNodes,
client, provider, validator: bool,
client, ethProvider, validator: bool,
topics: string = "validator,proving,market"): DebugNodes =
DebugNodes(client: client, provider: provider, validator: validator,
DebugNodes(client: client, ethProvider: ethProvider, validator: validator,
topics: topics)
template multinodesuite*(name: string,
startNodes: StartNodes, debugNodes: DebugNodes, body: untyped) =
if (debugNodes.client or debugNodes.provider) and
if (debugNodes.client or debugNodes.ethProvider) and
(enabledLogLevel > LogLevel.TRACE or
enabledLogLevel == LogLevel.NONE):
echo ""
@ -116,12 +116,12 @@ template multinodesuite*(name: string,
"--bootstrap-node=" & bootstrap,
"--persistence",
"--simulate-proof-failures=" & $failEveryNProofs],
debugNodes.provider)
debugNodes.ethProvider)
let restClient = newCodexClient(index)
running.add RunningNode.new(Role.Provider, node, restClient, datadir,
account)
if debugNodes.provider:
debug "started new provider node and codex client",
if debugNodes.ethProvider:
debug "started new ethProvider node and codex client",
restApiPort = 8080 + index, discPort = 8090 + index, account
proc startValidatorNode() =

View File

@ -28,10 +28,10 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false:
return purchase.state == state
setup:
# Our Hardhat configuration does use automine, which means that time tracked by `provider.currentTime()` is not
# Our Hardhat configuration does use automine, which means that time tracked by `ethProvider.currentTime()` is not
# advanced until blocks are mined and that happens only when transaction is submitted.
# As we use in tests provider.currentTime() which uses block timestamp this can lead to synchronization issues.
await provider.advanceTime(1.u256)
# As we use in tests ethProvider.currentTime() which uses block timestamp this can lead to synchronization issues.
await ethProvider.advanceTime(1.u256)
test "nodes can print their peer information":
check client1.info() != client2.info()
@ -111,7 +111,7 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false:
check availability in client1.getAvailabilities().get
test "node handles storage request":
let expiry = (await provider.currentTime()) + 10
let expiry = (await ethProvider.currentTime()) + 10
let cid = client1.upload("some file contents").get
let id1 = client1.requestStorage(cid, duration=100.u256, reward=2.u256, proofProbability=3.u256, expiry=expiry, collateral=200.u256).get
let id2 = client1.requestStorage(cid, duration=400.u256, reward=5.u256, proofProbability=6.u256, expiry=expiry, collateral=201.u256).get
@ -123,7 +123,7 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false:
let chunker = RandomChunker.new(rng, size = DefaultBlockSize * 2, chunkSize = DefaultBlockSize * 2)
let data = await chunker.getBytes()
let cid = client1.upload(byteutils.toHex(data)).get
let expiry = (await provider.currentTime()) + 30
let expiry = (await ethProvider.currentTime()) + 30
let id = client1.requestStorage(
cid,
duration=100.u256,
@ -145,7 +145,7 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false:
# TODO: We currently do not support encoding single chunks
# test "node retrieves purchase status with 1 chunk":
# let expiry = (await provider.currentTime()) + 30
# let expiry = (await ethProvider.currentTime()) + 30
# let cid = client1.upload("some file contents").get
# let id = client1.requestStorage(cid, duration=1.u256, reward=2.u256, proofProbability=3.u256, expiry=expiry, collateral=200.u256, nodes=2, tolerance=1).get
# let request = client1.getPurchase(id).get.request.get
@ -158,7 +158,7 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false:
# check request.ask.maxSlotLoss == 1'u64
test "node remembers purchase status after restart":
let expiry = (await provider.currentTime()) + 30
let expiry = (await ethProvider.currentTime()) + 30
let cid = client1.upload("some file contents").get
let id = client1.requestStorage(cid,
duration=100.u256,
@ -188,7 +188,7 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false:
discard client2.postAvailability(size=size, duration=200.u256, minPrice=300.u256, maxCollateral=300.u256)
# client 1 requests storage
let expiry = (await provider.currentTime()) + 30
let expiry = (await ethProvider.currentTime()) + 30
let cid = client1.upload("some file contents").get
let id = client1.requestStorage(cid, duration=100.u256, reward=400.u256, proofProbability=3.u256, expiry=expiry, collateral=200.u256).get
@ -201,9 +201,9 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false:
check newSize > 0 and newSize < size
test "node slots gets paid out":
let marketplace = Marketplace.new(Marketplace.address, provider.getSigner())
let marketplace = Marketplace.new(Marketplace.address, ethProvider.getSigner())
let tokenAddress = await marketplace.token()
let token = Erc20Token.new(tokenAddress, provider.getSigner())
let token = Erc20Token.new(tokenAddress, ethProvider.getSigner())
let reward = 400.u256
let duration = 100.u256
@ -212,7 +212,7 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false:
discard client2.postAvailability(size=0xFFFFF.u256, duration=200.u256, minPrice=300.u256, maxCollateral=300.u256).get
# client 1 requests storage
let expiry = (await provider.currentTime()) + 30
let expiry = (await ethProvider.currentTime()) + 30
let cid = client1.upload("some file contents").get
let id = client1.requestStorage(cid, duration=duration, reward=reward, proofProbability=3.u256, expiry=expiry, collateral=200.u256).get
@ -223,12 +223,12 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false:
# Proving mechanism uses blockchain clock to do proving/collect/cleanup round
# hence we must use `advanceTime` over `sleepAsync` as Hardhat does mine new blocks
# only with new transaction
await provider.advanceTime(duration)
await ethProvider.advanceTime(duration)
check eventually (await token.balanceOf(account2)) - startBalance == duration*reward
test "node requires expiry and its value to be in future":
let currentTime = await provider.currentTime()
let currentTime = await ethProvider.currentTime()
let cid = client1.upload("some file contents").get
let responseMissing = client1.requestStorageRaw(cid, duration=1.u256, reward=2.u256, proofProbability=3.u256, collateral=200.u256)
@ -244,9 +244,9 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false:
check responseBefore.body == "Expiry has to be before the request's end (now + duration)"
test "expired request partially pays out for stored time":
let marketplace = Marketplace.new(Marketplace.address, provider.getSigner())
let marketplace = Marketplace.new(Marketplace.address, ethProvider.getSigner())
let tokenAddress = await marketplace.token()
let token = Erc20Token.new(tokenAddress, provider.getSigner())
let token = Erc20Token.new(tokenAddress, ethProvider.getSigner())
let reward = 400.u256
let duration = 100.u256
@ -256,7 +256,7 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false:
# client 1 requests storage but requires two nodes to host the content
let startBalanceClient1 = await token.balanceOf(account1)
let expiry = (await provider.currentTime()) + 10
let expiry = (await ethProvider.currentTime()) + 10
let cid = client1.upload(exampleString(100000)).get
let id = client1.requestStorage(cid, duration=duration, reward=reward, proofProbability=3.u256, expiry=expiry, collateral=200.u256, nodes=2).get
@ -264,7 +264,7 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false:
# Until https://github.com/codex-storage/nim-codex/issues/594 is implemented nothing better then
# sleeping some seconds is available.
await sleepAsync(2.seconds)
await provider.advanceTimeTo(expiry+1)
await ethProvider.advanceTimeTo(expiry+1)
check eventually(client1.purchaseStateIs(id, "cancelled"), 20000)
check eventually ((await token.balanceOf(account2)) - startBalanceClient2) > 0 and ((await token.balanceOf(account2)) - startBalanceClient2) < 10*reward

View File

@ -22,13 +22,13 @@ twonodessuite "Proving integration test", debug1=false, debug2=false:
client.getPurchase(id).option.?state == some state
setup:
marketplace = Marketplace.new(Marketplace.address, provider)
marketplace = Marketplace.new(Marketplace.address, ethProvider)
period = (await marketplace.config()).proofs.period.truncate(uint64)
# Our Hardhat configuration does use automine, which means that time tracked by `provider.currentTime()` is not
# Our Hardhat configuration does use automine, which means that time tracked by `ethProvider.currentTime()` is not
# advanced until blocks are mined and that happens only when transaction is submitted.
# As we use in tests provider.currentTime() which uses block timestamp this can lead to synchronization issues.
await provider.advanceTime(1.u256)
# As we use in tests ethProvider.currentTime() which uses block timestamp this can lead to synchronization issues.
await ethProvider.advanceTime(1.u256)
proc waitUntilPurchaseIsStarted(proofProbability: uint64 = 3,
duration: uint64 = 100 * period,
@ -40,7 +40,7 @@ twonodessuite "Proving integration test", debug1=false, debug2=false:
maxCollateral=200.u256
)
let cid = client1.upload("some file contents").get
let expiry = (await provider.currentTime()) + expiry.u256
let expiry = (await ethProvider.currentTime()) + expiry.u256
let id = client1.requestStorage(
cid,
expiry=expiry,
@ -53,9 +53,9 @@ twonodessuite "Proving integration test", debug1=false, debug2=false:
proc advanceToNextPeriod {.async.} =
let periodicity = Periodicity(seconds: period.u256)
let currentPeriod = periodicity.periodOf(await provider.currentTime())
let currentPeriod = periodicity.periodOf(await ethProvider.currentTime())
let endOfPeriod = periodicity.periodEnd(currentPeriod)
await provider.advanceTimeTo(endOfPeriod + 1)
await ethProvider.advanceTimeTo(endOfPeriod + 1)
proc startValidator: NodeProcess =
let validator = startNode(
@ -81,7 +81,7 @@ twonodessuite "Proving integration test", debug1=false, debug2=false:
proc onProofSubmitted(event: ProofSubmitted) =
proofWasSubmitted = true
let subscription = await marketplace.subscribe(ProofSubmitted, onProofSubmitted)
await provider.advanceTime(period.u256)
await ethProvider.advanceTime(period.u256)
check eventually proofWasSubmitted
await subscription.unsubscribe()
@ -110,7 +110,7 @@ twonodessuite "Proving integration test", debug1=false, debug2=false:
multinodesuite "Simulate invalid proofs",
StartNodes.init(clients=1'u, providers=0'u, validators=1'u),
DebugNodes.init(client=false, provider=false, validator=false):
DebugNodes.init(client=false, ethProvider=false, validator=false):
proc purchaseStateIs(client: CodexClient, id: PurchaseId, state: string): bool =
client.getPurchase(id).option.?state == some state
@ -120,15 +120,15 @@ multinodesuite "Simulate invalid proofs",
var slotId: SlotId
setup:
marketplace = Marketplace.new(Marketplace.address, provider)
marketplace = Marketplace.new(Marketplace.address, ethProvider)
let config = await marketplace.config()
period = config.proofs.period.truncate(uint64)
slotId = SlotId(array[32, byte].default) # ensure we aren't reusing from prev test
# Our Hardhat configuration does use automine, which means that time tracked by `provider.currentTime()` is not
# Our Hardhat configuration does use automine, which means that time tracked by `ethProvider.currentTime()` is not
# advanced until blocks are mined and that happens only when transaction is submitted.
# As we use in tests provider.currentTime() which uses block timestamp this can lead to synchronization issues.
await provider.advanceTime(1.u256)
# As we use in tests ethProvider.currentTime() which uses block timestamp this can lead to synchronization issues.
await ethProvider.advanceTime(1.u256)
proc periods(p: Ordinal | uint): uint64 =
when p is uint:
@ -137,16 +137,16 @@ multinodesuite "Simulate invalid proofs",
proc advanceToNextPeriod {.async.} =
let periodicity = Periodicity(seconds: period.u256)
let currentPeriod = periodicity.periodOf(await provider.currentTime())
let currentPeriod = periodicity.periodOf(await ethProvider.currentTime())
let endOfPeriod = periodicity.periodEnd(currentPeriod)
await provider.advanceTimeTo(endOfPeriod + 1)
await ethProvider.advanceTimeTo(endOfPeriod + 1)
proc waitUntilPurchaseIsStarted(proofProbability: uint64 = 1,
duration: uint64 = 12.periods,
expiry: uint64 = 4.periods) {.async.} =
if clients().len < 1 or providers().len < 1:
raiseAssert("must start at least one client and one provider")
raiseAssert("must start at least one client and one ethProvider")
let client = clients()[0].restClient
let storageProvider = providers()[0].restClient
@ -158,7 +158,7 @@ multinodesuite "Simulate invalid proofs",
maxCollateral=200.u256
)
let cid = client.upload("some file contents " & $ getTime().toUnix).get
let expiry = (await provider.currentTime()) + expiry.u256
let expiry = (await ethProvider.currentTime()) + expiry.u256
# avoid timing issues by filling the slot at the start of the next period
await advanceToNextPeriod()
let id = client.requestStorage(