fix difficulty test

This commit is contained in:
jangko 2023-05-22 15:12:50 +07:00 committed by zah
parent 93ff745421
commit 27393e9420
12 changed files with 67 additions and 155924 deletions

View File

@ -361,7 +361,7 @@ proc chainConfigForNetwork*(id: NetworkId): ChainConfig =
arrowGlacierBlock: some(13_773_000.toBlockNumber), # 2021-12-09 19:55:23 UTC
grayGlacierBlock: some(15_050_000.toBlockNumber), # 2022-06-30 10:54:04 UTC
terminalTotalDifficulty: some(mainNetTTD),
shanghaiTime: some(1681338455.fromUnix)
shanghaiTime: some(1_681_338_455.fromUnix)
)
of RopstenNet:
ChainConfig(
@ -422,7 +422,7 @@ proc chainConfigForNetwork*(id: NetworkId): ChainConfig =
berlinBlock: some(4_460_644.toBlockNumber), # 2021-03-18 05:29:51 UTC
londonBlock: some(5_062_605.toBlockNumber), # 2021-07-01 03:19:39 UTC
terminalTotalDifficulty: some(10790000.u256),
shanghaiTime: some(1678832736.fromUnix)
shanghaiTime: some(1_678_832_736.fromUnix)
)
of SepoliaNet:
ChainConfig(
@ -441,7 +441,7 @@ proc chainConfigForNetwork*(id: NetworkId): ChainConfig =
muirGlacierBlock: some(0.toBlockNumber),
berlinBlock: some(0.toBlockNumber),
londonBlock: some(0.toBlockNumber),
shanghaiTime: some(1677557088.fromUnix)
shanghaiTime: some(1_677_557_088.fromUnix)
)
else:
ChainConfig()

View File

