2019-09-03 15:06:43 +00:00
|
|
|
# Nimbus
|
2024-01-09 15:15:19 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2019-09-03 15:06:43 +00:00
|
|
|
# Licensed under either of
|
2022-11-25 05:26:29 +00:00
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
2019-09-03 15:06:43 +00:00
|
|
|
|
|
|
|
import
|
2024-06-26 00:27:48 +00:00
|
|
|
std/json,
|
2022-12-02 04:39:12 +00:00
|
|
|
unittest2,
|
2023-01-31 12:38:08 +00:00
|
|
|
stew/byteutils,
|
2024-06-26 00:27:48 +00:00
|
|
|
./test_helpers,
|
|
|
|
./test_allowed_to_fail,
|
2024-06-16 03:21:02 +00:00
|
|
|
../nimbus/db/ledger,
|
2024-06-26 00:27:48 +00:00
|
|
|
../nimbus/core/chain/forked_chain,
|
2023-01-10 17:02:21 +00:00
|
|
|
../tools/common/helpers as chp,
|
|
|
|
../tools/evmstate/helpers,
|
2023-09-24 23:53:20 +00:00
|
|
|
../nimbus/common/common,
|
2024-06-26 00:27:48 +00:00
|
|
|
../nimbus/core/eip4844
|
2019-09-03 15:06:43 +00:00
|
|
|
|
2024-06-26 00:27:48 +00:00
|
|
|
const
|
|
|
|
debugMode = false
|
2019-09-03 15:06:43 +00:00
|
|
|
|
2024-06-26 00:27:48 +00:00
|
|
|
type
|
|
|
|
BlockDesc = object
|
|
|
|
blk: EthBlock
|
|
|
|
badBlock: bool
|
2019-09-03 15:06:43 +00:00
|
|
|
|
2024-06-26 00:27:48 +00:00
|
|
|
TestEnv = object
|
|
|
|
blocks: seq[BlockDesc]
|
2021-05-14 03:47:58 +00:00
|
|
|
genesisHeader: BlockHeader
|
2024-06-26 00:27:48 +00:00
|
|
|
lastBlockHash: Hash256
|
|
|
|
network: string
|
|
|
|
pre: JsonNode
|
2024-01-22 09:11:37 +00:00
|
|
|
|
2024-06-26 00:27:48 +00:00
|
|
|
proc parseBlocks(node: JsonNode): seq[BlockDesc] =
|
|
|
|
for x in node:
|
|
|
|
try:
|
|
|
|
let blockRLP = hexToSeqByte(x["rlp"].getStr)
|
|
|
|
let blk = rlp.decode(blockRLP, EthBlock)
|
|
|
|
result.add BlockDesc(
|
|
|
|
blk: blk,
|
|
|
|
badBlock: "expectException" in x,
|
|
|
|
)
|
|
|
|
except RlpError:
|
|
|
|
# invalid rlp will not participate in block validation
|
|
|
|
# e.g. invalid rlp received from network
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc parseEnv(node: JsonNode): TestEnv =
|
|
|
|
result.blocks = parseBlocks(node["blocks"])
|
|
|
|
let genesisRLP = hexToSeqByte(node["genesisRLP"].getStr)
|
|
|
|
result.genesisHeader = rlp.decode(genesisRLP, EthBlock).header
|
|
|
|
result.lastBlockHash = Hash256(data: hexToByteArray[32](node["lastblockhash"].getStr))
|
|
|
|
result.network = node["network"].getStr
|
|
|
|
result.pre = node["pre"]
|
|
|
|
|
|
|
|
proc rootExists(db: CoreDbRef; root: Hash256): bool =
|
2024-06-27 09:01:26 +00:00
|
|
|
let state = db.ctx.getAccounts().state(updateOk=true).valueOr:
|
2024-06-26 00:27:48 +00:00
|
|
|
return false
|
2024-06-27 09:01:26 +00:00
|
|
|
state == root
|
2024-06-26 00:27:48 +00:00
|
|
|
|
|
|
|
proc executeCase(node: JsonNode): bool =
|
2023-10-05 03:04:12 +00:00
|
|
|
let
|
2024-06-26 00:27:48 +00:00
|
|
|
env = parseEnv(node)
|
|
|
|
memDB = newCoreDbRef DefaultDbMemory
|
|
|
|
stateDB = LedgerRef.init(memDB, EMPTY_ROOT_HASH)
|
|
|
|
config = getChainConfig(env.network)
|
|
|
|
com = CommonRef.new(memDB, config)
|
|
|
|
|
|
|
|
setupStateDB(env.pre, stateDB)
|
|
|
|
stateDB.persist()
|
|
|
|
|
|
|
|
if not com.db.persistHeader(env.genesisHeader,
|
|
|
|
com.consensus == ConsensusType.POS):
|
|
|
|
debugEcho "Failed to put genesis header into database"
|
|
|
|
return false
|
|
|
|
|
|
|
|
if com.db.getCanonicalHead().blockHash != env.genesisHeader.blockHash:
|
2024-06-27 05:54:52 +00:00
|
|
|
debugEcho "Genesis block hash in database is different with expected genesis block hash"
|
2024-06-26 00:27:48 +00:00
|
|
|
return false
|
|
|
|
|
2024-06-27 05:54:52 +00:00
|
|
|
var c = newForkedChain(com, env.genesisHeader)
|
2024-06-26 00:27:48 +00:00
|
|
|
var lastStateRoot = env.genesisHeader.stateRoot
|
|
|
|
for blk in env.blocks:
|
|
|
|
let res = c.importBlock(blk.blk)
|
|
|
|
if res.isOk:
|
|
|
|
if env.lastBlockHash == blk.blk.header.blockHash:
|
|
|
|
lastStateRoot = blk.blk.header.stateRoot
|
|
|
|
if blk.badBlock:
|
|
|
|
debugEcho "A bug? bad block imported"
|
|
|
|
return false
|
2019-09-07 10:50:34 +00:00
|
|
|
else:
|
2024-06-26 00:27:48 +00:00
|
|
|
if not blk.badBlock:
|
|
|
|
debugEcho "A bug? good block rejected: ", res.error
|
|
|
|
return false
|
|
|
|
|
|
|
|
c.forkChoice(env.lastBlockHash, env.lastBlockHash).isOkOr:
|
|
|
|
debugEcho error
|
|
|
|
return false
|
|
|
|
|
|
|
|
let head = com.db.getCanonicalHead()
|
|
|
|
let headHash = head.blockHash
|
|
|
|
if headHash != env.lastBlockHash:
|
|
|
|
debugEcho "lastestBlockHash mismatch, get: ", headHash,
|
|
|
|
" expect: ", env.lastBlockHash
|
|
|
|
return false
|
|
|
|
|
|
|
|
if not memDB.rootExists(lastStateRoot):
|
|
|
|
debugEcho "Last stateRoot not exists"
|
|
|
|
return false
|
|
|
|
|
|
|
|
true
|
|
|
|
|
|
|
|
proc executeFile(node: JsonNode, testStatusIMPL: var TestStatus) =
|
|
|
|
for name, bctCase in node:
|
|
|
|
when debugMode:
|
|
|
|
debugEcho "TEST NAME: ", name
|
|
|
|
check executeCase(bctCase)
|
|
|
|
|
|
|
|
proc blockchainJsonMain*() =
|
2021-01-06 10:02:19 +00:00
|
|
|
const
|
2023-09-24 23:53:20 +00:00
|
|
|
legacyFolder = "eth_tests/LegacyTests/Constantinople/BlockchainTests"
|
2023-10-18 23:55:50 +00:00
|
|
|
newFolder = "eth_tests/BlockchainTests"
|
2021-01-06 10:02:19 +00:00
|
|
|
|
2024-06-27 05:54:52 +00:00
|
|
|
loadKzgTrustedSetup().isOkOr:
|
|
|
|
echo "FATAL: ", error
|
2023-10-05 03:04:12 +00:00
|
|
|
quit(QuitFailure)
|
|
|
|
|
2024-06-26 00:27:48 +00:00
|
|
|
if false:
|
|
|
|
suite "block chain json tests":
|
|
|
|
jsonTest(legacyFolder, "BlockchainTests", executeFile, skipBCTests)
|
2019-09-07 09:47:06 +00:00
|
|
|
else:
|
2024-06-26 00:27:48 +00:00
|
|
|
suite "new block chain json tests":
|
|
|
|
jsonTest(newFolder, "newBlockchainTests", executeFile, skipNewBCTests)
|
2019-09-07 09:47:06 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
2024-06-26 00:27:48 +00:00
|
|
|
when debugMode:
|
|
|
|
proc executeFile(name: string) =
|
2024-06-27 05:54:52 +00:00
|
|
|
loadKzgTrustedSetup().isOkOr:
|
|
|
|
echo "FATAL: ", error
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
2024-06-26 00:27:48 +00:00
|
|
|
var testStatusIMPL: TestStatus
|
|
|
|
let node = json.parseFile(name)
|
|
|
|
executeFile(node, testStatusIMPL)
|
2023-05-10 15:40:48 +00:00
|
|
|
|
2024-09-04 09:54:54 +00:00
|
|
|
executeFile("tests/fixtures/eth_tests/BlockchainTests/ValidBlocks/bcWalletTest/walletReorganizeOwners.json")
|
2019-09-07 09:47:06 +00:00
|
|
|
else:
|
2024-06-26 00:27:48 +00:00
|
|
|
blockchainJsonMain()
|