// Copyright 2024 The Erigon Authors
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.

package jsonrpc

import (
	"bytes"
	"context"
	"errors"
	"fmt"
	"math/big"
	"unsafe"

	"google.golang.org/grpc"

	"github.com/erigontech/erigon/common"
	"github.com/erigontech/erigon/common/crypto"
	"github.com/erigontech/erigon/common/empty"
	"github.com/erigontech/erigon/common/hexutil"
	"github.com/erigontech/erigon/common/log/v3"
	"github.com/erigontech/erigon/db/kv"
	"github.com/erigontech/erigon/db/kv/dbutils"
	"github.com/erigontech/erigon/db/kv/membatchwithdb"
	"github.com/erigontech/erigon/db/state/execctx"
	"github.com/erigontech/erigon/execution/commitment/trie"
	"github.com/erigontech/erigon/execution/protocol"
	"github.com/erigontech/erigon/execution/protocol/params"
	"github.com/erigontech/erigon/execution/protocol/rules"
	"github.com/erigontech/erigon/execution/stagedsync"
	"github.com/erigontech/erigon/execution/state"
	"github.com/erigontech/erigon/execution/tracing/tracers/logger"
	"github.com/erigontech/erigon/execution/types"
	"github.com/erigontech/erigon/execution/types/accounts"
	"github.com/erigontech/erigon/execution/vm"
	"github.com/erigontech/erigon/execution/vm/evmtypes"
	"github.com/erigontech/erigon/node/gointerfaces"
	"github.com/erigontech/erigon/node/gointerfaces/txpoolproto"
	"github.com/erigontech/erigon/rpc"
	ethapi2 "github.com/erigontech/erigon/rpc/ethapi"
	"github.com/erigontech/erigon/rpc/rpchelper"
	"github.com/erigontech/erigon/rpc/transactions"
)

var latestNumOrHash = rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)

const (
	// estimateGasErrorRatio is the amount of overestimation eth_estimateGas is
	// allowed to produce in order to speed up calculations.
	estimateGasErrorRatio = 0.015

	// maxGetProofKeys is the maximum number of storage keys allowed in a single eth_getProof request.
	maxGetProofKeys = 1024
)

// Call implements eth_call. Executes a new message call immediately without creating a transaction on the block chain.
func (api *APIImpl) Call(ctx context.Context, args ethapi2.CallArgs, requestedBlock *rpc.BlockNumberOrHash, stateOverrides *ethapi2.StateOverrides, blockOverrides *ethapi2.BlockOverrides) (hexutil.Bytes, error) {
	tx, err := api.db.BeginTemporalRo(ctx)
	if err != nil {
		return nil, err
	}
	defer tx.Rollback()

	var blockNrOrHash rpc.BlockNumberOrHash
	if requestedBlock != nil {
		blockNrOrHash = *requestedBlock
	} else {
		blockNrOrHash = latestNumOrHash
	}
	chainConfig, err := api.chainConfig(ctx, tx)
	if err != nil {
		return nil, err
	}
	engine := api.engine()

	if args.Gas == nil || uint64(*args.Gas) == 0 {
		args.Gas = (*hexutil.Uint64)(&api.GasCap)
	}

	header, _, err := api.headerByNumberOrHash(ctx, tx, blockNrOrHash)
	if err != nil {
		return nil, err
	}
	if header == nil {
		return nil, fmt.Errorf("header not found")
	}

	err = api.BaseAPI.checkPruneHistory(ctx, tx, header.Number.Uint64())
	if err != nil {
		return nil, err
	}

	err = rpchelper.CheckBlockExecuted(tx, header.Number.Uint64())
	if err != nil {
		return nil, err
	}

	stateReader, err := rpchelper.CreateStateReader(ctx, tx, api._blockReader, blockNrOrHash, 0, api.filters, api.stateCache, api._txNumReader)
	if err != nil {
		return nil, err
	}

	result, err := transactions.DoCall(ctx, engine, args, tx, blockNrOrHash, header, stateOverrides, blockOverrides, api.GasCap, chainConfig, stateReader, api._blockReader, api.evmCallTimeout)
	if err != nil {
		return nil, err
	}

	if len(result.ReturnData) > api.ReturnDataLimit {
		return nil, fmt.Errorf("call returned result on length %d exceeding --rpc.returndata.limit %d", len(result.ReturnData), api.ReturnDataLimit)
	}

	if errors.Is(result.Err, vm.ErrExecutionReverted) {
		return nil, ethapi2.NewRevertError(result)
	}

	return result.Return(), result.Err
}

