Skip to content
This repository has been archived by the owner on Jan 16, 2025. It is now read-only.

Commit

Permalink
match applyTransaction function signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
calbera committed May 24, 2023
1 parent 8e46b86 commit fc19d4f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
50 changes: 34 additions & 16 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb state.StateDBI, cfg
return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)
}
statedb.SetTxContext(tx.Hash(), i)
receipt, err := applyTransaction(msg, p.config, gp, statedb, blockNumber, blockHash, tx, usedGas, vmenv)
receipt, err := applyTransaction(vmenv, msg, p.config, gp, statedb, blockNumber, blockHash, tx, usedGas)
if err != nil {
return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)
}
Expand All @@ -97,7 +97,10 @@ func (p *StateProcessor) Process(block *types.Block, statedb state.StateDBI, cfg
return receipts, allLogs, *usedGas, nil
}

func applyTransaction(msg *Message, config *params.ChainConfig, gp *GasPool, statedb state.StateDBI, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (*types.Receipt, error) {
func applyTransaction(
evm *vm.EVM, msg *Message, config *params.ChainConfig, gp *GasPool, statedb state.StateDBI,
blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64,
) (*types.Receipt, error) {
// Create a new context to be used in the EVM environment.
txContext := NewEVMTxContext(msg)
evm.Reset(txContext, statedb)
Expand Down Expand Up @@ -142,7 +145,10 @@ func applyTransaction(msg *Message, config *params.ChainConfig, gp *GasPool, sta
return receipt, err
}

func applyTransactionWithResult(msg *Message, config *params.ChainConfig, author *common.Address, gp *GasPool, statedb state.StateDBI, header *types.Header, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (*types.Receipt, *ExecutionResult, error) {
func applyTransactionWithResult(
evm *vm.EVM, msg *Message, config *params.ChainConfig, gp *GasPool, statedb state.StateDBI,
blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64,
) (*types.Receipt, *ExecutionResult, error) {
// Create a new context to be used in the EVM environment.
txContext := NewEVMTxContext(msg)
evm.Reset(txContext, statedb)
Expand All @@ -155,10 +161,10 @@ func applyTransactionWithResult(msg *Message, config *params.ChainConfig, author

// Update the state with pending changes.
var root []byte
if config.IsByzantium(header.Number) {
if config.IsByzantium(blockNumber) {
statedb.Finalise(true)
} else {
root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes()
root = statedb.IntermediateRoot(config.IsEIP158(blockNumber)).Bytes()
}
*usedGas += result.UsedGas

Expand All @@ -179,10 +185,10 @@ func applyTransactionWithResult(msg *Message, config *params.ChainConfig, author
}

// Set the receipt logs and create the bloom filter.
receipt.Logs = statedb.GetLogs(tx.Hash(), header.Number.Uint64(), header.Hash())
receipt.Logs = statedb.GetLogs(tx.Hash(), blockNumber.Uint64(), blockHash)
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
receipt.BlockHash = header.Hash()
receipt.BlockNumber = header.Number
receipt.BlockHash = blockHash
receipt.BlockNumber = blockNumber
receipt.TransactionIndex = uint(statedb.TxIndex())
return receipt, result, err
}
Expand All @@ -191,40 +197,52 @@ func applyTransactionWithResult(msg *Message, config *params.ChainConfig, author
// 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 *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb state.StateDBI, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, error) {
func ApplyTransaction(
config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool,
statedb state.StateDBI, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config,
) (*types.Receipt, error) {
msg, err := TransactionToMessage(tx, types.MakeSigner(config, header.Number), header.BaseFee)
if err != nil {
return nil, err
}
// Create a new context to be used in the EVM environment
blockContext := NewEVMBlockContext(header, bc, author)
vmenv := vm.NewEVM(blockContext, vm.TxContext{}, statedb, config, cfg)
return applyTransaction(msg, config, gp, statedb, header.Number, header.Hash(), tx, usedGas, vmenv)
return applyTransaction(vmenv, msg, config, gp, statedb, header.Number, header.Hash(), tx, usedGas)
}

func ApplyTransactionWithResult(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb state.StateDBI, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, *ExecutionResult, error) {
func ApplyTransactionWithResult(
config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool,
statedb state.StateDBI, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config,
) (*types.Receipt, *ExecutionResult, error) {
msg, err := TransactionToMessage(tx, types.MakeSigner(config, header.Number), header.BaseFee)
if err != nil {
return nil, nil, err
}
// Create a new context to be used in the EVM environment
blockContext := NewEVMBlockContext(header, bc, author)
vmenv := vm.NewEVM(blockContext, vm.TxContext{}, statedb, config, cfg)
return applyTransactionWithResult(msg, config, author, gp, statedb, header, tx, usedGas, vmenv)
return applyTransactionWithResult(vmenv, msg, config, gp, statedb, header.Number, header.Hash(), tx, usedGas)
}

func ApplyTransactionWithEVM(vmenv *vm.EVM, config *params.ChainConfig, gp *GasPool, statedb state.StateDBI, header *types.Header, tx *types.Transaction, usedGas *uint64) (*types.Receipt, error) {
func ApplyTransactionWithEVM(
vmenv *vm.EVM, config *params.ChainConfig, gp *GasPool, statedb state.StateDBI,
header *types.Header, tx *types.Transaction, usedGas *uint64,
) (*types.Receipt, error) {
msg, err := TransactionToMessage(tx, types.MakeSigner(config, header.Number), header.BaseFee)
if err != nil {
return nil, err
}
return applyTransaction(msg, config, gp, statedb, header.Number, header.Hash(), tx, usedGas, vmenv)
return applyTransaction(vmenv, msg, config, gp, statedb, header.Number, header.Hash(), tx, usedGas)
}

func ApplyTransactionWithEVMWithResult(vmenv *vm.EVM, config *params.ChainConfig, author *common.Address, gp *GasPool, statedb state.StateDBI, header *types.Header, tx *types.Transaction, usedGas *uint64) (*types.Receipt, *ExecutionResult, error) {
func ApplyTransactionWithEVMWithResult(
vmenv *vm.EVM, config *params.ChainConfig, gp *GasPool, statedb state.StateDBI,
header *types.Header, tx *types.Transaction, usedGas *uint64,
) (*types.Receipt, *ExecutionResult, error) {
msg, err := TransactionToMessage(tx, types.MakeSigner(config, header.Number), header.BaseFee)
if err != nil {
return nil, nil, err
}
return applyTransactionWithResult(msg, config, author, gp, statedb, header, tx, usedGas, vmenv)
return applyTransactionWithResult(vmenv, msg, config, gp, statedb, header.Number, header.Hash(), tx, usedGas)
}
4 changes: 3 additions & 1 deletion ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2240,7 +2240,9 @@ func (s *BundleAPI) CallBundle(ctx context.Context, args CallBundleArgs) (map[st
coinbaseBalanceBeforeTx := state.GetBalance(coinbase)
state.SetTxContext(tx.Hash(), i)

receipt, result, err := core.ApplyTransactionWithEVMWithResult(vmenv, s.b.ChainConfig(), &coinbase, gp, state, header, tx, &header.GasUsed)
receipt, result, err := core.ApplyTransactionWithEVMWithResult(
vmenv, s.b.ChainConfig(), gp, state, header, tx, &header.GasUsed,
)
if err != nil {
return nil, fmt.Errorf("err: %w; txhash %s", err, tx.Hash())
}
Expand Down

0 comments on commit fc19d4f

Please sign in to comment.