2020-06-30 10:12:51 +02:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
2022-05-24 20:39:40 +02:00
|
|
|
// This file is part of go-ethereum.
|
2020-06-30 10:12:51 +02:00
|
|
|
//
|
2022-05-24 20:39:40 +02:00
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2020-06-30 10:12:51 +02:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2022-05-24 20:39:40 +02:00
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
2020-06-30 10:12:51 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2022-05-24 20:39:40 +02:00
|
|
|
// GNU General Public License for more details.
|
2020-06-30 10:12:51 +02:00
|
|
|
//
|
2022-05-24 20:39:40 +02:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2020-06-30 10:12:51 +02:00
|
|
|
|
|
|
|
package t8ntool
|
|
|
|
|
|
|
|
import (
|
2024-03-22 18:53:53 +01:00
|
|
|
"encoding/json"
|
2020-06-30 10:12:51 +02:00
|
|
|
"fmt"
|
2024-03-22 18:53:53 +01:00
|
|
|
"io"
|
2020-06-30 10:12:51 +02:00
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
2021-08-25 15:04:26 +02:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
2020-06-30 10:12:51 +02:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/misc"
|
2023-08-01 19:58:26 +08:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
|
2020-06-30 10:12:51 +02:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2024-03-22 18:53:53 +01:00
|
|
|
"github.com/ethereum/go-ethereum/core/tracing"
|
2020-06-30 10:12:51 +02:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2024-03-22 18:53:53 +01:00
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers"
|
2020-06-30 10:12:51 +02:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2020-08-21 20:10:40 +08:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2024-02-13 21:49:53 +08:00
|
|
|
"github.com/ethereum/go-ethereum/triedb"
|
2024-01-23 14:51:58 +01:00
|
|
|
"github.com/holiman/uint256"
|
2020-06-30 10:12:51 +02:00
|
|
|
"golang.org/x/crypto/sha3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Prestate struct {
|
2024-02-16 19:05:33 +01:00
|
|
|
Env stEnv `json:"env"`
|
|
|
|
Pre types.GenesisAlloc `json:"pre"`
|
2020-06-30 10:12:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExecutionResult contains the execution status after running a state test, any
|
|
|
|
// error that might have occurred and a dump of the final state if requested.
|
|
|
|
type ExecutionResult struct {
|
2023-08-01 19:58:26 +08:00
|
|
|
StateRoot common.Hash `json:"stateRoot"`
|
|
|
|
TxRoot common.Hash `json:"txRoot"`
|
|
|
|
ReceiptRoot common.Hash `json:"receiptsRoot"`
|
|
|
|
LogsHash common.Hash `json:"logsHash"`
|
|
|
|
Bloom types.Bloom `json:"logsBloom" gencodec:"required"`
|
|
|
|
Receipts types.Receipts `json:"receipts"`
|
|
|
|
Rejected []*rejectedTx `json:"rejected,omitempty"`
|
|
|
|
Difficulty *math.HexOrDecimal256 `json:"currentDifficulty" gencodec:"required"`
|
|
|
|
GasUsed math.HexOrDecimal64 `json:"gasUsed"`
|
|
|
|
BaseFee *math.HexOrDecimal256 `json:"currentBaseFee,omitempty"`
|
|
|
|
WithdrawalsRoot *common.Hash `json:"withdrawalsRoot,omitempty"`
|
|
|
|
CurrentExcessBlobGas *math.HexOrDecimal64 `json:"currentExcessBlobGas,omitempty"`
|
2023-10-03 01:26:33 -06:00
|
|
|
CurrentBlobGasUsed *math.HexOrDecimal64 `json:"blobGasUsed,omitempty"`
|
2024-09-04 14:33:51 +02:00
|
|
|
RequestsHash *common.Hash `json:"requestsRoot,omitempty"`
|
|
|
|
DepositRequests *types.Deposits `json:"depositRequests,omitempty"`
|
2020-06-30 10:12:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ommer struct {
|
|
|
|
Delta uint64 `json:"delta"`
|
|
|
|
Address common.Address `json:"address"`
|
|
|
|
}
|
|
|
|
|
2022-04-13 02:24:02 +08:00
|
|
|
//go:generate go run github.com/fjl/gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go
|
2020-06-30 10:12:51 +02:00
|
|
|
type stEnv struct {
|
2023-08-22 14:33:39 +02:00
|
|
|
Coinbase common.Address `json:"currentCoinbase" gencodec:"required"`
|
|
|
|
Difficulty *big.Int `json:"currentDifficulty"`
|
|
|
|
Random *big.Int `json:"currentRandom"`
|
|
|
|
ParentDifficulty *big.Int `json:"parentDifficulty"`
|
|
|
|
ParentBaseFee *big.Int `json:"parentBaseFee,omitempty"`
|
|
|
|
ParentGasUsed uint64 `json:"parentGasUsed,omitempty"`
|
|
|
|
ParentGasLimit uint64 `json:"parentGasLimit,omitempty"`
|
|
|
|
GasLimit uint64 `json:"currentGasLimit" gencodec:"required"`
|
|
|
|
Number uint64 `json:"currentNumber" gencodec:"required"`
|
|
|
|
Timestamp uint64 `json:"currentTimestamp" gencodec:"required"`
|
|
|
|
ParentTimestamp uint64 `json:"parentTimestamp,omitempty"`
|
|
|
|
BlockHashes map[math.HexOrDecimal64]common.Hash `json:"blockHashes,omitempty"`
|
|
|
|
Ommers []ommer `json:"ommers,omitempty"`
|
|
|
|
Withdrawals []*types.Withdrawal `json:"withdrawals,omitempty"`
|
|
|
|
BaseFee *big.Int `json:"currentBaseFee,omitempty"`
|
|
|
|
ParentUncleHash common.Hash `json:"parentUncleHash"`
|
2023-10-03 01:26:33 -06:00
|
|
|
ExcessBlobGas *uint64 `json:"currentExcessBlobGas,omitempty"`
|
2023-08-22 14:33:39 +02:00
|
|
|
ParentExcessBlobGas *uint64 `json:"parentExcessBlobGas,omitempty"`
|
|
|
|
ParentBlobGasUsed *uint64 `json:"parentBlobGasUsed,omitempty"`
|
|
|
|
ParentBeaconBlockRoot *common.Hash `json:"parentBeaconBlockRoot"`
|
2020-06-30 10:12:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type stEnvMarshaling struct {
|
2023-08-01 19:58:26 +08:00
|
|
|
Coinbase common.UnprefixedAddress
|
|
|
|
Difficulty *math.HexOrDecimal256
|
|
|
|
Random *math.HexOrDecimal256
|
|
|
|
ParentDifficulty *math.HexOrDecimal256
|
|
|
|
ParentBaseFee *math.HexOrDecimal256
|
|
|
|
ParentGasUsed math.HexOrDecimal64
|
|
|
|
ParentGasLimit math.HexOrDecimal64
|
|
|
|
GasLimit math.HexOrDecimal64
|
|
|
|
Number math.HexOrDecimal64
|
|
|
|
Timestamp math.HexOrDecimal64
|
|
|
|
ParentTimestamp math.HexOrDecimal64
|
|
|
|
BaseFee *math.HexOrDecimal256
|
|
|
|
ExcessBlobGas *math.HexOrDecimal64
|
|
|
|
ParentExcessBlobGas *math.HexOrDecimal64
|
|
|
|
ParentBlobGasUsed *math.HexOrDecimal64
|
2020-06-30 10:12:51 +02:00
|
|
|
}
|
|
|
|
|
2021-06-07 14:37:56 +02:00
|
|
|
type rejectedTx struct {
|
|
|
|
Index int `json:"index"`
|
|
|
|
Err string `json:"error"`
|
|
|
|
}
|
|
|
|
|
2020-06-30 10:12:51 +02:00
|
|
|
// Apply applies a set of transactions to a pre-state
|
|
|
|
func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
|
2023-10-25 14:08:52 +02:00
|
|
|
txIt txIterator, miningReward int64,
|
2024-03-22 18:53:53 +01:00
|
|
|
getTracerFn func(txIndex int, txHash common.Hash) (*tracers.Tracer, io.WriteCloser, error)) (*state.StateDB, *ExecutionResult, []byte, error) {
|
2020-06-30 10:12:51 +02:00
|
|
|
// Capture errors for BLOCKHASH operation, if we haven't been supplied the
|
|
|
|
// required blockhashes
|
|
|
|
var hashError error
|
|
|
|
getHash := func(num uint64) common.Hash {
|
|
|
|
if pre.Env.BlockHashes == nil {
|
|
|
|
hashError = fmt.Errorf("getHash(%d) invoked, no blockhashes provided", num)
|
|
|
|
return common.Hash{}
|
|
|
|
}
|
|
|
|
h, ok := pre.Env.BlockHashes[math.HexOrDecimal64(num)]
|
|
|
|
if !ok {
|
|
|
|
hashError = fmt.Errorf("getHash(%d) invoked, blockhash for that block not provided", num)
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
statedb = MakePreState(rawdb.NewMemoryDatabase(), pre.Pre)
|
2023-04-21 12:52:02 +03:00
|
|
|
signer = types.MakeSigner(chainConfig, new(big.Int).SetUint64(pre.Env.Number), pre.Env.Timestamp)
|
2020-06-30 10:12:51 +02:00
|
|
|
gaspool = new(core.GasPool)
|
|
|
|
blockHash = common.Hash{0x13, 0x37}
|
2021-06-07 14:37:56 +02:00
|
|
|
rejectedTxs []*rejectedTx
|
2020-06-30 10:12:51 +02:00
|
|
|
includedTxs types.Transactions
|
|
|
|
gasUsed = uint64(0)
|
2024-01-03 09:12:20 -06:00
|
|
|
blobGasUsed = uint64(0)
|
2020-06-30 10:12:51 +02:00
|
|
|
receipts = make(types.Receipts, 0)
|
|
|
|
txIndex = 0
|
|
|
|
)
|
|
|
|
gaspool.AddGas(pre.Env.GasLimit)
|
2020-11-13 13:42:19 +01:00
|
|
|
vmContext := vm.BlockContext{
|
2020-06-30 10:12:51 +02:00
|
|
|
CanTransfer: core.CanTransfer,
|
|
|
|
Transfer: core.Transfer,
|
|
|
|
Coinbase: pre.Env.Coinbase,
|
|
|
|
BlockNumber: new(big.Int).SetUint64(pre.Env.Number),
|
2023-01-25 06:12:28 -05:00
|
|
|
Time: pre.Env.Timestamp,
|
2020-06-30 10:12:51 +02:00
|
|
|
Difficulty: pre.Env.Difficulty,
|
|
|
|
GasLimit: pre.Env.GasLimit,
|
|
|
|
GetHash: getHash,
|
|
|
|
}
|
2021-05-17 15:13:22 +02:00
|
|
|
// If currentBaseFee is defined, add it to the vmContext.
|
|
|
|
if pre.Env.BaseFee != nil {
|
|
|
|
vmContext.BaseFee = new(big.Int).Set(pre.Env.BaseFee)
|
|
|
|
}
|
2022-01-10 08:44:21 +01:00
|
|
|
// If random is defined, add it to the vmContext.
|
|
|
|
if pre.Env.Random != nil {
|
|
|
|
rnd := common.BigToHash(pre.Env.Random)
|
|
|
|
vmContext.Random = &rnd
|
|
|
|
}
|
2023-10-02 11:49:29 +02:00
|
|
|
// Calculate the BlobBaseFee
|
|
|
|
var excessBlobGas uint64
|
2023-08-01 19:58:26 +08:00
|
|
|
if pre.Env.ExcessBlobGas != nil {
|
2024-03-14 21:38:11 +08:00
|
|
|
excessBlobGas = *pre.Env.ExcessBlobGas
|
2023-10-02 11:49:29 +02:00
|
|
|
vmContext.BlobBaseFee = eip4844.CalcBlobFee(excessBlobGas)
|
2023-08-01 19:58:26 +08:00
|
|
|
} else {
|
|
|
|
// If it is not explicitly defined, but we have the parent values, we try
|
|
|
|
// to calculate it ourselves.
|
|
|
|
parentExcessBlobGas := pre.Env.ParentExcessBlobGas
|
|
|
|
parentBlobGasUsed := pre.Env.ParentBlobGasUsed
|
|
|
|
if parentExcessBlobGas != nil && parentBlobGasUsed != nil {
|
2023-10-02 11:49:29 +02:00
|
|
|
excessBlobGas = eip4844.CalcExcessBlobGas(*parentExcessBlobGas, *parentBlobGasUsed)
|
|
|
|
vmContext.BlobBaseFee = eip4844.CalcBlobFee(excessBlobGas)
|
2023-08-01 19:58:26 +08:00
|
|
|
}
|
|
|
|
}
|
2020-06-30 10:12:51 +02:00
|
|
|
// If DAO is supported/enabled, we need to handle it here. In geth 'proper', it's
|
|
|
|
// done in StateProcessor.Process(block, ...), right before transactions are applied.
|
|
|
|
if chainConfig.DAOForkSupport &&
|
|
|
|
chainConfig.DAOForkBlock != nil &&
|
|
|
|
chainConfig.DAOForkBlock.Cmp(new(big.Int).SetUint64(pre.Env.Number)) == 0 {
|
|
|
|
misc.ApplyDAOHardFork(statedb)
|
|
|
|
}
|
2023-08-22 14:33:39 +02:00
|
|
|
if beaconRoot := pre.Env.ParentBeaconBlockRoot; beaconRoot != nil {
|
|
|
|
evm := vm.NewEVM(vmContext, vm.TxContext{}, statedb, chainConfig, vmConfig)
|
|
|
|
core.ProcessBeaconBlockRoot(*beaconRoot, evm, statedb)
|
|
|
|
}
|
2024-08-26 10:39:35 +02:00
|
|
|
if pre.Env.BlockHashes != nil && chainConfig.IsPrague(new(big.Int).SetUint64(pre.Env.Number), pre.Env.Timestamp) {
|
|
|
|
var (
|
|
|
|
prevNumber = pre.Env.Number - 1
|
|
|
|
prevHash = pre.Env.BlockHashes[math.HexOrDecimal64(prevNumber)]
|
|
|
|
evm = vm.NewEVM(vmContext, vm.TxContext{}, statedb, chainConfig, vmConfig)
|
|
|
|
)
|
|
|
|
core.ProcessParentBlockHash(prevHash, evm, statedb)
|
|
|
|
}
|
2023-10-25 14:08:52 +02:00
|
|
|
for i := 0; txIt.Next(); i++ {
|
|
|
|
tx, err := txIt.Tx()
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("rejected tx", "index", i, "error", err)
|
|
|
|
rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()})
|
|
|
|
continue
|
|
|
|
}
|
2023-10-02 11:49:29 +02:00
|
|
|
if tx.Type() == types.BlobTxType && vmContext.BlobBaseFee == nil {
|
2023-08-01 19:58:26 +08:00
|
|
|
errMsg := "blob tx used but field env.ExcessBlobGas missing"
|
|
|
|
log.Warn("rejected tx", "index", i, "hash", tx.Hash(), "error", errMsg)
|
|
|
|
rejectedTxs = append(rejectedTxs, &rejectedTx{i, errMsg})
|
|
|
|
continue
|
|
|
|
}
|
core, core/types: plain Message struct (#25977)
Here, the core.Message interface turns into a plain struct and
types.Message gets removed.
This is a breaking change to packages core and core/types. While we do
not promise API stability for package core, we do for core/types. An
exception can be made for types.Message, since it doesn't have any
purpose apart from invoking the state transition in package core.
types.Message was also marked deprecated by the same commit it
got added in, 4dca5d4db7 (November 2016).
The core.Message interface was added in December 2014, in commit
db494170dc, for the purpose of 'testing' state transitions. It's the
same change that made transaction struct fields private. Before that,
the state transition used *types.Transaction directly.
Over time, multiple implementations of the interface accrued across
different packages, since constructing a Message is required whenever
one wants to invoke the state transition. These implementations all
looked very similar, a struct with private fields exposing the fields
as accessor methods.
By changing Message into a struct with public fields we can remove all
these useless interface implementations. It will also hopefully
simplify future changes to the type with less updates to apply across
all of go-ethereum when a field is added to Message.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
2023-03-09 05:19:12 -08:00
|
|
|
msg, err := core.TransactionToMessage(tx, signer, pre.Env.BaseFee)
|
2020-06-30 10:12:51 +02:00
|
|
|
if err != nil {
|
2021-06-07 14:37:56 +02:00
|
|
|
log.Warn("rejected tx", "index", i, "hash", tx.Hash(), "error", err)
|
|
|
|
rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()})
|
2020-06-30 10:12:51 +02:00
|
|
|
continue
|
|
|
|
}
|
2024-01-03 09:12:20 -06:00
|
|
|
txBlobGas := uint64(0)
|
2023-10-25 14:08:52 +02:00
|
|
|
if tx.Type() == types.BlobTxType {
|
2024-01-03 09:12:20 -06:00
|
|
|
txBlobGas = uint64(params.BlobTxBlobGasPerBlob * len(tx.BlobHashes()))
|
2023-10-25 14:08:52 +02:00
|
|
|
if used, max := blobGasUsed+txBlobGas, uint64(params.MaxBlobGasPerBlock); used > max {
|
|
|
|
err := fmt.Errorf("blob gas (%d) would exceed maximum allowance %d", used, max)
|
|
|
|
log.Warn("rejected tx", "index", i, "err", err)
|
|
|
|
rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2024-03-22 18:53:53 +01:00
|
|
|
tracer, traceOutput, err := getTracerFn(txIndex, tx.Hash())
|
2020-06-30 10:12:51 +02:00
|
|
|
if err != nil {
|
2023-10-25 14:08:52 +02:00
|
|
|
return nil, nil, nil, err
|
2020-06-30 10:12:51 +02:00
|
|
|
}
|
2024-03-22 18:53:53 +01:00
|
|
|
if tracer != nil {
|
|
|
|
vmConfig.Tracer = tracer.Hooks
|
|
|
|
}
|
2022-11-16 01:18:52 -08:00
|
|
|
statedb.SetTxContext(tx.Hash(), txIndex)
|
2023-03-07 18:23:52 +08:00
|
|
|
|
|
|
|
var (
|
|
|
|
txContext = core.NewEVMTxContext(msg)
|
|
|
|
snapshot = statedb.Snapshot()
|
|
|
|
prevGas = gaspool.Gas()
|
|
|
|
)
|
2021-02-25 07:26:57 -07:00
|
|
|
evm := vm.NewEVM(vmContext, txContext, statedb, chainConfig, vmConfig)
|
|
|
|
|
2024-03-22 18:53:53 +01:00
|
|
|
if tracer != nil && tracer.OnTxStart != nil {
|
|
|
|
tracer.OnTxStart(evm.GetVMContext(), tx, msg.From)
|
|
|
|
}
|
2020-06-30 10:12:51 +02:00
|
|
|
// (ret []byte, usedGas uint64, failed bool, err error)
|
|
|
|
msgResult, err := core.ApplyMessage(evm, msg, gaspool)
|
|
|
|
if err != nil {
|
|
|
|
statedb.RevertToSnapshot(snapshot)
|
core, core/types: plain Message struct (#25977)
Here, the core.Message interface turns into a plain struct and
types.Message gets removed.
This is a breaking change to packages core and core/types. While we do
not promise API stability for package core, we do for core/types. An
exception can be made for types.Message, since it doesn't have any
purpose apart from invoking the state transition in package core.
types.Message was also marked deprecated by the same commit it
got added in, 4dca5d4db7 (November 2016).
The core.Message interface was added in December 2014, in commit
db494170dc, for the purpose of 'testing' state transitions. It's the
same change that made transaction struct fields private. Before that,
the state transition used *types.Transaction directly.
Over time, multiple implementations of the interface accrued across
different packages, since constructing a Message is required whenever
one wants to invoke the state transition. These implementations all
looked very similar, a struct with private fields exposing the fields
as accessor methods.
By changing Message into a struct with public fields we can remove all
these useless interface implementations. It will also hopefully
simplify future changes to the type with less updates to apply across
all of go-ethereum when a field is added to Message.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
2023-03-09 05:19:12 -08:00
|
|
|
log.Info("rejected tx", "index", i, "hash", tx.Hash(), "from", msg.From, "error", err)
|
2021-06-07 14:37:56 +02:00
|
|
|
rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()})
|
2023-03-07 18:23:52 +08:00
|
|
|
gaspool.SetGas(prevGas)
|
2024-03-22 18:53:53 +01:00
|
|
|
if tracer != nil {
|
|
|
|
if tracer.OnTxEnd != nil {
|
|
|
|
tracer.OnTxEnd(nil, err)
|
|
|
|
}
|
|
|
|
if err := writeTraceResult(tracer, traceOutput); err != nil {
|
|
|
|
log.Warn("Error writing tracer output", "err", err)
|
|
|
|
}
|
|
|
|
}
|
2020-06-30 10:12:51 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
includedTxs = append(includedTxs, tx)
|
|
|
|
if hashError != nil {
|
2023-10-25 14:08:52 +02:00
|
|
|
return nil, nil, nil, NewError(ErrorMissingBlockhash, hashError)
|
2020-06-30 10:12:51 +02:00
|
|
|
}
|
2024-01-03 09:12:20 -06:00
|
|
|
blobGasUsed += txBlobGas
|
2020-06-30 10:12:51 +02:00
|
|
|
gasUsed += msgResult.UsedGas
|
2021-02-25 07:26:57 -07:00
|
|
|
|
|
|
|
// Receipt:
|
2020-06-30 10:12:51 +02:00
|
|
|
{
|
|
|
|
var root []byte
|
|
|
|
if chainConfig.IsByzantium(vmContext.BlockNumber) {
|
|
|
|
statedb.Finalise(true)
|
|
|
|
} else {
|
|
|
|
root = statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber)).Bytes()
|
|
|
|
}
|
|
|
|
|
2021-02-25 07:26:57 -07:00
|
|
|
// Create a new receipt for the transaction, storing the intermediate root and
|
|
|
|
// gas used by the tx.
|
|
|
|
receipt := &types.Receipt{Type: tx.Type(), PostState: root, CumulativeGasUsed: gasUsed}
|
|
|
|
if msgResult.Failed() {
|
|
|
|
receipt.Status = types.ReceiptStatusFailed
|
|
|
|
} else {
|
|
|
|
receipt.Status = types.ReceiptStatusSuccessful
|
|
|
|
}
|
2020-06-30 10:12:51 +02:00
|
|
|
receipt.TxHash = tx.Hash()
|
|
|
|
receipt.GasUsed = msgResult.UsedGas
|
2021-02-25 07:26:57 -07:00
|
|
|
|
|
|
|
// If the transaction created a contract, store the creation address in the receipt.
|
core, core/types: plain Message struct (#25977)
Here, the core.Message interface turns into a plain struct and
types.Message gets removed.
This is a breaking change to packages core and core/types. While we do
not promise API stability for package core, we do for core/types. An
exception can be made for types.Message, since it doesn't have any
purpose apart from invoking the state transition in package core.
types.Message was also marked deprecated by the same commit it
got added in, 4dca5d4db7 (November 2016).
The core.Message interface was added in December 2014, in commit
db494170dc, for the purpose of 'testing' state transitions. It's the
same change that made transaction struct fields private. Before that,
the state transition used *types.Transaction directly.
Over time, multiple implementations of the interface accrued across
different packages, since constructing a Message is required whenever
one wants to invoke the state transition. These implementations all
looked very similar, a struct with private fields exposing the fields
as accessor methods.
By changing Message into a struct with public fields we can remove all
these useless interface implementations. It will also hopefully
simplify future changes to the type with less updates to apply across
all of go-ethereum when a field is added to Message.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
2023-03-09 05:19:12 -08:00
|
|
|
if msg.To == nil {
|
2020-11-13 13:42:19 +01:00
|
|
|
receipt.ContractAddress = crypto.CreateAddress(evm.TxContext.Origin, tx.Nonce())
|
2020-06-30 10:12:51 +02:00
|
|
|
}
|
2021-02-25 07:26:57 -07:00
|
|
|
|
|
|
|
// Set the receipt logs and create the bloom filter.
|
2022-12-13 20:54:16 +08:00
|
|
|
receipt.Logs = statedb.GetLogs(tx.Hash(), vmContext.BlockNumber.Uint64(), blockHash)
|
2020-06-30 10:12:51 +02:00
|
|
|
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
|
2021-02-25 07:26:57 -07:00
|
|
|
// These three are non-consensus fields:
|
2020-06-30 10:12:51 +02:00
|
|
|
//receipt.BlockHash
|
2021-02-25 07:26:57 -07:00
|
|
|
//receipt.BlockNumber
|
2020-06-30 10:12:51 +02:00
|
|
|
receipt.TransactionIndex = uint(txIndex)
|
|
|
|
receipts = append(receipts, receipt)
|
2024-03-22 18:53:53 +01:00
|
|
|
if tracer != nil {
|
|
|
|
if tracer.Hooks.OnTxEnd != nil {
|
|
|
|
tracer.Hooks.OnTxEnd(receipt, nil)
|
|
|
|
}
|
2024-06-29 00:05:57 +08:00
|
|
|
if err = writeTraceResult(tracer, traceOutput); err != nil {
|
|
|
|
log.Warn("Error writing tracer output", "err", err)
|
|
|
|
}
|
2024-03-22 18:53:53 +01:00
|
|
|
}
|
2020-06-30 10:12:51 +02:00
|
|
|
}
|
2021-02-25 07:26:57 -07:00
|
|
|
|
2020-06-30 10:12:51 +02:00
|
|
|
txIndex++
|
|
|
|
}
|
|
|
|
statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber))
|
2022-11-09 09:34:42 +01:00
|
|
|
// Add mining reward? (-1 means rewards are disabled)
|
|
|
|
if miningReward >= 0 {
|
2020-06-30 10:12:51 +02:00
|
|
|
// Add mining reward. The mining reward may be `0`, which only makes a difference in the cases
|
|
|
|
// where
|
2023-07-15 16:35:30 +02:00
|
|
|
// - the coinbase self-destructed, or
|
2020-06-30 10:12:51 +02:00
|
|
|
// - there are only 'bad' transactions, which aren't executed. In those cases,
|
|
|
|
// the coinbase gets no txfee, so isn't created, and thus needs to be touched
|
|
|
|
var (
|
|
|
|
blockReward = big.NewInt(miningReward)
|
|
|
|
minerReward = new(big.Int).Set(blockReward)
|
2024-06-29 00:08:31 +08:00
|
|
|
perOmmer = new(big.Int).Rsh(blockReward, 5)
|
2020-06-30 10:12:51 +02:00
|
|
|
)
|
|
|
|
for _, ommer := range pre.Env.Ommers {
|
|
|
|
// Add 1/32th for each ommer included
|
|
|
|
minerReward.Add(minerReward, perOmmer)
|
|
|
|
// Add (8-delta)/8
|
|
|
|
reward := big.NewInt(8)
|
2022-06-14 14:09:48 +02:00
|
|
|
reward.Sub(reward, new(big.Int).SetUint64(ommer.Delta))
|
2020-06-30 10:12:51 +02:00
|
|
|
reward.Mul(reward, blockReward)
|
2024-06-29 00:08:31 +08:00
|
|
|
reward.Rsh(reward, 3)
|
2024-03-22 18:53:53 +01:00
|
|
|
statedb.AddBalance(ommer.Address, uint256.MustFromBig(reward), tracing.BalanceIncreaseRewardMineUncle)
|
2020-06-30 10:12:51 +02:00
|
|
|
}
|
2024-03-22 18:53:53 +01:00
|
|
|
statedb.AddBalance(pre.Env.Coinbase, uint256.MustFromBig(minerReward), tracing.BalanceIncreaseRewardMineBlock)
|
2020-06-30 10:12:51 +02:00
|
|
|
}
|
2023-01-25 15:32:25 +01:00
|
|
|
// Apply withdrawals
|
|
|
|
for _, w := range pre.Env.Withdrawals {
|
|
|
|
// Amount is in gwei, turn into wei
|
|
|
|
amount := new(big.Int).Mul(new(big.Int).SetUint64(w.Amount), big.NewInt(params.GWei))
|
2024-03-22 18:53:53 +01:00
|
|
|
statedb.AddBalance(w.Address, uint256.MustFromBig(amount), tracing.BalanceIncreaseWithdrawal)
|
2023-01-25 15:32:25 +01:00
|
|
|
}
|
2020-06-30 10:12:51 +02:00
|
|
|
// Commit block
|
2023-07-24 18:22:09 +08:00
|
|
|
root, err := statedb.Commit(vmContext.BlockNumber.Uint64(), chainConfig.IsEIP158(vmContext.BlockNumber))
|
2020-06-30 10:12:51 +02:00
|
|
|
if err != nil {
|
2023-10-25 14:08:52 +02:00
|
|
|
return nil, nil, nil, NewError(ErrorEVM, fmt.Errorf("could not commit state: %v", err))
|
2020-06-30 10:12:51 +02:00
|
|
|
}
|
|
|
|
execRs := &ExecutionResult{
|
|
|
|
StateRoot: root,
|
2021-02-02 20:09:23 +08:00
|
|
|
TxRoot: types.DeriveSha(includedTxs, trie.NewStackTrie(nil)),
|
|
|
|
ReceiptRoot: types.DeriveSha(receipts, trie.NewStackTrie(nil)),
|
2020-06-30 10:12:51 +02:00
|
|
|
Bloom: types.CreateBloom(receipts),
|
|
|
|
LogsHash: rlpHash(statedb.Logs()),
|
|
|
|
Receipts: receipts,
|
|
|
|
Rejected: rejectedTxs,
|
2021-08-25 15:04:26 +02:00
|
|
|
Difficulty: (*math.HexOrDecimal256)(vmContext.Difficulty),
|
2021-11-16 00:43:58 -07:00
|
|
|
GasUsed: (math.HexOrDecimal64)(gasUsed),
|
2022-10-28 04:25:37 -06:00
|
|
|
BaseFee: (*math.HexOrDecimal256)(vmContext.BaseFee),
|
2020-06-30 10:12:51 +02:00
|
|
|
}
|
2023-01-25 15:32:25 +01:00
|
|
|
if pre.Env.Withdrawals != nil {
|
|
|
|
h := types.DeriveSha(types.Withdrawals(pre.Env.Withdrawals), trie.NewStackTrie(nil))
|
|
|
|
execRs.WithdrawalsRoot = &h
|
|
|
|
}
|
2023-10-02 11:49:29 +02:00
|
|
|
if vmContext.BlobBaseFee != nil {
|
|
|
|
execRs.CurrentExcessBlobGas = (*math.HexOrDecimal64)(&excessBlobGas)
|
2023-08-01 19:58:26 +08:00
|
|
|
execRs.CurrentBlobGasUsed = (*math.HexOrDecimal64)(&blobGasUsed)
|
|
|
|
}
|
2024-09-04 14:33:51 +02:00
|
|
|
if chainConfig.IsPrague(vmContext.BlockNumber, vmContext.Time) {
|
|
|
|
// Parse the requests from the logs
|
|
|
|
var allLogs []*types.Log
|
|
|
|
for _, receipt := range receipts {
|
|
|
|
allLogs = append(allLogs, receipt.Logs...)
|
|
|
|
}
|
|
|
|
requests, err := core.ParseDepositLogs(allLogs, chainConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, NewError(ErrorEVM, fmt.Errorf("could not parse requests logs: %v", err))
|
|
|
|
}
|
|
|
|
// Calculate the requests root
|
|
|
|
h := types.DeriveSha(requests, trie.NewStackTrie(nil))
|
|
|
|
execRs.RequestsHash = &h
|
|
|
|
// Get the deposits from the requests
|
|
|
|
deposits := make(types.Deposits, 0)
|
|
|
|
for _, req := range requests {
|
|
|
|
if dep, ok := req.Inner().(*types.Deposit); ok {
|
|
|
|
deposits = append(deposits, dep)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
execRs.DepositRequests = &deposits
|
|
|
|
}
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-21 03:31:45 +08:00
|
|
|
// Re-create statedb instance with new root upon the updated database
|
|
|
|
// for accessing latest states.
|
2024-09-05 18:10:47 +08:00
|
|
|
statedb, err = state.New(root, statedb.Database())
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-21 03:31:45 +08:00
|
|
|
if err != nil {
|
2023-10-25 14:08:52 +02:00
|
|
|
return nil, nil, nil, NewError(ErrorEVM, fmt.Errorf("could not reopen state: %v", err))
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-21 03:31:45 +08:00
|
|
|
}
|
2023-10-25 14:08:52 +02:00
|
|
|
body, _ := rlp.EncodeToBytes(includedTxs)
|
|
|
|
return statedb, execRs, body, nil
|
2020-06-30 10:12:51 +02:00
|
|
|
}
|
|
|
|
|
2024-02-16 19:05:33 +01:00
|
|
|
func MakePreState(db ethdb.Database, accounts types.GenesisAlloc) *state.StateDB {
|
2024-09-05 18:10:47 +08:00
|
|
|
tdb := triedb.NewDatabase(db, &triedb.Config{Preimages: true})
|
|
|
|
sdb := state.NewDatabase(tdb, nil)
|
|
|
|
statedb, _ := state.New(types.EmptyRootHash, sdb)
|
2020-06-30 10:12:51 +02:00
|
|
|
for addr, a := range accounts {
|
|
|
|
statedb.SetCode(addr, a.Code)
|
|
|
|
statedb.SetNonce(addr, a.Nonce)
|
2024-03-22 18:53:53 +01:00
|
|
|
statedb.SetBalance(addr, uint256.MustFromBig(a.Balance), tracing.BalanceIncreaseGenesisBalance)
|
2020-06-30 10:12:51 +02:00
|
|
|
for k, v := range a.Storage {
|
|
|
|
statedb.SetState(addr, k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Commit and re-open to start with a clean state.
|
2023-07-24 18:22:09 +08:00
|
|
|
root, _ := statedb.Commit(0, false)
|
2024-09-05 18:10:47 +08:00
|
|
|
statedb, _ = state.New(root, sdb)
|
2020-06-30 10:12:51 +02:00
|
|
|
return statedb
|
|
|
|
}
|
|
|
|
|
|
|
|
func rlpHash(x interface{}) (h common.Hash) {
|
|
|
|
hw := sha3.NewLegacyKeccak256()
|
|
|
|
rlp.Encode(hw, x)
|
|
|
|
hw.Sum(h[:0])
|
|
|
|
return h
|
|
|
|
}
|
2021-08-25 15:04:26 +02:00
|
|
|
|
|
|
|
// calcDifficulty is based on ethash.CalcDifficulty. This method is used in case
|
|
|
|
// the caller does not provide an explicit difficulty, but instead provides only
|
|
|
|
// parent timestamp + difficulty.
|
|
|
|
// Note: this method only works for ethash engine.
|
|
|
|
func calcDifficulty(config *params.ChainConfig, number, currentTime, parentTime uint64,
|
|
|
|
parentDifficulty *big.Int, parentUncleHash common.Hash) *big.Int {
|
|
|
|
uncleHash := parentUncleHash
|
|
|
|
if uncleHash == (common.Hash{}) {
|
|
|
|
uncleHash = types.EmptyUncleHash
|
|
|
|
}
|
|
|
|
parent := &types.Header{
|
|
|
|
ParentHash: common.Hash{},
|
|
|
|
UncleHash: uncleHash,
|
|
|
|
Difficulty: parentDifficulty,
|
|
|
|
Number: new(big.Int).SetUint64(number - 1),
|
|
|
|
Time: parentTime,
|
|
|
|
}
|
|
|
|
return ethash.CalcDifficulty(config, currentTime, parent)
|
|
|
|
}
|
2024-03-22 18:53:53 +01:00
|
|
|
|
|
|
|
func writeTraceResult(tracer *tracers.Tracer, f io.WriteCloser) error {
|
|
|
|
defer f.Close()
|
|
|
|
result, err := tracer.GetResult()
|
|
|
|
if err != nil || result == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = json.NewEncoder(f).Encode(result)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|