Use compile flag to determine if the latest block should be used for clock.now

Due to a bug in hardhat, the latest block timestamp was out of sync from the newHeads block timestamp which was causing some timing issues in the tests. To fix this, a compile flag, `codex_testing` (which was changed from `codex_enable_proof_failures`) is used to determine the bevhaviour of clock.now. If `codex_testing` is true, clock.now uses the latest block timestamp, else it uses the cached block.timestamp that was populated in the `newHeads` event.
This commit is contained in:
Eric 2023-11-15 17:24:15 +11:00
parent c19a94ddc1
commit 658302802f
No known key found for this signature in database
8 changed files with 21 additions and 16 deletions

View File

@ -25,13 +25,13 @@ task codex, "build codex binary":
buildBinary "codex", params = "-d:chronicles_runtime_filtering -d:chronicles_log_level=TRACE"
task testCodex, "Build & run Codex tests":
test "testCodex", params = "-d:codex_enable_proof_failures=true"
test "testCodex", params = "-d:codex_testing=true"
task testContracts, "Build & run Codex Contract tests":
test "testContracts"
task testIntegration, "Run integration tests":
buildBinary "codex", params = "-d:chronicles_runtime_filtering -d:chronicles_log_level=TRACE -d:codex_enable_proof_failures=true"
buildBinary "codex", params = "-d:chronicles_runtime_filtering -d:chronicles_log_level=TRACE -d:codex_testing=true"
test "testIntegration"
task build, "build codex binary":

View File

@ -118,7 +118,7 @@ proc bootstrapInteractions(
if config.persistence:
# This is used for simulation purposes. Normal nodes won't be compiled with this flag
# and hence the proof failure will always be 0.
when codex_enable_proof_failures:
when codex_testing:
let proofFailures = config.simulateProofFailures
if proofFailures > 0:
warn "Enabling proof failure simulation!"

View File

@ -44,7 +44,7 @@ export net, DefaultQuotaBytes, DefaultBlockTtl, DefaultBlockMaintenanceInterval,
const
codex_enable_api_debug_peers* {.booldefine.} = false
codex_enable_proof_failures* {.booldefine.} = false
codex_testing* {.booldefine.} = false
codex_enable_log_counter* {.booldefine.} = false
type

View File

@ -2,6 +2,7 @@ import pkg/ethers
import pkg/chronos
import pkg/stint
import ../clock
import ../conf
export clock
@ -41,14 +42,17 @@ method stop*(clock: OnChainClock) {.async.} =
await clock.subscription.unsubscribe()
method now*(clock: OnChainClock): SecondsSince1970 =
try:
if queriedBlock =? (waitFor clock.provider.getBlock(BlockTag.latest)):
if queriedBlock.timestamp != clock.lastBlockTime:
trace "queried block and event block are not in sync",
queriedBlockLessThanEventBlock = queriedBlock.timestamp < clock.lastBlockTime
return queriedBlock.timestamp.truncate(int64)
except CatchableError as e:
warn "failed to get latest block timestamp"
when codex_testing:
# hardhat's latest block.timestamp is usually 1s behind the block timestamp
# in the newHeads event. When testing, always return the latest block.
try:
if queriedBlock =? (waitFor clock.provider.getBlock(BlockTag.latest)):
return queriedBlock.timestamp.truncate(int64)
except CatchableError as e:
warn "failed to get latest block timestamp"
return clock.lastBlockTime.truncate(int64)
else:
return clock.lastBlockTime.truncate(int64)
method waitUntil*(clock: OnChainClock, time: SecondsSince1970) {.async.} =

View File

@ -51,7 +51,7 @@ method run*(state: SaleFilled, machine: Machine): Future[?State] {.async.} =
if err =? (await onExpiryUpdate(request.content.cid, requestEnd)).errorOption:
return some State(SaleErrored(error: err))
when codex_enable_proof_failures:
when codex_testing:
if context.simulateProofFailures > 0:
info "Proving with failure rate", rate = context.simulateProofFailures
return some State(SaleProvingSimulated(failEveryNProofs: context.simulateProofFailures))

View File

@ -1,5 +1,5 @@
import ../../conf
when codex_enable_proof_failures:
when codex_testing:
import std/strutils
import pkg/chronicles
import pkg/stint

View File

@ -4,6 +4,7 @@ import pkg/chronos
import pkg/chronicles
import ./market
import ./clock
import ./utils/exceptions
export market
export sets
@ -78,7 +79,7 @@ proc markProofAsMissing(validation: Validation,
except CancelledError:
raise
except CatchableError as e:
error "Marking proof as missing failed", msg = e.msg
error "Marking proof as missing failed", msg = e.msgDetail
proc markProofsAsMissing(validation: Validation) {.async.} =
for slotId in validation.slots:

View File

@ -95,7 +95,7 @@ if not defined(macosx):
--define:nimStackTraceOverride
switch("import", "libbacktrace")
switch("define", "codex_enable_proof_failures=true")
switch("define", "codex_testing=true")
# `switch("warning[CaseTransition]", "off")` fails with "Error: invalid command line option: '--warning[CaseTransition]'"
switch("warning", "CaseTransition:off")