nimbus-eth1/tests/test_rpc_experimental_json.nim

245 lines
7.4 KiB
Nim
Raw Normal View History

# 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/[json, os],
asynctest,
json_rpc/[rpcclient, rpcserver],
stew/byteutils,
../nimbus/core/chain,
../nimbus/common/common,
../nimbus/rpc,
../nimbus/db/[ledger, core_db],
./rpc/experimental_rpc_client
type
Hash256 = eth_types.Hash256
func ethAddr*(x: Address): EthAddress =
EthAddress x
template toHash256(hash: untyped): Hash256 =
fromHex(Hash256, hash.toHex())
proc importBlockData(node: JsonNode): (CommonRef, Hash256, Hash256, UInt256) {. raises: [Exception].} =
var
blockNumber = UInt256.fromHex(node["blockNumber"].getStr())
memoryDB = newCoreDbRef DefaultDbMemory
config = chainConfigForNetwork(MainNet)
com = CommonRef.new(memoryDB, config)
state = node["state"]
for k, v in state:
let key = hexToSeqByte(k)
let value = hexToSeqByte(v.getStr())
memoryDB.kvt.put(key, value)
let
parentNumber = blockNumber - 1
parent = com.db.getBlockHeader(parentNumber)
Consolidate block type for block processing (#2325) This PR consolidates the split header-body sequences into a single EthBlock sequence and cleans up the fallout from that which significantly reduces block processing overhead during import thanks to less garbage collection and fewer copies of things all around. Notably, since the number of headers must always match the number of bodies, we also get rid of a pointless degree of freedom that in the future could introduce unnecessary bugs. * only read header and body from era file * avoid several unnecessary copies along the block processing way * simplify signatures, cleaning up unused arguemnts and returns * use `stew/assign2` in a few strategic places where the generated nim assignent is slow and add a few `move` to work around poor analysis in nim 1.6 (will need to be revisited for 2.0) ``` stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv bps_x bps_y tps_x tps_y bpsd tpsd timed block_number (498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92% (713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21% (928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28% (1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50% (1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80% (1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91% (1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91% (2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61% (2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80% blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s bpsd (mean): 19.55% tpsd (mean): 19.55% Time (total): -29m13s, -15.14% ```
2024-06-09 14:32:20 +00:00
blk = com.db.getEthBlock(blockNumber)
chain = newChain(com)
# it's ok if setHead fails here because of missing ancestors
discard com.db.setHead(parent, true)
Consolidate block type for block processing (#2325) This PR consolidates the split header-body sequences into a single EthBlock sequence and cleans up the fallout from that which significantly reduces block processing overhead during import thanks to less garbage collection and fewer copies of things all around. Notably, since the number of headers must always match the number of bodies, we also get rid of a pointless degree of freedom that in the future could introduce unnecessary bugs. * only read header and body from era file * avoid several unnecessary copies along the block processing way * simplify signatures, cleaning up unused arguemnts and returns * use `stew/assign2` in a few strategic places where the generated nim assignent is slow and add a few `move` to work around poor analysis in nim 1.6 (will need to be revisited for 2.0) ``` stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv bps_x bps_y tps_x tps_y bpsd tpsd timed block_number (498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92% (713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21% (928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28% (1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50% (1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80% (1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91% (1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91% (2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61% (2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80% blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s bpsd (mean): 19.55% tpsd (mean): 19.55% Time (total): -29m13s, -15.14% ```
2024-06-09 14:32:20 +00:00
let validationResult = chain.persistBlocks([blk])
doAssert validationResult.isOk()
Consolidate block type for block processing (#2325) This PR consolidates the split header-body sequences into a single EthBlock sequence and cleans up the fallout from that which significantly reduces block processing overhead during import thanks to less garbage collection and fewer copies of things all around. Notably, since the number of headers must always match the number of bodies, we also get rid of a pointless degree of freedom that in the future could introduce unnecessary bugs. * only read header and body from era file * avoid several unnecessary copies along the block processing way * simplify signatures, cleaning up unused arguemnts and returns * use `stew/assign2` in a few strategic places where the generated nim assignent is slow and add a few `move` to work around poor analysis in nim 1.6 (will need to be revisited for 2.0) ``` stats-20240607_2223-a814aa0b.csv vs stats-20240608_0714-21c1d0a9.csv bps_x bps_y tps_x tps_y bpsd tpsd timed block_number (498305, 713245] 1,540.52 1,809.73 2,361.58 2775.340189 17.63% 17.63% -14.92% (713245, 928185] 730.36 865.26 1,715.90 2028.973852 18.01% 18.01% -15.21% (928185, 1143126] 663.03 789.10 2,529.26 3032.490771 19.79% 19.79% -16.28% (1143126, 1358066] 393.46 508.05 2,152.50 2777.578119 29.13% 29.13% -22.50% (1358066, 1573007] 370.88 440.72 2,351.31 2791.896052 18.81% 18.81% -15.80% (1573007, 1787947] 283.65 335.11 2,068.93 2441.373402 17.60% 17.60% -14.91% (1787947, 2002888] 287.29 342.11 2,078.39 2474.179448 18.99% 18.99% -15.91% (2002888, 2217828] 293.38 343.16 2,208.83 2584.77457 17.16% 17.16% -14.61% (2217828, 2432769] 140.09 167.86 1,081.87 1296.336926 18.82% 18.82% -15.80% blocks: 1934464, baseline: 3h13m1s, contender: 2h43m47s bpsd (mean): 19.55% tpsd (mean): 19.55% Time (total): -29m13s, -15.14% ```
2024-06-09 14:32:20 +00:00
return (com, parent.stateRoot, blk.header.stateRoot, blockNumber)
proc checkAndValidateProofs(
db: CoreDbRef,
parentStateRoot: KeccakHash,
expectedStateRoot: KeccakHash,
proofs: seq[ProofResponse]) =
let
stateDB = LedgerRef.init(db, parentStateRoot)
check:
proofs.len() > 0
for proof in proofs:
let
address = proof.address.ethAddr()
balance = proof.balance
nonce = proof.nonce.uint64
codeHash = proof.codeHash.toHash256()
storageHash = proof.storageHash.toHash256()
slotProofs = proof.storageProof
# TODO: Fix this test. Update the code by checking if codeHash has changed
# and calling eth_getCode to set the updated code in the stateDB
# if code.len() > 0:
# stateDB.setCode(address, code)
stateDB.setBalance(address, balance)
stateDB.setNonce(address, nonce)
for slotProof in slotProofs:
stateDB.setStorage(address, slotProof.key, slotProof.value)
# the account doesn't exist due to a self destruct
if codeHash == ZERO_HASH256 and storageHash == ZERO_HASH256:
stateDB.deleteAccount(address)
stateDB.persist()
check stateDB.getBalance(address) == balance
check stateDB.getNonce(address) == nonce
if codeHash == ZERO_HASH256 or codeHash == EMPTY_CODE_HASH:
check stateDB.getCode(address).len() == 0
check stateDB.getCodeHash(address) == EMPTY_CODE_HASH
else:
check stateDB.getCodeHash(address) == codeHash
if storageHash == ZERO_HASH256 or storageHash == EMPTY_ROOT_HASH:
check stateDB.getStorageRoot(address) == EMPTY_ROOT_HASH
else:
check stateDB.getStorageRoot(address) == storageHash
check stateDB.rootHash == expectedStateRoot
proc importBlockDataFromFile(file: string): (CommonRef, Hash256, Hash256, UInt256) {. raises: [].} =
try:
let
fileJson = json.parseFile("tests" / "fixtures" / "PersistBlockTests" / file)
return importBlockData(fileJson)
except Exception as ex:
doAssert false, ex.msg
proc rpcExperimentalJsonMain*() =
suite "rpc experimental json tests":
let importFiles = [
"block97.json",
"block98.json",
"block46147.json",
"block46400.json",
"block46402.json",
"block47205.json",
"block47216.json",
"block48712.json",
"block48915.json",
"block49018.json",
"block49439.json",
"block49891.json",
"block50111.json",
"block78458.json",
"block81383.json",
"block81666.json",
"block85858.json",
"block146675.json",
"block116524.json",
"block196647.json",
"block226147.json",
"block226522.json",
"block231501.json",
"block243826.json",
"block248032.json",
"block299804.json",
"block420301.json",
"block512335.json",
"block652148.json",
"block668910.json",
"block1017395.json",
"block1149150.json",
"block1155095.json",
"block1317742.json",
"block1352922.json",
"block1368834.json",
"block1417555.json",
"block1431916.json",
"block1487668.json",
"block1920000.json",
"block1927662.json",
"block2463413.json",
"block2675000.json",
"block2675002.json",
"block4370000.json"
]
let
RPC_HOST = "127.0.0.1"
RPC_PORT = 0 # let the OS choose a port
var
rpcServer = newRpcHttpServerWithParams(initTAddress(RPC_HOST, RPC_PORT)).valueOr:
echo "Failed to create RPC server: ", error
quit(QuitFailure)
client = newRpcHttpClient()
rpcServer.start()
waitFor client.connect(RPC_HOST, rpcServer.localAddress[0].port, secure = false)
test "exp_getProofsByBlockNumber - latest block pre-execution state":
for file in importFiles:
let (com, parentStateRoot, _, _) = importBlockDataFromFile(file)
setupExpRpc(com, rpcServer)
let proofs = await client.exp_getProofsByBlockNumber("latest", false)
checkAndValidateProofs(com.db, parentStateRoot, parentStateRoot, proofs)
test "exp_getProofsByBlockNumber - latest block post-execution state":
for file in importFiles:
let (com, parentStateRoot, stateRoot, _) = importBlockDataFromFile(file)
setupExpRpc(com, rpcServer)
let proofs = await client.exp_getProofsByBlockNumber("latest", true)
checkAndValidateProofs(com.db, parentStateRoot, stateRoot, proofs)
test "exp_getProofsByBlockNumber - block by number pre-execution state":
for file in importFiles:
let
(com, parentStateRoot, _, blockNumber) = importBlockDataFromFile(file)
blockNum = blockId(blockNumber.truncate(uint64))
setupExpRpc(com, rpcServer)
let proofs = await client.exp_getProofsByBlockNumber(blockNum, false)
checkAndValidateProofs(com.db, parentStateRoot, parentStateRoot, proofs)
test "exp_getProofsByBlockNumber - block by number post-execution state":
for file in importFiles:
let
(com, parentStateRoot, stateRoot, blockNumber) = importBlockDataFromFile(file)
blockNum = blockId(blockNumber.truncate(uint64))
setupExpRpc(com, rpcServer)
let proofs = await client.exp_getProofsByBlockNumber(blockNum, true)
checkAndValidateProofs(com.db, parentStateRoot, stateRoot, proofs)
test "exp_getProofsByBlockNumber - block by number that doesn't exist":
for file in importFiles:
let
(com, _, _, blockNumber) = importBlockDataFromFile(file)
blockNum = blockId(blockNumber.truncate(uint64) + 1) # doesn't exist
setupExpRpc(com, rpcServer)
expect JsonRpcError:
discard await client.exp_getProofsByBlockNumber(blockNum, false)
expect JsonRpcError:
discard await client.exp_getProofsByBlockNumber(blockNum, true)
waitFor rpcServer.stop()
waitFor rpcServer.closeWait()
when isMainModule:
rpcExperimentalJsonMain()