From 760f5e5b2414039d15d8a2c412cd9e9da42b7eb2 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Wed, 20 Sep 2023 18:54:43 +0200 Subject: [PATCH] fix tx gaslimit and type default --- internal/ethapi/api.go | 24 ++++++++++++------------ internal/ethapi/api_test.go | 6 +++--- internal/ethapi/transaction_args.go | 8 ++++---- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index afcbce8a2e..47687fb242 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -449,7 +449,7 @@ func (s *PersonalAccountAPI) signTransaction(ctx context.Context, args *Transact return nil, err } // Assemble the transaction and sign with the wallet - tx := args.toTransaction() + tx := args.toTransaction(false) return wallet.SignTxWithPassphrase(account, passwd, tx, s.b.ChainConfig().ChainID) } @@ -492,7 +492,7 @@ func (s *PersonalAccountAPI) SignTransaction(ctx context.Context, args Transacti return nil, errors.New("nonce not specified") } // Before actually signing the transaction, ensure the transaction fee is reasonable. - tx := args.toTransaction() + tx := args.toTransaction(false) if err := checkTxFee(tx.GasPrice(), tx.Gas(), s.b.RPCTxFeeCap()); err != nil { return nil, err } @@ -1299,7 +1299,7 @@ func (s *BlockChainAPI) MulticallV1(ctx context.Context, opts multicallOpts, blo if blockContext.Random != nil { results[bi].PrevRandao = *blockContext.Random } - gasUsed := uint64(0) + var gasUsed uint64 for i, call := range block.Calls { // setDefaults will consult txpool's nonce tracker. Work around that. if call.Nonce == nil { @@ -1308,8 +1308,8 @@ func (s *BlockChainAPI) MulticallV1(ctx context.Context, opts multicallOpts, blo } // Let the call run wild unless explicitly specified. if call.Gas == nil { - leftGas := gp.Gas() - call.Gas = (*hexutil.Uint64)(&leftGas) + remaining := blockContext.GasLimit - gasUsed + call.Gas = (*hexutil.Uint64)(&remaining) } if call.GasPrice == nil && call.MaxFeePerGas == nil && call.MaxPriorityFeePerGas == nil { call.GasPrice = (*hexutil.Big)(big.NewInt(0)) @@ -1318,7 +1318,7 @@ func (s *BlockChainAPI) MulticallV1(ctx context.Context, opts multicallOpts, blo if err := call.setDefaults(ctx, s.b); err != nil { return nil, err } - tx := call.ToTransaction() + tx := call.ToTransaction(true) vmConfig := &vm.Config{ NoBaseFee: true, Tracer: newTracer(opts.TraceTransfers, blockContext.BlockNumber.Uint64(), hash, tx.Hash(), uint(i)), @@ -1853,7 +1853,7 @@ func AccessList(ctx context.Context, b Backend, blockNrOrHash rpc.BlockNumberOrH vmenv, _ := b.GetEVM(ctx, msg, statedb, header, &config, nil) res, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.GasLimit)) if err != nil { - return nil, 0, nil, fmt.Errorf("failed to apply transaction: %v err: %v", args.toTransaction().Hash(), err) + return nil, 0, nil, fmt.Errorf("failed to apply transaction: %v err: %v", args.toTransaction(false).Hash(), err) } if tracer.Equal(prevTracer) { return accessList, res.UsedGas, res.Err, nil @@ -2121,7 +2121,7 @@ func (s *TransactionAPI) SendTransaction(ctx context.Context, args TransactionAr return common.Hash{}, err } // Assemble the transaction and sign with the wallet - tx := args.toTransaction() + tx := args.toTransaction(false) signed, err := wallet.SignTx(account, tx, s.b.ChainConfig().ChainID) if err != nil { @@ -2139,7 +2139,7 @@ func (s *TransactionAPI) FillTransaction(ctx context.Context, args TransactionAr return nil, err } // Assemble the transaction and obtain rlp - tx := args.toTransaction() + tx := args.toTransaction(false) data, err := tx.MarshalBinary() if err != nil { return nil, err @@ -2205,7 +2205,7 @@ func (s *TransactionAPI) SignTransaction(ctx context.Context, args TransactionAr return nil, err } // Before actually sign the transaction, ensure the transaction fee is reasonable. - tx := args.toTransaction() + tx := args.toTransaction(false) if err := checkTxFee(tx.GasPrice(), tx.Gas(), s.b.RPCTxFeeCap()); err != nil { return nil, err } @@ -2253,7 +2253,7 @@ func (s *TransactionAPI) Resend(ctx context.Context, sendArgs TransactionArgs, g if err := sendArgs.setDefaults(ctx, s.b); err != nil { return common.Hash{}, err } - matchTx := sendArgs.toTransaction() + matchTx := sendArgs.toTransaction(false) // Before replacing the old transaction, ensure the _new_ transaction fee is reasonable. var price = matchTx.GasPrice() @@ -2283,7 +2283,7 @@ func (s *TransactionAPI) Resend(ctx context.Context, sendArgs TransactionArgs, g if gasLimit != nil && *gasLimit != 0 { sendArgs.Gas = gasLimit } - signedTx, err := s.sign(sendArgs.from(), sendArgs.toTransaction()) + signedTx, err := s.sign(sendArgs.from(), sendArgs.toTransaction(false)) if err != nil { return common.Hash{}, err } diff --git a/internal/ethapi/api_test.go b/internal/ethapi/api_test.go index 6766dff434..56b08fc59e 100644 --- a/internal/ethapi/api_test.go +++ b/internal/ethapi/api_test.go @@ -992,7 +992,7 @@ func TestMulticallV1(t *testing.T) { Index hexutil.Uint `json:"logIndex"` } type callRes struct { - ReturnValue string `json:"return"` + ReturnValue string `json:"returnData"` Error string Logs []log GasUsed string @@ -1123,7 +1123,7 @@ func TestMulticallV1(t *testing.T) { GasUsed: "0x0", Logs: []log{}, Status: "0x0", - Error: fmt.Sprintf("err: insufficient funds for gas * price + value: address %s have 0 want 1000 (supplied gas 9937000)", randomAccounts[3].addr.String()), + Error: fmt.Sprintf("err: insufficient funds for gas * price + value: address %s have 0 want 1000 (supplied gas 4691388)", randomAccounts[3].addr.String()), }}, }}, }, { @@ -1574,7 +1574,7 @@ func TestMulticallV1(t *testing.T) { GasUsed: "0x0", Logs: []log{}, Status: "0x0", - Error: fmt.Sprintf("err: nonce too high: address %s, tx: 2 state: 0 (supplied gas 10000000)", accounts[2].addr), + Error: fmt.Sprintf("err: nonce too high: address %s, tx: 2 state: 0 (supplied gas 4712388)", accounts[2].addr), }}, }}, }, diff --git a/internal/ethapi/transaction_args.go b/internal/ethapi/transaction_args.go index 7ce61b78d3..95983d797c 100644 --- a/internal/ethapi/transaction_args.go +++ b/internal/ethapi/transaction_args.go @@ -286,10 +286,10 @@ func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int, sk // toTransaction converts the arguments to a transaction. // This assumes that setDefaults has been called. -func (args *TransactionArgs) toTransaction() *types.Transaction { +func (args *TransactionArgs) toTransaction(type2 bool) *types.Transaction { var data types.TxData switch { - case args.MaxFeePerGas != nil: + case args.MaxFeePerGas != nil || type2: al := types.AccessList{} if args.AccessList != nil { al = *args.AccessList @@ -331,6 +331,6 @@ func (args *TransactionArgs) toTransaction() *types.Transaction { // ToTransaction converts the arguments to a transaction. // This assumes that setDefaults has been called. -func (args *TransactionArgs) ToTransaction() *types.Transaction { - return args.toTransaction() +func (args *TransactionArgs) ToTransaction(type2 bool) *types.Transaction { + return args.toTransaction(type2) }