2024-01-22 09:11:37 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2024 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/tables,
|
|
|
|
stint,
|
|
|
|
eth/[common, rlp],
|
2024-05-30 12:54:03 +00:00
|
|
|
results,
|
2024-01-22 09:11:37 +00:00
|
|
|
../nimbus/db/[core_db, state_db],
|
|
|
|
./[tree_from_witness, witness_types]
|
|
|
|
|
|
|
|
export results
|
|
|
|
|
|
|
|
type
|
|
|
|
BlockWitness* = seq[byte]
|
|
|
|
|
|
|
|
AccountData* = object
|
|
|
|
account*: Account
|
|
|
|
code* : seq[byte]
|
|
|
|
storage*: Table[UInt256, UInt256]
|
|
|
|
|
|
|
|
proc buildAccountsTableFromKeys(
|
|
|
|
db: ReadOnlyStateDB,
|
|
|
|
keys: openArray[AccountAndSlots]): TableRef[EthAddress, AccountData] {.raises: [RlpError].} =
|
|
|
|
|
|
|
|
var accounts = newTable[EthAddress, AccountData]()
|
|
|
|
|
|
|
|
for key in keys:
|
|
|
|
let account = db.getAccount(key.address)
|
|
|
|
let code = if key.codeLen > 0:
|
2024-06-05 20:52:04 +00:00
|
|
|
db.db.newKvt().get(account.codeHash.data).valueOr: EmptyBlob
|
2024-01-22 09:11:37 +00:00
|
|
|
else: @[]
|
2024-06-10 09:05:30 +00:00
|
|
|
var storage = Table[UInt256, UInt256]()
|
2024-01-22 09:11:37 +00:00
|
|
|
|
2024-01-24 17:18:45 +00:00
|
|
|
for slot in key.slots:
|
|
|
|
let slotKey = fromBytesBE(UInt256, slot)
|
2024-06-05 20:52:04 +00:00
|
|
|
let rc = db.getStorage(key.address, slotKey)
|
|
|
|
if rc.isOK:
|
|
|
|
storage[slotKey] = rc.value
|
2024-01-22 09:11:37 +00:00
|
|
|
|
|
|
|
accounts[key.address] = AccountData(
|
2024-06-05 20:52:04 +00:00
|
|
|
account: account.to(Account),
|
2024-01-22 09:11:37 +00:00
|
|
|
code: code,
|
|
|
|
storage: storage)
|
|
|
|
|
|
|
|
return accounts
|
|
|
|
|
|
|
|
proc verifyWitness*(
|
|
|
|
trustedStateRoot: KeccakHash,
|
|
|
|
witness: BlockWitness,
|
|
|
|
flags: WitnessFlags): Result[TableRef[EthAddress, AccountData], string] =
|
|
|
|
if witness.len() == 0:
|
|
|
|
return err("witness is empty")
|
|
|
|
|
2024-05-20 10:17:51 +00:00
|
|
|
let db = newCoreDbRef(AristoDbMemory) # `AristoDbVoid` has smaller footprint
|
2024-01-22 09:11:37 +00:00
|
|
|
var tb = initTreeBuilder(witness, db, flags)
|
|
|
|
|
|
|
|
try:
|
|
|
|
let stateRoot = tb.buildTree()
|
|
|
|
if stateRoot != trustedStateRoot:
|
|
|
|
return err("witness stateRoot doesn't match trustedStateRoot")
|
|
|
|
|
2024-05-20 10:17:51 +00:00
|
|
|
let ac = newAccountStateDB(db, trustedStateRoot)
|
2024-01-22 09:11:37 +00:00
|
|
|
let accounts = buildAccountsTableFromKeys(ReadOnlyStateDB(ac), tb.keys)
|
|
|
|
ok(accounts)
|
|
|
|
except Exception as e:
|
|
|
|
err(e.msg)
|