@ -1,114 +0,0 @@
{
"preExpDiffIncrease" : {
"parentTimestamp" : "42",
"parentDifficulty" : "1000000",
"currentTimestamp" : "43",
"currentBlockNumber" : "42",
"currentDifficulty" : "1000488",
"parentUncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
},
"preExpDiffDecrease" : {
"parentTimestamp" : "42",
"parentDifficulty" : "1000000",
"currentTimestamp" : "60",
"currentBlockNumber" : "42",
"currentDifficulty" : "999512",
"parentUncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
},
"ExpDiffAtBlock200000Increase" : {
"parentTimestamp" : "42",
"parentDifficulty" : "1000000",
"currentTimestamp" : "43",
"currentBlockNumber" : "200000",
"currentDifficulty" : "1000489",
"parentUncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
},
"ExpDiffAtBlock200000Decrease" : {
"parentTimestamp" : "42",
"parentDifficulty" : "1000000",
"currentTimestamp" : "60",
"currentBlockNumber" : "200000",
"currentDifficulty" : "999513",
"parentUncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
},
"ExpDiffPostBlock200000Increase" : {
"parentTimestamp" : "42",
"parentDifficulty" : "1000000",
"currentTimestamp" : "43",
"currentBlockNumber" : "200001",
"currentDifficulty" : "1000489",
"parentUncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
},
"ExpDiffPostBlock200000Decrease" : {
"parentTimestamp" : "42",
"parentDifficulty" : "1000000",
"currentTimestamp" : "60",
"currentBlockNumber" : "200001",
"currentDifficulty" : "999513",
"parentUncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
},
"ExpDiffPreBlock300000Increase" : {
"parentTimestamp" : "42",
"parentDifficulty" : "1000000",
"currentTimestamp" : "43",
"currentBlockNumber" : "299999",
"currentDifficulty" : "1000489",
"parentUncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
},
"ExpDiffPreBlock300000Decrease" : {
"parentTimestamp" : "42",
"parentDifficulty" : "1000000",
"currentTimestamp" : "60",
"currentBlockNumber" : "299999",
"currentDifficulty" : "999513",
"parentUncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
},
"ExpDiffAtBlock300000Increase" : {
"parentTimestamp" : "42",
"parentDifficulty" : "1000000",
"currentTimestamp" : "43",
"currentBlockNumber" : "300000",
"currentDifficulty" : "1000490",
"parentUncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
},
"ExpDiffAtBlock300000Decrease" : {
"parentTimestamp" : "42",
"parentDifficulty" : "1000000",
"currentTimestamp" : "60",
"currentBlockNumber" : "300000",
"currentDifficulty" : "999514",
"parentUncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
},
"ExpDiffPostBlock300000Increase" : {
"parentTimestamp" : "42",
"parentDifficulty" : "1000000",
"currentTimestamp" : "43",
"currentBlockNumber" : "300001",
"currentDifficulty" : "1000490",
"parentUncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
},
"ExpDiffPostBlock300000Decrease" : {
"parentTimestamp" : "42",
"parentDifficulty" : "1000000",
"currentTimestamp" : "60",
"currentBlockNumber" : "300001",
"currentDifficulty" : "999514",
"parentUncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
},
"ExpDiffInAYearIncrease" : {
"parentTimestamp" : "42",
"parentDifficulty" : "1000000",
"currentTimestamp" : "43",
"currentBlockNumber" : "2302400",
"currentDifficulty" : "3097640",
"parentUncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
},
"ExpDiffInAYearDecrease" : {
"parentTimestamp" : "42",
"parentDifficulty" : "1000000",
"currentTimestamp" : "60",
"currentBlockNumber" : "2302400",
"currentDifficulty" : "3097152",
"parentUncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,7 @@ import
../nimbus/core/pow/difficulty,
../nimbus/constants,
../nimbus/common/common,
test_helpers
./test_helpers
type
Tester = object
@ -18,6 +18,9 @@ type
Tests = Table[string, Tester]
const
inputPath = "tests" / "fixtures" / "eth_tests" / "DifficultyTests"
proc hexOrInt64(data: JsonNode, key: string, hex: static[bool]): int64 =
when hex:
getHexadecimalInt data[key]
@ -30,59 +33,85 @@ proc hexOrInt256(data: JsonNode, key: string, hex: static[bool]): Uint256 =
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))
proc parseHash(data: string): Hash256 =
case data
of "0x00": result = EMPTY_UNCLE_HASH
of "0x01": result.data[0] = 1.byte
else:
doAssert(false, "invalid uncle hash")
proc parseTests(testData: JSonNode): Tests =
const hex = true
result = initTable[string, Tester]()
var t: Tester
for title, data in fixtures:
for title, data in testData:
t.parentTimestamp = hexOrInt64(data, "parentTimestamp", hex)
t.parentDifficulty = hexOrInt256(data, "parentDifficulty", hex)
let pu = data.fields.getOrDefault("parentUncles")
if pu.isNil:
t.parentUncles = EMPTY_UNCLE_HASH
else:
hexToByteArray(pu.getStr, t.parentUncles.data)
t.parentUncles = parseHash(pu.getStr)
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) =
let testTitle = if name == "": "Difficulty" else: name
test testTitle:
let data = parseTests(name, hex)
for title, t in data:
var p = BlockHeader(
proc calculator(revision: string, timestamp: EthTime, header: BlockHeader): DifficultyInt =
case revision
of "Homestead": result = calcDifficultyHomestead(timestamp, header)
of "GrayGlacier": result = calcDifficultyGrayGlacier(timestamp, header)
of "Frontier": result = calcDifficultyFrontier(timestamp, header)
of "Berlin": result = calcDifficultyMuirGlacier(timestamp, header)
of "Constantinople": result = calcDifficultyConstantinople(timestamp, header)
of "Byzantium": result = calcDifficultyByzantium(timestamp, header)
of "ArrowGlacier": result = calcDifficultyArrowGlacier(timestamp, header)
else:
doAssert(false, "unknown revision: " & revision)
proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) =
var fixture: JsonNode
for _, child in fixtures:
fixture = child
break
for revision, child in fixture:
if revision == "_info":
continue
let tests = parseTests(child)
for title, t in tests:
let p = BlockHeader(
difficulty : t.parentDifficulty,
timestamp : times.fromUnix(t.parentTimestamp),
blockNumber: t.currentBlockNumber - 1,
ommersHash: t.parentUncles)
ommersHash : t.parentUncles
)
let diff = calculator(times.fromUnix(t.currentTimeStamp), p)
let timestamp = times.fromUnix(t.currentTimeStamp)
let diff = calculator(revision, timestamp, p)
check diff == t.currentDifficulty
template runTest() =
var filenames: seq[string] = @[]
for filename in walkDirRec(inputPath):
if not filename.endsWith(".json"):
continue
filenames.add filename
doAssert(filenames.len > 0)
for fname in filenames:
let filename = fname
test fname.subStr(inputPath.len + 1):
let fixtures = parseJson(readFile(filename))
testFixture(fixtures, testStatusIMPL)
proc difficultyMain*() =
let mainnetCom = CommonRef.new(nil, chainConfigForNetwork(MainNet))
func calcDifficultyMainNet(timeStamp: EthTime, parent: BlockHeader): DifficultyInt =
mainnetCom.calcDifficulty(timeStamp, parent)
let ropstenCom = CommonRef.new(nil, chainConfigForNetwork(RopstenNet))
func calcDifficultyRopsten(timeStamp: EthTime, parent: BlockHeader): DifficultyInt =
ropstenCom.calcDifficulty(timeStamp, parent)
suite "DifficultyTest":
runTests("EIP2384_random_to20M", true, calcDifficultyMuirGlacier)
runTests("EIP2384_random", true, calcDifficultyMuirGlacier)
runTests("EIP2384", true, calcDifficultyMuirGlacier)
runTests("Byzantium", true, calcDifficultyByzantium)
runTests("Constantinople", true, calcDifficultyConstantinople)
runTests("Homestead", true, calcDifficultyHomestead)
runTests("MainNetwork", true, calcDifficultyMainNet)
runTests("Frontier", true, calcDifficultyFrontier)
runTests("", false, calcDifficultyMainNet)
runTests("Ropsten", true, calcDifficultyRopsten)
runTest()
when isMainModule:
difficultyMain()