// EstimateGas implements eth_estimateGas. Returns an estimate of how much gas is necessary to allow the transaction to complete. The transaction will not be added to the blockchain.
func (api *APIImpl) EstimateGas(ctx context.Context, argsOrNil *ethapi2.CallArgs, blockNrOrHash *rpc.BlockNumberOrHash, stateOverrides *ethapi2.StateOverrides, blockOverrides *ethapi2.BlockOverrides) (hexutil.Uint64, error) {
	var args ethapi2.CallArgs
	// if we actually get CallArgs here, we use them
	if argsOrNil != nil {
		args = *argsOrNil
	}

	dbtx, err := api.db.BeginTemporalRo(ctx)
	if err != nil {
		return 0, err
	}
	defer dbtx.Rollback()

	// Use latest block by default
	if blockNrOrHash == nil {
		blockNrOrHash = &latestNumOrHash
	}

	chainConfig, err := api.chainConfig(ctx, dbtx)
	if err != nil {
		return 0, err
	}
	engine := api.engine()

	header, isLatest, err := api.headerByNumberOrHash(ctx, dbtx, *blockNrOrHash)
	if err != nil {
		return 0, err
	}

	// try to check if it is a pending block
	if header == nil {
		b := api.filters.LastPendingBlock()
		blockNum, _, _, err := rpchelper.GetBlockNumber(ctx, *blockNrOrHash, dbtx, api._blockReader, api.filters)
		if err != nil {
			return 0, err
		}
		if b != nil && blockNum == b.NumberU64() {
			header = b.HeaderNoCopy()
		}
	}

	if header == nil {
		return 0, fmt.Errorf("could not find the header %s in cache or db", blockNrOrHash.String())
	}

	blockNum := header.Number

	err = api.BaseAPI.checkPruneHistory(ctx, dbtx, blockNum.Uint64())
	if err != nil {
		return 0, err
	}

	err = rpchelper.CheckBlockExecuted(dbtx, header.Number.Uint64())
	if err != nil {
		return 0, err
	}

	stateReader, err := rpchelper.CreateStateReaderFromBlockNumber(ctx, dbtx, blockNum.Uint64(), isLatest, 0, api.stateCache, api._txNumReader)
	if err != nil {
		return 0, err
	}

	// Binary search the gas requirement, as it may be higher than the amount used
	var (
		lo uint64
		hi uint64
	)
	// Use zero address if sender unspecified.
	if args.From == nil {
		args.From = new(common.Address)
	}

	// Determine the highest gas limit can be used during the estimation.
	if args.Gas != nil && uint64(*args.Gas) >= params.TxGas {
		hi = uint64(*args.Gas)
	} else {
		// Retrieve the block to act as the gas ceiling
		hi = header.GasLimit
	}
	if hi > params.MaxTxnGasLimit && chainConfig.IsOsaka(header.Time) {
		// Cap the maximum gas allowance according to EIP-7825 if Osaka
		hi = params.MaxTxnGasLimit
	}

	var feeCap *big.Int
	if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) {
		return 0, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified")
	} else if args.GasPrice != nil {
		feeCap = args.GasPrice.ToInt()
	} else if args.MaxFeePerGas != nil {
		feeCap = args.MaxFeePerGas.ToInt()
	} else {
		feeCap = common.Big0
	}
	// Recap the highest gas limit with account's available balance.
	if feeCap.Sign() != 0 {
		state := state.New(stateReader)
		if state == nil {
			return 0, errors.New("can't get the current state")
		}

		balance, err := state.GetBalance(accounts.InternAddress(*args.From)) // from can't be nil
		if err != nil {
			return 0, err
		}
		available := balance.ToBig()
		if args.Value != nil {
			if args.Value.ToInt().Cmp(available) >= 0 {
				return 0, errors.New("insufficient funds for transfer")
			}
			available.Sub(available, args.Value.ToInt())
		}

		allowance := new(big.Int).Div(available, feeCap)

		// If the allowance is larger than maximum uint64, skip checking
		if allowance.IsUint64() && hi > allowance.Uint64() {
			transfer := args.Value
			if transfer == nil {
				transfer = new(hexutil.Big)
			}
			log.Warn("Gas estimation capped by limited funds", "original", hi, "balance", balance,
				"sent", transfer.ToInt(), "maxFeePerGas", feeCap, "fundable", allowance)
			hi = allowance.Uint64()
		}
	}

	// Recap the highest gas allowance with specified gascap.
	if hi > api.GasCap {
		log.Warn("Caller gas above allowance, capping", "requested", hi, "cap", api.GasCap)
		hi = api.GasCap
	}

	caller, err := transactions.NewReusableCaller(engine, stateReader, stateOverrides, blockOverrides, header, args, api.GasCap, *blockNrOrHash, dbtx, api._blockReader, chainConfig, api.evmCallTimeout)
	if err != nil {
		return 0, err
	}

	// If the transaction is a plain value transfer, short circuit estimation and
	// directly try 21000. Returning 21000 without any execution is dangerous as
	// some tx field combos might bump the price up even for plain transfers (e.g.
	// unused access list items). Ever so slightly wasteful, but safer overall.

	if args.Data == nil {
		state := state.New(stateReader)
		if state == nil {
			return 0, errors.New("can't get the current state")
		}
		codeSize, err := state.GetCodeSize(accounts.InternAddress(*args.To))
		if err != nil {
			return 0, errors.New("getCodeSize failed")
		}
		if args.To != nil && codeSize == 0 {
			failed, _, err := doCall(ctx, caller, params.TxGas, engine)
			if err == nil && !failed {
				return hexutil.Uint64(params.TxGas), nil
			}
		}
	}

	// We first execute the transaction at the highest allowable gas limit, since if this fails we
	// can return error immediately.
	failed, result, err := doCall(ctx, caller, hi, engine)
	if err != nil {
		return 0, err
	}
	if failed {
		if result != nil && !errors.Is(result.Err, vm.ErrOutOfGas) {
			if len(result.Revert()) > 0 {
				return 0, ethapi2.NewRevertError(result)
			}
			return 0, result.Err
		}
		// Otherwise, the specified gas cap is too low
		return 0, fmt.Errorf("gas required exceeds allowance (%d)", hi)
	}

	// above lower-bounds the gas limit required for it to succeed. One exception
	// is those that explicitly check gas remaining in order to execute within a
	// given limit, but we probably don't want to return the lowest possible gas
	// limit for these cases anyway.
	lo = result.ReceiptGasUsed - 1

	// There's a high probability that the transaction will successfully
	// execute using the 'usedGas' from the first execution as the gasLimit.
	// We explicitly check this value and use it as the upper bound for the
	// binary search.
	optimisticGasLimit := (result.ReceiptGasUsed + params.CallStipend) * 64 / 63
	if optimisticGasLimit < hi {
		failed, _, err := doCall(ctx, caller, optimisticGasLimit, engine)
		if err != nil {
			return 0, err
		}
		if failed {
			lo = optimisticGasLimit
		} else {
			hi = optimisticGasLimit
		}
	}

	// Binary search for the smallest gas limit that allows the tx to execute successfully.
	for lo+1 < hi {
		if float64(hi-lo)/float64(hi) < estimateGasErrorRatio {
			break
		}
		mid := min((hi+lo)/2,
			// Most txs don't need much higher gas limit than their gas used, and most txs don't
			// require near the full block limit of gas, so the selection of where to bisect the
			// range here is skewed to favor the low side.
			lo*2)
		failed, _, err := doCall(ctx, caller, mid, engine)
		// If the error is not nil(consensus error), it means the provided message
		// call or transaction will never be accepted no matter how much gas it is
		// assigned. Return the error directly, don't struggle any more.
		if err != nil {
			return 0, err
		}
		if failed {
			lo = mid
		} else {
			hi = mid
		}
	}
	return hexutil.Uint64(hi), nil
}

