2019-05-27 13:51:49 +00:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
2016-04-14 16:18:24 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library 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.
|
|
|
|
//
|
|
|
|
// The go-ethereum library 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 the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-10-19 14:08:17 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2023-01-13 18:12:18 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/chain"
|
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/consensus"
|
|
|
|
"github.com/ledgerwatch/erigon/core/state"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
"github.com/ledgerwatch/erigon/core/vm"
|
2022-11-30 01:31:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/crypto"
|
2015-10-19 14:08:17 +00:00
|
|
|
)
|
|
|
|
|
2021-02-25 14:26:57 +00:00
|
|
|
// applyTransaction attempts to apply a transaction to the given state database
|
2017-01-05 10:52:10 +00:00
|
|
|
// and uses the input parameters for its environment. It returns the receipt
|
|
|
|
// for the transaction, gas used and an error if the transaction failed,
|
|
|
|
// indicating the block was invalid.
|
2023-06-23 09:10:23 +00:00
|
|
|
func applyTransaction(config *chain.Config, engine consensus.EngineReader, gp *GasPool, ibs *state.IntraBlockState,
|
2023-07-28 10:12:05 +00:00
|
|
|
stateWriter state.StateWriter, header *types.Header, tx types.Transaction, usedGas, usedBlobGas *uint64,
|
2023-10-05 05:23:08 +00:00
|
|
|
evm *vm.EVM, cfg vm.Config) (*types.Receipt, []byte, error) {
|
2022-05-26 16:20:34 +00:00
|
|
|
rules := evm.ChainRules()
|
2023-06-15 08:57:27 +00:00
|
|
|
msg, err := tx.AsMessage(*types.MakeSigner(config, header.Number.Uint64(), header.Time), header.BaseFee, rules)
|
2016-11-02 12:44:13 +00:00
|
|
|
if err != nil {
|
2021-06-25 18:13:40 +00:00
|
|
|
return nil, nil, err
|
2016-11-02 12:44:13 +00:00
|
|
|
}
|
2022-10-12 08:27:48 +00:00
|
|
|
msg.SetCheckNonce(!cfg.StatelessExec)
|
2021-02-25 14:26:57 +00:00
|
|
|
|
2022-11-07 12:10:07 +00:00
|
|
|
if msg.FeeCap().IsZero() && engine != nil {
|
|
|
|
// Only zero-gas transactions may be service ones
|
2023-01-13 18:12:18 +00:00
|
|
|
syscall := func(contract libcommon.Address, data []byte) ([]byte, error) {
|
2023-06-02 20:26:19 +00:00
|
|
|
return SysCallContract(contract, data, config, ibs, header, engine, true /* constCall */)
|
2022-10-26 11:03:47 +00:00
|
|
|
}
|
|
|
|
msg.SetIsFree(engine.IsServiceTransaction(msg.From(), syscall))
|
|
|
|
}
|
|
|
|
|
2020-11-13 12:42:19 +00:00
|
|
|
txContext := NewEVMTxContext(msg)
|
2020-08-01 16:56:57 +00:00
|
|
|
if cfg.TraceJumpDest {
|
2021-03-12 17:26:06 +00:00
|
|
|
txContext.TxHash = tx.Hash()
|
2020-08-01 16:56:57 +00:00
|
|
|
}
|
2020-10-23 06:26:57 +00:00
|
|
|
|
2020-11-13 12:42:19 +00:00
|
|
|
// Update the evm with the new transaction context.
|
2022-10-26 11:03:47 +00:00
|
|
|
evm.Reset(txContext, ibs)
|
2021-07-08 12:40:43 +00:00
|
|
|
|
2022-01-23 10:32:50 +00:00
|
|
|
result, err := ApplyMessage(evm, msg, gp, true /* refunds */, false /* gasBailout */)
|
2015-10-19 14:08:17 +00:00
|
|
|
if err != nil {
|
2021-06-25 18:13:40 +00:00
|
|
|
return nil, nil, err
|
2015-10-19 14:08:17 +00:00
|
|
|
}
|
|
|
|
// Update the state with pending changes
|
2022-10-26 11:03:47 +00:00
|
|
|
if err = ibs.FinalizeTx(rules, stateWriter); err != nil {
|
2021-06-25 18:13:40 +00:00
|
|
|
return nil, nil, err
|
2017-07-17 08:34:53 +00:00
|
|
|
}
|
2022-06-17 13:14:32 +00:00
|
|
|
*usedGas += result.UsedGas
|
2023-07-28 10:12:05 +00:00
|
|
|
if usedBlobGas != nil {
|
|
|
|
*usedBlobGas += tx.GetBlobGas()
|
2023-06-23 09:10:23 +00:00
|
|
|
}
|
2017-07-17 08:34:53 +00:00
|
|
|
|
2021-02-25 14:26:57 +00:00
|
|
|
// Set the receipt logs and create the bloom filter.
|
2020-04-22 08:25:36 +00:00
|
|
|
// based on the eip phase, we're passing whether the root touch-delete accounts.
|
2020-10-12 08:39:04 +00:00
|
|
|
var receipt *types.Receipt
|
|
|
|
if !cfg.NoReceipts {
|
2021-02-25 14:26:57 +00:00
|
|
|
// by the tx.
|
2021-03-12 17:26:06 +00:00
|
|
|
receipt = &types.Receipt{Type: tx.Type(), CumulativeGasUsed: *usedGas}
|
2021-02-25 14:26:57 +00:00
|
|
|
if result.Failed() {
|
|
|
|
receipt.Status = types.ReceiptStatusFailed
|
|
|
|
} else {
|
|
|
|
receipt.Status = types.ReceiptStatusSuccessful
|
|
|
|
}
|
2020-10-12 08:39:04 +00:00
|
|
|
receipt.TxHash = tx.Hash()
|
|
|
|
receipt.GasUsed = result.UsedGas
|
|
|
|
// if the transaction created a contract, store the creation address in the receipt.
|
|
|
|
if msg.To() == nil {
|
2023-11-29 02:29:16 +00:00
|
|
|
receipt.ContractAddress = crypto.CreateAddress(evm.Origin, tx.GetNonce())
|
2020-10-12 08:39:04 +00:00
|
|
|
}
|
|
|
|
// Set the receipt logs and create a bloom for filtering
|
2022-10-26 11:03:47 +00:00
|
|
|
receipt.Logs = ibs.GetLogs(tx.Hash())
|
2020-10-12 08:39:04 +00:00
|
|
|
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
|
2021-02-25 14:26:57 +00:00
|
|
|
receipt.BlockNumber = header.Number
|
2022-10-26 11:03:47 +00:00
|
|
|
receipt.TransactionIndex = uint(ibs.TxIndex())
|
2015-10-19 14:08:17 +00:00
|
|
|
}
|
2022-03-09 22:45:24 +00:00
|
|
|
|
2021-06-25 18:13:40 +00:00
|
|
|
return receipt, result.ReturnData, err
|
2015-10-19 14:08:17 +00:00
|
|
|
}
|
2020-11-13 12:42:19 +00:00
|
|
|
|
|
|
|
// ApplyTransaction attempts to apply a transaction to the given state database
|
|
|
|
// and uses the input parameters for its environment. It returns the receipt
|
|
|
|
// for the transaction, gas used and an error if the transaction failed,
|
|
|
|
// indicating the block was invalid.
|
2023-06-23 09:10:23 +00:00
|
|
|
func ApplyTransaction(config *chain.Config, blockHashFunc func(n uint64) libcommon.Hash, engine consensus.EngineReader,
|
|
|
|
author *libcommon.Address, gp *GasPool, ibs *state.IntraBlockState, stateWriter state.StateWriter,
|
2023-07-28 10:12:05 +00:00
|
|
|
header *types.Header, tx types.Transaction, usedGas, usedBlobGas *uint64, cfg vm.Config,
|
2023-06-23 09:10:23 +00:00
|
|
|
) (*types.Receipt, []byte, error) {
|
2020-11-13 12:42:19 +00:00
|
|
|
// Create a new context to be used in the EVM environment
|
2021-12-06 14:58:53 +00:00
|
|
|
|
2022-05-14 19:17:23 +00:00
|
|
|
// Add addresses to access list if applicable
|
|
|
|
// about the transaction and calling mechanisms.
|
|
|
|
cfg.SkipAnalysis = SkipAnalysis(config, header.Number.Uint64())
|
|
|
|
|
2023-06-02 20:26:19 +00:00
|
|
|
blockContext := NewEVMBlockContext(header, blockHashFunc, engine, author)
|
2023-01-30 15:56:58 +00:00
|
|
|
vmenv := vm.NewEVM(blockContext, evmtypes.TxContext{}, ibs, config, cfg)
|
2021-12-06 14:58:53 +00:00
|
|
|
|
2023-07-28 10:12:05 +00:00
|
|
|
return applyTransaction(config, engine, gp, ibs, stateWriter, header, tx, usedGas, usedBlobGas, vmenv, cfg)
|
2020-11-13 12:42:19 +00:00
|
|
|
}
|