2020-10-26 13:58:37 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package eth
|
|
|
|
|
|
|
|
import (
|
2022-12-19 08:42:23 +00:00
|
|
|
"bytes"
|
2020-10-28 13:22:57 +00:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-10-26 13:58:37 +00:00
|
|
|
"math/big"
|
|
|
|
|
2023-09-21 06:55:26 +00:00
|
|
|
nodeiter "github.com/cerc-io/eth-iterator-utils"
|
|
|
|
"github.com/cerc-io/plugeth-statediff/utils"
|
2020-10-28 13:22:57 +00:00
|
|
|
"github.com/ethereum/go-ethereum"
|
2020-10-26 13:58:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2022-12-19 08:42:23 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2024-05-29 12:29:55 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2020-10-26 13:58:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2021-06-18 06:42:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2022-12-19 08:42:23 +00:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2023-04-14 06:26:46 +00:00
|
|
|
|
|
|
|
"github.com/cerc-io/ipld-eth-statedb/trie_by_cid/state"
|
2020-10-26 13:58:37 +00:00
|
|
|
)
|
|
|
|
|
2022-12-19 08:42:23 +00:00
|
|
|
var nullHashBytes = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")
|
|
|
|
var emptyCodeHash = crypto.Keccak256([]byte{})
|
|
|
|
|
2024-05-29 12:29:55 +00:00
|
|
|
// These marshalling functions are from internal/ethapi so we have to make our own versions here:
|
|
|
|
|
2020-10-26 13:58:37 +00:00
|
|
|
// RPCMarshalHeader converts the given header to the RPC output.
|
2021-10-07 09:35:11 +00:00
|
|
|
func RPCMarshalHeader(head *types.Header) map[string]interface{} {
|
2024-05-29 12:29:55 +00:00
|
|
|
result := map[string]interface{}{
|
2020-10-26 13:58:37 +00:00
|
|
|
"number": (*hexutil.Big)(head.Number),
|
|
|
|
"hash": head.Hash(),
|
|
|
|
"parentHash": head.ParentHash,
|
|
|
|
"nonce": head.Nonce,
|
|
|
|
"mixHash": head.MixDigest,
|
|
|
|
"sha3Uncles": head.UncleHash,
|
|
|
|
"logsBloom": head.Bloom,
|
|
|
|
"stateRoot": head.Root,
|
|
|
|
"miner": head.Coinbase,
|
|
|
|
"difficulty": (*hexutil.Big)(head.Difficulty),
|
|
|
|
"extraData": hexutil.Bytes(head.Extra),
|
|
|
|
"gasLimit": hexutil.Uint64(head.GasLimit),
|
|
|
|
"gasUsed": hexutil.Uint64(head.GasUsed),
|
|
|
|
"timestamp": hexutil.Uint64(head.Time),
|
|
|
|
"transactionsRoot": head.TxHash,
|
|
|
|
"receiptsRoot": head.ReceiptHash,
|
|
|
|
}
|
2021-07-27 12:07:50 +00:00
|
|
|
if head.BaseFee != nil {
|
2024-05-29 12:29:55 +00:00
|
|
|
result["baseFeePerGas"] = (*hexutil.Big)(head.BaseFee)
|
|
|
|
}
|
|
|
|
if head.WithdrawalsHash != nil {
|
|
|
|
result["withdrawalsRoot"] = head.WithdrawalsHash
|
|
|
|
}
|
|
|
|
if head.BlobGasUsed != nil {
|
|
|
|
result["blobGasUsed"] = hexutil.Uint64(*head.BlobGasUsed)
|
2021-07-27 12:07:50 +00:00
|
|
|
}
|
2024-05-29 12:29:55 +00:00
|
|
|
if head.ExcessBlobGas != nil {
|
|
|
|
result["excessBlobGas"] = hexutil.Uint64(*head.ExcessBlobGas)
|
|
|
|
}
|
|
|
|
if head.ParentBeaconRoot != nil {
|
|
|
|
result["parentBeaconBlockRoot"] = head.ParentBeaconRoot
|
|
|
|
}
|
|
|
|
return result
|
2020-10-26 13:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RPCMarshalBlock converts the given block to the RPC output which depends on fullTx. If inclTx is true transactions are
|
|
|
|
// returned. When fullTx is true the returned block contains full transaction details, otherwise it will only contain
|
|
|
|
// transaction hashes.
|
2024-05-29 12:29:55 +00:00
|
|
|
func RPCMarshalBlock(block *types.Block, inclTx bool, fullTx bool, config *params.ChainConfig) map[string]interface{} {
|
2021-10-07 09:35:11 +00:00
|
|
|
fields := RPCMarshalHeader(block.Header())
|
2020-10-26 13:58:37 +00:00
|
|
|
fields["size"] = hexutil.Uint64(block.Size())
|
|
|
|
|
|
|
|
if inclTx {
|
2024-05-29 12:29:55 +00:00
|
|
|
formatTx := func(idx int, tx *types.Transaction) interface{} {
|
|
|
|
return tx.Hash()
|
2020-10-26 13:58:37 +00:00
|
|
|
}
|
|
|
|
if fullTx {
|
2024-05-29 12:29:55 +00:00
|
|
|
formatTx = func(idx int, tx *types.Transaction) interface{} {
|
|
|
|
return newRPCTransactionFromBlockIndex(block, uint64(idx), config)
|
2020-10-26 13:58:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
txs := block.Transactions()
|
|
|
|
transactions := make([]interface{}, len(txs))
|
|
|
|
for i, tx := range txs {
|
2024-05-29 12:29:55 +00:00
|
|
|
transactions[i] = formatTx(i, tx)
|
2020-10-26 13:58:37 +00:00
|
|
|
}
|
|
|
|
fields["transactions"] = transactions
|
|
|
|
}
|
|
|
|
uncles := block.Uncles()
|
|
|
|
uncleHashes := make([]common.Hash, len(uncles))
|
|
|
|
for i, uncle := range uncles {
|
|
|
|
uncleHashes[i] = uncle.Hash()
|
|
|
|
}
|
|
|
|
fields["uncles"] = uncleHashes
|
2024-05-29 12:29:55 +00:00
|
|
|
if block.Header().WithdrawalsHash != nil {
|
|
|
|
fields["withdrawals"] = block.Withdrawals()
|
|
|
|
}
|
|
|
|
return fields
|
2020-10-26 13:58:37 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 13:22:57 +00:00
|
|
|
// RPCMarshalBlockWithUncleHashes marshals the block with the provided uncle hashes
|
2024-05-29 12:29:55 +00:00
|
|
|
func RPCMarshalBlockWithUncleHashes(block *types.Block, uncleHashes []common.Hash, inclTx bool, fullTx bool, config *params.ChainConfig) (map[string]interface{}, error) {
|
2021-10-07 09:35:11 +00:00
|
|
|
fields := RPCMarshalHeader(block.Header())
|
2020-10-28 13:22:57 +00:00
|
|
|
fields["size"] = hexutil.Uint64(block.Size())
|
|
|
|
|
|
|
|
if inclTx {
|
|
|
|
formatTx := func(tx *types.Transaction) (interface{}, error) {
|
|
|
|
return tx.Hash(), nil
|
|
|
|
}
|
|
|
|
if fullTx {
|
|
|
|
formatTx = func(tx *types.Transaction) (interface{}, error) {
|
2024-05-29 12:29:55 +00:00
|
|
|
return NewRPCTransactionFromBlockHash(block, tx.Hash(), config), nil
|
2020-10-28 13:22:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
txs := block.Transactions()
|
|
|
|
transactions := make([]interface{}, len(txs))
|
|
|
|
var err error
|
|
|
|
for i, tx := range txs {
|
|
|
|
if transactions[i], err = formatTx(tx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fields["transactions"] = transactions
|
|
|
|
}
|
|
|
|
|
2021-08-20 07:37:11 +00:00
|
|
|
fields["uncles"] = uncleHashes
|
2020-10-28 13:22:57 +00:00
|
|
|
return fields, nil
|
|
|
|
}
|
|
|
|
|
2024-05-29 12:29:55 +00:00
|
|
|
// newRPCTransactionFromBlockHash returns a transaction that will serialize to the RPC representation.
|
|
|
|
func NewRPCTransactionFromBlockHash(b *types.Block, hash common.Hash, config *params.ChainConfig) *RPCTransaction {
|
2020-10-26 13:58:37 +00:00
|
|
|
for idx, tx := range b.Transactions() {
|
|
|
|
if tx.Hash() == hash {
|
2024-05-29 12:29:55 +00:00
|
|
|
return newRPCTransactionFromBlockIndex(b, uint64(idx), config)
|
2020-10-26 13:58:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-12 16:37:46 +00:00
|
|
|
// SignerForTx returns an appropriate Signer for this Transaction
|
|
|
|
func SignerForTx(tx *types.Transaction) types.Signer {
|
2021-07-27 12:07:50 +00:00
|
|
|
var signer types.Signer
|
2020-10-26 13:58:37 +00:00
|
|
|
if tx.Protected() {
|
2021-07-27 12:07:50 +00:00
|
|
|
signer = types.LatestSignerForChainID(tx.ChainId())
|
|
|
|
} else {
|
|
|
|
signer = types.HomesteadSigner{}
|
2020-10-26 13:58:37 +00:00
|
|
|
}
|
2023-06-12 16:37:46 +00:00
|
|
|
return signer
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRPCTransaction returns a transaction that will serialize to the RPC
|
|
|
|
// representation, with the given location metadata set (if available).
|
2024-05-29 12:29:55 +00:00
|
|
|
func NewRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber uint64, blockTime uint64, index uint64, baseFee *big.Int, config *params.ChainConfig) *RPCTransaction {
|
|
|
|
signer := types.MakeSigner(config, new(big.Int).SetUint64(blockNumber), blockTime)
|
2020-10-26 13:58:37 +00:00
|
|
|
from, _ := types.Sender(signer, tx)
|
|
|
|
v, r, s := tx.RawSignatureValues()
|
|
|
|
result := &RPCTransaction{
|
2024-05-29 12:29:55 +00:00
|
|
|
Type: hexutil.Uint64(tx.Type()),
|
2020-10-26 13:58:37 +00:00
|
|
|
From: from,
|
|
|
|
Gas: hexutil.Uint64(tx.Gas()),
|
|
|
|
GasPrice: (*hexutil.Big)(tx.GasPrice()),
|
|
|
|
Hash: tx.Hash(),
|
|
|
|
Input: hexutil.Bytes(tx.Data()), // somehow this is ending up `nil`
|
|
|
|
Nonce: hexutil.Uint64(tx.Nonce()),
|
|
|
|
To: tx.To(),
|
|
|
|
Value: (*hexutil.Big)(tx.Value()),
|
|
|
|
V: (*hexutil.Big)(v),
|
|
|
|
R: (*hexutil.Big)(r),
|
|
|
|
S: (*hexutil.Big)(s),
|
|
|
|
}
|
|
|
|
if blockHash != (common.Hash{}) {
|
|
|
|
result.BlockHash = &blockHash
|
|
|
|
result.BlockNumber = (*hexutil.Big)(new(big.Int).SetUint64(blockNumber))
|
|
|
|
result.TransactionIndex = (*hexutil.Uint64)(&index)
|
|
|
|
}
|
2024-05-29 12:29:55 +00:00
|
|
|
|
2021-07-27 12:07:50 +00:00
|
|
|
switch tx.Type() {
|
2022-08-01 17:27:29 +00:00
|
|
|
case types.LegacyTxType:
|
|
|
|
// if a legacy transaction has an EIP-155 chain id, include it explicitly
|
|
|
|
if id := tx.ChainId(); id.Sign() != 0 {
|
|
|
|
result.ChainID = (*hexutil.Big)(id)
|
|
|
|
}
|
2024-05-29 12:29:55 +00:00
|
|
|
|
2021-07-27 12:07:50 +00:00
|
|
|
case types.AccessListTxType:
|
|
|
|
al := tx.AccessList()
|
2024-05-29 12:29:55 +00:00
|
|
|
yparity := hexutil.Uint64(v.Sign())
|
2021-07-27 12:07:50 +00:00
|
|
|
result.Accesses = &al
|
|
|
|
result.ChainID = (*hexutil.Big)(tx.ChainId())
|
2024-05-29 12:29:55 +00:00
|
|
|
result.YParity = &yparity
|
|
|
|
|
2021-07-27 12:07:50 +00:00
|
|
|
case types.DynamicFeeTxType:
|
|
|
|
al := tx.AccessList()
|
2024-05-29 12:29:55 +00:00
|
|
|
yparity := hexutil.Uint64(v.Sign())
|
|
|
|
result.Accesses = &al
|
|
|
|
result.ChainID = (*hexutil.Big)(tx.ChainId())
|
|
|
|
result.YParity = &yparity
|
|
|
|
result.GasFeeCap = (*hexutil.Big)(tx.GasFeeCap())
|
|
|
|
result.GasTipCap = (*hexutil.Big)(tx.GasTipCap())
|
|
|
|
// if the transaction has been mined, compute the effective gas price
|
|
|
|
if baseFee != nil && blockHash != (common.Hash{}) {
|
|
|
|
// price = min(gasTipCap + baseFee, gasFeeCap)
|
|
|
|
result.GasPrice = (*hexutil.Big)(effectiveGasPrice(tx, baseFee))
|
|
|
|
} else {
|
|
|
|
result.GasPrice = (*hexutil.Big)(tx.GasFeeCap())
|
|
|
|
}
|
|
|
|
|
|
|
|
case types.BlobTxType:
|
|
|
|
al := tx.AccessList()
|
|
|
|
yparity := hexutil.Uint64(v.Sign())
|
2021-07-27 12:07:50 +00:00
|
|
|
result.Accesses = &al
|
|
|
|
result.ChainID = (*hexutil.Big)(tx.ChainId())
|
2024-05-29 12:29:55 +00:00
|
|
|
result.YParity = &yparity
|
2021-07-27 12:07:50 +00:00
|
|
|
result.GasFeeCap = (*hexutil.Big)(tx.GasFeeCap())
|
|
|
|
result.GasTipCap = (*hexutil.Big)(tx.GasTipCap())
|
|
|
|
// if the transaction has been mined, compute the effective gas price
|
|
|
|
if baseFee != nil && blockHash != (common.Hash{}) {
|
2024-05-29 12:29:55 +00:00
|
|
|
result.GasPrice = (*hexutil.Big)(effectiveGasPrice(tx, baseFee))
|
2021-07-27 12:07:50 +00:00
|
|
|
} else {
|
2023-06-14 16:49:57 +00:00
|
|
|
result.GasPrice = (*hexutil.Big)(tx.GasFeeCap())
|
2021-07-27 12:07:50 +00:00
|
|
|
}
|
2024-05-29 12:29:55 +00:00
|
|
|
result.MaxFeePerBlobGas = (*hexutil.Big)(tx.BlobGasFeeCap())
|
|
|
|
result.BlobVersionedHashes = tx.BlobHashes()
|
2021-07-27 12:07:50 +00:00
|
|
|
}
|
2020-10-26 13:58:37 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2024-05-29 12:29:55 +00:00
|
|
|
// effectiveGasPrice computes the transaction gas fee, based on the given basefee value.
|
|
|
|
//
|
|
|
|
// price = min(gasTipCap + baseFee, gasFeeCap)
|
|
|
|
func effectiveGasPrice(tx *types.Transaction, baseFee *big.Int) *big.Int {
|
|
|
|
fee := tx.GasTipCap()
|
|
|
|
fee = fee.Add(fee, baseFee)
|
|
|
|
if tx.GasFeeCapIntCmp(fee) < 0 {
|
|
|
|
return tx.GasFeeCap()
|
|
|
|
}
|
|
|
|
return fee
|
|
|
|
}
|
|
|
|
|
2020-10-28 13:22:57 +00:00
|
|
|
type rpcBlock struct {
|
|
|
|
Hash common.Hash `json:"hash"`
|
|
|
|
Transactions []rpcTransaction `json:"transactions"`
|
|
|
|
UncleHashes []common.Hash `json:"uncles"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type rpcTransaction struct {
|
|
|
|
tx *types.Transaction
|
|
|
|
txExtraInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
type txExtraInfo struct {
|
|
|
|
BlockNumber *string `json:"blockNumber,omitempty"`
|
|
|
|
BlockHash *common.Hash `json:"blockHash,omitempty"`
|
|
|
|
From *common.Address `json:"from,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *rpcTransaction) UnmarshalJSON(msg []byte) error {
|
|
|
|
if err := json.Unmarshal(msg, &tx.tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return json.Unmarshal(msg, &tx.txExtraInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBlockAndUncleHashes(cli *rpc.Client, ctx context.Context, method string, args ...interface{}) (*types.Block, []common.Hash, error) {
|
|
|
|
var raw json.RawMessage
|
|
|
|
err := cli.CallContext(ctx, &raw, method, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
} else if len(raw) == 0 {
|
|
|
|
return nil, nil, ethereum.NotFound
|
|
|
|
}
|
|
|
|
// Decode header and transactions.
|
|
|
|
var head *types.Header
|
|
|
|
var body rpcBlock
|
|
|
|
if err := json.Unmarshal(raw, &head); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(raw, &body); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
// Quick-verify transaction and uncle lists. This mostly helps with debugging the server.
|
|
|
|
if head.UncleHash == types.EmptyUncleHash && len(body.UncleHashes) > 0 {
|
|
|
|
return nil, nil, fmt.Errorf("server returned non-empty uncle list but block header indicates no uncles")
|
|
|
|
}
|
|
|
|
if head.UncleHash != types.EmptyUncleHash && len(body.UncleHashes) == 0 {
|
|
|
|
return nil, nil, fmt.Errorf("server returned empty uncle list but block header indicates uncles")
|
|
|
|
}
|
|
|
|
if head.TxHash == types.EmptyRootHash && len(body.Transactions) > 0 {
|
|
|
|
return nil, nil, fmt.Errorf("server returned non-empty transaction list but block header indicates no transactions")
|
|
|
|
}
|
|
|
|
if head.TxHash != types.EmptyRootHash && len(body.Transactions) == 0 {
|
|
|
|
return nil, nil, fmt.Errorf("server returned empty transaction list but block header indicates transactions")
|
|
|
|
}
|
|
|
|
txs := make([]*types.Transaction, len(body.Transactions))
|
|
|
|
for i, tx := range body.Transactions {
|
|
|
|
txs[i] = tx.tx
|
|
|
|
}
|
|
|
|
return types.NewBlockWithHeader(head).WithBody(txs, nil), body.UncleHashes, nil
|
|
|
|
}
|
|
|
|
|
2024-05-29 12:29:55 +00:00
|
|
|
// newRPCTransactionFromBlockIndex returns a transaction that will serialize to the RPC representation.
|
|
|
|
func newRPCTransactionFromBlockIndex(b *types.Block, index uint64, config *params.ChainConfig) *RPCTransaction {
|
2020-10-28 03:04:19 +00:00
|
|
|
txs := b.Transactions()
|
|
|
|
if index >= uint64(len(txs)) {
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-29 12:29:55 +00:00
|
|
|
return NewRPCTransaction(txs[index], b.Hash(), b.NumberU64(), b.Time(), index, b.BaseFee(), config)
|
2020-10-28 03:04:19 +00:00
|
|
|
}
|
|
|
|
|
2024-05-29 12:29:55 +00:00
|
|
|
// newRPCRawTransactionFromBlockIndex returns the bytes of a transaction given a block and a transaction index.
|
|
|
|
func newRPCRawTransactionFromBlockIndex(b *types.Block, index uint64) hexutil.Bytes {
|
2020-10-26 13:58:37 +00:00
|
|
|
txs := b.Transactions()
|
|
|
|
if index >= uint64(len(txs)) {
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-29 12:29:55 +00:00
|
|
|
blob, _ := txs[index].MarshalBinary()
|
|
|
|
return blob
|
2020-10-26 13:58:37 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 13:22:57 +00:00
|
|
|
func toFilterArg(q ethereum.FilterQuery) (interface{}, error) {
|
|
|
|
arg := map[string]interface{}{
|
|
|
|
"address": q.Addresses,
|
|
|
|
"topics": q.Topics,
|
|
|
|
}
|
|
|
|
if q.BlockHash != nil {
|
|
|
|
arg["blockHash"] = *q.BlockHash
|
|
|
|
if q.FromBlock != nil || q.ToBlock != nil {
|
|
|
|
return nil, fmt.Errorf("cannot specify both BlockHash and FromBlock/ToBlock")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if q.FromBlock == nil {
|
|
|
|
arg["fromBlock"] = "0x0"
|
|
|
|
} else {
|
|
|
|
arg["fromBlock"] = toBlockNumArg(q.FromBlock)
|
|
|
|
}
|
|
|
|
arg["toBlock"] = toBlockNumArg(q.ToBlock)
|
|
|
|
}
|
|
|
|
return arg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toBlockNumArg(number *big.Int) string {
|
|
|
|
if number == nil {
|
|
|
|
return "latest"
|
|
|
|
}
|
|
|
|
return hexutil.EncodeBig(number)
|
|
|
|
}
|
2022-12-19 08:42:23 +00:00
|
|
|
|
2024-05-29 12:29:55 +00:00
|
|
|
func getIteratorAtPath(t state.Trie, startKey []byte) (trie.NodeIterator, int64, error) {
|
2022-12-19 08:42:23 +00:00
|
|
|
startTime := makeTimestamp()
|
|
|
|
var it trie.NodeIterator
|
2024-05-29 12:29:55 +00:00
|
|
|
var err error
|
2022-12-19 08:42:23 +00:00
|
|
|
|
|
|
|
if len(startKey)%2 != 0 {
|
|
|
|
// Zero-pad for odd-length keys, required by HexToKeyBytes()
|
|
|
|
startKey = append(startKey, 0)
|
2024-05-29 12:29:55 +00:00
|
|
|
it, err = t.NodeIterator(nodeiter.HexToKeyBytes(startKey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2022-12-19 08:42:23 +00:00
|
|
|
} else {
|
2024-05-29 12:29:55 +00:00
|
|
|
it, err = t.NodeIterator(nodeiter.HexToKeyBytes(startKey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2022-12-19 08:42:23 +00:00
|
|
|
// Step to the required node (not required if original startKey was odd-length)
|
|
|
|
it.Next(true)
|
|
|
|
}
|
|
|
|
|
2024-05-29 12:29:55 +00:00
|
|
|
return it, makeTimestamp() - startTime, nil
|
2022-12-19 08:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func fillSliceNodeData(
|
2023-04-14 06:26:46 +00:00
|
|
|
sdb state.Database,
|
2022-12-19 08:42:23 +00:00
|
|
|
nodesMap map[string]string,
|
|
|
|
leavesMap map[string]GetSliceResponseAccount,
|
2023-03-08 03:48:58 +00:00
|
|
|
node StateNode,
|
2022-12-19 08:42:23 +00:00
|
|
|
nodeElements []interface{},
|
|
|
|
storage bool,
|
|
|
|
) (int64, error) {
|
|
|
|
// Populate the nodes map
|
|
|
|
nodeValHash := crypto.Keccak256Hash(node.NodeValue)
|
|
|
|
nodesMap[common.Bytes2Hex(nodeValHash.Bytes())] = common.Bytes2Hex(node.NodeValue)
|
|
|
|
|
|
|
|
// Extract account data if it's a Leaf node
|
|
|
|
leafStartTime := makeTimestamp()
|
2023-03-08 03:48:58 +00:00
|
|
|
if node.NodeType == Leaf && !storage {
|
2023-04-14 06:26:46 +00:00
|
|
|
stateLeafKey, storageRoot, code, err := extractContractAccountInfo(sdb, node, nodeElements)
|
2022-12-19 08:42:23 +00:00
|
|
|
if err != nil {
|
2024-05-29 12:29:55 +00:00
|
|
|
return 0, fmt.Errorf("GetSlice account lookup error: %w", err)
|
2022-12-19 08:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(code) > 0 {
|
|
|
|
// Populate the leaves map
|
|
|
|
leavesMap[stateLeafKey] = GetSliceResponseAccount{
|
|
|
|
StorageRoot: storageRoot,
|
|
|
|
EVMCode: common.Bytes2Hex(code),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return makeTimestamp() - leafStartTime, nil
|
|
|
|
}
|
|
|
|
|
2023-04-14 06:26:46 +00:00
|
|
|
func extractContractAccountInfo(sdb state.Database, node StateNode, nodeElements []interface{}) (string, string, []byte, error) {
|
2022-12-19 08:42:23 +00:00
|
|
|
var account types.StateAccount
|
|
|
|
if err := rlp.DecodeBytes(nodeElements[1].([]byte), &account); err != nil {
|
|
|
|
return "", "", nil, fmt.Errorf("error decoding account for leaf node at path %x nerror: %v", node.Path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Equal(account.CodeHash, emptyCodeHash) {
|
|
|
|
return "", "", nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract state leaf key
|
2023-09-21 06:55:26 +00:00
|
|
|
partialPath := utils.CompactToHex(nodeElements[0].([]byte))
|
2022-12-19 08:42:23 +00:00
|
|
|
valueNodePath := append(node.Path, partialPath...)
|
2023-09-21 06:55:26 +00:00
|
|
|
encodedPath := utils.HexToCompact(valueNodePath)
|
2022-12-19 08:42:23 +00:00
|
|
|
leafKey := encodedPath[1:]
|
|
|
|
stateLeafKeyString := common.BytesToHash(leafKey).String()
|
|
|
|
|
|
|
|
storageRootString := account.Root.String()
|
|
|
|
|
|
|
|
// Extract codeHash and get code
|
|
|
|
codeHash := common.BytesToHash(account.CodeHash)
|
2024-05-29 12:29:55 +00:00
|
|
|
codeBytes, err := sdb.ContractCode(common.Address{}, codeHash)
|
2023-04-14 06:26:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", nil, err
|
|
|
|
}
|
2022-12-19 08:42:23 +00:00
|
|
|
|
|
|
|
return stateLeafKeyString, storageRootString, codeBytes, nil
|
|
|
|
}
|
2023-04-14 06:26:46 +00:00
|
|
|
|
|
|
|
// IsLeaf checks if the node we are at is a leaf
|
|
|
|
func IsLeaf(elements []interface{}) (bool, error) {
|
|
|
|
if len(elements) > 2 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if len(elements) < 2 {
|
|
|
|
return false, fmt.Errorf("node cannot be less than two elements in length")
|
|
|
|
}
|
|
|
|
switch elements[0].([]byte)[0] / 16 {
|
|
|
|
case '\x00':
|
|
|
|
return false, nil
|
|
|
|
case '\x01':
|
|
|
|
return false, nil
|
|
|
|
case '\x02':
|
|
|
|
return true, nil
|
|
|
|
case '\x03':
|
|
|
|
return true, nil
|
|
|
|
default:
|
|
|
|
return false, fmt.Errorf("unknown hex prefix")
|
|
|
|
}
|
|
|
|
}
|