Skip to content

Commit

Permalink
Revert "feat: eip 1559 dynamic transactions (ethersphere#3504)"
Browse files Browse the repository at this point in the history
This reverts commit e11d96b.
  • Loading branch information
darkobas2 committed May 30, 2023
1 parent 292cc16 commit 26f21dd
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 137 deletions.
2 changes: 1 addition & 1 deletion pkg/crypto/signer.go
Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions pkg/node/chain.go
Expand Up @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/postage/postagecontract/contract.go
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/settlement/swap/chequebook/cashout.go
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/settlement/swap/chequebook/chequebook.go
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/settlement/swap/chequebook/factory.go
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/settlement/swap/erc20/erc20.go
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/storageincentives/staking/contract.go
Expand Up @@ -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
}
Expand Down
1 change: 0 additions & 1 deletion pkg/transaction/backend.go
Expand Up @@ -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)
Expand Down
10 changes: 0 additions & 10 deletions pkg/transaction/backendmock/backend.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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
Expand Down
80 changes: 12 additions & 68 deletions pkg/transaction/transaction.go
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -293,60 +294,25 @@ 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,
ChainID: t.chainID,
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)
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
})

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 26f21dd

Please sign in to comment.