// execute is a helper that executes the transaction under a given gas limit and
// returns true if the transaction fails for a reason that might be related to
// not enough gas. A non-nil error means execution failed due to reasons unrelated
// to the gas limit.
func doCall(ctx context.Context, caller *transactions.ReusableCaller, gasLimit uint64, engine rules.EngineReader) (bool, *evmtypes.ExecutionResult, error) {
	result, err := caller.DoCallWithNewGas(ctx, gasLimit, engine)
	if err != nil {
		if errors.Is(err, protocol.ErrIntrinsicGas) {
			return true, nil, nil // Special case, raise gas limit
		}

		if errors.Is(err, protocol.ErrGasLimitTooHigh) {
			return true, nil, nil // Special case, lower gas limit
		}
		return true, nil, err
	}
	return result.Failed(), result, nil
}

type StorageKeysInfo struct {
	Hash      common.Hash
	KeyLength int
}

// GetProof implements eth_getProof partially; Proofs are available only with the `latest` block tag.
func (api *APIImpl) GetProof(ctx context.Context, address common.Address, storageKeys []hexutil.Bytes, blockNrOrHash rpc.BlockNumberOrHash) (*accounts.AccProofResult, error) {
	if len(storageKeys) > maxGetProofKeys {
		return nil, &rpc.CustomError{
			Message: fmt.Sprintf("too many storage keys requested (max %d, got %d)", maxGetProofKeys, len(storageKeys)),
			Code:    rpc.ErrCodeInvalidParams,
		}
	}

	roTx, err := api.db.BeginTemporalRo(ctx)
	if err != nil {
		return nil, err
	}
	defer roTx.Rollback()

	requestedBlockNr, _, _, err := rpchelper.GetCanonicalBlockNumber(ctx, blockNrOrHash, roTx, api._blockReader, api.filters)
	if err != nil {
		return nil, err
	} else if requestedBlockNr == 0 {
		return nil, errors.New("block not found")
	}

	err = api.BaseAPI.checkPruneHistory(ctx, roTx, uint64(requestedBlockNr))
	if err != nil {
		return nil, err
	}

	storageKeysConverted := make([]StorageKeysInfo, len(storageKeys))
	for i, s := range storageKeys {
		storageKeysConverted[i].Hash.SetBytes(s)
		storageKeysConverted[i].KeyLength = len(s)
	}
	return api.getProof(ctx, roTx, address, storageKeysConverted, rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(requestedBlockNr)), api.logger)
}

