Adds more logging and makes testing earliest block boundary more reliable
This commit is contained in:
parent
ea0f967596
commit
57e2a97d20
|
@ -1,7 +1,4 @@
|
||||||
# import std/sequtils
|
|
||||||
import std/strutils
|
import std/strutils
|
||||||
import std/strformat
|
|
||||||
# import std/sugar
|
|
||||||
import pkg/ethers
|
import pkg/ethers
|
||||||
import pkg/upraises
|
import pkg/upraises
|
||||||
import pkg/questionable
|
import pkg/questionable
|
||||||
|
@ -450,14 +447,17 @@ proc binarySearchBlockNumberForEpoch*(provider: Provider,
|
||||||
|
|
||||||
debug "[binarySearchBlockNumberForEpoch]:", low = low, high = high
|
debug "[binarySearchBlockNumberForEpoch]:", low = low, high = high
|
||||||
while low <= 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) =
|
let (midBlockNumber, midBlockTimestamp) =
|
||||||
await provider.blockNumberAndTimestamp(BlockTag.init(mid))
|
await provider.blockNumberAndTimestamp(BlockTag.init(mid))
|
||||||
|
|
||||||
if midBlockTimestamp < epochTime:
|
if midBlockTimestamp < epochTime:
|
||||||
low = mid + 1.u256
|
low = mid + 1
|
||||||
elif midBlockTimestamp > epochTime:
|
elif midBlockTimestamp > epochTime:
|
||||||
high = mid - 1.u256
|
high = mid - 1
|
||||||
else:
|
else:
|
||||||
return midBlockNumber
|
return midBlockNumber
|
||||||
# NOTICE that by how the binaty search is implemented, when it finishes
|
# NOTICE that by how the binaty search is implemented, when it finishes
|
||||||
|
@ -466,10 +466,11 @@ proc binarySearchBlockNumberForEpoch*(provider: Provider,
|
||||||
await provider.binarySearchFindClosestBlock(
|
await provider.binarySearchFindClosestBlock(
|
||||||
epochTime.truncate(int), low=high, high=low)
|
epochTime.truncate(int), low=high, high=low)
|
||||||
|
|
||||||
proc blockNumberForEpoch*(provider: Provider, epochTime: int64): Future[UInt256]
|
proc blockNumberForEpoch*(provider: Provider,
|
||||||
{.async.} =
|
epochTime: SecondsSince1970): Future[UInt256] {.async.} =
|
||||||
let avgBlockTime = await provider.estimateAverageBlockTime()
|
let avgBlockTime = await provider.estimateAverageBlockTime()
|
||||||
debug "[blockNumberForEpoch]:", avgBlockTime = avgBlockTime
|
debug "[blockNumberForEpoch]:", avgBlockTime = avgBlockTime
|
||||||
|
debug "[blockNumberForEpoch]:", epochTime = epochTime
|
||||||
let epochTimeUInt256 = epochTime.u256
|
let epochTimeUInt256 = epochTime.u256
|
||||||
let (latestBlockNumber, latestBlockTimestamp) =
|
let (latestBlockNumber, latestBlockTimestamp) =
|
||||||
await provider.blockNumberAndTimestamp(BlockTag.latest)
|
await provider.blockNumberAndTimestamp(BlockTag.latest)
|
||||||
|
@ -484,6 +485,8 @@ proc blockNumberForEpoch*(provider: Provider, epochTime: int64): Future[UInt256]
|
||||||
let timeDiff = latestBlockTimestamp - epochTimeUInt256
|
let timeDiff = latestBlockTimestamp - epochTimeUInt256
|
||||||
let blockDiff = timeDiff div avgBlockTime
|
let blockDiff = timeDiff div avgBlockTime
|
||||||
|
|
||||||
|
debug "[blockNumberForEpoch]:", timeDiff = timeDiff, blockDiff = blockDiff
|
||||||
|
|
||||||
if blockDiff >= latestBlockNumber - earliestBlockNumber:
|
if blockDiff >= latestBlockNumber - earliestBlockNumber:
|
||||||
return earliestBlockNumber
|
return earliestBlockNumber
|
||||||
|
|
||||||
|
@ -511,7 +514,7 @@ method queryPastSlotFilledEvents*(
|
||||||
|
|
||||||
method queryPastSlotFilledEvents*(
|
method queryPastSlotFilledEvents*(
|
||||||
market: OnChainMarket,
|
market: OnChainMarket,
|
||||||
fromTime: int64): Future[seq[SlotFilled]] {.async.} =
|
fromTime: SecondsSince1970): Future[seq[SlotFilled]] {.async.} =
|
||||||
|
|
||||||
convertEthersError:
|
convertEthersError:
|
||||||
let fromBlock =
|
let fromBlock =
|
||||||
|
|
|
@ -265,5 +265,5 @@ method queryPastStorageRequestedEvents*(
|
||||||
|
|
||||||
method queryPastSlotFilledEvents*(
|
method queryPastSlotFilledEvents*(
|
||||||
market: Market,
|
market: Market,
|
||||||
fromTime: int64): Future[seq[SlotFilled]] {.base, async.} =
|
fromTime: SecondsSince1970): Future[seq[SlotFilled]] {.base, async.} =
|
||||||
raiseAssert("not implemented")
|
raiseAssert("not implemented")
|
||||||
|
|
|
@ -470,7 +470,7 @@ ethersuite "On-Chain Market":
|
||||||
await market.fillSlot(request.id, 2.u256, proof, request.ask.collateral)
|
await market.fillSlot(request.id, 2.u256, proof, request.ask.collateral)
|
||||||
|
|
||||||
let events = await market.queryPastSlotFilledEvents(
|
let events = await market.queryPastSlotFilledEvents(
|
||||||
fromTime = fromTime.truncate(int64))
|
fromTime = fromTime.truncate(SecondsSince1970))
|
||||||
|
|
||||||
check events == @[
|
check events == @[
|
||||||
SlotFilled(requestId: request.id, slotIndex: 1.u256),
|
SlotFilled(requestId: request.id, slotIndex: 1.u256),
|
||||||
|
@ -490,7 +490,7 @@ ethersuite "On-Chain Market":
|
||||||
await ethProvider.blockNumberAndTimestamp(BlockTag.latest)
|
await ethProvider.blockNumberAndTimestamp(BlockTag.latest)
|
||||||
|
|
||||||
let events = await market.queryPastSlotFilledEvents(
|
let events = await market.queryPastSlotFilledEvents(
|
||||||
fromTime = fromTime.truncate(int64))
|
fromTime = fromTime.truncate(SecondsSince1970))
|
||||||
|
|
||||||
check events.len == 0
|
check events.len == 0
|
||||||
|
|
||||||
|
@ -514,8 +514,10 @@ ethersuite "On-Chain Market":
|
||||||
|
|
||||||
test "blockNumberForEpoch returns the earliest block when retained history " &
|
test "blockNumberForEpoch returns the earliest block when retained history " &
|
||||||
"is shorter than the given epoch time":
|
"is shorter than the given epoch time":
|
||||||
# create predictable conditions for computing average block time
|
# create predictable conditions
|
||||||
let averageBlockTime = 10.u256
|
# 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.mineNBlocks(1)
|
||||||
await ethProvider.advanceTime(averageBlockTime)
|
await ethProvider.advanceTime(averageBlockTime)
|
||||||
let (earliestBlockNumber, earliestTimestamp) =
|
let (earliestBlockNumber, earliestTimestamp) =
|
||||||
|
@ -524,12 +526,8 @@ ethersuite "On-Chain Market":
|
||||||
let fromTime = earliestTimestamp - 1
|
let fromTime = earliestTimestamp - 1
|
||||||
|
|
||||||
let actual = await ethProvider.blockNumberForEpoch(
|
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
|
check actual == earliestBlockNumber
|
||||||
|
|
||||||
test "blockNumberForEpoch finds closest blockNumber for given epoch time":
|
test "blockNumberForEpoch finds closest blockNumber for given epoch time":
|
||||||
|
@ -615,7 +613,7 @@ ethersuite "On-Chain Market":
|
||||||
debug "Validating", epochTime = epochTime,
|
debug "Validating", epochTime = epochTime,
|
||||||
expectedBlockNumber = expectedBlockNumber
|
expectedBlockNumber = expectedBlockNumber
|
||||||
let actualBlockNumber = await ethProvider.blockNumberForEpoch(
|
let actualBlockNumber = await ethProvider.blockNumberForEpoch(
|
||||||
epochTime.truncate(int64))
|
epochTime.truncate(SecondsSince1970))
|
||||||
check actualBlockNumber == expectedBlockNumber
|
check actualBlockNumber == expectedBlockNumber
|
||||||
|
|
||||||
test "past event query can specify negative `blocksAgo` parameter":
|
test "past event query can specify negative `blocksAgo` parameter":
|
||||||
|
|
Loading…
Reference in New Issue