2024-06-19 01:58:08 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2024 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * 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.
|
|
|
|
|
|
|
|
import
|
|
|
|
std/osproc,
|
|
|
|
eth/rlp,
|
2024-10-16 01:34:12 +00:00
|
|
|
eth/common/eth_types_rlp,
|
2024-06-19 01:58:08 +00:00
|
|
|
web3/conversions,
|
|
|
|
web3/engine_api_types,
|
|
|
|
../../nimbus/beacon/web3_eth_conv
|
|
|
|
|
|
|
|
const
|
2024-06-30 07:39:45 +00:00
|
|
|
testFile = "tests/fixtures/eth_tests/BlockchainTests/GeneralStateTests/Pyspecs/cancun/eip4844_blobs/fork_transition_excess_blob_gas.json"
|
2024-06-19 01:58:08 +00:00
|
|
|
#testFile = "tests/fixtures/eth_tests/BlockchainTests/ValidBlocks/bcRandomBlockhashTest/randomStatetest224BC.json"
|
|
|
|
#testFile = "tests/fixtures/eth_tests/BlockchainTests/ValidBlocks/bcRandomBlockhashTest/randomStatetest631BC.json"
|
2024-06-30 07:39:45 +00:00
|
|
|
#testFile = "tests/fixtures/eth_tests/BlockchainTests/ValidBlocks/bcStateTests/blockhashTests.json"
|
2024-06-19 01:58:08 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
BCTConv* = JrpcConv
|
|
|
|
|
|
|
|
BCTBlock* = object
|
|
|
|
rlp*: seq[byte]
|
|
|
|
|
|
|
|
BCTData* = object
|
|
|
|
blocks*: seq[BCTBlock]
|
|
|
|
genesisRLP*: seq[byte]
|
|
|
|
network*: string
|
|
|
|
pre*: JsonString
|
|
|
|
postState*: JsonString
|
|
|
|
|
|
|
|
BCTCase* = object
|
|
|
|
name*: string
|
|
|
|
data*: BCTData
|
|
|
|
|
|
|
|
BCTFile* = object
|
|
|
|
cases*: seq[BCTCase]
|
|
|
|
|
|
|
|
BCTHash* = object
|
|
|
|
number*: Quantity
|
2024-10-16 01:34:12 +00:00
|
|
|
hash: Hash32
|
2024-06-19 01:58:08 +00:00
|
|
|
|
|
|
|
BCTHashes* = object
|
|
|
|
hashes: seq[BCTHash]
|
|
|
|
|
|
|
|
BCTEnv* = object
|
2024-09-29 12:37:09 +00:00
|
|
|
currentCoinbase*: primitives.Address
|
2024-06-19 01:58:08 +00:00
|
|
|
currentDifficulty*: UInt256
|
2024-10-03 11:42:24 +00:00
|
|
|
currentRandom*: Opt[Bytes32]
|
2024-06-19 01:58:08 +00:00
|
|
|
parentDifficulty*: Opt[UInt256]
|
|
|
|
currentGasLimit*: Quantity
|
|
|
|
currentNumber*: Quantity
|
|
|
|
currentTimestamp*: Opt[Quantity]
|
|
|
|
parentTimestamp*: Opt[Quantity]
|
|
|
|
blockHashes*: BCTHashes
|
|
|
|
# ommers
|
|
|
|
currentBaseFee*: Opt[UInt256]
|
2024-10-16 01:34:12 +00:00
|
|
|
parentUncleHash*: Opt[Hash32]
|
2024-06-19 01:58:08 +00:00
|
|
|
parentBaseFee*: Opt[UInt256]
|
|
|
|
parentGasUsed*: Opt[Quantity]
|
|
|
|
parentGasLimit*: Opt[Quantity]
|
|
|
|
withdrawals*: Opt[seq[WithdrawalV1]]
|
|
|
|
currentBlobGasUsed*: Opt[Quantity]
|
|
|
|
currentExcessBlobGas*: Opt[Quantity]
|
|
|
|
parentBlobGasUsed*: Opt[Quantity]
|
|
|
|
parentExcessBlobGas*: Opt[Quantity]
|
2024-10-16 01:34:12 +00:00
|
|
|
parentBeaconBlockRoot*: Opt[Hash32]
|
2024-06-19 01:58:08 +00:00
|
|
|
|
|
|
|
BCTInput* = object
|
|
|
|
alloc: JsonString
|
|
|
|
env: BCTEnv
|
|
|
|
txsRlp: seq[byte]
|
|
|
|
|
|
|
|
BCTResult* = object
|
2024-10-16 01:34:12 +00:00
|
|
|
stateRoot*: Hash32
|
2024-06-19 01:58:08 +00:00
|
|
|
|
|
|
|
BCTOutput* = object
|
|
|
|
result*: BCTResult
|
|
|
|
alloc*: JsonString
|
|
|
|
|
|
|
|
BCTData.useDefaultReaderIn BCTConv
|
|
|
|
BCTBlock.useDefaultReaderIn BCTConv
|
|
|
|
BCTOutput.useDefaultReaderIn BCTConv
|
|
|
|
BCTResult.useDefaultReaderIn BCTConv
|
|
|
|
|
|
|
|
BCTEnv.useDefaultWriterIn BCTConv
|
|
|
|
BCTInput.useDefaultWriterIn BCTConv
|
|
|
|
|
|
|
|
proc readValue*(r: var JsonReader[BCTConv], val: var BCTFile)
|
|
|
|
{.gcsafe, raises: [IOError, SerializationError].} =
|
|
|
|
r.parseObject(key):
|
|
|
|
val.cases.add BCTCase(
|
|
|
|
name: key,
|
|
|
|
data: r.readValue(BCTData)
|
|
|
|
)
|
|
|
|
|
|
|
|
proc writeValue*(w: var JsonWriter[BCTConv], v: BCTHashes)
|
|
|
|
{.gcsafe, raises: [IOError].} =
|
|
|
|
w.writeObject():
|
|
|
|
for x in v.hashes:
|
|
|
|
w.writeField($x.number, x.hash)
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
func toBlocks(list: openArray[BCTBlock]): seq[Block] =
|
|
|
|
result = newSeqOfCap[Block](list.len)
|
2024-06-19 01:58:08 +00:00
|
|
|
for x in list:
|
2024-10-16 01:34:12 +00:00
|
|
|
result.add rlp.decode(x.rlp, Block)
|
2024-06-19 01:58:08 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc toBctEnv(parentBlock, currentBlock: Block, hashes: BCTHashes): BCTEnv =
|
2024-06-19 01:58:08 +00:00
|
|
|
let
|
|
|
|
parent = parentBlock.header
|
|
|
|
current = currentBlock.header
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
result.currentCoinbase = current.coinbase
|
2024-06-19 01:58:08 +00:00
|
|
|
result.currentGasLimit = w3Qty(current.gasLimit)
|
|
|
|
result.currentNumber = w3Qty(current.number)
|
|
|
|
result.currentTimestamp = Opt.some w3Qty(current.timestamp)
|
|
|
|
result.currentDifficulty = current.difficulty
|
2024-10-03 11:42:24 +00:00
|
|
|
result.currentRandom = Opt.some current.mixHash
|
2024-06-19 01:58:08 +00:00
|
|
|
|
2024-06-30 07:39:45 +00:00
|
|
|
# t8n should able to calculate these values itself if not supplied
|
|
|
|
#result.currentBaseFee = current.baseFeePerGas
|
|
|
|
#result.currentBlobGasUsed = w3Qty(current.blobGasUsed)
|
|
|
|
#result.currentExcessBlobGas = w3Qty(current.excessBlobGas)
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
result.parentBeaconBlockRoot = current.parentBeaconBlockRoot
|
2024-06-19 01:58:08 +00:00
|
|
|
result.parentDifficulty = Opt.some parent.difficulty
|
|
|
|
result.parentTimestamp = Opt.some w3Qty(parent.timestamp)
|
2024-10-16 01:34:12 +00:00
|
|
|
result.parentUncleHash = Opt.some parent.ommersHash
|
2024-06-19 01:58:08 +00:00
|
|
|
|
|
|
|
result.parentBaseFee = parent.baseFeePerGas
|
|
|
|
result.parentGasUsed = Opt.some w3Qty(parent.gasUsed)
|
|
|
|
result.parentGasLimit = Opt.some w3Qty(parent.gasLimit)
|
|
|
|
result.parentBlobGasUsed = w3Qty(parent.blobGasUsed)
|
|
|
|
result.parentExcessBlobGas = w3Qty(parent.excessBlobGas)
|
|
|
|
result.withdrawals = w3Withdrawals(currentBlock.withdrawals)
|
|
|
|
result.blockHashes = hashes
|
|
|
|
|
|
|
|
func toInput(prevAlloc: JsonString,
|
2024-10-16 01:34:12 +00:00
|
|
|
prevBlock, currBlock: Block,
|
2024-06-19 01:58:08 +00:00
|
|
|
hashes: BCTHashes): string =
|
|
|
|
let input = BCTInput(
|
|
|
|
alloc: prevAlloc,
|
|
|
|
env: toBctEnv(prevBlock, currBlock, hashes),
|
|
|
|
txsRlp: rlp.encode(currBlock.transactions),
|
|
|
|
)
|
|
|
|
BCTConv.encode(input)
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
func collectHashes(genesis: Block, blocks: openArray[Block]): BCTHashes =
|
2024-06-19 01:58:08 +00:00
|
|
|
result.hashes.add BCTHash(
|
|
|
|
number: w3Qty(genesis.header.number),
|
2024-10-16 01:34:12 +00:00
|
|
|
hash: rlpHash(genesis.header),
|
2024-06-19 01:58:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for blk in blocks:
|
|
|
|
result.hashes.add BCTHash(
|
|
|
|
number: w3Qty(blk.header.number),
|
2024-10-16 01:34:12 +00:00
|
|
|
hash: rlpHash(blk.header),
|
2024-06-19 01:58:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func eth(n: int): UInt256 {.compileTime.} =
|
|
|
|
n.u256 * pow(10.u256, 18)
|
|
|
|
|
|
|
|
const
|
|
|
|
eth5 = 5.eth
|
|
|
|
eth3 = 3.eth
|
|
|
|
eth2 = 2.eth
|
|
|
|
eth0 = 0.u256
|
|
|
|
|
|
|
|
const
|
|
|
|
BlockRewards = [
|
|
|
|
(eth5, "Frontier"),
|
|
|
|
(eth5, "Homestead"),
|
|
|
|
(eth5, "DAOFork"),
|
|
|
|
(eth5, "Tangerine"),
|
|
|
|
(eth5, "Spurious"),
|
|
|
|
(eth3, "Byzantium"),
|
|
|
|
(eth2, "Constantinople"),
|
|
|
|
(eth2, "Petersburg"),
|
|
|
|
(eth2, "Istanbul"),
|
|
|
|
(eth2, "MuirGlacier"),
|
|
|
|
(eth2, "Berlin"),
|
|
|
|
(eth2, "London"),
|
|
|
|
(eth2, "ArrowGlacier"),
|
|
|
|
(eth2, "GrayGlacier"),
|
|
|
|
(eth0, "MergeFork"),
|
|
|
|
(eth0, "Shanghai"),
|
|
|
|
(eth0, "Cancun"),
|
|
|
|
(eth0, "Prague"),
|
|
|
|
]
|
|
|
|
|
|
|
|
func blockReward(network: string): string =
|
|
|
|
for z in BlockRewards:
|
|
|
|
if network == z[1]:
|
|
|
|
return $z[0]
|
|
|
|
|
|
|
|
proc BCTMain() =
|
|
|
|
try:
|
|
|
|
let bctFile = BCTConv.loadFile(testFile, BCTFile, allowUnknownFields = true)
|
|
|
|
let cmd = "t8n --output.alloc stdout --output.result stdout --input.alloc stdin --input.env stdin --input.txs stdin --state.fork "
|
|
|
|
|
|
|
|
for c in bctFile.cases:
|
|
|
|
var
|
|
|
|
prevAlloc = c.data.pre
|
2024-10-16 01:34:12 +00:00
|
|
|
prevBlock = rlp.decode(c.data.genesisRLP, Block)
|
2024-06-19 01:58:08 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
blocks = toBlocks(c.data.blocks)
|
|
|
|
hashes = collectHashes(prevBlock, blocks)
|
|
|
|
|
|
|
|
debugEcho "NAME: ", c.name
|
|
|
|
for currBlock in blocks:
|
|
|
|
let input = toInput(prevAlloc, prevBlock, currBlock, hashes)
|
|
|
|
|
|
|
|
var cmdLine = cmd & c.data.network
|
|
|
|
let reward = blockReward(c.data.network)
|
|
|
|
if reward.len > 0:
|
|
|
|
cmdLine.add " --state.reward " & reward
|
|
|
|
|
|
|
|
let (output, exitCode) = execCmdEx(cmdLine, input = input)
|
|
|
|
prevBlock = currBlock
|
|
|
|
|
|
|
|
if exitCode != QuitSuccess:
|
|
|
|
debugEcho output
|
|
|
|
break
|
|
|
|
|
|
|
|
let
|
|
|
|
parsedOutput = BCTConv.decode(output, BCTOutput, allowUnknownFields = true)
|
2024-10-16 01:34:12 +00:00
|
|
|
stateRoot = parsedOutput.result.stateRoot
|
2024-06-19 01:58:08 +00:00
|
|
|
|
|
|
|
debugEcho "BlockNumber, EXITCODE, resultStateRoot, expectedStateRoot, status: ",
|
|
|
|
currBlock.header.number, ", ",
|
|
|
|
exitCode, ", ",
|
|
|
|
stateRoot, ", ",
|
|
|
|
currBlock.header.stateRoot, ", ",
|
|
|
|
if stateRoot != currBlock.header.stateRoot: "FAILED" else: "OK"
|
|
|
|
|
|
|
|
if stateRoot != currBlock.header.stateRoot:
|
|
|
|
debugEcho "STATE ROOT MISMATCH: ", currBlock.header.number
|
|
|
|
debugEcho "WANT: ", currBlock.header.stateRoot
|
|
|
|
debugEcho "GET: ", stateRoot
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
prevAlloc = parsedOutput.alloc
|
|
|
|
|
|
|
|
except SerializationError as exc:
|
|
|
|
debugEcho "Something error"
|
|
|
|
debugEcho exc.formatMsg(testFile)
|
|
|
|
|
|
|
|
BCTMain()
|