nimbus-eth1/tests/test_difficulty.nim

89 lines
3.1 KiB
Nim
Raw Normal View History

2022-12-02 04:39:12 +00:00
import
std/[strutils, tables, os, json, times],
unittest2,
stew/byteutils,
../nimbus/core/pow/difficulty,
../nimbus/constants,
../nimbus/common/common,
test_helpers
2019-08-23 15:54:25 +00:00
type
Tester = object
parentTimestamp: int64
parentDifficulty: Uint256
parentUncles: Hash256
currentTimestamp: int64
currentBlockNumber: Uint256
currentDifficulty: Uint256
Tests = Table[string, Tester]
proc hexOrInt64(data: JsonNode, key: string, hex: static[bool]): int64 =
when hex:
getHexadecimalInt data[key]
else:
int64(parseInt data[key].getStr)
proc hexOrInt256(data: JsonNode, key: string, hex: static[bool]): Uint256 =
when hex:
UInt256.fromHex data[key].getStr
else:
parse(data[key].getStr, Uint256)
proc parseTests(name: string, hex: static[bool]): Tests =
let fileName = "tests" / "fixtures" / "DifficultyTests" / "difficulty" & name & ".json"
let fixtures = parseJSON(readFile(fileName))
result = initTable[string, Tester]()
var t: Tester
for title, data in fixtures:
t.parentTimestamp = hexOrInt64(data, "parentTimestamp", hex)
t.parentDifficulty = hexOrInt256(data, "parentDifficulty", hex)
2020-02-20 03:43:47 +00:00
let pu = data.fields.getOrDefault("parentUncles")
if pu.isNil:
t.parentUncles = EMPTY_UNCLE_HASH
else:
hexToByteArray(pu.getStr, t.parentUncles.data)
2019-08-23 15:54:25 +00:00
t.currentTimestamp = hexOrInt64(data, "currentTimestamp", hex)
t.currentBlockNumber = hexOrInt256(data, "currentBlockNumber", hex)
t.currentDifficulty = hexOrInt256(data, "currentDifficulty", hex)
result[title] = t
template runTests(name: string, hex: bool, calculator: typed) =
2020-02-20 03:43:47 +00:00
let testTitle = if name == "": "Difficulty" else: name
test testTitle:
let data = parseTests(name, hex)
for title, t in data:
var p = BlockHeader(
difficulty: t.parentDifficulty,
timestamp: times.fromUnix(t.parentTimestamp),
blockNumber: t.currentBlockNumber - 1,
ommersHash: t.parentUncles)
2019-08-23 15:54:25 +00:00
let diff = calculator(times.fromUnix(t.currentTimeStamp), p)
2019-08-23 15:54:25 +00:00
check diff == t.currentDifficulty
2019-09-21 05:45:23 +00:00
proc difficultyMain*() =
2022-12-02 04:39:12 +00:00
let mainnetCom = CommonRef.new(nil, chainConfigForNetwork(MainNet))
func calcDifficultyMainNet(timeStamp: EthTime, parent: BlockHeader): DifficultyInt =
mainnetCom.calcDifficulty(timeStamp, parent)
2020-04-12 10:33:17 +00:00
2022-12-02 04:39:12 +00:00
let ropstenCom = CommonRef.new(nil, chainConfigForNetwork(RopstenNet))
2020-04-12 11:09:18 +00:00
func calcDifficultyRopsten(timeStamp: EthTime, parent: BlockHeader): DifficultyInt =
2022-12-02 04:39:12 +00:00
ropstenCom.calcDifficulty(timeStamp, parent)
2020-04-12 11:09:18 +00:00
2019-09-21 05:45:23 +00:00
suite "DifficultyTest":
2020-04-12 11:13:22 +00:00
runTests("EIP2384_random_to20M", true, calcDifficultyMuirGlacier)
runTests("EIP2384_random", true, calcDifficultyMuirGlacier)
runTests("EIP2384", true, calcDifficultyMuirGlacier)
2019-09-21 05:45:23 +00:00
runTests("Byzantium", true, calcDifficultyByzantium)
runTests("Constantinople", true, calcDifficultyConstantinople)
runTests("Homestead", true, calcDifficultyHomestead)
2022-12-02 04:39:12 +00:00
runTests("MainNetwork", true, calcDifficultyMainNet)
2019-09-21 05:45:23 +00:00
runTests("Frontier", true, calcDifficultyFrontier)
2022-12-02 04:39:12 +00:00
runTests("", false, calcDifficultyMainNet)
2020-04-12 11:09:18 +00:00
runTests("Ropsten", true, calcDifficultyRopsten)
when isMainModule:
difficultyMain()