func (api *APIImpl) getProof(ctx context.Context, roTx kv.TemporalTx, address common.Address, storageKeys []StorageKeysInfo, blockNrOrHash rpc.BlockNumberOrHash, logger log.Logger) (*accounts.AccProofResult, error) {

	// Output key encoding is a bit special: if the input was a 32-byte hash, it is
	// returned as such. Otherwise, we apply the QUANTITY encoding mandated by the
	// JSON-RPC spec for getProof. This behavior exists to preserve backwards
	// compatibility with older client versions.
	getKey := func(storageKey StorageKeysInfo) string {
		var outputKey string

		if storageKey.KeyLength != 32 {
			outputKey = hexutil.EncodeBig(storageKey.Hash.Big())
		} else {
			outputKey = hexutil.Encode(storageKey.Hash[:])
		}
		return outputKey
	}

	tx, err := api.db.BeginTemporalRo(ctx)
	if err != nil {
		return nil, err
	}
	defer tx.Rollback()
	// get the root hash from header to validate proofs along the way
	header, err := api._blockReader.HeaderByNumber(ctx, roTx, blockNrOrHash.BlockNumber.Uint64())
	if err != nil {
		return nil, err
	}

	domains, err := execctx.NewSharedDomains(ctx, tx, log.New())
	if err != nil {
		return nil, err
	}
	sdCtx := domains.GetCommitmentContext()
	sdCtx.SetDeferBranchUpdates(false)

	latestBlock, err := rpchelper.GetLatestBlockNumber(roTx)
	if err != nil {
		return nil, err
	}
	if latestBlock < blockNrOrHash.BlockNumber.Uint64() {
		return nil, fmt.Errorf("block number is in the future latest=%d requested=%d", latestBlock, blockNrOrHash.BlockNumber.Uint64())
	}
	if blockNrOrHash.BlockNumber.Uint64() < latestBlock {
		// Get first txnum of blockNumber+1 to ensure that correct state root will be restored as of blockNumber has been executed
		lastTxnInBlock, err := api._txNumReader.Min(ctx, tx, blockNrOrHash.BlockNumber.Uint64()+1)
		if err != nil {
			return nil, err
		}
		commitmentStartingTxNum := tx.Debug().HistoryStartFrom(kv.CommitmentDomain)
		if lastTxnInBlock < commitmentStartingTxNum {
			return nil, fmt.Errorf("%w: commitment start: %d, last tx: %d", state.PrunedError, commitmentStartingTxNum, lastTxnInBlock)
		}

		sdCtx.SetHistoryStateReader(roTx, lastTxnInBlock)
		//domains.SetTrace(true)
		if _, _, err := domains.SeekCommitment(context.Background(), roTx); err != nil {
			return nil, err
		}
		domains.SetTrace(false, false)
	}

	// touch account
	sdCtx.TouchKey(kv.AccountsDomain, string(address.Bytes()), nil)

	// generate the trie for proofs, this works by loading the merkle paths to the touched keys
	proofTrie, calculatedAccountProofRoot, err := sdCtx.Witness(ctx, nil, "eth_getProof")
	if err != nil {
		return nil, err
	}
	if !bytes.Equal(calculatedAccountProofRoot, header.Root[:]) {
		return nil, fmt.Errorf("root hash mismatch in account proof trie calculatedAccountProofRoot(%x)!=expectedRoot(%x)", calculatedAccountProofRoot, header.Root[:])
	}

	// set initial response fields
	proof := &accounts.AccProofResult{
		Address:      address,
		Balance:      new(hexutil.Big),
		Nonce:        hexutil.Uint64(0),
		CodeHash:     common.Hash{},
		StorageHash:  common.Hash{},
		StorageProof: make([]accounts.StorProofResult, len(storageKeys)),
	}

	// get account proof
	accountProof, err := proofTrie.Prove(crypto.Keccak256(address.Bytes()), 0, false)
	if err != nil {
		return nil, err
	}
	proof.AccountProof = *(*[]hexutil.Bytes)(unsafe.Pointer(&accountProof))

	// get account data from the trie
	acc, _ := proofTrie.GetAccount(crypto.Keccak256(address.Bytes()))
	if acc == nil {
		for i, storageKey := range storageKeys {
			proof.StorageProof[i] = accounts.StorProofResult{
				Key:   getKey(storageKey),
				Value: new(hexutil.Big),
				Proof: []hexutil.Bytes{},
			}
		}
		err = trie.VerifyAccountProof(header.Root, proof)
		if err != nil {
			return nil, err
		}
		return proof, nil
	}

	proof.Balance = (*hexutil.Big)(acc.Balance.ToBig())
	proof.Nonce = hexutil.Uint64(acc.Nonce)
	proof.CodeHash = acc.CodeHash.Value()
	proof.StorageHash = acc.Root

	// if storage is not empty touch keys and build trie
	if proof.StorageHash.Cmp(common.BytesToHash(empty.RootHash.Bytes())) != 0 && len(storageKeys) != 0 {
		// touch storage keys
		for _, storageKey := range storageKeys {
			sdCtx.TouchKey(kv.StorageDomain, string(common.FromHex(address.Hex()[2:]+storageKey.Hash.String()[2:])), nil)
		}

		// generate the trie for proofs, this works by loading the merkle paths to the touched key
		var storageProofRoot []byte
		proofTrie, storageProofRoot, err = sdCtx.Witness(ctx, nil, "eth_getProof")
		if err != nil {
			return nil, err
		}
		if !bytes.Equal(storageProofRoot, header.Root[:]) {
			return nil, fmt.Errorf("root hash mismatch in storage proof trie storageProofRoot(%x)!=expectedRoot(%x)", storageProofRoot, header.Root[:])
		}
	}

	reader, err := rpchelper.CreateStateReader(ctx, tx, api._blockReader, blockNrOrHash, 0, api.filters, api.stateCache, api._txNumReader)
	if err != nil {
		return nil, err
	}

	// get storage key proofs
	for i, storageKey := range storageKeys {
		// Stop early if the RPC request was canceled while proofs are being built.
		if err := ctx.Err(); err != nil {
			return nil, err
		}
		proof.StorageProof[i].Key = getKey(storageKey)
		// if we have simple non contract account just set values directly without requesting any key proof
		if proof.StorageHash.Cmp(common.BytesToHash(empty.RootHash.Bytes())) == 0 {
			proof.StorageProof[i].Proof = []hexutil.Bytes{}
			proof.StorageProof[i].Value = new(hexutil.Big)
			continue
		}

		// prepare key path (keccak(address) | keccak(key))
		addrHash := crypto.HashData(address.Bytes())
		keyHash := crypto.HashData(storageKey.Hash.Bytes())
		fullKey := make([]byte, 0, 64)
		fullKey = append(fullKey, addrHash[:]...)
		fullKey = append(fullKey, keyHash[:]...)

		// get proof for the given key
		storageProof, err := proofTrie.Prove(fullKey, len(proof.AccountProof), true)
		if err != nil {
			return nil, errors.New("cannot verify store proof")
		}

		res, _, err := reader.ReadAccountStorage(accounts.InternAddress(address), accounts.InternKey(storageKey.Hash))
		if err != nil {
			logger.Warn(fmt.Sprintf("couldn't read account storage for the address %s\n", address.String()))
		}
		proof.StorageProof[i].Value = (*hexutil.Big)(res.ToBig())

		// 0x80 represents RLP encoding of an empty proof slice
		proof.StorageProof[i].Proof = []hexutil.Bytes{[]byte{0x80}}
		if len(storageProof) != 0 {
			proof.StorageProof[i].Proof = *(*[]hexutil.Bytes)(unsafe.Pointer(&storageProof))
		}
	}

	// Verify proofs before returning result to the user
	err = trie.VerifyAccountProof(header.Root, proof)
	if err != nil {
		return nil, fmt.Errorf("internal error: failed to verify account proof for generated proof : %w", err)
	}

	// verify storage proofs
	for _, storageProof := range proof.StorageProof {
		err = trie.VerifyStorageProof(proof.StorageHash, storageProof)
		if err != nil {
			return nil, fmt.Errorf("internal error: failed to verify storage proof for key=%x , proof=%+v : %w", storageProof.Key, proof, err)
		}
	}

	return proof, nil
}

