From 8938e279320d45e08eac61d2adb3e31c0e236ec8 Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Wed, 8 Oct 2025 12:29:38 +0800 Subject: [PATCH] all: move err to the last position of return values (#1581) --- consensus/tests/engine_v1_tests/helper.go | 2 +- consensus/tests/engine_v2_tests/helper.go | 2 +- core/chain_makers.go | 2 +- core/state_processor.go | 28 +++++++++++------------ eth/tracers/api.go | 2 +- miner/worker.go | 2 +- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/consensus/tests/engine_v1_tests/helper.go b/consensus/tests/engine_v1_tests/helper.go index e21dd5b6ad..f798ac94c0 100644 --- a/consensus/tests/engine_v1_tests/helper.go +++ b/consensus/tests/engine_v1_tests/helper.go @@ -374,7 +374,7 @@ func createBlockFromHeader(bc *core.BlockChain, customHeader *types.Header, txs var receipts types.Receipts for i, tx := range txs { statedb.SetTxContext(tx.Hash(), i) - receipt, _, err, _ := core.ApplyTransaction(bc.Config(), nil, bc, &header.Coinbase, gp, statedb, nil, &header, tx, gasUsed, vm.Config{}) + receipt, _, _, err := core.ApplyTransaction(bc.Config(), nil, bc, &header.Coinbase, gp, statedb, nil, &header, tx, gasUsed, vm.Config{}) if err != nil { return nil, fmt.Errorf("%v when applying transaction", err) } diff --git a/consensus/tests/engine_v2_tests/helper.go b/consensus/tests/engine_v2_tests/helper.go index 09da7d881b..fe54c87e1c 100644 --- a/consensus/tests/engine_v2_tests/helper.go +++ b/consensus/tests/engine_v2_tests/helper.go @@ -903,7 +903,7 @@ func createBlockFromHeader(bc *core.BlockChain, customHeader *types.Header, txs var receipts types.Receipts for i, tx := range txs { statedb.SetTxContext(tx.Hash(), i) - receipt, _, err, _ := core.ApplyTransaction(bc.Config(), nil, bc, &header.Coinbase, gp, statedb, nil, &header, tx, gasUsed, vm.Config{}) + receipt, _, _, err := core.ApplyTransaction(bc.Config(), nil, bc, &header.Coinbase, gp, statedb, nil, &header, tx, gasUsed, vm.Config{}) if err != nil { return nil, fmt.Errorf("%v when applying transaction", err) } diff --git a/core/chain_makers.go b/core/chain_makers.go index 39979664cb..d2075e7481 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -87,7 +87,7 @@ func (b *BlockGen) addTx(bc *BlockChain, vmConfig vm.Config, tx *types.Transacti } feeCapacity := state.GetTRC21FeeCapacityFromState(b.statedb) b.statedb.SetTxContext(tx.Hash(), len(b.txs)) - receipt, gas, err, tokenFeeUsed := ApplyTransaction(b.config, feeCapacity, bc, &b.header.Coinbase, b.gasPool, b.statedb, nil, b.header, tx, &b.header.GasUsed, vmConfig) + receipt, gas, tokenFeeUsed, err := ApplyTransaction(b.config, feeCapacity, bc, &b.header.Coinbase, b.gasPool, b.statedb, nil, b.header, tx, &b.header.GasUsed, vmConfig) if err != nil { panic(err) } diff --git a/core/state_processor.go b/core/state_processor.go index fe3366515e..03d531a2b6 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -131,7 +131,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, tra } statedb.SetTxContext(tx.Hash(), i) - receipt, gas, err, tokenFeeUsed := ApplyTransactionWithEVM(msg, p.config, gp, statedb, blockNumber, blockHash, tx, usedGas, vmenv, balanceFee, coinbaseOwner) + receipt, gas, tokenFeeUsed, err := ApplyTransactionWithEVM(msg, p.config, gp, statedb, blockNumber, blockHash, tx, usedGas, vmenv, balanceFee, coinbaseOwner) if err != nil { return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err) } @@ -222,7 +222,7 @@ func (p *StateProcessor) ProcessBlockNoValidator(cBlock *CalculatedBlock, stated return nil, nil, 0, err } statedb.SetTxContext(tx.Hash(), i) - receipt, gas, err, tokenFeeUsed := ApplyTransactionWithEVM(msg, p.config, gp, statedb, blockNumber, blockHash, tx, usedGas, vmenv, balanceFee, coinbaseOwner) + receipt, gas, tokenFeeUsed, err := ApplyTransactionWithEVM(msg, p.config, gp, statedb, blockNumber, blockHash, tx, usedGas, vmenv, balanceFee, coinbaseOwner) if err != nil { return nil, nil, 0, err } @@ -247,7 +247,7 @@ func (p *StateProcessor) ProcessBlockNoValidator(cBlock *CalculatedBlock, stated // ApplyTransactionWithEVM attempts to apply a transaction to the given state database // and uses the input parameters for its environment similar to ApplyTransaction. However, // this method takes an already created EVM instance as input. -func ApplyTransactionWithEVM(msg *Message, config *params.ChainConfig, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM, balanceFee *big.Int, coinbaseOwner common.Address) (receipt *types.Receipt, gasUsed uint64, err error, tokenFeeUsed bool) { +func ApplyTransactionWithEVM(msg *Message, config *params.ChainConfig, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM, balanceFee *big.Int, coinbaseOwner common.Address) (receipt *types.Receipt, gasUsed uint64, tokenFeeUsed bool, err error) { to := tx.To() if to != nil { if *to == common.BlockSignersBinary && config.IsTIPSigning(blockNumber) { @@ -428,7 +428,7 @@ func ApplyTransactionWithEVM(msg *Message, config *params.ChainConfig, gp *GasPo // Apply the transaction to the current state (included in the env) result, err := ApplyMessage(evm, msg, gp, coinbaseOwner) if err != nil { - return nil, 0, err, false + return nil, 0, false, err } // Update the state with pending changes. @@ -465,7 +465,7 @@ func ApplyTransactionWithEVM(msg *Message, config *params.ChainConfig, gp *GasPo if balanceFee != nil && result.Failed() { state.PayFeeWithTRC21TxFail(statedb, msg.From, *to) } - return receipt, result.UsedGas, err, balanceFee != nil + return receipt, result.UsedGas, balanceFee != nil, nil } func getCoinbaseOwner(bc *BlockChain, statedb *state.StateDB, header *types.Header, author *common.Address) common.Address { @@ -483,7 +483,7 @@ func getCoinbaseOwner(bc *BlockChain, statedb *state.StateDB, header *types.Head // 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, tokensFee map[common.Address]*big.Int, bc *BlockChain, author *common.Address, gp *GasPool, statedb *state.StateDB, XDCxState *tradingstate.TradingStateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, uint64, error, bool) { +func ApplyTransaction(config *params.ChainConfig, tokensFee map[common.Address]*big.Int, bc *BlockChain, author *common.Address, gp *GasPool, statedb *state.StateDB, XDCxState *tradingstate.TradingStateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, uint64, bool, error) { var balanceFee *big.Int if tx.To() != nil { if value, ok := tokensFee[*tx.To()]; ok { @@ -497,13 +497,13 @@ func ApplyTransaction(config *params.ChainConfig, tokensFee map[common.Address]* signer := types.MakeSigner(config, header.Number) msg, err := TransactionToMessage(tx, signer, balanceFee, header.Number, header.BaseFee) if err != nil { - return nil, 0, err, false + return nil, 0, false, err } coinbaseOwner := getCoinbaseOwner(bc, statedb, header, author) return ApplyTransactionWithEVM(msg, config, gp, statedb, header.Number, header.Hash(), tx, usedGas, vmenv, balanceFee, coinbaseOwner) } -func ApplySignTransaction(config *params.ChainConfig, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64) (*types.Receipt, uint64, error, bool) { +func ApplySignTransaction(config *params.ChainConfig, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64) (*types.Receipt, uint64, bool, error) { // Update the state with pending changes var root []byte if config.IsByzantium(blockNumber) { @@ -513,13 +513,13 @@ func ApplySignTransaction(config *params.ChainConfig, statedb *state.StateDB, bl } from, err := types.Sender(types.MakeSigner(config, blockNumber), tx) if err != nil { - return nil, 0, err, false + return nil, 0, false, err } nonce := statedb.GetNonce(from) if nonce < tx.Nonce() { - return nil, 0, ErrNonceTooHigh, false + return nil, 0, false, ErrNonceTooHigh } else if nonce > tx.Nonce() { - return nil, 0, ErrNonceTooLow, false + return nil, 0, false, ErrNonceTooLow } statedb.SetNonce(from, nonce+1) // Create a new receipt for the transaction, storing the intermediate root and gas used by the tx @@ -538,10 +538,10 @@ func ApplySignTransaction(config *params.ChainConfig, statedb *state.StateDB, bl receipt.BlockHash = blockHash receipt.BlockNumber = blockNumber receipt.TransactionIndex = uint(statedb.TxIndex()) - return receipt, 0, nil, false + return receipt, 0, false, nil } -func ApplyEmptyTransaction(config *params.ChainConfig, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64) (*types.Receipt, uint64, error, bool) { +func ApplyEmptyTransaction(config *params.ChainConfig, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64) (*types.Receipt, uint64, bool, error) { // Update the state with pending changes var root []byte if config.IsByzantium(blockNumber) { @@ -565,7 +565,7 @@ func ApplyEmptyTransaction(config *params.ChainConfig, statedb *state.StateDB, b receipt.BlockHash = blockHash receipt.BlockNumber = blockNumber receipt.TransactionIndex = uint(statedb.TxIndex()) - return receipt, 0, nil, false + return receipt, 0, false, nil } func InitSignerInTransactions(config *params.ChainConfig, header *types.Header, txs types.Transactions) { diff --git a/eth/tracers/api.go b/eth/tracers/api.go index a4920b6ac1..af4d18f7d3 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -892,7 +892,7 @@ func (api *API) traceTx(ctx context.Context, tx *types.Transaction, message *cor // Call SetTxContext to clear out the statedb access list statedb.SetTxContext(txctx.TxHash, txctx.TxIndex) - _, _, err, _ = core.ApplyTransactionWithEVM(message, api.backend.ChainConfig(), new(core.GasPool).AddGas(message.GasLimit), statedb, vmctx.BlockNumber, txctx.BlockHash, tx, &usedGas, vmenv, balance, common.Address{}) + _, _, _, err = core.ApplyTransactionWithEVM(message, api.backend.ChainConfig(), new(core.GasPool).AddGas(message.GasLimit), statedb, vmctx.BlockNumber, txctx.BlockHash, tx, &usedGas, vmenv, balance, common.Address{}) if err != nil { return nil, fmt.Errorf("tracing failed: %w", err) } diff --git a/miner/worker.go b/miner/worker.go index 7a92b437d4..933b14ff5f 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -1134,7 +1134,7 @@ func (w *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Addr func (w *Work) commitTransaction(balanceFee map[common.Address]*big.Int, tx *types.Transaction, bc *core.BlockChain, coinbase common.Address, gp *core.GasPool) ([]*types.Log, bool, uint64, error) { snap := w.state.Snapshot() - receipt, gas, err, tokenFeeUsed := core.ApplyTransaction(w.config, balanceFee, bc, &coinbase, gp, w.state, w.tradingState, w.header, tx, &w.header.GasUsed, vm.Config{}) + receipt, gas, tokenFeeUsed, err := core.ApplyTransaction(w.config, balanceFee, bc, &coinbase, gp, w.state, w.tradingState, w.header, tx, &w.header.GasUsed, vm.Config{}) if err != nil { w.state.RevertToSnapshot(snap) return nil, false, 0, err