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 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 grayGlacierBlock: some(15_050_000.toBlockNumber), # 2022-06-30 10:54:04 UTC
terminalTotalDifficulty: some(mainNetTTD), terminalTotalDifficulty: some(mainNetTTD),
shanghaiTime: some(1681338455.fromUnix) shanghaiTime: some(1_681_338_455.fromUnix)
) )
of RopstenNet: of RopstenNet:
ChainConfig( ChainConfig(
@ -422,7 +422,7 @@ proc chainConfigForNetwork*(id: NetworkId): ChainConfig =
berlinBlock: some(4_460_644.toBlockNumber), # 2021-03-18 05:29:51 UTC 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 londonBlock: some(5_062_605.toBlockNumber), # 2021-07-01 03:19:39 UTC
terminalTotalDifficulty: some(10790000.u256), terminalTotalDifficulty: some(10790000.u256),
shanghaiTime: some(1678832736.fromUnix) shanghaiTime: some(1_678_832_736.fromUnix)
) )
of SepoliaNet: of SepoliaNet:
ChainConfig( ChainConfig(
@ -441,7 +441,7 @@ proc chainConfigForNetwork*(id: NetworkId): ChainConfig =
muirGlacierBlock: some(0.toBlockNumber), muirGlacierBlock: some(0.toBlockNumber),
berlinBlock: some(0.toBlockNumber), berlinBlock: some(0.toBlockNumber),
londonBlock: some(0.toBlockNumber), londonBlock: some(0.toBlockNumber),
shanghaiTime: some(1677557088.fromUnix) shanghaiTime: some(1_677_557_088.fromUnix)
) )
else: else:
ChainConfig() 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/core/pow/difficulty,
../nimbus/constants, ../nimbus/constants,
../nimbus/common/common, ../nimbus/common/common,
test_helpers ./test_helpers
type type
Tester = object Tester = object
@ -18,6 +18,9 @@ type
Tests = Table[string, Tester] Tests = Table[string, Tester]
const
inputPath = "tests" / "fixtures" / "eth_tests" / "DifficultyTests"
proc hexOrInt64(data: JsonNode, key: string, hex: static[bool]): int64 = proc hexOrInt64(data: JsonNode, key: string, hex: static[bool]): int64 =
when hex: when hex:
getHexadecimalInt data[key] getHexadecimalInt data[key]
@ -30,59 +33,85 @@ proc hexOrInt256(data: JsonNode, key: string, hex: static[bool]): Uint256 =
else: else:
parse(data[key].getStr, Uint256) parse(data[key].getStr, Uint256)
proc parseTests(name: string, hex: static[bool]): Tests = proc parseHash(data: string): Hash256 =
let fileName = "tests" / "fixtures" / "DifficultyTests" / "difficulty" & name & ".json" case data
let fixtures = parseJSON(readFile(fileName)) 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]() result = initTable[string, Tester]()
var t: Tester var t: Tester
for title, data in fixtures: for title, data in testData:
t.parentTimestamp = hexOrInt64(data, "parentTimestamp", hex) t.parentTimestamp = hexOrInt64(data, "parentTimestamp", hex)
t.parentDifficulty = hexOrInt256(data, "parentDifficulty", hex) t.parentDifficulty = hexOrInt256(data, "parentDifficulty", hex)
let pu = data.fields.getOrDefault("parentUncles") let pu = data.fields.getOrDefault("parentUncles")
if pu.isNil: if pu.isNil:
t.parentUncles = EMPTY_UNCLE_HASH t.parentUncles = EMPTY_UNCLE_HASH
else: else:
hexToByteArray(pu.getStr, t.parentUncles.data) t.parentUncles = parseHash(pu.getStr)
t.currentTimestamp = hexOrInt64(data, "currentTimestamp", hex) t.currentTimestamp = hexOrInt64(data, "currentTimestamp", hex)
t.currentBlockNumber = hexOrInt256(data, "currentBlockNumber", hex) t.currentBlockNumber = hexOrInt256(data, "currentBlockNumber", hex)
t.currentDifficulty = hexOrInt256(data, "currentDifficulty", hex) t.currentDifficulty = hexOrInt256(data, "currentDifficulty", hex)
result[title] = t result[title] = t
template runTests(name: string, hex: bool, calculator: typed) = proc calculator(revision: string, timestamp: EthTime, header: BlockHeader): DifficultyInt =
let testTitle = if name == "": "Difficulty" else: name case revision
test testTitle: of "Homestead": result = calcDifficultyHomestead(timestamp, header)
let data = parseTests(name, hex) of "GrayGlacier": result = calcDifficultyGrayGlacier(timestamp, header)
for title, t in data: of "Frontier": result = calcDifficultyFrontier(timestamp, header)
var p = BlockHeader( of "Berlin": result = calcDifficultyMuirGlacier(timestamp, header)
difficulty: t.parentDifficulty, of "Constantinople": result = calcDifficultyConstantinople(timestamp, header)
timestamp: times.fromUnix(t.parentTimestamp), of "Byzantium": result = calcDifficultyByzantium(timestamp, header)
blockNumber: t.currentBlockNumber - 1, of "ArrowGlacier": result = calcDifficultyArrowGlacier(timestamp, header)
ommersHash: t.parentUncles) else:
doAssert(false, "unknown revision: " & revision)
let diff = calculator(times.fromUnix(t.currentTimeStamp), p) 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
)
let timestamp = times.fromUnix(t.currentTimeStamp)
let diff = calculator(revision, timestamp, p)
check diff == t.currentDifficulty 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*() = 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": suite "DifficultyTest":
runTests("EIP2384_random_to20M", true, calcDifficultyMuirGlacier) runTest()
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)
when isMainModule: when isMainModule:
difficultyMain() difficultyMain()