|
|
@ -18,6 +18,7 @@ package core |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
"math/big" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common" |
|
|
|
"github.com/ethereum/go-ethereum/common" |
|
|
|
"github.com/ethereum/go-ethereum/consensus" |
|
|
|
"github.com/ethereum/go-ethereum/consensus" |
|
|
@ -57,11 +58,13 @@ func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consen |
|
|
|
// transactions failed to execute due to insufficient gas it will return an error.
|
|
|
|
// transactions failed to execute due to insufficient gas it will return an error.
|
|
|
|
func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) { |
|
|
|
func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) { |
|
|
|
var ( |
|
|
|
var ( |
|
|
|
receipts types.Receipts |
|
|
|
receipts types.Receipts |
|
|
|
usedGas = new(uint64) |
|
|
|
usedGas = new(uint64) |
|
|
|
header = block.Header() |
|
|
|
header = block.Header() |
|
|
|
allLogs []*types.Log |
|
|
|
blockHash = block.Hash() |
|
|
|
gp = new(GasPool).AddGas(block.GasLimit()) |
|
|
|
blockNumber = block.Number() |
|
|
|
|
|
|
|
allLogs []*types.Log |
|
|
|
|
|
|
|
gp = new(GasPool).AddGas(block.GasLimit()) |
|
|
|
) |
|
|
|
) |
|
|
|
// Mutate the block and state according to any hard-fork specs
|
|
|
|
// Mutate the block and state according to any hard-fork specs
|
|
|
|
if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 { |
|
|
|
if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 { |
|
|
@ -75,8 +78,8 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err) |
|
|
|
return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err) |
|
|
|
} |
|
|
|
} |
|
|
|
statedb.Prepare(tx.Hash(), block.Hash(), i) |
|
|
|
statedb.Prepare(tx.Hash(), i) |
|
|
|
receipt, err := applyTransaction(msg, p.config, p.bc, nil, gp, statedb, header, tx, usedGas, vmenv) |
|
|
|
receipt, err := applyTransaction(msg, p.config, p.bc, nil, gp, statedb, blockNumber, blockHash, tx, usedGas, vmenv) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err) |
|
|
|
return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err) |
|
|
|
} |
|
|
|
} |
|
|
@ -89,7 +92,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg |
|
|
|
return receipts, allLogs, *usedGas, nil |
|
|
|
return receipts, allLogs, *usedGas, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func applyTransaction(msg types.Message, config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (*types.Receipt, error) { |
|
|
|
func applyTransaction(msg types.Message, config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (*types.Receipt, error) { |
|
|
|
// Create a new context to be used in the EVM environment.
|
|
|
|
// Create a new context to be used in the EVM environment.
|
|
|
|
txContext := NewEVMTxContext(msg) |
|
|
|
txContext := NewEVMTxContext(msg) |
|
|
|
evm.Reset(txContext, statedb) |
|
|
|
evm.Reset(txContext, statedb) |
|
|
@ -102,10 +105,10 @@ func applyTransaction(msg types.Message, config *params.ChainConfig, bc ChainCon |
|
|
|
|
|
|
|
|
|
|
|
// Update the state with pending changes.
|
|
|
|
// Update the state with pending changes.
|
|
|
|
var root []byte |
|
|
|
var root []byte |
|
|
|
if config.IsByzantium(header.Number) { |
|
|
|
if config.IsByzantium(blockNumber) { |
|
|
|
statedb.Finalise(true) |
|
|
|
statedb.Finalise(true) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes() |
|
|
|
root = statedb.IntermediateRoot(config.IsEIP158(blockNumber)).Bytes() |
|
|
|
} |
|
|
|
} |
|
|
|
*usedGas += result.UsedGas |
|
|
|
*usedGas += result.UsedGas |
|
|
|
|
|
|
|
|
|
|
@ -126,10 +129,10 @@ func applyTransaction(msg types.Message, config *params.ChainConfig, bc ChainCon |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Set the receipt logs and create the bloom filter.
|
|
|
|
// Set the receipt logs and create the bloom filter.
|
|
|
|
receipt.Logs = statedb.GetLogs(tx.Hash()) |
|
|
|
receipt.Logs = statedb.GetLogs(tx.Hash(), blockHash) |
|
|
|
receipt.Bloom = types.CreateBloom(types.Receipts{receipt}) |
|
|
|
receipt.Bloom = types.CreateBloom(types.Receipts{receipt}) |
|
|
|
receipt.BlockHash = statedb.BlockHash() |
|
|
|
receipt.BlockHash = blockHash |
|
|
|
receipt.BlockNumber = header.Number |
|
|
|
receipt.BlockNumber = blockNumber |
|
|
|
receipt.TransactionIndex = uint(statedb.TxIndex()) |
|
|
|
receipt.TransactionIndex = uint(statedb.TxIndex()) |
|
|
|
return receipt, err |
|
|
|
return receipt, err |
|
|
|
} |
|
|
|
} |
|
|
@ -146,5 +149,5 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo |
|
|
|
// Create a new context to be used in the EVM environment
|
|
|
|
// Create a new context to be used in the EVM environment
|
|
|
|
blockContext := NewEVMBlockContext(header, bc, author) |
|
|
|
blockContext := NewEVMBlockContext(header, bc, author) |
|
|
|
vmenv := vm.NewEVM(blockContext, vm.TxContext{}, statedb, config, cfg) |
|
|
|
vmenv := vm.NewEVM(blockContext, vm.TxContext{}, statedb, config, cfg) |
|
|
|
return applyTransaction(msg, config, bc, author, gp, statedb, header, tx, usedGas, vmenv) |
|
|
|
return applyTransaction(msg, config, bc, author, gp, statedb, header.Number, header.Hash(), tx, usedGas, vmenv) |
|
|
|
} |
|
|
|
} |
|
|
|