Skip to content

Commit

Permalink
Adds more logging and makes testing earliest block boundary more reli…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
marcinczenko committed Oct 11, 2024
1 parent 1c01f89 commit 32e31c6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
21 changes: 12 additions & 9 deletions codex/contracts/market.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# import std/sequtils
import std/strutils
import std/strformat
# import std/sugar
import pkg/ethers
import pkg/upraises
import pkg/questionable
Expand Down Expand Up @@ -450,14 +447,17 @@ proc binarySearchBlockNumberForEpoch*(provider: Provider,

debug "[binarySearchBlockNumberForEpoch]:", low = low, high = high
while low <= high:
let mid = (low + high) div 2.u256
if low == 0 and high == 0:
return low
let mid = (low + high) div 2
debug "[binarySearchBlockNumberForEpoch]:", low = low, mid = mid, high = high
let (midBlockNumber, midBlockTimestamp) =
await provider.blockNumberAndTimestamp(BlockTag.init(mid))

if midBlockTimestamp < epochTime:
low = mid + 1.u256
low = mid + 1
elif midBlockTimestamp > epochTime:
high = mid - 1.u256
high = mid - 1
else:
return midBlockNumber
# NOTICE that by how the binaty search is implemented, when it finishes
Expand All @@ -466,10 +466,11 @@ proc binarySearchBlockNumberForEpoch*(provider: Provider,
await provider.binarySearchFindClosestBlock(
epochTime.truncate(int), low=high, high=low)

proc blockNumberForEpoch*(provider: Provider, epochTime: int64): Future[UInt256]
{.async.} =
proc blockNumberForEpoch*(provider: Provider,
epochTime: SecondsSince1970): Future[UInt256] {.async.} =
let avgBlockTime = await provider.estimateAverageBlockTime()
debug "[blockNumberForEpoch]:", avgBlockTime = avgBlockTime
debug "[blockNumberForEpoch]:", epochTime = epochTime
let epochTimeUInt256 = epochTime.u256
let (latestBlockNumber, latestBlockTimestamp) =
await provider.blockNumberAndTimestamp(BlockTag.latest)
Expand All @@ -484,6 +485,8 @@ proc blockNumberForEpoch*(provider: Provider, epochTime: int64): Future[UInt256]
let timeDiff = latestBlockTimestamp - epochTimeUInt256
let blockDiff = timeDiff div avgBlockTime

debug "[blockNumberForEpoch]:", timeDiff = timeDiff, blockDiff = blockDiff

if blockDiff >= latestBlockNumber - earliestBlockNumber:
return earliestBlockNumber

Expand Down Expand Up @@ -511,7 +514,7 @@ method queryPastSlotFilledEvents*(

method queryPastSlotFilledEvents*(
market: OnChainMarket,
fromTime: int64): Future[seq[SlotFilled]] {.async.} =
fromTime: SecondsSince1970): Future[seq[SlotFilled]] {.async.} =

convertEthersError:
let fromBlock =
Expand Down
2 changes: 1 addition & 1 deletion codex/market.nim
Original file line number Diff line number Diff line change
Expand Up @@ -265,5 +265,5 @@ method queryPastStorageRequestedEvents*(

method queryPastSlotFilledEvents*(
market: Market,
fromTime: int64): Future[seq[SlotFilled]] {.base, async.} =
fromTime: SecondsSince1970): Future[seq[SlotFilled]] {.base, async.} =
raiseAssert("not implemented")
18 changes: 8 additions & 10 deletions tests/contracts/testMarket.nim
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ ethersuite "On-Chain Market":
await market.fillSlot(request.id, 2.u256, proof, request.ask.collateral)

let events = await market.queryPastSlotFilledEvents(
fromTime = fromTime.truncate(int64))
fromTime = fromTime.truncate(SecondsSince1970))

check events == @[
SlotFilled(requestId: request.id, slotIndex: 1.u256),
Expand All @@ -490,7 +490,7 @@ ethersuite "On-Chain Market":
await ethProvider.blockNumberAndTimestamp(BlockTag.latest)

let events = await market.queryPastSlotFilledEvents(
fromTime = fromTime.truncate(int64))
fromTime = fromTime.truncate(SecondsSince1970))

check events.len == 0

Expand All @@ -514,8 +514,10 @@ ethersuite "On-Chain Market":

test "blockNumberForEpoch returns the earliest block when retained history " &
"is shorter than the given epoch time":
# create predictable conditions for computing average block time
let averageBlockTime = 10.u256
# create predictable conditions
# we keep minimal resultion of 1s so that we are sure that
# we will land before the earliest (genesis in our case) block
let averageBlockTime = 1.u256
await ethProvider.mineNBlocks(1)
await ethProvider.advanceTime(averageBlockTime)
let (earliestBlockNumber, earliestTimestamp) =
Expand All @@ -524,12 +526,8 @@ ethersuite "On-Chain Market":
let fromTime = earliestTimestamp - 1

let actual = await ethProvider.blockNumberForEpoch(
fromTime.truncate(int64))
fromTime.truncate(SecondsSince1970))

# Notice this could fail in a network where "earliest" block is
# not the genesis block - we run the tests agains local network
# so we know the earliest block is the same as genesis block
# earliestBlockNumber is 0.u256 in our case.
check actual == earliestBlockNumber

test "blockNumberForEpoch finds closest blockNumber for given epoch time":
Expand Down Expand Up @@ -615,7 +613,7 @@ ethersuite "On-Chain Market":
debug "Validating", epochTime = epochTime,
expectedBlockNumber = expectedBlockNumber
let actualBlockNumber = await ethProvider.blockNumberForEpoch(
epochTime.truncate(int64))
epochTime.truncate(SecondsSince1970))
check actualBlockNumber == expectedBlockNumber

test "past event query can specify negative `blocksAgo` parameter":
Expand Down

0 comments on commit 32e31c6

Please sign in to comment.