-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds mockprovider to simplify and improve testing of the edge conditions
- Loading branch information
1 parent
e801178
commit b63dc91
Showing
3 changed files
with
169 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import std/strutils | ||
import std/tables | ||
|
||
import pkg/ethers/provider | ||
from codex/clock import SecondsSince1970 | ||
|
||
export provider.Block | ||
|
||
type MockProvider* = ref object of Provider | ||
blocks: OrderedTableRef[int, Block] | ||
earliest: ?int | ||
latest: ?int | ||
|
||
method getBlock*( | ||
provider: MockProvider, | ||
tag: BlockTag | ||
): Future[?Block] {.async.} = | ||
if $tag == "latest": | ||
if latestBlock =? provider.latest: | ||
if provider.blocks.hasKey(latestBlock): | ||
return provider.blocks[latestBlock].some | ||
elif $tag == "earliest": | ||
if earliestBlock =? provider.earliest: | ||
if provider.blocks.hasKey(earliestBlock): | ||
return provider.blocks[earliestBlock].some | ||
else: | ||
let blockNumber = parseHexInt($tag) | ||
if provider.blocks.hasKey(blockNumber): | ||
return provider.blocks[blockNumber].some | ||
return Block.none | ||
|
||
proc updateEarliestAndLatest(provider: MockProvider, blockNumber: int) = | ||
if provider.earliest.isNone: | ||
provider.earliest = blockNumber.some | ||
provider.latest = blockNumber.some | ||
|
||
proc addBlocks*(provider: MockProvider, blocks: OrderedTableRef[int, Block]) = | ||
for number, blk in blocks.pairs: | ||
if provider.blocks.hasKey(number): | ||
continue | ||
provider.updateEarliestAndLatest(number) | ||
provider.blocks[number] = blk | ||
|
||
proc addBlock*(provider: MockProvider, number: int, blk: Block) = | ||
if not provider.blocks.hasKey(number): | ||
provider.updateEarliestAndLatest(number) | ||
provider.blocks[number] = blk | ||
|
||
proc newMockProvider*(): MockProvider = | ||
MockProvider( | ||
blocks: newOrderedTable[int, Block](), | ||
earliest: int.none, | ||
latest: int.none | ||
) | ||
|
||
proc newMockProvider*(blocks: OrderedTableRef[int, Block]): MockProvider = | ||
let provider = newMockProvider() | ||
provider.addBlocks(blocks) | ||
provider | ||
|
||
proc newMockProvider*( | ||
numberOfBlocks: int, | ||
earliestBlockNumber: int, | ||
earliestBlockTimestamp: SecondsSince1970, | ||
timeIntervalBetweenBlocks: SecondsSince1970 | ||
): MockProvider = | ||
var blocks = newOrderedTable[int, provider.Block]() | ||
var blockNumber = earliestBlockNumber | ||
var blockTime = earliestBlockTimestamp | ||
for i in 0..<numberOfBlocks: | ||
blocks[blockNumber] = provider.Block(number: blockNumber.u256.some, | ||
timestamp: blockTime.u256, hash: BlockHash.none) | ||
inc blockNumber | ||
inc blockTime, timeIntervalBetweenBlocks.int | ||
MockProvider( | ||
blocks: blocks, | ||
earliest: earliestBlockNumber.some, | ||
latest: (earliestBlockNumber + numberOfBlocks - 1).some | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters