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 (
|
2019-12-17 11:56:38 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
2019-05-27 13:51:49 +00:00
|
|
|
|
|
|
|
"context"
|
2019-12-20 12:25:40 +00:00
|
|
|
"fmt"
|
2019-05-27 13:51:49 +00:00
|
|
|
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/math"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/consensus"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/consensus/misc"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/state"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/types"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/vm"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/crypto"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/params"
|
2015-10-19 14:08:17 +00:00
|
|
|
)
|
|
|
|
|
2016-03-01 22:32:43 +00:00
|
|
|
// StateProcessor is a basic Processor, which takes care of transitioning
|
|
|
|
// state from one point to another.
|
|
|
|
//
|
|
|
|
// StateProcessor implements Processor.
|
2015-10-19 14:08:17 +00:00
|
|
|
type StateProcessor struct {
|
2019-12-17 11:56:38 +00:00
|
|
|
config *params.ChainConfig // Chain configuration options
|
|
|
|
bc *BlockChain // Canonical block chain
|
|
|
|
engine consensus.Engine // Consensus engine used for block rewards
|
|
|
|
txTraceHash []byte // Hash of the transaction to trace (or nil if there nothing to trace)
|
2015-10-19 14:08:17 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 22:32:43 +00:00
|
|
|
// NewStateProcessor initialises a new StateProcessor.
|
2017-04-04 22:16:29 +00:00
|
|
|
func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consensus.Engine) *StateProcessor {
|
2016-03-01 22:32:43 +00:00
|
|
|
return &StateProcessor{
|
|
|
|
config: config,
|
|
|
|
bc: bc,
|
2017-04-04 22:16:29 +00:00
|
|
|
engine: engine,
|
2016-03-01 22:32:43 +00:00
|
|
|
}
|
2015-10-19 14:08:17 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 11:56:38 +00:00
|
|
|
// SetTxTraceHash allows setting the hash of the transaction to trace
|
|
|
|
func (p *StateProcessor) SetTxTraceHash(txTraceHash common.Hash) {
|
|
|
|
p.txTraceHash = txTraceHash[:]
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
// StructLogRes stores a structured log emitted by the EVM while replaying a
|
|
|
|
// transaction in debug mode
|
|
|
|
type StructLogRes struct {
|
|
|
|
Pc uint64 `json:"pc"`
|
|
|
|
Op string `json:"op"`
|
|
|
|
Gas uint64 `json:"gas"`
|
|
|
|
GasCost uint64 `json:"gasCost"`
|
|
|
|
Depth int `json:"depth"`
|
|
|
|
Error error `json:"error,omitempty"`
|
|
|
|
Stack *[]string `json:"stack,omitempty"`
|
|
|
|
Memory *[]string `json:"memory,omitempty"`
|
|
|
|
Storage *map[string]string `json:"storage,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-12-17 11:56:38 +00:00
|
|
|
// FormatLogs formats EVM returned structured logs for json output
|
2019-05-27 13:51:49 +00:00
|
|
|
func FormatLogs(logs []vm.StructLog) []StructLogRes {
|
|
|
|
formatted := make([]StructLogRes, len(logs))
|
|
|
|
for index, trace := range logs {
|
|
|
|
formatted[index] = StructLogRes{
|
|
|
|
Pc: trace.Pc,
|
|
|
|
Op: trace.Op.String(),
|
|
|
|
Gas: trace.Gas,
|
|
|
|
GasCost: trace.GasCost,
|
|
|
|
Depth: trace.Depth,
|
|
|
|
Error: trace.Err,
|
|
|
|
}
|
|
|
|
if trace.Stack != nil {
|
|
|
|
stack := make([]string, len(trace.Stack))
|
|
|
|
for i, stackValue := range trace.Stack {
|
|
|
|
stack[i] = fmt.Sprintf("%x", math.PaddedBigBytes(stackValue, 32))
|
|
|
|
}
|
|
|
|
formatted[index].Stack = &stack
|
|
|
|
}
|
|
|
|
if trace.Memory != nil {
|
|
|
|
memory := make([]string, 0, (len(trace.Memory)+31)/32)
|
|
|
|
for i := 0; i+32 <= len(trace.Memory); i += 32 {
|
|
|
|
memory = append(memory, fmt.Sprintf("%x", trace.Memory[i:i+32]))
|
|
|
|
}
|
|
|
|
formatted[index].Memory = &memory
|
|
|
|
}
|
|
|
|
if trace.Storage != nil {
|
|
|
|
storage := make(map[string]string)
|
|
|
|
for i, storageValue := range trace.Storage {
|
|
|
|
storage[fmt.Sprintf("%x", i)] = fmt.Sprintf("%x", storageValue)
|
|
|
|
}
|
|
|
|
formatted[index].Storage = &storage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return formatted
|
|
|
|
}
|
|
|
|
|
2015-10-19 14:08:17 +00:00
|
|
|
// Process processes the state changes according to the Ethereum rules by running
|
|
|
|
// the transaction messages using the statedb and applying any rewards to both
|
|
|
|
// the processor (coinbase) and any included uncles.
|
|
|
|
//
|
|
|
|
// Process returns the receipts and logs accumulated during the process and
|
|
|
|
// returns the amount of gas that was used in the process. If any of the
|
|
|
|
// transactions failed to execute due to insufficient gas it will return an error.
|
2019-05-27 13:51:49 +00:00
|
|
|
func (p *StateProcessor) Process(block *types.Block, statedb *state.IntraBlockState, tds *state.TrieDbState, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) {
|
2015-10-19 14:08:17 +00:00
|
|
|
var (
|
2017-11-13 11:47:27 +00:00
|
|
|
receipts types.Receipts
|
|
|
|
usedGas = new(uint64)
|
|
|
|
header = block.Header()
|
|
|
|
allLogs []*types.Log
|
|
|
|
gp = new(GasPool).AddGas(block.GasLimit())
|
2015-10-19 14:08:17 +00:00
|
|
|
)
|
2018-08-27 08:49:29 +00:00
|
|
|
// Mutate the block and state according to any hard-fork specs
|
2016-07-11 10:55:11 +00:00
|
|
|
if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 {
|
2017-04-04 22:16:29 +00:00
|
|
|
misc.ApplyDAOHardFork(statedb)
|
2016-07-11 10:55:11 +00:00
|
|
|
}
|
|
|
|
// Iterate over and process the individual transactions
|
2019-05-27 13:51:49 +00:00
|
|
|
tds.StartNewBuffer()
|
2015-10-19 14:08:17 +00:00
|
|
|
for i, tx := range block.Transactions() {
|
2019-12-17 11:56:38 +00:00
|
|
|
txHash := tx.Hash()
|
|
|
|
statedb.Prepare(txHash, block.Hash(), i)
|
|
|
|
writeTrace := false
|
|
|
|
if !cfg.Debug && p.txTraceHash != nil && bytes.Equal(p.txTraceHash, txHash[:]) {
|
|
|
|
// This code is useful when debugging a certain transaction. If uncommented, together with the code
|
|
|
|
// at the end of this function, after the execution of transaction with given hash, the file
|
|
|
|
// structlogs.txt will contain full trace of the transactin in JSON format. This can be compared
|
|
|
|
// to another trace, obtained from the correct version of the turbo-geth or go-ethereum
|
|
|
|
cfg.Tracer = vm.NewStructLogger(&vm.LogConfig{})
|
|
|
|
cfg.Debug = true
|
|
|
|
writeTrace = true
|
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
receipt, err := ApplyTransaction(p.config, p.bc, nil, gp, statedb, tds.TrieStateWriter(), header, tx, usedGas, cfg)
|
2019-12-17 11:56:38 +00:00
|
|
|
// This code is useful when debugging a certain transaction. If uncommented, together with the code
|
|
|
|
// at the end of this function, after the execution of transaction with given hash, the file
|
|
|
|
// structlogs.txt will contain full trace of the transactin in JSON format. This can be compared
|
|
|
|
// to another trace, obtained from the correct version of the turbo-geth or go-ethereum
|
|
|
|
if writeTrace {
|
|
|
|
w, err1 := os.Create(fmt.Sprintf("txtrace_%x.txt", p.txTraceHash))
|
|
|
|
if err1 != nil {
|
|
|
|
panic(err1)
|
|
|
|
}
|
|
|
|
encoder := json.NewEncoder(w)
|
|
|
|
logs := FormatLogs(cfg.Tracer.(*vm.StructLogger).StructLogs())
|
|
|
|
if err2 := encoder.Encode(logs); err2 != nil {
|
|
|
|
panic(err2)
|
|
|
|
}
|
|
|
|
if err2 := w.Close(); err2 != nil {
|
|
|
|
panic(err2)
|
|
|
|
}
|
|
|
|
cfg.Debug = false
|
|
|
|
cfg.Tracer = nil
|
|
|
|
}
|
2015-10-19 14:08:17 +00:00
|
|
|
if err != nil {
|
2017-11-13 11:47:27 +00:00
|
|
|
return nil, nil, 0, err
|
2015-10-19 14:08:17 +00:00
|
|
|
}
|
|
|
|
receipts = append(receipts, receipt)
|
2017-01-05 10:52:10 +00:00
|
|
|
allLogs = append(allLogs, receipt.Logs...)
|
2019-05-27 13:51:49 +00:00
|
|
|
if !p.config.IsByzantium(header.Number) {
|
|
|
|
tds.StartNewBuffer()
|
|
|
|
}
|
2015-10-19 14:08:17 +00:00
|
|
|
}
|
2017-04-04 22:16:29 +00:00
|
|
|
// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
|
2019-05-27 13:51:49 +00:00
|
|
|
p.engine.Finalize(p.config, header, statedb, block.Transactions(), block.Uncles())
|
|
|
|
ctx := p.config.WithEIPsFlags(context.Background(), header.Number)
|
|
|
|
if err := statedb.FinalizeTx(ctx, tds.TrieStateWriter()); err != nil {
|
|
|
|
return receipts, allLogs, *usedGas, err
|
|
|
|
}
|
|
|
|
roots, err := tds.ComputeTrieRoots()
|
|
|
|
if err != nil {
|
|
|
|
return receipts, allLogs, *usedGas, err
|
|
|
|
}
|
|
|
|
if !p.config.IsByzantium(header.Number) {
|
|
|
|
for i, receipt := range receipts {
|
|
|
|
receipt.PostState = roots[i].Bytes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
header.Root = roots[len(roots)-1]
|
|
|
|
return receipts, allLogs, *usedGas, err
|
2015-10-19 14:08:17 +00:00
|
|
|
}
|
|
|
|
|
2016-03-15 18:08:18 +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.
|
2019-05-27 13:51:49 +00:00
|
|
|
func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.IntraBlockState, stateWriter state.StateWriter, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, error) {
|
2016-11-02 12:44:13 +00:00
|
|
|
msg, err := tx.AsMessage(types.MakeSigner(config, header.Number))
|
|
|
|
if err != nil {
|
2019-09-12 19:22:22 +00:00
|
|
|
return nil, err
|
2016-11-02 12:44:13 +00:00
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
ctx := config.WithEIPsFlags(context.Background(), header.Number)
|
2016-12-06 01:16:03 +00:00
|
|
|
// Create a new context to be used in the EVM environment
|
2017-04-12 13:38:31 +00:00
|
|
|
context := NewEVMContext(msg, header, bc, author)
|
2016-12-06 01:16:03 +00:00
|
|
|
// Create a new environment which holds all relevant information
|
|
|
|
// about the transaction and calling mechanisms.
|
2017-01-17 11:19:50 +00:00
|
|
|
vmenv := vm.NewEVM(context, statedb, config, cfg)
|
2016-12-06 01:16:03 +00:00
|
|
|
// Apply the transaction to the current state (included in the env)
|
2017-08-21 00:47:15 +00:00
|
|
|
_, gas, failed, err := ApplyMessage(vmenv, msg, gp)
|
2015-10-19 14:08:17 +00:00
|
|
|
if err != nil {
|
2019-09-12 19:22:22 +00:00
|
|
|
return nil, err
|
2015-10-19 14:08:17 +00:00
|
|
|
}
|
|
|
|
// Update the state with pending changes
|
2019-05-27 13:51:49 +00:00
|
|
|
if err = statedb.FinalizeTx(ctx, stateWriter); err != nil {
|
|
|
|
return nil, err
|
2017-07-17 08:34:53 +00:00
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
|
2017-11-13 11:47:27 +00:00
|
|
|
*usedGas += gas
|
2017-07-17 08:34:53 +00:00
|
|
|
|
2016-12-06 01:16:03 +00:00
|
|
|
// Create a new receipt for the transaction, storing the intermediate root and gas used by the tx
|
2019-05-27 13:51:49 +00:00
|
|
|
// based on the eip phase, we're passing wether the root touch-delete accounts.
|
|
|
|
receipt := types.NewReceipt(failed, *usedGas)
|
2015-10-19 14:08:17 +00:00
|
|
|
receipt.TxHash = tx.Hash()
|
2017-11-13 11:47:27 +00:00
|
|
|
receipt.GasUsed = gas
|
2016-12-06 01:16:03 +00:00
|
|
|
// if the transaction created a contract, store the creation address in the receipt.
|
|
|
|
if msg.To() == nil {
|
|
|
|
receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())
|
2015-10-19 14:08:17 +00:00
|
|
|
}
|
2016-12-06 01:16:03 +00:00
|
|
|
// Set the receipt logs and create a bloom for filtering
|
|
|
|
receipt.Logs = statedb.GetLogs(tx.Hash())
|
2015-10-19 14:08:17 +00:00
|
|
|
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
|
2019-09-12 19:22:22 +00:00
|
|
|
return receipt, err
|
2015-10-19 14:08:17 +00:00
|
|
|
}
|