From 26f21dd83805f49b77cb649f845a01d233fcf607 Mon Sep 17 00:00:00 2001 From: darkobas Date: Tue, 30 May 2023 13:57:21 +0200 Subject: [PATCH] Revert "feat: eip 1559 dynamic transactions (#3504)" This reverts commit e11d96b808547b55ea85e7ee49a2be2539f2d929. --- pkg/crypto/signer.go | 2 +- pkg/node/chain.go | 3 - pkg/postage/postagecontract/contract.go | 4 +- pkg/settlement/swap/chequebook/cashout.go | 2 +- pkg/settlement/swap/chequebook/chequebook.go | 2 +- pkg/settlement/swap/chequebook/factory.go | 2 +- pkg/settlement/swap/erc20/erc20.go | 2 +- pkg/storageincentives/staking/contract.go | 2 +- pkg/transaction/backend.go | 1 - pkg/transaction/backendmock/backend.go | 10 --- pkg/transaction/transaction.go | 80 +++----------------- pkg/transaction/transaction_test.go | 78 ++++++++++--------- pkg/transaction/wrapped/wrapped.go | 11 --- 13 files changed, 62 insertions(+), 137 deletions(-) diff --git a/pkg/crypto/signer.go b/pkg/crypto/signer.go index 6a82101eff0..4156e697503 100644 --- a/pkg/crypto/signer.go +++ b/pkg/crypto/signer.go @@ -90,7 +90,7 @@ func (d *defaultSigner) Sign(data []byte) (signature []byte, err error) { // SignTx signs an ethereum transaction. func (d *defaultSigner) SignTx(transaction *types.Transaction, chainID *big.Int) (*types.Transaction, error) { - txSigner := types.NewLondonSigner(chainID) + txSigner := types.NewEIP155Signer(chainID) hash := txSigner.Hash(transaction).Bytes() // isCompressedKey is false here so we get the expected v value (27 or 28) signature, err := d.sign(hash, false) diff --git a/pkg/node/chain.go b/pkg/node/chain.go index 3d5e38dac2c..6518375a23d 100644 --- a/pkg/node/chain.go +++ b/pkg/node/chain.go @@ -399,9 +399,6 @@ func (m noOpChainBackend) PendingNonceAt(context.Context, common.Address) (uint6 func (m noOpChainBackend) SuggestGasPrice(context.Context) (*big.Int, error) { panic("chain no op: SuggestGasPrice") } -func (m noOpChainBackend) SuggestGasTipCap(context.Context) (*big.Int, error) { - panic("chain no op: SuggestGasPrice") -} func (m noOpChainBackend) EstimateGas(context.Context, ethereum.CallMsg) (uint64, error) { panic("chain no op: EstimateGas") } diff --git a/pkg/postage/postagecontract/contract.go b/pkg/postage/postagecontract/contract.go index 5a1c8cd5d62..4c5327676c3 100644 --- a/pkg/postage/postagecontract/contract.go +++ b/pkg/postage/postagecontract/contract.go @@ -161,7 +161,7 @@ func (c *postageContract) sendApproveTransaction(ctx context.Context, amount *bi GasLimit: 65000, Value: big.NewInt(0), Description: approveDescription, - }, transaction.DefaultTipBoostPercent) + }, 0) if err != nil { return nil, err } @@ -188,7 +188,7 @@ func (c *postageContract) sendTransaction(ctx context.Context, callData []byte, Description: desc, } - txHash, err := c.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent) + txHash, err := c.transactionService.Send(ctx, request, 0) if err != nil { return nil, err } diff --git a/pkg/settlement/swap/chequebook/cashout.go b/pkg/settlement/swap/chequebook/cashout.go index 157af01ad56..a7d00d87f40 100644 --- a/pkg/settlement/swap/chequebook/cashout.go +++ b/pkg/settlement/swap/chequebook/cashout.go @@ -149,7 +149,7 @@ func (s *cashoutService) CashCheque(ctx context.Context, chequebook, recipient c Description: "cheque cashout", } - txHash, err := s.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent) + txHash, err := s.transactionService.Send(ctx, request, 0) if err != nil { return common.Hash{}, err } diff --git a/pkg/settlement/swap/chequebook/chequebook.go b/pkg/settlement/swap/chequebook/chequebook.go index 8b7f2a02d8c..3d986aba435 100644 --- a/pkg/settlement/swap/chequebook/chequebook.go +++ b/pkg/settlement/swap/chequebook/chequebook.go @@ -335,7 +335,7 @@ func (s *service) Withdraw(ctx context.Context, amount *big.Int) (hash common.Ha Description: fmt.Sprintf("chequebook withdrawal of %d BZZ", amount), } - txHash, err := s.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent) + txHash, err := s.transactionService.Send(ctx, request, 0) if err != nil { return common.Hash{}, err } diff --git a/pkg/settlement/swap/chequebook/factory.go b/pkg/settlement/swap/chequebook/factory.go index ae38abe705e..584027f6881 100644 --- a/pkg/settlement/swap/chequebook/factory.go +++ b/pkg/settlement/swap/chequebook/factory.go @@ -88,7 +88,7 @@ func (c *factory) Deploy(ctx context.Context, issuer common.Address, defaultHard Description: "chequebook deployment", } - txHash, err := c.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent) + txHash, err := c.transactionService.Send(ctx, request, 0) if err != nil { return common.Hash{}, err } diff --git a/pkg/settlement/swap/erc20/erc20.go b/pkg/settlement/swap/erc20/erc20.go index a50a06962dc..916f25f756b 100644 --- a/pkg/settlement/swap/erc20/erc20.go +++ b/pkg/settlement/swap/erc20/erc20.go @@ -84,7 +84,7 @@ func (c *erc20Service) Transfer(ctx context.Context, address common.Address, val Description: "token transfer", } - txHash, err := c.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent) + txHash, err := c.transactionService.Send(ctx, request, 0) if err != nil { return common.Hash{}, err } diff --git a/pkg/storageincentives/staking/contract.go b/pkg/storageincentives/staking/contract.go index 5726c083afb..da32a154a3a 100644 --- a/pkg/storageincentives/staking/contract.go +++ b/pkg/storageincentives/staking/contract.go @@ -117,7 +117,7 @@ func (c *contract) sendTransaction(ctx context.Context, callData []byte, desc st Description: desc, } - txHash, err := c.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent) + txHash, err := c.transactionService.Send(ctx, request, 0) if err != nil { return nil, err } diff --git a/pkg/transaction/backend.go b/pkg/transaction/backend.go index cb7718dbfc2..a2f44f6fe43 100644 --- a/pkg/transaction/backend.go +++ b/pkg/transaction/backend.go @@ -23,7 +23,6 @@ type Backend interface { HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) SuggestGasPrice(ctx context.Context) (*big.Int, error) - SuggestGasTipCap(ctx context.Context) (*big.Int, error) EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) SendTransaction(ctx context.Context, tx *types.Transaction) error TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) diff --git a/pkg/transaction/backendmock/backend.go b/pkg/transaction/backendmock/backend.go index dde7635a093..4fede18b5c0 100644 --- a/pkg/transaction/backendmock/backend.go +++ b/pkg/transaction/backendmock/backend.go @@ -19,7 +19,6 @@ type backendMock struct { codeAt func(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) sendTransaction func(ctx context.Context, tx *types.Transaction) error suggestGasPrice func(ctx context.Context) (*big.Int, error) - suggestGasTipCap func(ctx context.Context) (*big.Int, error) estimateGas func(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) transactionReceipt func(ctx context.Context, txHash common.Hash) (*types.Receipt, error) pendingNonceAt func(ctx context.Context, account common.Address) (uint64, error) @@ -131,9 +130,6 @@ func (m *backendMock) NonceAt(ctx context.Context, account common.Address, block } func (m *backendMock) SuggestGasTipCap(ctx context.Context) (*big.Int, error) { - if m.suggestGasPrice != nil { - return m.suggestGasTipCap(ctx) - } return nil, errors.New("not implemented") } @@ -189,12 +185,6 @@ func WithSuggestGasPriceFunc(f func(ctx context.Context) (*big.Int, error)) Opti }) } -func WithSuggestGasTipCapFunc(f func(ctx context.Context) (*big.Int, error)) Option { - return optionFunc(func(s *backendMock) { - s.suggestGasTipCap = f - }) -} - func WithEstimateGasFunc(f func(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error)) Option { return optionFunc(func(s *backendMock) { s.estimateGas = f diff --git a/pkg/transaction/transaction.go b/pkg/transaction/transaction.go index 26f8a49edd7..71ec792a957 100644 --- a/pkg/transaction/transaction.go +++ b/pkg/transaction/transaction.go @@ -18,7 +18,6 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethersphere/bee/pkg/crypto" "github.com/ethersphere/bee/pkg/log" - "github.com/ethersphere/bee/pkg/sctx" "github.com/ethersphere/bee/pkg/storage" "golang.org/x/net/context" ) @@ -40,7 +39,9 @@ var ( ErrAlreadyImported = errors.New("already imported") ) -const DefaultTipBoostPercent = 20 +// minGasPrice determines the minimum gas price +// threshold (in wei) for the creation of a transaction. +var minGasPrice = big.NewInt(1000) // TxRequest describes a request for a transaction that can be executed. type TxRequest struct { @@ -73,7 +74,7 @@ type StoredTransaction struct { type Service interface { io.Closer // Send creates a transaction based on the request (with gasprice increased by provided percentage) and sends it. - Send(ctx context.Context, request *TxRequest, tipCapBoostPercent int) (txHash common.Hash, err error) + Send(ctx context.Context, request *TxRequest, priceBoostPercent int) (txHash common.Hash, err error) // Call simulate a transaction based on the request. Call(ctx context.Context, request *TxRequest) (result []byte, err error) // WaitForReceipt waits until either the transaction with the given hash has been mined or the context is cancelled. @@ -293,22 +294,14 @@ func (t *transactionService) prepareTransaction(ctx context.Context, request *Tx gasLimit = request.GasLimit } - /* - Transactions are EIP 1559 dynamic transactions where there are three fee related fields: - 1. base fee is the price that will be burned as part of the transaction. - 2. max fee is the max price we are willing to spend as gas price. - 3. max priority fee is max price want to give to the miner to prioritize the transaction. - as an example: - if base fee is 15, max fee is 20, and max priority is 3, gas price will be 15 + 3 = 18 - if base is 15, max fee is 20, and max priority fee is 10, - gas price will be 15 + 10 = 25, but since 25 > 20, gas price is 20. - notice that gas price does not exceed 20 as defined by max fee. - */ - - gasFeeCap, gasTipCap, err := t.suggestedFeeAndTip(ctx, request.GasPrice, boostPercent) - if err != nil { - return nil, err - } + gasPrice := request.GasPrice + if gasPrice == nil { + gasPriceSuggested, err := t.backend.SuggestGasPrice(ctx) + gasPrice = new(big.Int).Div(new(big.Int).Mul(big.NewInt(int64(boostPercent)+100), gasPriceSuggested), big.NewInt(100)) + if err != nil { + return nil, err + } + } return types.NewTx(&types.DynamicFeeTx{ Nonce: nonce, @@ -316,37 +309,10 @@ func (t *transactionService) prepareTransaction(ctx context.Context, request *Tx To: request.To, Value: request.Value, Gas: gasLimit, - GasFeeCap: gasFeeCap, - GasTipCap: gasTipCap, Data: request.Data, }), nil } -func (t *transactionService) suggestedFeeAndTip(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) { - var err error - - if gasPrice == nil { - gasPrice, err = t.backend.SuggestGasPrice(ctx) - if err != nil { - return nil, nil, err - } - gasPrice = new(big.Int).Div(new(big.Int).Mul(big.NewInt(int64(boostPercent)+100), gasPrice), big.NewInt(100)) - } - - gasTipCap, err := new(big.Int).SetUint64(2999999993), nil - if err != nil { - return nil, nil, err - } - - gasTipCap = new(big.Int).Div(new(big.Int).Mul(big.NewInt(int64(boostPercent)+100), gasTipCap), big.NewInt(100)) - gasFeeCap := new(big.Int).Add(gasTipCap, gasPrice) - - t.logger.Debug("prepare transaction", "gas_price", gasPrice, "gas_max_fee", gasFeeCap, "gas_max_tip", gasTipCap) - - return gasFeeCap, gasTipCap, nil - -} - func (t *transactionService) nonceKey() string { return fmt.Sprintf("%s%x", noncePrefix, t.sender) } @@ -471,7 +437,6 @@ func (t *transactionService) ResendTransaction(ctx context.Context, txHash commo return err } - gasFeeCap, gasTipCap, err := t.suggestedFeeAndTip(ctx, sctx.GetGasPrice(ctx), storedTransaction.GasTipBoost) if err != nil { return err } @@ -482,8 +447,6 @@ func (t *transactionService) ResendTransaction(ctx context.Context, txHash commo To: storedTransaction.To, Value: storedTransaction.Value, Gas: storedTransaction.GasLimit, - GasTipCap: gasTipCap, - GasFeeCap: gasFeeCap, Data: storedTransaction.Data, }) @@ -511,31 +474,12 @@ func (t *transactionService) CancelTransaction(ctx context.Context, originalTxHa return common.Hash{}, err } - gasFeeCap, gasTipCap, err := t.suggestedFeeAndTip(ctx, sctx.GetGasPrice(ctx), 0) - if err != nil { - return common.Hash{}, err - } - - if gasFeeCap.Cmp(storedTransaction.GasFeeCap) <= 0 { - gasFeeCap = storedTransaction.GasFeeCap - } - - if gasTipCap.Cmp(storedTransaction.GasTipCap) <= 0 { - gasTipCap = storedTransaction.GasTipCap - } - - gasTipCap = new(big.Int).Div(new(big.Int).Mul(big.NewInt(int64(10)+100), gasTipCap), big.NewInt(100)) - - gasFeeCap.Add(gasFeeCap, gasTipCap) - signedTx, err := t.signer.SignTx(types.NewTx(&types.DynamicFeeTx{ Nonce: storedTransaction.Nonce, ChainID: t.chainID, To: &t.sender, Value: big.NewInt(0), Gas: 21000, - GasTipCap: gasTipCap, - GasFeeCap: gasFeeCap, Data: []byte{}, }), t.chainID) if err != nil { diff --git a/pkg/transaction/transaction_test.go b/pkg/transaction/transaction_test.go index 60e0989a8a0..9e1e56f1fdc 100644 --- a/pkg/transaction/transaction_test.go +++ b/pkg/transaction/transaction_test.go @@ -31,6 +31,7 @@ func nonceKey(sender common.Address) string { func signerMockForTransaction(t *testing.T, signedTx *types.Transaction, sender common.Address, signerChainID *big.Int) crypto.Signer { t.Helper() + return signermock.New( signermock.WithSignTxFunc(func(transaction *types.Transaction, chainID *big.Int) (*types.Transaction, error) { if transaction.Type() != 2 { @@ -97,8 +98,6 @@ func TestTransactionSend(t *testing.T) { To: &recipient, Value: value, Gas: estimatedGasLimit, - GasFeeCap: defaultGasFee, - GasTipCap: suggestedGasTip, Data: txData, }) request := &transaction.TxRequest{ @@ -135,9 +134,6 @@ func TestTransactionSend(t *testing.T) { backendmock.WithPendingNonceAtFunc(func(ctx context.Context, account common.Address) (uint64, error) { return nonce - 1, nil }), - backendmock.WithSuggestGasTipCapFunc(func(ctx context.Context) (*big.Int, error) { - return suggestedGasTip, nil - }), ), signerMockForTransaction(t, signedTx, sender, chainID), store, @@ -216,20 +212,12 @@ func TestTransactionSend(t *testing.T) { t.Run("sendWithBoost", func(t *testing.T) { t.Parallel() - tip := big.NewInt(0).Div(new(big.Int).Mul(suggestedGasTip, big.NewInt(15)), big.NewInt(10)) - fee := big.NewInt(0).Div(new(big.Int).Mul(suggestedGasPrice, big.NewInt(15)), big.NewInt(10)) - fee = fee.Add(fee, tip) - // tip is the same as suggestedGasPrice and boost is 50% - // so final gas price will be 2.5x suggestedGasPrice - signedTx := types.NewTx(&types.DynamicFeeTx{ ChainID: chainID, Nonce: nonce, To: &recipient, Value: value, Gas: estimatedGasLimit, - GasFeeCap: fee, - GasTipCap: tip, Data: txData, }) request := &transaction.TxRequest{ @@ -266,9 +254,6 @@ func TestTransactionSend(t *testing.T) { backendmock.WithPendingNonceAtFunc(func(ctx context.Context, account common.Address) (uint64, error) { return nonce - 1, nil }), - backendmock.WithSuggestGasTipCapFunc(func(ctx context.Context) (*big.Int, error) { - return suggestedGasTip, nil - }), ), signerMockForTransaction(t, signedTx, sender, chainID), store, @@ -323,8 +308,8 @@ func TestTransactionSend(t *testing.T) { t.Fatalf("got wrong gas limit in stored transaction. wanted %d, got %d", estimatedGasLimit, storedTransaction.GasLimit) } - if fee.Cmp(storedTransaction.GasPrice) != 0 { - t.Fatalf("got wrong gas price in stored transaction. wanted %d, got %d", fee, storedTransaction.GasPrice) + if new(big.Int).Div(new(big.Int).Mul(suggestedGasPrice, big.NewInt(15)), big.NewInt(10)).Cmp(storedTransaction.GasPrice) != 0 { + t.Fatalf("got wrong gas price in stored transaction. wanted %d, got %d", suggestedGasPrice, storedTransaction.GasPrice) } if storedTransaction.Nonce != nonce { @@ -353,8 +338,6 @@ func TestTransactionSend(t *testing.T) { To: &recipient, Value: value, Gas: estimatedGasLimit, - GasTipCap: suggestedGasTip, - GasFeeCap: defaultGasFee, Data: txData, }) request := &transaction.TxRequest{ @@ -387,9 +370,6 @@ func TestTransactionSend(t *testing.T) { backendmock.WithPendingNonceAtFunc(func(ctx context.Context, account common.Address) (uint64, error) { return nonce, nil }), - backendmock.WithSuggestGasTipCapFunc(func(ctx context.Context) (*big.Int, error) { - return suggestedGasTip, nil - }), ), signerMockForTransaction(t, signedTx, sender, chainID), store, @@ -430,8 +410,6 @@ func TestTransactionSend(t *testing.T) { To: &recipient, Value: value, Gas: estimatedGasLimit, - GasTipCap: suggestedGasTip, - GasFeeCap: defaultGasFee, Data: txData, }) request := &transaction.TxRequest{ @@ -468,9 +446,6 @@ func TestTransactionSend(t *testing.T) { backendmock.WithPendingNonceAtFunc(func(ctx context.Context, account common.Address) (uint64, error) { return nextNonce, nil }), - backendmock.WithSuggestGasTipCapFunc(func(ctx context.Context) (*big.Int, error) { - return suggestedGasTip, nil - }), ), signerMockForTransaction(t, signedTx, sender, chainID), store, @@ -584,8 +559,6 @@ func TestTransactionResend(t *testing.T) { To: &recipient, Value: value, Gas: gasLimit, - GasTipCap: gasTip, - GasFeeCap: gasFee, Data: data, }) @@ -655,8 +628,6 @@ func TestTransactionCancel(t *testing.T) { To: &recipient, Value: value, Gas: gasLimit, - GasFeeCap: gasFee, - GasTipCap: gasTip, Data: data, }) err := store.Put(transaction.StoredTransactionKey(signedTx.Hash()), transaction.StoredTransaction{ @@ -685,8 +656,6 @@ func TestTransactionCancel(t *testing.T) { To: &recipient, Value: big.NewInt(0), Gas: 21000, - GasTipCap: gasTipCap, - GasFeeCap: gasFeeCap, Data: []byte{}, }) @@ -736,8 +705,6 @@ func TestTransactionCancel(t *testing.T) { To: &recipient, Value: big.NewInt(0), Gas: 21000, - GasFeeCap: gasFeeCap, - GasTipCap: gasTip, Data: []byte{}, }) @@ -776,4 +743,43 @@ func TestTransactionCancel(t *testing.T) { t.Fatalf("returned wrong hash. wanted %v, got %v", cancelTx.Hash(), cancelTxHash) } }) + + t.Run("too low gas price", func(t *testing.T) { + t.Parallel() + + customGasPrice := big.NewInt(0) + cancelTx := types.NewTx(&types.LegacyTx{ + Nonce: nonce, + To: &recipient, + Value: big.NewInt(0), + Gas: 21000, + GasPrice: customGasPrice, + Data: []byte{}, + }) + + transactionService, err := transaction.NewService(logger, + backendmock.New( + backendmock.WithSendTransactionFunc(func(ctx context.Context, tx *types.Transaction) error { + if tx != cancelTx { + t.Fatal("not sending signed transaction") + } + return nil + }), + ), + signerMockForTransaction(t, cancelTx, recipient, chainID), + store, + chainID, + monitormock.New(), + ) + if err != nil { + t.Fatal(err) + } + defer transactionService.Close() + + ctx := sctx.SetGasPrice(context.Background(), customGasPrice) + _, err = transactionService.CancelTransaction(ctx, signedTx.Hash()) + if !errors.Is(err, transaction.ErrGasPriceTooLow) { + t.Fatalf("returned wrong error. wanted %v, got %v", transaction.ErrGasPriceTooLow, err) + } + }) } diff --git a/pkg/transaction/wrapped/wrapped.go b/pkg/transaction/wrapped/wrapped.go index 7bb1624b31f..b389bc0d3f6 100644 --- a/pkg/transaction/wrapped/wrapped.go +++ b/pkg/transaction/wrapped/wrapped.go @@ -147,17 +147,6 @@ func (b *wrappedBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) return gasPrice, nil } -func (b *wrappedBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) { - b.metrics.TotalRPCCalls.Inc() - b.metrics.SuggestGasPriceCalls.Inc() - gasTipCap, err := b.backend.SuggestGasTipCap(ctx) - if err != nil { - b.metrics.TotalRPCErrors.Inc() - return nil, err - } - return gasTipCap, nil -} - func (b *wrappedBackend) EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) { b.metrics.TotalRPCCalls.Inc() b.metrics.EstimateGasCalls.Inc()