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

View File

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

View File

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

View File

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

View File

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

View File

@ -28,10 +28,10 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false:
return purchase.state == state return purchase.state == state
setup: 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. # 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. # As we use in tests ethProvider.currentTime() which uses block timestamp this can lead to synchronization issues.
await provider.advanceTime(1.u256) await ethProvider.advanceTime(1.u256)
test "nodes can print their peer information": test "nodes can print their peer information":
check client1.info() != client2.info() check client1.info() != client2.info()
@ -111,7 +111,7 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false:
check availability in client1.getAvailabilities().get check availability in client1.getAvailabilities().get
test "node handles storage request": 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 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 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 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 chunker = RandomChunker.new(rng, size = DefaultBlockSize * 2, chunkSize = DefaultBlockSize * 2)
let data = await chunker.getBytes() let data = await chunker.getBytes()
let cid = client1.upload(byteutils.toHex(data)).get let cid = client1.upload(byteutils.toHex(data)).get
let expiry = (await provider.currentTime()) + 30 let expiry = (await ethProvider.currentTime()) + 30
let id = client1.requestStorage( let id = client1.requestStorage(
cid, cid,
duration=100.u256, duration=100.u256,
@ -145,7 +145,7 @@ twonodessuite "Integration tests", debug1 = false, debug2 = false:
# TODO: We currently do not support encoding single chunks # TODO: We currently do not support encoding single chunks
# test "node retrieves purchase status with 1 chunk": # 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 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 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 # 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 # check request.ask.maxSlotLoss == 1'u64
test "node remembers purchase status after restart": 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 cid = client1.upload("some file contents").get
let id = client1.requestStorage(cid, let id = client1.requestStorage(cid,
duration=100.u256, 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) discard client2.postAvailability(size=size, duration=200.u256, minPrice=300.u256, maxCollateral=300.u256)
# client 1 requests storage # 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 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 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 check newSize > 0 and newSize < size
test "node slots gets paid out": 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 tokenAddress = await marketplace.token()
let token = Erc20Token.new(tokenAddress, provider.getSigner()) let token = Erc20Token.new(tokenAddress, ethProvider.getSigner())
let reward = 400.u256 let reward = 400.u256
let duration = 100.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 discard client2.postAvailability(size=0xFFFFF.u256, duration=200.u256, minPrice=300.u256, maxCollateral=300.u256).get
# client 1 requests storage # 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 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 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 # Proving mechanism uses blockchain clock to do proving/collect/cleanup round
# hence we must use `advanceTime` over `sleepAsync` as Hardhat does mine new blocks # hence we must use `advanceTime` over `sleepAsync` as Hardhat does mine new blocks
# only with new transaction # only with new transaction
await provider.advanceTime(duration) await ethProvider.advanceTime(duration)
check eventually (await token.balanceOf(account2)) - startBalance == duration*reward check eventually (await token.balanceOf(account2)) - startBalance == duration*reward
test "node requires expiry and its value to be in future": 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 cid = client1.upload("some file contents").get
let responseMissing = client1.requestStorageRaw(cid, duration=1.u256, reward=2.u256, proofProbability=3.u256, collateral=200.u256) 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)" check responseBefore.body == "Expiry has to be before the request's end (now + duration)"
test "expired request partially pays out for stored time": 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 tokenAddress = await marketplace.token()
let token = Erc20Token.new(tokenAddress, provider.getSigner()) let token = Erc20Token.new(tokenAddress, ethProvider.getSigner())
let reward = 400.u256 let reward = 400.u256
let duration = 100.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 # client 1 requests storage but requires two nodes to host the content
let startBalanceClient1 = await token.balanceOf(account1) 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 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 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 # Until https://github.com/codex-storage/nim-codex/issues/594 is implemented nothing better then
# sleeping some seconds is available. # sleeping some seconds is available.
await sleepAsync(2.seconds) await sleepAsync(2.seconds)
await provider.advanceTimeTo(expiry+1) await ethProvider.advanceTimeTo(expiry+1)
check eventually(client1.purchaseStateIs(id, "cancelled"), 20000) check eventually(client1.purchaseStateIs(id, "cancelled"), 20000)
check eventually ((await token.balanceOf(account2)) - startBalanceClient2) > 0 and ((await token.balanceOf(account2)) - startBalanceClient2) < 10*reward 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 client.getPurchase(id).option.?state == some state
setup: setup:
marketplace = Marketplace.new(Marketplace.address, provider) marketplace = Marketplace.new(Marketplace.address, ethProvider)
period = (await marketplace.config()).proofs.period.truncate(uint64) 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. # 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. # As we use in tests ethProvider.currentTime() which uses block timestamp this can lead to synchronization issues.
await provider.advanceTime(1.u256) await ethProvider.advanceTime(1.u256)
proc waitUntilPurchaseIsStarted(proofProbability: uint64 = 3, proc waitUntilPurchaseIsStarted(proofProbability: uint64 = 3,
duration: uint64 = 100 * period, duration: uint64 = 100 * period,
@ -40,7 +40,7 @@ twonodessuite "Proving integration test", debug1=false, debug2=false:
maxCollateral=200.u256 maxCollateral=200.u256
) )
let cid = client1.upload("some file contents").get 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( let id = client1.requestStorage(
cid, cid,
expiry=expiry, expiry=expiry,
@ -53,9 +53,9 @@ twonodessuite "Proving integration test", debug1=false, debug2=false:
proc advanceToNextPeriod {.async.} = proc advanceToNextPeriod {.async.} =
let periodicity = Periodicity(seconds: period.u256) 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) let endOfPeriod = periodicity.periodEnd(currentPeriod)
await provider.advanceTimeTo(endOfPeriod + 1) await ethProvider.advanceTimeTo(endOfPeriod + 1)
proc startValidator: NodeProcess = proc startValidator: NodeProcess =
let validator = startNode( let validator = startNode(
@ -81,7 +81,7 @@ twonodessuite "Proving integration test", debug1=false, debug2=false:
proc onProofSubmitted(event: ProofSubmitted) = proc onProofSubmitted(event: ProofSubmitted) =
proofWasSubmitted = true proofWasSubmitted = true
let subscription = await marketplace.subscribe(ProofSubmitted, onProofSubmitted) let subscription = await marketplace.subscribe(ProofSubmitted, onProofSubmitted)
await provider.advanceTime(period.u256) await ethProvider.advanceTime(period.u256)
check eventually proofWasSubmitted check eventually proofWasSubmitted
await subscription.unsubscribe() await subscription.unsubscribe()
@ -110,7 +110,7 @@ twonodessuite "Proving integration test", debug1=false, debug2=false:
multinodesuite "Simulate invalid proofs", multinodesuite "Simulate invalid proofs",
StartNodes.init(clients=1'u, providers=0'u, validators=1'u), 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 = proc purchaseStateIs(client: CodexClient, id: PurchaseId, state: string): bool =
client.getPurchase(id).option.?state == some state client.getPurchase(id).option.?state == some state
@ -120,15 +120,15 @@ multinodesuite "Simulate invalid proofs",
var slotId: SlotId var slotId: SlotId
setup: setup:
marketplace = Marketplace.new(Marketplace.address, provider) marketplace = Marketplace.new(Marketplace.address, ethProvider)
let config = await marketplace.config() let config = await marketplace.config()
period = config.proofs.period.truncate(uint64) period = config.proofs.period.truncate(uint64)
slotId = SlotId(array[32, byte].default) # ensure we aren't reusing from prev test 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. # 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. # As we use in tests ethProvider.currentTime() which uses block timestamp this can lead to synchronization issues.
await provider.advanceTime(1.u256) await ethProvider.advanceTime(1.u256)
proc periods(p: Ordinal | uint): uint64 = proc periods(p: Ordinal | uint): uint64 =
when p is uint: when p is uint:
@ -137,16 +137,16 @@ multinodesuite "Simulate invalid proofs",
proc advanceToNextPeriod {.async.} = proc advanceToNextPeriod {.async.} =
let periodicity = Periodicity(seconds: period.u256) 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) let endOfPeriod = periodicity.periodEnd(currentPeriod)
await provider.advanceTimeTo(endOfPeriod + 1) await ethProvider.advanceTimeTo(endOfPeriod + 1)
proc waitUntilPurchaseIsStarted(proofProbability: uint64 = 1, proc waitUntilPurchaseIsStarted(proofProbability: uint64 = 1,
duration: uint64 = 12.periods, duration: uint64 = 12.periods,
expiry: uint64 = 4.periods) {.async.} = expiry: uint64 = 4.periods) {.async.} =
if clients().len < 1 or providers().len < 1: 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 client = clients()[0].restClient
let storageProvider = providers()[0].restClient let storageProvider = providers()[0].restClient
@ -158,7 +158,7 @@ multinodesuite "Simulate invalid proofs",
maxCollateral=200.u256 maxCollateral=200.u256
) )
let cid = client.upload("some file contents " & $ getTime().toUnix).get 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 # avoid timing issues by filling the slot at the start of the next period
await advanceToNextPeriod() await advanceToNextPeriod()
let id = client.requestStorage( let id = client.requestStorage(