Skip to content

Commit 48d1f22

Browse files
JukLee0iragzliudan
authored andcommitted
all: simplify function TransitionDb (ethereum#20830)
1 parent 69bca27 commit 48d1f22

File tree

11 files changed

+42
-43
lines changed

11 files changed

+42
-43
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,7 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call XDPoSChain.Cal
478478
vmenv := vm.NewEVM(evmContext, txContext, statedb, nil, b.config, vm.Config{NoBaseFee: true})
479479
gaspool := new(core.GasPool).AddGas(math.MaxUint64)
480480
owner := common.Address{}
481-
res, err, _ := core.NewStateTransition(vmenv, msg, gaspool).TransitionDb(owner)
482-
return res, err
481+
return core.NewStateTransition(vmenv, msg, gaspool).TransitionDb(owner)
483482
}
484483

485484
// SendTransaction updates the pending block to include the given transaction.

core/state_processor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ func applyTransaction(config *params.ChainConfig, tokensFee map[common.Address]*
403403
// End Bypass blacklist address
404404

405405
// Apply the transaction to the current state (included in the env)
406-
result, err, _ := ApplyMessage(evm, msg, gp, coinbaseOwner)
406+
result, err := ApplyMessage(evm, msg, gp, coinbaseOwner)
407407

408408
if err != nil {
409409
return nil, 0, err, false

core/state_transition.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func NewStateTransition(evm *vm.EVM, msg Message, gp *GasPool) *StateTransition
194194
// the gas used (which includes gas refunds) and an error if it failed. An error always
195195
// indicates a core error meaning that the message would always fail for that particular
196196
// state and would never be accepted within a block.
197-
func ApplyMessage(evm *vm.EVM, msg Message, gp *GasPool, owner common.Address) (*ExecutionResult, error, error) {
197+
func ApplyMessage(evm *vm.EVM, msg Message, gp *GasPool, owner common.Address) (*ExecutionResult, error) {
198198
return NewStateTransition(evm, msg, gp).TransitionDb(owner)
199199
}
200200

@@ -317,7 +317,7 @@ func (st *StateTransition) preCheck() error {
317317
//
318318
// However if any consensus issue encountered, return the error directly with
319319
// nil evm execution result.
320-
func (st *StateTransition) TransitionDb(owner common.Address) (*ExecutionResult, error, error) {
320+
func (st *StateTransition) TransitionDb(owner common.Address) (*ExecutionResult, error) {
321321
// First check this message satisfies all consensus rules before
322322
// applying the message. The rules include these clauses
323323
//
@@ -330,7 +330,7 @@ func (st *StateTransition) TransitionDb(owner common.Address) (*ExecutionResult,
330330

331331
// Check clauses 1-3, buy gas if everything is correct
332332
if err := st.preCheck(); err != nil {
333-
return nil, err, nil
333+
return nil, err
334334
}
335335

336336
var (
@@ -345,16 +345,16 @@ func (st *StateTransition) TransitionDb(owner common.Address) (*ExecutionResult,
345345
// Check clauses 4-5, subtract intrinsic gas if everything is correct
346346
gas, err := IntrinsicGas(st.data, st.msg.AccessList(), contractCreation, homestead, rules.IsEIP1559)
347347
if err != nil {
348-
return nil, err, nil
348+
return nil, err
349349
}
350350
if st.gas < gas {
351-
return nil, fmt.Errorf("%w: have %d, want %d", ErrIntrinsicGas, st.gas, gas), nil
351+
return nil, fmt.Errorf("%w: have %d, want %d", ErrIntrinsicGas, st.gas, gas)
352352
}
353353
st.gas -= gas
354354

355355
// Check whether the init code size has been exceeded.
356356
if rules.IsEIP1559 && contractCreation && len(st.data) > params.MaxInitCodeSize {
357-
return nil, fmt.Errorf("%w: code size %v limit %v", ErrMaxInitCodeSizeExceeded, len(st.data), params.MaxInitCodeSize), nil
357+
return nil, fmt.Errorf("%w: code size %v limit %v", ErrMaxInitCodeSizeExceeded, len(st.data), params.MaxInitCodeSize)
358358
}
359359

360360
// Execute the preparatory steps for state transition which includes:
@@ -365,10 +365,10 @@ func (st *StateTransition) TransitionDb(owner common.Address) (*ExecutionResult,
365365
// Check clause 6
366366
value, overflow := uint256.FromBig(msg.Value())
367367
if overflow {
368-
return nil, fmt.Errorf("%w: address %v", ErrInsufficientFundsForTransfer, msg.From().Hex()), nil
368+
return nil, fmt.Errorf("%w: address %v", ErrInsufficientFundsForTransfer, msg.From().Hex())
369369
}
370370
if !value.IsZero() && !st.evm.Context.CanTransfer(st.state, msg.From(), value.ToBig()) {
371-
return nil, fmt.Errorf("%w: address %v", ErrInsufficientFundsForTransfer, msg.From().Hex()), nil
371+
return nil, fmt.Errorf("%w: address %v", ErrInsufficientFundsForTransfer, msg.From().Hex())
372372
}
373373

374374
var (
@@ -406,7 +406,7 @@ func (st *StateTransition) TransitionDb(owner common.Address) (*ExecutionResult,
406406
UsedGas: st.gasUsed(),
407407
Err: vmerr,
408408
ReturnData: ret,
409-
}, nil, vmerr
409+
}, nil
410410
}
411411

412412
func (st *StateTransition) refundGas(refundQuotient uint64) {

core/token_validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func CallContractWithState(call ethereum.CallMsg, chain consensus.ChainContext,
117117
vmenv := vm.NewEVM(evmContext, txContext, statedb, nil, chain.Config(), vm.Config{})
118118
gaspool := new(GasPool).AddGas(1000000)
119119
owner := common.Address{}
120-
result, err, _ := NewStateTransition(vmenv, msg, gaspool).TransitionDb(owner)
120+
result, err := NewStateTransition(vmenv, msg, gaspool).TransitionDb(owner)
121121
if err != nil {
122122
return nil, err
123123
}

eth/api_tracer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ func (api *PrivateDebugAPI) traceBlock(ctx context.Context, block *types.Block,
526526

527527
vmenv := vm.NewEVM(blockCtx, txContext, statedb, XDCxState, api.config, vm.Config{})
528528
owner := common.Address{}
529-
if _, err, _ := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas()), owner); err != nil {
529+
if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas()), owner); err != nil {
530530
failed = err
531531
break
532532
}
@@ -750,7 +750,7 @@ func (api *PrivateDebugAPI) traceTx(ctx context.Context, message core.Message, t
750750
statedb.SetTxContext(txctx.TxHash, txctx.TxIndex)
751751

752752
owner := common.Address{}
753-
result, err, _ := core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.Gas()), owner)
753+
result, err := core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.Gas()), owner)
754754
if err != nil {
755755
return nil, fmt.Errorf("tracing failed: %v", err)
756756
}

eth/tracers/testing/calltrace_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func testCallTracer(tracerName string, dirPath string, t *testing.T) {
120120
t.Fatalf("failed to prepare transaction for tracing: %v", err)
121121
}
122122
st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
123-
if _, err, _ = st.TransitionDb(common.Address{}); err != nil {
123+
if _, err = st.TransitionDb(common.Address{}); err != nil {
124124
t.Fatalf("failed to execute transaction: %v", err)
125125
}
126126
// Retrieve the trace result and compare against the etalon
@@ -231,7 +231,7 @@ func benchTracer(tracerName string, test *callTracerTest, b *testing.B) {
231231
evm := vm.NewEVM(context, txContext, statedb, nil, test.Genesis.Config, vm.Config{Debug: true, Tracer: tracer})
232232
snap := statedb.Snapshot()
233233
st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
234-
if _, err, _ = st.TransitionDb(common.Address{}); err != nil {
234+
if _, err = st.TransitionDb(common.Address{}); err != nil {
235235
b.Fatalf("failed to execute transaction: %v", err)
236236
}
237237
if _, err = tracer.GetResult(); err != nil {

eth/tracers/tracers_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func TestZeroValueToNotExitCall(t *testing.T) {
158158
t.Fatalf("failed to prepare transaction for tracing: %v", err)
159159
}
160160
st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
161-
if _, err, _ = st.TransitionDb(common.Address{}); err != nil {
161+
if _, err = st.TransitionDb(common.Address{}); err != nil {
162162
t.Fatalf("failed to execute transaction: %v", err)
163163
}
164164
// Retrieve the trace result and compare against the etalon
@@ -244,7 +244,7 @@ func TestPrestateTracerCreate2(t *testing.T) {
244244
t.Fatalf("failed to prepare transaction for tracing: %v", err)
245245
}
246246
st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
247-
if _, err, _ = st.TransitionDb(common.Address{}); err != nil {
247+
if _, err = st.TransitionDb(common.Address{}); err != nil {
248248
t.Fatalf("failed to execute transaction: %v", err)
249249
}
250250
// Retrieve the trace result and compare against the etalon
@@ -346,7 +346,7 @@ func BenchmarkTransactionTrace(b *testing.B) {
346346
for i := 0; i < b.N; i++ {
347347
snap := statedb.Snapshot()
348348
st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
349-
_, err, _ = st.TransitionDb(common.Address{})
349+
_, err = st.TransitionDb(common.Address{})
350350
if err != nil {
351351
b.Fatal(err)
352352
}

internal/ethapi/api.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,18 +1322,18 @@ func (s *PublicBlockChainAPI) getCandidatesFromSmartContract() ([]utils.Masterno
13221322
return candidatesWithStakeInfo, nil
13231323
}
13241324

1325-
func DoCall(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides *StateOverride, timeout time.Duration, globalGasCap uint64) (*core.ExecutionResult, error, error) {
1325+
func DoCall(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides *StateOverride, timeout time.Duration, globalGasCap uint64) (*core.ExecutionResult, error) {
13261326
defer func(start time.Time) { log.Debug("Executing EVM call finished", "runtime", time.Since(start)) }(time.Now())
13271327

13281328
statedb, header, err := b.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
13291329
if statedb == nil || err != nil {
1330-
return nil, err, nil
1330+
return nil, err
13311331
}
13321332
if header == nil {
1333-
return nil, errors.New("nil header in DoCall"), nil
1333+
return nil, errors.New("nil header in DoCall")
13341334
}
13351335
if err := overrides.Apply(statedb); err != nil {
1336-
return nil, err, nil
1336+
return nil, err
13371337
}
13381338

13391339
// Setup context so it may be cancelled the call has completed
@@ -1350,32 +1350,32 @@ func DoCall(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash
13501350

13511351
block, err := b.BlockByNumberOrHash(ctx, blockNrOrHash)
13521352
if err != nil {
1353-
return nil, err, nil
1353+
return nil, err
13541354
}
13551355
if block == nil {
1356-
return nil, fmt.Errorf("nil block in DoCall: number=%d, hash=%s", header.Number.Uint64(), header.Hash().Hex()), nil
1356+
return nil, fmt.Errorf("nil block in DoCall: number=%d, hash=%s", header.Number.Uint64(), header.Hash().Hex())
13571357
}
13581358
author, err := b.GetEngine().Author(block.Header())
13591359
if err != nil {
1360-
return nil, err, nil
1360+
return nil, err
13611361
}
13621362
XDCxState, err := b.XDCxService().GetTradingState(block, author)
13631363
if err != nil {
1364-
return nil, err, nil
1364+
return nil, err
13651365
}
13661366

13671367
// TODO: replace header.BaseFee with blockCtx.BaseFee
13681368
// reference: https://github.com/ethereum/go-ethereum/pull/29051
13691369
msg, err := args.ToMessage(b, header.Number, globalGasCap, header.BaseFee)
13701370
if err != nil {
1371-
return nil, err, nil
1371+
return nil, err
13721372
}
13731373
msg.SetBalanceTokenFeeForCall()
13741374

13751375
// Get a new instance of the EVM.
13761376
evm, vmError, err := b.GetEVM(ctx, msg, statedb, XDCxState, header, &vm.Config{NoBaseFee: true})
13771377
if err != nil {
1378-
return nil, err, nil
1378+
return nil, err
13791379
}
13801380
// Wait for the context to be done and cancel the evm. Even if the
13811381
// EVM has finished, cancelling may be done (repeatedly)
@@ -1387,19 +1387,19 @@ func DoCall(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash
13871387
// Execute the message.
13881388
gp := new(core.GasPool).AddGas(math.MaxUint64)
13891389
owner := common.Address{}
1390-
result, err, vmErr := core.ApplyMessage(evm, msg, gp, owner)
1390+
result, err := core.ApplyMessage(evm, msg, gp, owner)
13911391
if err := vmError(); err != nil {
1392-
return nil, err, nil
1392+
return nil, err
13931393
}
13941394

13951395
// If the timer caused an abort, return an appropriate error message
13961396
if evm.Cancelled() {
1397-
return nil, fmt.Errorf("execution aborted (timeout = %v)", timeout), nil
1397+
return nil, fmt.Errorf("execution aborted (timeout = %v)", timeout)
13981398
}
13991399
if err != nil {
1400-
return result, fmt.Errorf("err: %w (supplied gas %d)", err, msg.Gas()), nil
1400+
return result, fmt.Errorf("err: %w (supplied gas %d)", err, msg.Gas())
14011401
}
1402-
return result, err, vmErr
1402+
return result, err
14031403
}
14041404

14051405
func newRevertError(res []byte) *revertError {
@@ -1443,15 +1443,15 @@ func (s *PublicBlockChainAPI) Call(ctx context.Context, args TransactionArgs, bl
14431443
if args.To != nil && *args.To == common.MasternodeVotingSMCBinary {
14441444
timeout = 0
14451445
}
1446-
result, err, vmErr := DoCall(ctx, s.b, args, *blockNrOrHash, overrides, timeout, s.b.RPCGasCap())
1446+
result, err := DoCall(ctx, s.b, args, *blockNrOrHash, overrides, timeout, s.b.RPCGasCap())
14471447
if err != nil {
14481448
return nil, err
14491449
}
14501450
// If the result contains a revert reason, try to unpack and return it.
14511451
if result.Failed() && len(result.Return()) > 0 {
14521452
return nil, newRevertError(result.Return())
14531453
}
1454-
return result.Return(), vmErr
1454+
return result.Return(), nil
14551455
}
14561456

14571457
type estimateGasError struct {
@@ -1515,7 +1515,7 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr
15151515
executable := func(gas uint64) (bool, *core.ExecutionResult, error) {
15161516
args.Gas = (*hexutil.Uint64)(&gas)
15171517

1518-
result, err, _ := DoCall(ctx, b, args, blockNrOrHash, nil, 0, gasCap)
1518+
result, err := DoCall(ctx, b, args, blockNrOrHash, nil, 0, gasCap)
15191519
if err != nil {
15201520
if errors.Is(err, vm.ErrOutOfGas) || errors.Is(err, core.ErrIntrinsicGas) {
15211521
return true, nil, nil // Special case, raise gas limit
@@ -2096,7 +2096,7 @@ func AccessList(ctx context.Context, b Backend, blockNrOrHash rpc.BlockNumberOrH
20962096
return nil, 0, nil, err
20972097
}
20982098
// TODO: determine the value of owner
2099-
res, err, _ := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas()), owner)
2099+
res, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas()), owner)
21002100
if err != nil {
21012101
return nil, 0, nil, fmt.Errorf("failed to apply transaction: %v err: %v", args.toTransaction().Hash(), err)
21022102
}

les/odr_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func odrContractCall(ctx context.Context, db ethdb.Database, config *params.Chai
142142
//vmenv := core.NewEnv(statedb, config, bc, msg, header, vm.Config{})
143143
gp := new(core.GasPool).AddGas(math.MaxUint64)
144144
owner := common.Address{}
145-
result, _, _ := core.ApplyMessage(vmenv, msg, gp, owner)
145+
result, _ := core.ApplyMessage(vmenv, msg, gp, owner)
146146
res = append(res, result.Return()...)
147147
}
148148
} else {
@@ -160,7 +160,7 @@ func odrContractCall(ctx context.Context, db ethdb.Database, config *params.Chai
160160
vmenv := vm.NewEVM(context, txContext, statedb, nil, config, vm.Config{NoBaseFee: true})
161161
gp := new(core.GasPool).AddGas(math.MaxUint64)
162162
owner := common.Address{}
163-
result, _, _ := core.ApplyMessage(vmenv, msg, gp, owner)
163+
result, _ := core.ApplyMessage(vmenv, msg, gp, owner)
164164
if statedb.Error() == nil {
165165
res = append(res, result.Return()...)
166166
}

light/odr_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func odrContractCall(ctx context.Context, db ethdb.Database, bc *core.BlockChain
191191
vmenv := vm.NewEVM(context, txContext, st, nil, config, vm.Config{NoBaseFee: true})
192192
gp := new(core.GasPool).AddGas(math.MaxUint64)
193193
owner := common.Address{}
194-
result, _, _ := core.ApplyMessage(vmenv, msg, gp, owner)
194+
result, _ := core.ApplyMessage(vmenv, msg, gp, owner)
195195
res = append(res, result.Return()...)
196196
if st.Error() != nil {
197197
return res, st.Error()

0 commit comments

Comments
 (0)