2024-01-22 09:11:37 +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
|
2024-06-08 08:05:00 +00:00
|
|
|
std/[json, os],
|
2024-01-22 09:11:37 +00:00
|
|
|
asynctest,
|
|
|
|
json_rpc/[rpcclient, rpcserver],
|
|
|
|
stew/byteutils,
|
|
|
|
../nimbus/core/chain,
|
|
|
|
../nimbus/common/common,
|
|
|
|
../nimbus/rpc,
|
2024-02-15 02:57:05 +00:00
|
|
|
../nimbus/db/[ledger, core_db],
|
2024-01-22 09:11:37 +00:00
|
|
|
./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())
|
2024-05-20 10:17:51 +00:00
|
|
|
memoryDB = newCoreDbRef DefaultDbMemory
|
2024-01-22 09:11:37 +00:00
|
|
|
config = chainConfigForNetwork(MainNet)
|
2024-05-20 10:17:51 +00:00
|
|
|
com = CommonRef.new(memoryDB, config)
|
2024-01-22 09:11:37 +00:00
|
|
|
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)
|
2024-01-22 09:11:37 +00:00
|
|
|
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])
|
2024-05-31 07:13:56 +00:00
|
|
|
doAssert validationResult.isOk()
|
2024-01-22 09:11:37 +00:00
|
|
|
|
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)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
2024-06-08 08:05:00 +00:00
|
|
|
proc checkAndValidateProofs(
|
2024-02-09 04:09:02 +00:00
|
|
|
db: CoreDbRef,
|
|
|
|
parentStateRoot: KeccakHash,
|
2024-01-22 09:11:37 +00:00
|
|
|
expectedStateRoot: KeccakHash,
|
|
|
|
proofs: seq[ProofResponse]) =
|
|
|
|
|
2024-02-09 04:09:02 +00:00
|
|
|
let
|
2024-05-29 11:06:49 +00:00
|
|
|
stateDB = LedgerRef.init(db, parentStateRoot)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
proofs.len() > 0
|
|
|
|
|
|
|
|
for proof in proofs:
|
|
|
|
let
|
|
|
|
address = proof.address.ethAddr()
|
2024-02-09 04:09:02 +00:00
|
|
|
balance = proof.balance
|
|
|
|
nonce = proof.nonce.uint64
|
|
|
|
codeHash = proof.codeHash.toHash256()
|
|
|
|
storageHash = proof.storageHash.toHash256()
|
2024-01-22 09:11:37 +00:00
|
|
|
slotProofs = proof.storageProof
|
|
|
|
|
2024-06-08 08:05:00 +00:00
|
|
|
# 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)
|
2024-02-09 04:09:02 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-06-02 19:21:29 +00:00
|
|
|
if codeHash == ZERO_HASH256 or codeHash == EMPTY_CODE_HASH:
|
2024-02-09 04:09:02 +00:00
|
|
|
check stateDB.getCode(address).len() == 0
|
2024-06-02 19:21:29 +00:00
|
|
|
check stateDB.getCodeHash(address) == EMPTY_CODE_HASH
|
2024-02-09 04:09:02 +00:00
|
|
|
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
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
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",
|
2024-01-24 17:18:45 +00:00
|
|
|
"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
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
var
|
2024-01-29 13:20:04 +00:00
|
|
|
rpcServer = newRpcHttpServerWithParams(initTAddress(RPC_HOST, RPC_PORT)).valueOr:
|
|
|
|
echo "Failed to create RPC server: ", error
|
|
|
|
quit(QuitFailure)
|
2024-01-24 17:18:45 +00:00
|
|
|
client = newRpcHttpClient()
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
rpcServer.start()
|
2024-01-24 17:18:45 +00:00
|
|
|
waitFor client.connect(RPC_HOST, rpcServer.localAddress[0].port, secure = false)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
|
2024-06-08 08:05:00 +00:00
|
|
|
test "exp_getProofsByBlockNumber - latest block pre-execution state":
|
2024-01-22 09:11:37 +00:00
|
|
|
for file in importFiles:
|
|
|
|
let (com, parentStateRoot, _, _) = importBlockDataFromFile(file)
|
|
|
|
|
|
|
|
setupExpRpc(com, rpcServer)
|
|
|
|
|
2024-06-08 08:05:00 +00:00
|
|
|
let proofs = await client.exp_getProofsByBlockNumber("latest", false)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
2024-06-08 08:05:00 +00:00
|
|
|
checkAndValidateProofs(com.db, parentStateRoot, parentStateRoot, proofs)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
2024-06-08 08:05:00 +00:00
|
|
|
test "exp_getProofsByBlockNumber - latest block post-execution state":
|
2024-01-22 09:11:37 +00:00
|
|
|
for file in importFiles:
|
2024-02-09 04:09:02 +00:00
|
|
|
let (com, parentStateRoot, stateRoot, _) = importBlockDataFromFile(file)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
setupExpRpc(com, rpcServer)
|
|
|
|
|
2024-06-08 08:05:00 +00:00
|
|
|
let proofs = await client.exp_getProofsByBlockNumber("latest", true)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
2024-06-08 08:05:00 +00:00
|
|
|
checkAndValidateProofs(com.db, parentStateRoot, stateRoot, proofs)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
2024-06-08 08:05:00 +00:00
|
|
|
test "exp_getProofsByBlockNumber - block by number pre-execution state":
|
2024-01-22 09:11:37 +00:00
|
|
|
for file in importFiles:
|
|
|
|
let
|
|
|
|
(com, parentStateRoot, _, blockNumber) = importBlockDataFromFile(file)
|
|
|
|
blockNum = blockId(blockNumber.truncate(uint64))
|
|
|
|
|
|
|
|
setupExpRpc(com, rpcServer)
|
|
|
|
|
2024-06-08 08:05:00 +00:00
|
|
|
let proofs = await client.exp_getProofsByBlockNumber(blockNum, false)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
2024-06-08 08:05:00 +00:00
|
|
|
checkAndValidateProofs(com.db, parentStateRoot, parentStateRoot, proofs)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
2024-06-08 08:05:00 +00:00
|
|
|
test "exp_getProofsByBlockNumber - block by number post-execution state":
|
2024-01-22 09:11:37 +00:00
|
|
|
for file in importFiles:
|
|
|
|
let
|
2024-02-09 04:09:02 +00:00
|
|
|
(com, parentStateRoot, stateRoot, blockNumber) = importBlockDataFromFile(file)
|
2024-01-22 09:11:37 +00:00
|
|
|
blockNum = blockId(blockNumber.truncate(uint64))
|
|
|
|
|
|
|
|
setupExpRpc(com, rpcServer)
|
|
|
|
|
2024-06-08 08:05:00 +00:00
|
|
|
let proofs = await client.exp_getProofsByBlockNumber(blockNum, true)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
2024-06-08 08:05:00 +00:00
|
|
|
checkAndValidateProofs(com.db, parentStateRoot, stateRoot, proofs)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
2024-06-08 08:05:00 +00:00
|
|
|
test "exp_getProofsByBlockNumber - block by number that doesn't exist":
|
2024-01-22 09:11:37 +00:00
|
|
|
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)
|
|
|
|
|
2024-01-24 17:18:45 +00:00
|
|
|
waitFor rpcServer.stop()
|
|
|
|
waitFor rpcServer.closeWait()
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
rpcExperimentalJsonMain()
|