refactor vmConfiguration in test_blockchain_json

instead of using it's own blockNumber to Fork calculation,
we switch to ChainConfig to Fork to ensure smooth transition
when using common block validator.
This commit is contained in:
jangko 2021-05-14 12:19:49 +07:00
parent beb274d98c
commit db65f92e66
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
1 changed files with 72 additions and 78 deletions

View File

@ -24,43 +24,40 @@ type
NoProof NoProof
Ethash Ethash
VMConfig = array[2, tuple[blockNumber: int, fork: Fork]]
EthBlock = object EthBlock = object
header: BlockHeader header : BlockHeader
transactions: seq[Transaction] transactions: seq[Transaction]
uncles: seq[BlockHeader] uncles : seq[BlockHeader]
TestBlock = object TestBlock = object
goodBlock: bool goodBlock: bool
blockRLP: Blob blockRLP : Blob
Tester = object Tester = object
lastBlockHash: Hash256 lastBlockHash: Hash256
genesisHeader: BlockHeader genesisHeader: BlockHeader
blocks: seq[TestBlock] blocks : seq[TestBlock]
sealEngine: Option[SealEngine] sealEngine : Option[SealEngine]
vmConfig: VMConfig debugMode : bool
debugMode: bool trace : bool
trace: bool vmState : BaseVMState
vmState: BaseVMState debugData : JsonNode
debugData: JsonNode network : string
network: string
MiningHeader = object MiningHeader = object
parentHash: Hash256 parentHash : Hash256
ommersHash: Hash256 ommersHash : Hash256
coinbase: EthAddress coinbase : EthAddress
stateRoot: Hash256 stateRoot : Hash256
txRoot: Hash256 txRoot : Hash256
receiptRoot: Hash256 receiptRoot : Hash256
bloom: common.BloomFilter bloom : common.BloomFilter
difficulty: DifficultyInt difficulty : DifficultyInt
blockNumber: BlockNumber blockNumber : BlockNumber
gasLimit: GasInt gasLimit : GasInt
gasUsed: GasInt gasUsed : GasInt
timestamp: EthTime timestamp : EthTime
extraData: Blob extraData : Blob
proc testFixture(node: JsonNode, testStatusIMPL: var TestStatus, debugMode = false, trace = false) proc testFixture(node: JsonNode, testStatusIMPL: var TestStatus, debugMode = false, trace = false)
@ -123,76 +120,73 @@ proc parseBlocks(blocks: JsonNode): seq[TestBlock] =
result.add t result.add t
func vmConfiguration(network: string, c: var ChainConfig): VMConfig = func vmConfiguration(network: string, c: var ChainConfig) =
const
H = high(BlockNumber)
Zero = 0.toBlockNumber
Five = 5.toBlockNumber
c.homesteadBlock = high(BlockNumber) proc assignNumber(c: var ChainConfig,
c.daoForkBlock = high(BlockNumber) fork: Fork, n: BlockNumber) =
c.daoForkSupport = false var number: array[Fork, BlockNumber]
c.eip150Block = high(BlockNumber) var z = low(Fork)
c.eip158Block = high(BlockNumber) while z < fork:
c.byzantiumBlock = high(BlockNumber) number[z] = Zero
c.constantinopleBlock = high(BlockNumber) z = z.succ
c.petersburgBlock = high(BlockNumber) number[fork] = n
c.istanbulBlock = high(BlockNumber) z = high(Fork)
c.muirGlacierBlock = high(BlockNumber) while z > fork:
number[z] = H
z = z.pred
c.daoForkSupport = false
c.homesteadBlock = number[FkHomestead]
c.daoForkBlock = number[FkHomestead]
c.eip150Block = number[FkTangerine]
c.eip155Block = number[FkSpurious]
c.eip158Block = number[FkSpurious]
c.byzantiumBlock = number[FkByzantium]
c.constantinopleBlock = number[FkConstantinople]
c.petersburgBlock = number[FkPetersburg]
c.istanbulBlock = number[FkIstanbul]
c.muirGlacierBlock = number[FkBerlin]
c.berlinBlock = number[FkBerlin]
case network case network
of "EIP150": of "EIP150":
result = [(0, FkTangerine), (0, FkTangerine)] c.assignNumber(FkTangerine, Zero)
c.eip150Block = 0.toBlockNumber
of "ConstantinopleFix": of "ConstantinopleFix":
result = [(0, FkPetersburg), (0, FkPetersburg)] c.assignNumber(FkPetersburg, Zero)
c.petersburgBlock = 0.toBlockNumber
of "Homestead": of "Homestead":
result = [(0, FkHomestead), (0, FkHomestead)] c.assignNumber(FkHomestead, Zero)
c.homesteadBlock = 0.toBlockNumber
of "Frontier": of "Frontier":
result = [(0, FkFrontier), (0, FkFrontier)] c.assignNumber(FkFrontier, Zero)
#c.frontierBlock = 0.toBlockNumber
of "Byzantium": of "Byzantium":
result = [(0, FkByzantium), (0, FkByzantium)] c.assignNumber(FkByzantium, Zero)
c.byzantiumBlock = 0.toBlockNumber
of "EIP158ToByzantiumAt5": of "EIP158ToByzantiumAt5":
result = [(0, FkSpurious), (5, FkByzantium)] c.assignNumber(FkByzantium, Five)
c.eip158Block = 0.toBlockNumber
c.byzantiumBlock = 5.toBlockNumber
of "EIP158": of "EIP158":
result = [(0, FkSpurious), (0, FkSpurious)] c.assignNumber(FkSpurious, Zero)
c.eip158Block = 0.toBlockNumber
of "HomesteadToDaoAt5": of "HomesteadToDaoAt5":
result = [(0, FkHomestead), (5, FkHomestead)] c.assignNumber(FkHomestead, Zero)
c.homesteadBlock = 0.toBlockNumber c.daoForkBlock = Five
c.daoForkBlock = 5.toBlockNumber
c.daoForkSupport = true c.daoForkSupport = true
of "Constantinople": of "Constantinople":
result = [(0, FkConstantinople), (0, FkConstantinople)] c.assignNumber(FkConstantinople, Zero)
c.constantinopleBlock = 0.toBlockNumber
of "HomesteadToEIP150At5": of "HomesteadToEIP150At5":
result = [(0, FkHomestead), (5, FkTangerine)] c.assignNumber(FkTangerine, Five)
c.homesteadBlock = 0.toBlockNumber
c.eip150Block = 5.toBlockNumber
of "FrontierToHomesteadAt5": of "FrontierToHomesteadAt5":
result = [(0, FkFrontier), (5, FkHomestead)] c.assignNumber(FkHomestead, Five)
#c.frontierBlock = 0.toBlockNumber
c.homesteadBlock = 5.toBlockNumber
of "ByzantiumToConstantinopleFixAt5": of "ByzantiumToConstantinopleFixAt5":
result = [(0, FkByzantium), (5, FkPetersburg)] c.assignNumber(FkPetersburg, Five)
c.byzantiumBlock = 0.toBlockNumber c.constantinopleBlock = Five
c.petersburgBlock = 5.toBlockNumber
of "Istanbul": of "Istanbul":
result = [(0, FkIstanbul), (0, FkIstanbul)] c.assignNumber(FkIstanbul, Zero)
c.istanbulBlock = 0.toBlockNumber
of "Berlin": of "Berlin":
result = [(0, FkBerlin), (0, FkBerlin)] c.assignNumber(FkBerlin, Zero)
c.berlinBlock = 0.toBlockNumber
else: else:
raise newException(ValueError, "unsupported network " & network) raise newException(ValueError, "unsupported network " & network)
func vmConfigToFork(vmConfig: VMConfig, blockNumber: Uint256): Fork =
if blockNumber >= vmConfig[1].blockNumber.u256: return vmConfig[1].fork
if blockNumber >= vmConfig[0].blockNumber.u256: return vmConfig[0].fork
raise newException(ValueError, "unreachable code")
proc parseTester(fixture: JsonNode, testStatusIMPL: var TestStatus): Tester = proc parseTester(fixture: JsonNode, testStatusIMPL: var TestStatus): Tester =
result.blocks = parseBlocks(fixture["blocks"]) result.blocks = parseBlocks(fixture["blocks"])
@ -547,11 +541,11 @@ proc applyFixtureBlockToChain(tester: var Tester, tb: TestBlock,
chainDB: BaseChainDB, checkSeal, validation = true): (EthBlock, EthBlock, Blob) = chainDB: BaseChainDB, checkSeal, validation = true): (EthBlock, EthBlock, Blob) =
# we hack the ChainConfig here and let it works with calcDifficulty # we hack the ChainConfig here and let it works with calcDifficulty
tester.vmConfig = vmConfiguration(tester.network, chainDB.config) vmConfiguration(tester.network, chainDB.config)
var var
preminedBlock = rlp.decode(tb.blockRLP, EthBlock) preminedBlock = rlp.decode(tb.blockRLP, EthBlock)
fork = vmConfigToFork(tester.vmConfig, preminedBlock.header.blockNumber) fork = toFork(chainDB.config, preminedBlock.header.blockNumber)
minedBlock = tester.importBlock(chainDB, preminedBlock, fork, checkSeal, validation) minedBlock = tester.importBlock(chainDB, preminedBlock, fork, checkSeal, validation)
rlpEncodedMinedBlock = rlp.encode(minedBlock) rlpEncodedMinedBlock = rlp.encode(minedBlock)
result = (preminedBlock, minedBlock, rlpEncodedMinedBlock) result = (preminedBlock, minedBlock, rlpEncodedMinedBlock)
@ -708,7 +702,7 @@ proc blockchainJsonMain*(debugMode = false) =
let path = "tests" / "fixtures" / folder let path = "tests" / "fixtures" / folder
let n = json.parseFile(path / config.testSubject) let n = json.parseFile(path / config.testSubject)
var testStatusIMPL: TestStatus var testStatusIMPL: TestStatus
testFixture(n, testStatusIMPL, debugMode = true, config.trace) testFixture(n, testStatusIMPL, debugMode = false, config.trace)
when isMainModule: when isMainModule:
var message: string var message: string