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
|
|
|
|
std/[json, os, tables],
|
|
|
|
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
|
|
|
../stateless/[witness_verification, witness_types],
|
|
|
|
./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)
|
|
|
|
header = com.db.getBlockHeader(blockNumber)
|
|
|
|
headerHash = header.blockHash
|
|
|
|
blockBody = com.db.getBlockBody(headerHash)
|
|
|
|
chain = newChain(com)
|
|
|
|
headers = @[header]
|
|
|
|
bodies = @[blockBody]
|
|
|
|
|
|
|
|
# it's ok if setHead fails here because of missing ancestors
|
|
|
|
discard com.db.setHead(parent, true)
|
|
|
|
let validationResult = chain.persistBlocks(headers, bodies)
|
|
|
|
doAssert validationResult == ValidationResult.OK
|
|
|
|
|
|
|
|
return (com, parent.stateRoot, header.stateRoot, blockNumber)
|
|
|
|
|
|
|
|
proc checkAndValidateWitnessAgainstProofs(
|
2024-02-09 04:09:02 +00:00
|
|
|
db: CoreDbRef,
|
|
|
|
parentStateRoot: KeccakHash,
|
2024-01-22 09:11:37 +00:00
|
|
|
expectedStateRoot: KeccakHash,
|
|
|
|
witness: seq[byte],
|
|
|
|
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-02-09 04:09:02 +00:00
|
|
|
verifyWitnessResult = verifyWitness(expectedStateRoot, witness, {wfNoFlag})
|
|
|
|
|
2024-01-22 09:11:37 +00:00
|
|
|
check verifyWitnessResult.isOk()
|
|
|
|
let witnessData = verifyWitnessResult.value()
|
|
|
|
|
|
|
|
check:
|
|
|
|
witness.len() > 0
|
|
|
|
proofs.len() > 0
|
|
|
|
witnessData.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-02-09 04:09:02 +00:00
|
|
|
if witnessData.contains(address):
|
|
|
|
let
|
|
|
|
storageData = witnessData[address].storage
|
|
|
|
code = witnessData[address].code
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
check:
|
2024-02-09 04:09:02 +00:00
|
|
|
witnessData[address].account.balance == balance
|
|
|
|
witnessData[address].account.nonce == nonce
|
|
|
|
witnessData[address].account.codeHash == codeHash
|
|
|
|
|
|
|
|
for slotProof in slotProofs:
|
|
|
|
if storageData.contains(slotProof.key):
|
|
|
|
check storageData[slotProof.key] == slotProof.value
|
|
|
|
|
|
|
|
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_SHA3:
|
|
|
|
check stateDB.getCode(address).len() == 0
|
|
|
|
check stateDB.getCodeHash(address) == EMPTY_SHA3
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
test "exp_getWitnessByBlockNumber and exp_getProofsByBlockNumber - latest block pre-execution state":
|
|
|
|
for file in importFiles:
|
|
|
|
let (com, parentStateRoot, _, _) = importBlockDataFromFile(file)
|
|
|
|
|
|
|
|
setupExpRpc(com, rpcServer)
|
|
|
|
|
|
|
|
let
|
|
|
|
witness = await client.exp_getWitnessByBlockNumber("latest", false)
|
|
|
|
proofs = await client.exp_getProofsByBlockNumber("latest", false)
|
|
|
|
|
2024-02-09 04:09:02 +00:00
|
|
|
checkAndValidateWitnessAgainstProofs(com.db, parentStateRoot, parentStateRoot, witness, proofs)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
test "exp_getWitnessByBlockNumber and exp_getProofsByBlockNumber - latest block post-execution state":
|
|
|
|
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)
|
|
|
|
|
|
|
|
let
|
|
|
|
witness = await client.exp_getWitnessByBlockNumber("latest", true)
|
|
|
|
proofs = await client.exp_getProofsByBlockNumber("latest", true)
|
|
|
|
|
2024-02-09 04:09:02 +00:00
|
|
|
checkAndValidateWitnessAgainstProofs(com.db, parentStateRoot, stateRoot, witness, proofs)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
test "exp_getWitnessByBlockNumber and 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
|
|
|
|
witness = await client.exp_getWitnessByBlockNumber(blockNum, false)
|
|
|
|
proofs = await client.exp_getProofsByBlockNumber(blockNum, false)
|
|
|
|
|
2024-02-09 04:09:02 +00:00
|
|
|
checkAndValidateWitnessAgainstProofs(com.db, parentStateRoot, parentStateRoot, witness, proofs)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
test "exp_getWitnessByBlockNumber and exp_getProofsByBlockNumber - block by number post-execution state":
|
|
|
|
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)
|
|
|
|
|
|
|
|
let
|
|
|
|
witness = await client.exp_getWitnessByBlockNumber(blockNum, true)
|
|
|
|
proofs = await client.exp_getProofsByBlockNumber(blockNum, true)
|
|
|
|
|
2024-02-09 04:09:02 +00:00
|
|
|
checkAndValidateWitnessAgainstProofs(com.db, parentStateRoot, stateRoot, witness, proofs)
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
test "exp_getWitnessByBlockNumber and 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_getWitnessByBlockNumber(blockNum, false)
|
|
|
|
|
|
|
|
expect JsonRpcError:
|
|
|
|
discard await client.exp_getProofsByBlockNumber(blockNum, false)
|
|
|
|
|
|
|
|
expect JsonRpcError:
|
|
|
|
discard await client.exp_getWitnessByBlockNumber(blockNum, true)
|
|
|
|
|
|
|
|
expect JsonRpcError:
|
|
|
|
discard await client.exp_getProofsByBlockNumber(blockNum, true)
|
|
|
|
|
2024-01-24 17:18:45 +00:00
|
|
|
test "Contract storage updated - bytecode should exist in witness":
|
|
|
|
for file in importFiles:
|
|
|
|
let
|
2024-02-16 09:08:07 +00:00
|
|
|
(com, parentStateRoot, _, blockNumber) = importBlockDataFromFile(file)
|
2024-01-24 17:18:45 +00:00
|
|
|
blockNum = blockId(blockNumber.truncate(uint64))
|
|
|
|
|
|
|
|
setupExpRpc(com, rpcServer)
|
|
|
|
|
|
|
|
let
|
|
|
|
witness = await client.exp_getWitnessByBlockNumber(blockNum, false)
|
|
|
|
proofs = await client.exp_getProofsByBlockNumber(blockNum, true)
|
|
|
|
verifyWitnessResult = verifyWitness(parentStateRoot, witness, {wfNoFlag})
|
|
|
|
|
|
|
|
check verifyWitnessResult.isOk()
|
|
|
|
let witnessData = verifyWitnessResult.value()
|
|
|
|
|
|
|
|
for proof in proofs:
|
|
|
|
let address = ethAddr(proof.address)
|
2024-02-09 04:09:02 +00:00
|
|
|
# if the storage was read or updated on an existing contract
|
2024-01-24 17:18:45 +00:00
|
|
|
if proof.storageProof.len() > 0 and witnessData.contains(address):
|
|
|
|
check witnessData[address].code.len() > 0
|
|
|
|
|
|
|
|
waitFor rpcServer.stop()
|
|
|
|
waitFor rpcServer.closeWait()
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
rpcExperimentalJsonMain()
|