func (api *APIImpl) GetWitness(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) {
	return api.getWitness(ctx, api.db, blockNrOrHash, 0, true, api.MaxGetProofRewindBlockCount, api.logger)
}

func (api *APIImpl) GetTxWitness(ctx context.Context, blockNr rpc.BlockNumberOrHash, txIndex hexutil.Uint) (hexutil.Bytes, error) {
	return api.getWitness(ctx, api.db, blockNr, txIndex, false, api.MaxGetProofRewindBlockCount, api.logger)
}

func verifyExecResult(execResult *protocol.EphemeralExecResult, block *types.Block) error {
	actualTxRoot := execResult.TxRoot.Bytes()
	expectedTxRoot := block.TxHash().Bytes()
	if !bytes.Equal(actualTxRoot, expectedTxRoot) {
		return fmt.Errorf("mismatch in block TxRoot actual(%x) != expected(%x)", actualTxRoot, expectedTxRoot)
	}

	actualGasUsed := uint64(execResult.GasUsed)
	expectedGasUsed := block.GasUsed()
	if actualGasUsed != expectedGasUsed {
		return fmt.Errorf("mismatch in block gas used actual(%x) != expected(%x)", actualGasUsed, expectedGasUsed)
	}

	actualReceiptsHash := execResult.ReceiptRoot.Bytes()
	expectedReceiptsHash := block.ReceiptHash().Bytes()
	if !bytes.Equal(actualReceiptsHash, expectedReceiptsHash) {
		return fmt.Errorf("mismatch in receipts hash actual(%x) != expected(%x)", actualReceiptsHash, expectedReceiptsHash)
	}

	// check the state root
	resultingStateRoot := execResult.StateRoot.Bytes()
	expectedBlockStateRoot := block.Root().Bytes()
	if !bytes.Equal(resultingStateRoot, expectedBlockStateRoot) {
		return fmt.Errorf("resulting state root after execution doesn't match state root in block actual(%x)!=expected(%x)", resultingStateRoot, expectedBlockStateRoot)
	}
	return nil
}

