nimbus-eth1/nimbus_verified_proxy/rpc/rpc_utils.nim

157 lines
5.4 KiB
Nim
Raw Normal View History

# nimbus_verified_proxy
# Copyright (c) 2022-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/typetraits,
eth/common/eth_types as etypes,
eth/rlp,
nimcrypto/hash,
stint,
Unified database frontend integration (#1670) * Nimbus folder environment update details: * Integrated `CoreDbRef` for the sources in the `nimbus` sub-folder. * The `nimbus` program does not compile yet as it needs the updates in the parallel `stateless` sub-folder. * Stateless environment update details: * Integrated `CoreDbRef` for the sources in the `stateless` sub-folder. * The `nimbus` program compiles now. * Premix environment update details: * Integrated `CoreDbRef` for the sources in the `premix` sub-folder. * Fluffy environment update details: * Integrated `CoreDbRef` for the sources in the `fluffy` sub-folder. * Tools environment update details: * Integrated `CoreDbRef` for the sources in the `tools` sub-folder. * Nodocker environment update details: * Integrated `CoreDbRef` for the sources in the `hive_integration/nodocker` sub-folder. * Tests environment update details: * Integrated `CoreDbRef` for the sources in the `tests` sub-folder. * The unit tests compile and run cleanly now. * Generalise `CoreDbRef` to any `select_backend` supported database why: Generalisation was just missed due to overcoming some compiler oddity which was tied to rocksdb for testing. * Suppress compiler warning for `newChainDB()` why: Warning was added to this function which must be wrapped so that any `CatchableError` is re-raised as `Defect`. * Split off persistent `CoreDbRef` constructor into separate file why: This allows to compile a memory only database version without linking the backend library. * Use memory `CoreDbRef` database by default detail: Persistent DB constructor needs to import `db/core_db/persistent why: Most tests use memory DB anyway. This avoids linking `-lrocksdb` or any other backend by default. * fix `toLegacyBackend()` availability check why: got garbled after memory/persistent split. * Clarify raw access to MPT for snap sync handler why: Logically, `kvt` is not the raw access for the hexary trie (although this holds for the legacy database)
2023-08-04 11:10:09 +00:00
web3,
web3/engine_api_types,
Unified database frontend integration (#1670) * Nimbus folder environment update details: * Integrated `CoreDbRef` for the sources in the `nimbus` sub-folder. * The `nimbus` program does not compile yet as it needs the updates in the parallel `stateless` sub-folder. * Stateless environment update details: * Integrated `CoreDbRef` for the sources in the `stateless` sub-folder. * The `nimbus` program compiles now. * Premix environment update details: * Integrated `CoreDbRef` for the sources in the `premix` sub-folder. * Fluffy environment update details: * Integrated `CoreDbRef` for the sources in the `fluffy` sub-folder. * Tools environment update details: * Integrated `CoreDbRef` for the sources in the `tools` sub-folder. * Nodocker environment update details: * Integrated `CoreDbRef` for the sources in the `hive_integration/nodocker` sub-folder. * Tests environment update details: * Integrated `CoreDbRef` for the sources in the `tests` sub-folder. * The unit tests compile and run cleanly now. * Generalise `CoreDbRef` to any `select_backend` supported database why: Generalisation was just missed due to overcoming some compiler oddity which was tied to rocksdb for testing. * Suppress compiler warning for `newChainDB()` why: Warning was added to this function which must be wrapped so that any `CatchableError` is re-raised as `Defect`. * Split off persistent `CoreDbRef` constructor into separate file why: This allows to compile a memory only database version without linking the backend library. * Use memory `CoreDbRef` database by default detail: Persistent DB constructor needs to import `db/core_db/persistent why: Most tests use memory DB anyway. This avoids linking `-lrocksdb` or any other backend by default. * fix `toLegacyBackend()` availability check why: got garbled after memory/persistent split. * Clarify raw access to MPT for snap sync handler why: Logically, `kvt` is not the raw access for the hexary trie (although this holds for the legacy database)
2023-08-04 11:10:09 +00:00
../../nimbus/db/core_db
type
FixedBytes[N: static int] = primitives.FixedBytes[N]
Address = primitives.Address
type ExecutionData* = object
parentHash*: BlockHash
feeRecipient*: Address
stateRoot*: BlockHash
receiptsRoot*: BlockHash
logsBloom*: FixedBytes[256]
prevRandao*: FixedBytes[32]
blockNumber*: Quantity
gasLimit*: Quantity
gasUsed*: Quantity
timestamp*: Quantity
extraData*: DynamicBytes[0, 32]
baseFeePerGas*: UInt256
blockHash*: BlockHash
transactions*: seq[TypedTransaction]
withdrawals*: seq[WithdrawalV1]
proc asExecutionData*(payload: SomeExecutionPayload): ExecutionData =
when payload is ExecutionPayloadV1:
return ExecutionData(
parentHash: payload.parentHash,
feeRecipient: payload.feeRecipient,
stateRoot: payload.stateRoot,
receiptsRoot: payload.receiptsRoot,
logsBloom: payload.logsBloom,
prevRandao: payload.prevRandao,
blockNumber: payload.blockNumber,
gasLimit: payload.gasLimit,
gasUsed: payload.gasUsed,
timestamp: payload.timestamp,
extraData: payload.extraData,
baseFeePerGas: payload.baseFeePerGas,
blockHash: payload.blockHash,
transactions: payload.transactions,
withdrawals: @[],
)
else:
# TODO: Deal with different payload types
return ExecutionData(
parentHash: payload.parentHash,
feeRecipient: payload.feeRecipient,
stateRoot: payload.stateRoot,
receiptsRoot: payload.receiptsRoot,
logsBloom: payload.logsBloom,
prevRandao: payload.prevRandao,
blockNumber: payload.blockNumber,
gasLimit: payload.gasLimit,
gasUsed: payload.gasUsed,
timestamp: payload.timestamp,
extraData: payload.extraData,
baseFeePerGas: payload.baseFeePerGas,
blockHash: payload.blockHash,
transactions: payload.transactions,
withdrawals: payload.withdrawals,
)
func toFixedBytes(d: MDigest[256]): FixedBytes[32] =
FixedBytes[32](d.data)
template asEthHash(hash: BlockHash): etypes.Hash256 =
etypes.Hash256(distinctBase(hash))
proc calculateTransactionData(
items: openArray[TypedTransaction]
): (etypes.Hash256, seq[TxOrHash], uint64) =
## returns tuple composed of
## - root of transactions trie
## - list of transactions hashes
## - total size of transactions in block
var tr = newCoreDbRef(DefaultDbMemory).ctx.getGeneric()
var txHashes: seq[TxOrHash]
var txSize: uint64
for i, t in items:
let tx = distinctBase(t)
txSize = txSize + uint64(len(tx))
2024-10-01 17:40:37 +00:00
tr.merge(rlp.encode(uint64 i), tx).expect "merge data"
txHashes.add(txOrHash keccakHash(tx))
Update storage tree admin (#2419) * Tighten `CoreDb` API for accounts why: Apart from cruft, the way to fetch the accounts state root via a `CoreDbColRef` record was unnecessarily complicated. * Extend `CoreDb` API for accounts to cover storage tries why: In future, this will make the notion of column objects obsolete. Storage trees will then be indexed by the account address rather than the vertex ID equivalent like a `CoreDbColRef`. * Apply new/extended accounts API to ledger and tests details: This makes the `distinct_ledger` module obsolete * Remove column object constructors why: They were needed as an abstraction of MPT sub-trees including storage trees. Now, storage trees are handled by the account (e.g. via address) they belong to and all other trees can be identified by a constant well known vertex ID. So there is no need for column objects anymore. Still there are some left-over column object methods wnich will be removed next. * Remove `serialise()` and `PayloadRef` from default Aristo API why: Not needed. `PayloadRef` was used for unstructured/unknown payload formats (account or blob) and `serialise()` was used for decodng `PayloadRef`. Now it is known in advance what the payload looks like. * Added query function `hasStorageData()` whether a storage area exists why: Useful for supporting `slotStateEmpty()` of the `CoreDb` API * In the `Ledger` replace `storage.stateEmpty()` by `slotStateEmpty()` * On Aristo, hide the storage root/vertex ID in the `PayloadRef` why: The storage vertex ID is fully controlled by Aristo while the `AristoAccount` object is controlled by the application. With the storage root part of the `AristoAccount` object, there was a useless administrative burden to keep that storage root field up to date. * Remove cruft, update comments etc. * Update changed MPT access paradigms why: Fixes verified proxy tests * Fluffy cosmetics
2024-06-27 09:01:26 +00:00
let rootHash = tr.state(updateOk = true).expect "hash"
(rootHash, txHashes, txSize)
func blockHeaderSize(payload: ExecutionData, txRoot: etypes.Hash256): uint64 =
let bh = etypes.BlockHeader(
parentHash: payload.parentHash.asEthHash,
ommersHash: etypes.EMPTY_UNCLE_HASH,
coinbase: etypes.EthAddress payload.feeRecipient,
stateRoot: payload.stateRoot.asEthHash,
transactionsRoot: txRoot,
receiptsRoot: payload.receiptsRoot.asEthHash,
logsBloom: distinctBase(payload.logsBloom).to(Bloom),
difficulty: default(etypes.DifficultyInt),
number: payload.blockNumber.distinctBase,
gasLimit: distinctBase(payload.gasLimit),
gasUsed: distinctBase(payload.gasUsed),
timestamp: payload.timestamp.EthTime,
extraData: bytes payload.extraData,
mixHash: payload.prevRandao,
nonce: default(etypes.BlockNonce),
baseFeePerGas: Opt.some payload.baseFeePerGas,
)
return uint64(len(rlp.encode(bh)))
proc asBlockObject*(p: ExecutionData): BlockObject {.raises: [ValueError].} =
# TODO: currently we always calculate txHashes as BlockObject does not have
# option of returning full transactions. It needs fixing at nim-web3 library
# level
let (txRoot, txHashes, txSize) = calculateTransactionData(p.transactions)
let headerSize = blockHeaderSize(p, txRoot)
let blockSize = txSize + headerSize
BlockObject(
number: web3.BlockNumber p.blockNumber,
hash: p.blockHash,
parentHash: p.parentHash,
sha3Uncles: etypes.EMPTY_UNCLE_HASH,
logsBloom: p.logsBloom,
transactionsRoot: txRoot,
stateRoot: p.stateRoot,
receiptsRoot: p.receiptsRoot,
miner: p.feeRecipient,
difficulty: UInt256.zero,
2023-06-04 06:00:50 +00:00
extraData: fromHex(DynamicBytes[0, 4096], p.extraData.toHex),
gasLimit: p.gasLimit,
gasUsed: p.gasUsed,
timestamp: p.timestamp,
nonce: Opt.some(default(FixedBytes[8])),
size: Quantity(blockSize),
# TODO: It does not matter what we put here after merge blocks.
# Other projects like `helios` return `0`, data providers like alchemy return
# transition difficulty. For now retruning `0` as this is a bit easier to do.
totalDifficulty: UInt256.zero,
transactions: txHashes,
uncles: @[],
baseFeePerGas: Opt.some(p.baseFeePerGas),
)