erigon-pulse/core/state_processor.go

120 lines
4.8 KiB
Go
Raw Permalink Normal View History

// 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/>.
package core
import (
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/vm"
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
"github.com/ledgerwatch/erigon/crypto"
)
all: add support for EIP-2718, EIP-2930 transactions (#21502) This adds support for EIP-2718 typed transactions as well as EIP-2930 access list transactions (tx type 1). These EIPs are scheduled for the Berlin fork. There very few changes to existing APIs in core/types, and several new APIs to deal with access list transactions. In particular, there are two new constructor functions for transactions: types.NewTx and types.SignNewTx. Since the canonical encoding of typed transactions is not RLP-compatible, Transaction now has new methods for encoding and decoding: MarshalBinary and UnmarshalBinary. The existing EIP-155 signer does not support the new transaction types. All code dealing with transaction signatures should be updated to use the newer EIP-2930 signer. To make this easier for future updates, we have added new constructor functions for types.Signer: types.LatestSigner and types.LatestSignerForChainID. This change also adds support for the YoloV3 testnet. Co-authored-by: Martin Holst Swende <martin@swende.se> Co-authored-by: Felix Lange <fjl@twurst.com> Co-authored-by: Ryan Schneider <ryanleeschneider@gmail.com> # Conflicts: # accounts/abi/bind/backends/simulated.go # cmd/evm/internal/t8ntool/execution.go # cmd/evm/internal/t8ntool/transition.go # cmd/geth/main.go # cmd/geth/usage.go # core/bench_test.go # core/state/statedb.go # core/state_prefetcher.go # core/state_processor.go # core/state_transition.go # core/tx_pool.go # core/types/block.go # core/types/derive_sha.go # core/types/gen_tx_json.go # core/types/receipt.go # core/types/receipt_test.go # core/types/transaction.go # core/types/transaction_signing.go # core/types/transaction_test.go # ethclient/ethclient.go # ethclient/signer.go # graphql/graphql.go # internal/ethapi/api.go # internal/guide/guide_test.go # les/benchmark.go # les/odr_test.go # light/odr_test.go # light/txpool.go # miner/worker.go # miner/worker_test.go # signer/core/api.go # tests/state_test_util.go # trie/stacktrie_test.go # turbo/stages/blockchain_test.go
2021-02-25 14:26:57 +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.
func applyTransaction(config *chain.Config, engine consensus.EngineReader, gp *GasPool, ibs *state.IntraBlockState,
stateWriter state.StateWriter, header *types.Header, tx types.Transaction, usedGas, usedBlobGas *uint64,
evm *vm.EVM, cfg vm.Config) (*types.Receipt, []byte, error) {
rules := evm.ChainRules()
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 {
return nil, nil, err
2016-11-02 12:44:13 +00:00
}
msg.SetCheckNonce(!cfg.StatelessExec)
all: add support for EIP-2718, EIP-2930 transactions (#21502) This adds support for EIP-2718 typed transactions as well as EIP-2930 access list transactions (tx type 1). These EIPs are scheduled for the Berlin fork. There very few changes to existing APIs in core/types, and several new APIs to deal with access list transactions. In particular, there are two new constructor functions for transactions: types.NewTx and types.SignNewTx. Since the canonical encoding of typed transactions is not RLP-compatible, Transaction now has new methods for encoding and decoding: MarshalBinary and UnmarshalBinary. The existing EIP-155 signer does not support the new transaction types. All code dealing with transaction signatures should be updated to use the newer EIP-2930 signer. To make this easier for future updates, we have added new constructor functions for types.Signer: types.LatestSigner and types.LatestSignerForChainID. This change also adds support for the YoloV3 testnet. Co-authored-by: Martin Holst Swende <martin@swende.se> Co-authored-by: Felix Lange <fjl@twurst.com> Co-authored-by: Ryan Schneider <ryanleeschneider@gmail.com> # Conflicts: # accounts/abi/bind/backends/simulated.go # cmd/evm/internal/t8ntool/execution.go # cmd/evm/internal/t8ntool/transition.go # cmd/geth/main.go # cmd/geth/usage.go # core/bench_test.go # core/state/statedb.go # core/state_prefetcher.go # core/state_processor.go # core/state_transition.go # core/tx_pool.go # core/types/block.go # core/types/derive_sha.go # core/types/gen_tx_json.go # core/types/receipt.go # core/types/receipt_test.go # core/types/transaction.go # core/types/transaction_signing.go # core/types/transaction_test.go # ethclient/ethclient.go # ethclient/signer.go # graphql/graphql.go # internal/ethapi/api.go # internal/guide/guide_test.go # les/benchmark.go # les/odr_test.go # light/odr_test.go # light/txpool.go # miner/worker.go # miner/worker_test.go # signer/core/api.go # tests/state_test_util.go # trie/stacktrie_test.go # turbo/stages/blockchain_test.go
2021-02-25 14:26:57 +00:00
if msg.FeeCap().IsZero() && engine != nil {
// Only zero-gas transactions may be service ones
syscall := func(contract libcommon.Address, data []byte) ([]byte, error) {
return SysCallContract(contract, data, config, ibs, header, engine, true /* constCall */)
}
msg.SetIsFree(engine.IsServiceTransaction(msg.From(), syscall))
}
txContext := NewEVMTxContext(msg)
if cfg.TraceJumpDest {
2021-03-12 17:26:06 +00:00
txContext.TxHash = tx.Hash()
}
// Update the evm with the new transaction context.
evm.Reset(txContext, ibs)
result, err := ApplyMessage(evm, msg, gp, true /* refunds */, false /* gasBailout */)
if err != nil {
return nil, nil, err
}
// Update the state with pending changes
if err = ibs.FinalizeTx(rules, stateWriter); err != nil {
return nil, nil, err
}
2022-06-17 13:14:32 +00:00
*usedGas += result.UsedGas
if usedBlobGas != nil {
*usedBlobGas += tx.GetBlobGas()
}
all: add support for EIP-2718, EIP-2930 transactions (#21502) This adds support for EIP-2718 typed transactions as well as EIP-2930 access list transactions (tx type 1). These EIPs are scheduled for the Berlin fork. There very few changes to existing APIs in core/types, and several new APIs to deal with access list transactions. In particular, there are two new constructor functions for transactions: types.NewTx and types.SignNewTx. Since the canonical encoding of typed transactions is not RLP-compatible, Transaction now has new methods for encoding and decoding: MarshalBinary and UnmarshalBinary. The existing EIP-155 signer does not support the new transaction types. All code dealing with transaction signatures should be updated to use the newer EIP-2930 signer. To make this easier for future updates, we have added new constructor functions for types.Signer: types.LatestSigner and types.LatestSignerForChainID. This change also adds support for the YoloV3 testnet. Co-authored-by: Martin Holst Swende <martin@swende.se> Co-authored-by: Felix Lange <fjl@twurst.com> Co-authored-by: Ryan Schneider <ryanleeschneider@gmail.com> # Conflicts: # accounts/abi/bind/backends/simulated.go # cmd/evm/internal/t8ntool/execution.go # cmd/evm/internal/t8ntool/transition.go # cmd/geth/main.go # cmd/geth/usage.go # core/bench_test.go # core/state/statedb.go # core/state_prefetcher.go # core/state_processor.go # core/state_transition.go # core/tx_pool.go # core/types/block.go # core/types/derive_sha.go # core/types/gen_tx_json.go # core/types/receipt.go # core/types/receipt_test.go # core/types/transaction.go # core/types/transaction_signing.go # core/types/transaction_test.go # ethclient/ethclient.go # ethclient/signer.go # graphql/graphql.go # internal/ethapi/api.go # internal/guide/guide_test.go # les/benchmark.go # les/odr_test.go # light/odr_test.go # light/txpool.go # miner/worker.go # miner/worker_test.go # signer/core/api.go # tests/state_test_util.go # trie/stacktrie_test.go # turbo/stages/blockchain_test.go
2021-02-25 14:26:57 +00:00
// Set the receipt logs and create the bloom filter.
all: seperate consensus error and evm internal error (#20830) * all: seperate consensus error and evm internal error There are actually two types of error will be returned when a tranaction/message call is executed: (a) consensus error (b) evm internal error. The former should be converted to a consensus issue, e.g. The sender doesn't enough asset to purchase the gas it specifies. The latter is allowed since evm itself is a blackbox and internal error is allowed to happen. This PR emphasizes the difference by introducing a executionResult structure. The evm error is embedded inside. So if any error returned, it indicates consensus issue happens. And also this PR improve the `EstimateGas` API to return the concrete revert reason if the transaction always fails * all: polish * accounts/abi/bind/backends: add tests * accounts/abi/bind/backends, internal: cleanup error message * all: address comments * core: fix lint * accounts, core, eth, internal: address comments * accounts, internal: resolve revert reason if possible * accounts, internal: address comments # Conflicts: # accounts/abi/abi.go # accounts/abi/bind/backends/simulated.go # cmd/geth/retesteth.go # core/state/snapshot/difflayer_test.go # core/state/snapshot/disklayer_test.go # core/state/snapshot/iterator_test.go # core/state_processor.go # core/state_transition.go # core/vm/evm.go # core/vm/instructions.go # core/vm/jump_table.go # eth/api_tracer.go # internal/ethapi/api.go # les/odr_test.go # light/odr_test.go # tests/state_test_util.go
2020-04-22 08:25:36 +00:00
// based on the eip phase, we're passing whether the root touch-delete accounts.
var receipt *types.Receipt
if !cfg.NoReceipts {
all: add support for EIP-2718, EIP-2930 transactions (#21502) This adds support for EIP-2718 typed transactions as well as EIP-2930 access list transactions (tx type 1). These EIPs are scheduled for the Berlin fork. There very few changes to existing APIs in core/types, and several new APIs to deal with access list transactions. In particular, there are two new constructor functions for transactions: types.NewTx and types.SignNewTx. Since the canonical encoding of typed transactions is not RLP-compatible, Transaction now has new methods for encoding and decoding: MarshalBinary and UnmarshalBinary. The existing EIP-155 signer does not support the new transaction types. All code dealing with transaction signatures should be updated to use the newer EIP-2930 signer. To make this easier for future updates, we have added new constructor functions for types.Signer: types.LatestSigner and types.LatestSignerForChainID. This change also adds support for the YoloV3 testnet. Co-authored-by: Martin Holst Swende <martin@swende.se> Co-authored-by: Felix Lange <fjl@twurst.com> Co-authored-by: Ryan Schneider <ryanleeschneider@gmail.com> # Conflicts: # accounts/abi/bind/backends/simulated.go # cmd/evm/internal/t8ntool/execution.go # cmd/evm/internal/t8ntool/transition.go # cmd/geth/main.go # cmd/geth/usage.go # core/bench_test.go # core/state/statedb.go # core/state_prefetcher.go # core/state_processor.go # core/state_transition.go # core/tx_pool.go # core/types/block.go # core/types/derive_sha.go # core/types/gen_tx_json.go # core/types/receipt.go # core/types/receipt_test.go # core/types/transaction.go # core/types/transaction_signing.go # core/types/transaction_test.go # ethclient/ethclient.go # ethclient/signer.go # graphql/graphql.go # internal/ethapi/api.go # internal/guide/guide_test.go # les/benchmark.go # les/odr_test.go # light/odr_test.go # light/txpool.go # miner/worker.go # miner/worker_test.go # signer/core/api.go # tests/state_test_util.go # trie/stacktrie_test.go # turbo/stages/blockchain_test.go
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}
all: add support for EIP-2718, EIP-2930 transactions (#21502) This adds support for EIP-2718 typed transactions as well as EIP-2930 access list transactions (tx type 1). These EIPs are scheduled for the Berlin fork. There very few changes to existing APIs in core/types, and several new APIs to deal with access list transactions. In particular, there are two new constructor functions for transactions: types.NewTx and types.SignNewTx. Since the canonical encoding of typed transactions is not RLP-compatible, Transaction now has new methods for encoding and decoding: MarshalBinary and UnmarshalBinary. The existing EIP-155 signer does not support the new transaction types. All code dealing with transaction signatures should be updated to use the newer EIP-2930 signer. To make this easier for future updates, we have added new constructor functions for types.Signer: types.LatestSigner and types.LatestSignerForChainID. This change also adds support for the YoloV3 testnet. Co-authored-by: Martin Holst Swende <martin@swende.se> Co-authored-by: Felix Lange <fjl@twurst.com> Co-authored-by: Ryan Schneider <ryanleeschneider@gmail.com> # Conflicts: # accounts/abi/bind/backends/simulated.go # cmd/evm/internal/t8ntool/execution.go # cmd/evm/internal/t8ntool/transition.go # cmd/geth/main.go # cmd/geth/usage.go # core/bench_test.go # core/state/statedb.go # core/state_prefetcher.go # core/state_processor.go # core/state_transition.go # core/tx_pool.go # core/types/block.go # core/types/derive_sha.go # core/types/gen_tx_json.go # core/types/receipt.go # core/types/receipt_test.go # core/types/transaction.go # core/types/transaction_signing.go # core/types/transaction_test.go # ethclient/ethclient.go # ethclient/signer.go # graphql/graphql.go # internal/ethapi/api.go # internal/guide/guide_test.go # les/benchmark.go # les/odr_test.go # light/odr_test.go # light/txpool.go # miner/worker.go # miner/worker_test.go # signer/core/api.go # tests/state_test_util.go # trie/stacktrie_test.go # turbo/stages/blockchain_test.go
2021-02-25 14:26:57 +00:00
if result.Failed() {
receipt.Status = types.ReceiptStatusFailed
} else {
receipt.Status = types.ReceiptStatusSuccessful
}
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 {
receipt.ContractAddress = crypto.CreateAddress(evm.Origin, tx.GetNonce())
}
// Set the receipt logs and create a bloom for filtering
receipt.Logs = ibs.GetLogs(tx.Hash())
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
all: add support for EIP-2718, EIP-2930 transactions (#21502) This adds support for EIP-2718 typed transactions as well as EIP-2930 access list transactions (tx type 1). These EIPs are scheduled for the Berlin fork. There very few changes to existing APIs in core/types, and several new APIs to deal with access list transactions. In particular, there are two new constructor functions for transactions: types.NewTx and types.SignNewTx. Since the canonical encoding of typed transactions is not RLP-compatible, Transaction now has new methods for encoding and decoding: MarshalBinary and UnmarshalBinary. The existing EIP-155 signer does not support the new transaction types. All code dealing with transaction signatures should be updated to use the newer EIP-2930 signer. To make this easier for future updates, we have added new constructor functions for types.Signer: types.LatestSigner and types.LatestSignerForChainID. This change also adds support for the YoloV3 testnet. Co-authored-by: Martin Holst Swende <martin@swende.se> Co-authored-by: Felix Lange <fjl@twurst.com> Co-authored-by: Ryan Schneider <ryanleeschneider@gmail.com> # Conflicts: # accounts/abi/bind/backends/simulated.go # cmd/evm/internal/t8ntool/execution.go # cmd/evm/internal/t8ntool/transition.go # cmd/geth/main.go # cmd/geth/usage.go # core/bench_test.go # core/state/statedb.go # core/state_prefetcher.go # core/state_processor.go # core/state_transition.go # core/tx_pool.go # core/types/block.go # core/types/derive_sha.go # core/types/gen_tx_json.go # core/types/receipt.go # core/types/receipt_test.go # core/types/transaction.go # core/types/transaction_signing.go # core/types/transaction_test.go # ethclient/ethclient.go # ethclient/signer.go # graphql/graphql.go # internal/ethapi/api.go # internal/guide/guide_test.go # les/benchmark.go # les/odr_test.go # light/odr_test.go # light/txpool.go # miner/worker.go # miner/worker_test.go # signer/core/api.go # tests/state_test_util.go # trie/stacktrie_test.go # turbo/stages/blockchain_test.go
2021-02-25 14:26:57 +00:00
receipt.BlockNumber = header.Number
receipt.TransactionIndex = uint(ibs.TxIndex())
}
return receipt, result.ReturnData, err
}
// 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.
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,
header *types.Header, tx types.Transaction, usedGas, usedBlobGas *uint64, cfg vm.Config,
) (*types.Receipt, []byte, error) {
// Create a new context to be used in the EVM environment
// Add addresses to access list if applicable
// about the transaction and calling mechanisms.
cfg.SkipAnalysis = SkipAnalysis(config, header.Number.Uint64())
blockContext := NewEVMBlockContext(header, blockHashFunc, engine, author)
vmenv := vm.NewEVM(blockContext, evmtypes.TxContext{}, ibs, config, cfg)
return applyTransaction(config, engine, gp, ibs, stateWriter, header, tx, usedGas, usedBlobGas, vmenv, cfg)
}