func (api *BaseAPI) getWitness(ctx context.Context, db kv.TemporalRoDB, blockNrOrHash rpc.BlockNumberOrHash, txIndex hexutil.Uint, fullBlock bool, maxGetProofRewindBlockCount int, logger log.Logger) (hexutil.Bytes, error) {
	roTx, err := db.BeginRo(ctx)
	if err != nil {
		return nil, err
	}
	defer roTx.Rollback()

	blockNr, hash, _, err := rpchelper.GetCanonicalBlockNumber(ctx, blockNrOrHash, roTx, api._blockReader, api.filters) // DoCall cannot be executed on non-canonical blocks
	if err != nil {
		return nil, err
	}

	// Witness for genesis block is empty
	if blockNr == 0 {
		w := trie.NewWitness(make([]trie.WitnessOperator, 0))

		var buf bytes.Buffer
		_, err = w.WriteInto(&buf)
		if err != nil {
			return nil, err
		}

		return buf.Bytes(), nil
	}

	err = api.checkPruneHistory(ctx, roTx, blockNr)
	if err != nil {
		return nil, err
	}

	block, err := api.blockWithSenders(ctx, roTx, hash, blockNr)
	if err != nil {
		return nil, err
	}
	if block == nil {
		return nil, nil
	}

	if !fullBlock && int(txIndex) >= len(block.Transactions()) {
		return nil, fmt.Errorf("transaction index out of bounds: %d", txIndex)
	}

	latestBlock, err := rpchelper.GetLatestBlockNumber(roTx)
	if err != nil {
		return nil, err
	}

	if latestBlock < blockNr {
		// shouldn't happen, but check anyway
		return nil, fmt.Errorf("block number is in the future latest=%d requested=%d", latestBlock, blockNr)
	}

	// Compute the witness if it's for a tx or it's not present in db
	prevHeader, err := api._blockReader.HeaderByNumber(ctx, roTx, blockNr-1)
	if err != nil {
		return nil, err
	}

	regenerateHash := false
	if latestBlock-blockNr > uint64(maxGetProofRewindBlockCount) {
		regenerateHash = true
	}

	engine, ok := api.engine().(rules.Engine)
	if !ok {
		return nil, errors.New("engine is not rules.Engine")
	}

	roTx2, err := db.BeginTemporalRo(ctx)
	if err != nil {
		return nil, err
	}
	defer roTx2.Rollback()
	txBatch2 := membatchwithdb.NewMemoryBatch(roTx2, "", logger)
	defer txBatch2.Rollback()

	domains, err := execctx.NewSharedDomains(ctx, txBatch2, log.New())
	if err != nil {
		return nil, err
	}
	defer domains.Close()

	// Prepare witness config
	chainConfig, err := api.chainConfig(ctx, roTx2)
	if err != nil {
		return nil, fmt.Errorf("error loading chain config: %v", err)
	}

	// Unwind to blockNr
	cfg := stagedsync.StageWitnessCfg(chainConfig, engine, api._blockReader, api.dirs)
	err = stagedsync.RewindStagesForWitness(domains, txBatch2, blockNr, latestBlock, &cfg, regenerateHash, ctx, logger)
	if err != nil {
		return nil, err
	}

	store, err := stagedsync.PrepareForWitness(txBatch2, block, prevHeader.Root, &cfg, ctx, logger)
	if err != nil {
		return nil, err
	}

	sdCtx := domains.GetCommitmentContext()
	// execute block #blockNr ephemerally. This will use TrieStateWriter to record touches of accounts and storage keys.
	_, err = protocol.ExecuteBlockEphemerally(chainConfig, &vm.Config{}, store.GetHashFn, engine, block, store.Tds, store.TrieStateWriter, store.ChainReader, nil, logger)
	if err != nil {
		return nil, err
	}

	// gather touched keys from ephemeral block execution
	touchedPlainKeys, touchedHashedKeys := store.Tds.GetTouchedPlainKeys()
	codeReads := store.Tds.BuildCodeTouches()

	// marking keys we want to get witness for
	for _, key := range touchedPlainKeys {
		sdCtx.TouchKey(kv.AccountsDomain, string(key), nil)
	}

	// generate the block witness, this works by loading the merkle paths to the touched keys (they are loaded from the state at block #blockNr-1)
	witnessTrie, witnessRootHash, err := sdCtx.Witness(ctx, codeReads, "computeWitness")
	if err != nil {
		return nil, err
	}

	if !bytes.Equal(witnessRootHash, prevHeader.Root[:]) {
		return nil, fmt.Errorf("witness root hash mismatch actual(%x)!=expected(%x)", witnessRootHash, prevHeader.Root[:])
	}

	// retain list is need for the serialization of the trie.Trie into a witness
	retainListBuilder := trie.NewRetainListBuilder()
	for _, key := range touchedHashedKeys {
		if len(key) == 32 {
			retainListBuilder.AddTouch(key)
		} else {
			addr, _, hash := dbutils.ParseCompositeStorageKey(key)
			storageTouch := dbutils.GenerateCompositeTrieKey(addr, hash)
			retainListBuilder.AddStorageTouch(storageTouch)
		}
	}

	for _, codeWithHash := range codeReads {
		retainListBuilder.ReadCode(codeWithHash.CodeHash, codeWithHash.Code)
	}

	retainList := retainListBuilder.Build(false)

	// serialize witness trie
	witness, err := witnessTrie.ExtractWitness(true, retainList)
	if err != nil {
		return nil, err
	}

	var witnessBuffer bytes.Buffer
	_, err = witness.WriteInto(&witnessBuffer)
	if err != nil {
		return nil, err
	}

	// this is a verification step: we execute block #blockNr statelessly using the witness, and we expect to get the same state root as in the header
	// otherwise something went wrong
	store.Tds.SetTrie(witnessTrie)
	newStateRoot, err := stagedsync.ExecuteBlockStatelessly(block, prevHeader, store.ChainReader, store.Tds, &cfg, &witnessBuffer, store.GetHashFn, logger)
	if err != nil {
		return nil, err
	}
	if !bytes.Equal(newStateRoot.Bytes(), block.Root().Bytes()) {
		fmt.Printf("state root mismatch after stateless execution actual(%x) != expected(%x)\n", newStateRoot.Bytes(), block.Root().Bytes())
	}
	witnessBufBytes := witnessBuffer.Bytes()
	witnessBufBytesCopy := common.Copy(witnessBufBytes)
	return witnessBufBytesCopy, nil
}

func (api *APIImpl) tryBlockFromLru(hash common.Hash) *types.Block {
	var block *types.Block
	if api.blocksLRU != nil {
		if it, ok := api.blocksLRU.Get(hash); ok && it != nil {
			block = it
		}
	}
	return block
}

// accessListResult returns an optional accesslist
// Its the result of the `eth_createAccessList` RPC call.
// It contains an error if the transaction itself failed.
type accessListResult struct {
	Accesslist *types.AccessList `json:"accessList"`
	Error      string            `json:"error,omitempty"`
	GasUsed    hexutil.Uint64    `json:"gasUsed"`
}

// CreateAccessList implements eth_createAccessList. It creates an access list for the given transaction.
// If the accesslist creation fails an error is returned.
// If the transaction itself fails, an vmErr is returned.
func (api *APIImpl) CreateAccessList(ctx context.Context, args ethapi2.CallArgs, blockNrOrHash *rpc.BlockNumberOrHash, stateOverrides *ethapi2.StateOverrides, optimizeGas *bool) (*accessListResult, error) {
	bNrOrHash := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber)
	if blockNrOrHash != nil {
		bNrOrHash = *blockNrOrHash
	}

	tx, err := api.db.BeginTemporalRo(ctx)
	if err != nil {
		return nil, err
	}
	defer tx.Rollback()

	chainConfig, err := api.chainConfig(ctx, tx)
	if err != nil {
		return nil, err
	}
	engine := api.engine()

	header, latest, err := api.headerByNumberOrHash(ctx, tx, *blockNrOrHash)
	if err != nil {
		return nil, err
	}
	if header == nil {
		return nil, fmt.Errorf("header not found")
	}
	blockNumber := header.Number.Uint64()

	var stateReader state.StateReader
	if latest {
		cacheView, err := api.stateCache.View(ctx, tx)
		if err != nil {
			return nil, err
		}
		stateReader = rpchelper.CreateLatestCachedStateReader(cacheView, tx)
	} else {

		err = api.BaseAPI.checkPruneHistory(ctx, tx, blockNumber+1)
		if err != nil {
			return nil, err
		}

		err = rpchelper.CheckBlockExecuted(tx, header.Number.Uint64())
		if err != nil {
			return nil, err
		}

		stateReader, err = rpchelper.CreateHistoryStateReader(ctx, tx, blockNumber+1, 0, api._txNumReader)
		if err != nil {
			return nil, err
		}
	}

	// If the gas amount is not set, extract this as it will depend on access
	// lists and we'll need to reestimate every time
	nogas := args.Gas == nil

	var to common.Address
	if args.To != nil {
		to = *args.To
	} else {
		// Require nonce to calculate address of created contract
		if args.Nonce == nil {
			var nonce uint64
			reply, err := api.txPool.Nonce(ctx, &txpoolproto.NonceRequest{
				Address: gointerfaces.ConvertAddressToH160(*args.From),
			}, &grpc.EmptyCallOption{})
			if err != nil {
				return nil, err
			}
			if reply.Found {
				nonce = reply.Nonce + 1
			} else {
				a, err := stateReader.ReadAccountData(accounts.InternAddress(*args.From))
				if err != nil {
					return nil, err
				}
				if a == nil {
					return nil, errors.New("Account: " + args.From.Hex() + " not found")
				}
				nonce = a.Nonce + 1
			}

			args.Nonce = (*hexutil.Uint64)(&nonce)
		}
		to = types.CreateAddress(*args.From, uint64(*args.Nonce))
	}

	if args.From == nil {
		args.From = &common.Address{}
	}

	// Retrieve the precompiles since they don't need to be added to the access list
	blockCtx := transactions.NewEVMBlockContext(engine, header, bNrOrHash.RequireCanonical, tx, api._blockReader, chainConfig)
	precompiles := vm.ActivePrecompiles(blockCtx.Rules(chainConfig))
	excl := make(map[common.Address]struct{})
	// Add 'from', 'to', precompiles to the exclusion list
	excl[*args.From] = struct{}{}
	excl[to] = struct{}{}
	for _, pc := range precompiles {
		excl[pc.Value()] = struct{}{}
	}

	// Create an initial tracer
	prevTracer := logger.NewAccessListTracer(nil, excl, nil)
	if args.AccessList != nil {
		prevTracer = logger.NewAccessListTracer(*args.AccessList, excl, nil)
	}

	for {
		state := state.New(stateReader)
		// Override the fields of specified contracts before execution.
		if stateOverrides != nil {
			if err := stateOverrides.Override(state, nil, blockCtx.Rules(chainConfig)); err != nil {
				return nil, err
			}
		}

		// Retrieve the current access list to expand
		accessList := prevTracer.AccessList()
		log.Trace("Creating access list", "input", accessList)

		// If no gas amount was specified, each unique access list needs it's own
		// gas calculation. This is quite expensive, but we need to be accurate
		// and it's convered by the sender only anyway.
		if nogas {
			args.Gas = nil
		}
		// Set the accesslist to the last al
		args.AccessList = &accessList

		msg, err := args.ToMessage(api.GasCap, header.BaseFee)
		if err != nil {
			return nil, err
		}

		// Apply the transaction with the access list tracer
		tracer := logger.NewAccessListTracer(accessList, excl, state)
		config := vm.Config{Tracer: tracer.Hooks(), NoBaseFee: true}
		txCtx := protocol.NewEVMTxContext(msg)

		evm := vm.NewEVM(blockCtx, txCtx, state, chainConfig, config)
		gp := new(protocol.GasPool).AddGas(msg.Gas()).AddBlobGas(msg.BlobGas())
		res, err := protocol.ApplyMessage(evm, msg, gp, true /* refunds */, false /* gasBailout */, engine)
		if err != nil {
			return nil, err
		}
		if tracer.Equal(prevTracer) {
			var errString string
			if res.Err != nil {
				errString = res.Err.Error()
			}
			accessList := &accessListResult{Accesslist: &accessList, Error: errString, GasUsed: hexutil.Uint64(res.ReceiptGasUsed)}
			if optimizeGas != nil && *optimizeGas {
				optimizeWarmAddrInAccessList(accessList, *args.From)
				optimizeWarmAddrInAccessList(accessList, to)
				optimizeWarmAddrInAccessList(accessList, header.Coinbase)
				for addr := range tracer.CreatedContracts() {
					if !tracer.UsedBeforeCreation(addr) {
						optimizeWarmAddrInAccessList(accessList, addr)
					}
				}
			}
			return accessList, nil
		}
		prevTracer = tracer
	}
}

// some addresses (like sender, recipient, block producer, and created contracts)
// are considered warm already, so we can save by adding these to the access list
// only if we are adding a lot of their respective storage slots as well
func optimizeWarmAddrInAccessList(accessList *accessListResult, addr common.Address) {
	indexToRemove := -1

	for i := 0; i < len(*accessList.Accesslist); i++ {
		entry := (*accessList.Accesslist)[i]
		if entry.Address != addr {
			continue
		}

		// https://eips.ethereum.org/EIPS/eip-2930#charging-less-for-accesses-in-the-access-list
		accessListSavingPerSlot := params.ColdSloadCostEIP2929 - params.WarmStorageReadCostEIP2929 - params.TxAccessListStorageKeyGas

		numSlots := uint64(len(entry.StorageKeys))
		if numSlots*accessListSavingPerSlot <= params.TxAccessListAddressGas {
			indexToRemove = i
		}
	}

	if indexToRemove >= 0 {
		*accessList.Accesslist = removeIndex(*accessList.Accesslist, indexToRemove)
	}
}

func removeIndex(s types.AccessList, index int) types.AccessList {
	return append(s[:index], s[index+1:]...)
}
