Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/api/redistribution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func TestRedistributionStatus(t *testing.T) {
backendmock.WithBalanceAt(func(ctx context.Context, address common.Address, block *big.Int) (*big.Int, error) {
return big.NewInt(100000000), nil
}),
backendmock.WithSuggestGasPriceFunc(func(ctx context.Context) (*big.Int, error) {
return big.NewInt(1), nil
backendmock.WithSuggestedFeeAndTipFunc(func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
return big.NewInt(1), big.NewInt(2), nil
}),
},
})
Expand Down
6 changes: 3 additions & 3 deletions pkg/node/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,12 @@ func (m noOpChainBackend) PendingNonceAt(context.Context, common.Address) (uint6
panic("chain no op: PendingNonceAt")
}

func (m noOpChainBackend) SuggestGasPrice(context.Context) (*big.Int, error) {
panic("chain no op: SuggestGasPrice")
func (m noOpChainBackend) SuggestedFeeAndTip(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
panic("chain no op: SuggestedFeeAndTip")
}

func (m noOpChainBackend) SuggestGasTipCap(context.Context) (*big.Int, error) {
panic("chain no op: SuggestGasPrice")
panic("chain no op: SuggestGasTipCap")
}

func (m noOpChainBackend) EstimateGas(context.Context, ethereum.CallMsg) (uint64, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/settlement/swap/chequebook/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func checkBalance(
minimumEth := big.NewInt(0)

if gasPrice == nil {
gasPrice, err = swapBackend.SuggestGasPrice(timeoutCtx)
gasPrice, _, err = swapBackend.SuggestedFeeAndTip(timeoutCtx, gasPrice, 0)
if err != nil {
return err
}

minimumEth = gasPrice.Mul(gasPrice, big.NewInt(250000))
minimumEth = new(big.Int).Mul(gasPrice, big.NewInt(250000))
}

insufficientERC20 := erc20Balance.Cmp(swapInitialDeposit) < 0
Expand Down
4 changes: 2 additions & 2 deletions pkg/storageincentives/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type ChainBackend interface {
BlockNumber(context.Context) (uint64, error)
HeaderByNumber(context.Context, *big.Int) (*types.Header, error)
BalanceAt(ctx context.Context, address common.Address, block *big.Int) (*big.Int, error)
SuggestGasPrice(ctx context.Context) (*big.Int, error)
SuggestedFeeAndTip(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error)
}

type Health interface {
Expand Down Expand Up @@ -604,7 +604,7 @@ func (a *Agent) HasEnoughFundsToPlay(ctx context.Context) (*big.Int, bool, error
return nil, false, err
}

price, err := a.backend.SuggestGasPrice(ctx)
price, _, err := a.backend.SuggestedFeeAndTip(ctx, nil, redistribution.BoostTipPercent)
if err != nil {
return nil, false, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/storageincentives/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ func (m *mockchainBackend) BalanceAt(ctx context.Context, address common.Address
return m.balance, nil
}

func (m *mockchainBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
return big.NewInt(4), nil
func (m *mockchainBackend) SuggestedFeeAndTip(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
return big.NewInt(4), big.NewInt(5), nil
}

type contractCall int
Expand Down
11 changes: 7 additions & 4 deletions pkg/storageincentives/redistribution/redistribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import (
"github.com/ethersphere/bee/v2/pkg/transaction"
)

const loggerName = "redistributionContract"
const (
loggerName = "redistributionContract"
BoostTipPercent = 50
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in a future PR, this could be dynamic based on how far the node is from the end of the round phase. wdyt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is nice idea

)

type Contract interface {
ReserveSalt(context.Context) ([]byte, error)
Expand Down Expand Up @@ -117,7 +120,7 @@ func (c *contract) Claim(ctx context.Context, proofs ChunkInclusionProofs) (comm
Value: big.NewInt(0),
Description: "claim win transaction",
}
txHash, err := c.sendAndWait(ctx, request, transaction.RedistributionTipBoostPercent)
txHash, err := c.sendAndWait(ctx, request, BoostTipPercent)
if err != nil {
return txHash, fmt.Errorf("claim: %w", err)
}
Expand All @@ -140,7 +143,7 @@ func (c *contract) Commit(ctx context.Context, obfusHash []byte, round uint64) (
Value: big.NewInt(0),
Description: "commit transaction",
}
txHash, err := c.sendAndWait(ctx, request, transaction.RedistributionTipBoostPercent)
txHash, err := c.sendAndWait(ctx, request, BoostTipPercent)
if err != nil {
return txHash, fmt.Errorf("commit: obfusHash %v: %w", common.BytesToHash(obfusHash), err)
}
Expand All @@ -163,7 +166,7 @@ func (c *contract) Reveal(ctx context.Context, storageDepth uint8, reserveCommit
Value: big.NewInt(0),
Description: "reveal transaction",
}
txHash, err := c.sendAndWait(ctx, request, transaction.RedistributionTipBoostPercent)
txHash, err := c.sendAndWait(ctx, request, BoostTipPercent)
if err != nil {
return txHash, fmt.Errorf("reveal: storageDepth %d reserveCommitmentHash %v RandomNonce %v: %w", storageDepth, common.BytesToHash(reserveCommitmentHash), common.BytesToHash(RandomNonce), err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/transaction/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type Backend interface {
CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
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)
SuggestedFeeAndTip(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *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
14 changes: 7 additions & 7 deletions pkg/transaction/backendmock/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type backendMock struct {
codeAt func(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error)
callContract func(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
sendTransaction func(ctx context.Context, tx *types.Transaction) error
suggestGasPrice func(ctx context.Context) (*big.Int, error)
suggestedFeeAndTip func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *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)
Expand Down Expand Up @@ -57,11 +57,11 @@ func (m *backendMock) PendingNonceAt(ctx context.Context, account common.Address
return 0, errors.New("not implemented")
}

func (m *backendMock) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
if m.suggestGasPrice != nil {
return m.suggestGasPrice(ctx)
func (m *backendMock) SuggestedFeeAndTip(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
if m.suggestedFeeAndTip != nil {
return m.suggestedFeeAndTip(ctx, gasPrice, boostPercent)
}
return nil, errors.New("not implemented")
return nil, nil, errors.New("not implemented")
}

func (m *backendMock) EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
Expand Down Expand Up @@ -191,9 +191,9 @@ func WithPendingNonceAtFunc(f func(ctx context.Context, account common.Address)
})
}

func WithSuggestGasPriceFunc(f func(ctx context.Context) (*big.Int, error)) Option {
func WithSuggestedFeeAndTipFunc(f func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error)) Option {
return optionFunc(func(s *backendMock) {
s.suggestGasPrice = f
s.suggestedFeeAndTip = f
})
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/transaction/backendsimulation/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func (m *simulatedBackend) PendingNonceAt(ctx context.Context, account common.Ad
return 0, errors.New("not implemented")
}

func (m *simulatedBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
return nil, errors.New("not implemented")
func (m *simulatedBackend) SuggestedFeeAndTip(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
return nil, nil, errors.New("not implemented")
}

func (m *simulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
Expand Down
62 changes: 7 additions & 55 deletions pkg/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,11 @@ var (
ErrTransactionReverted = errors.New("transaction reverted")
ErrUnknownTransaction = errors.New("unknown transaction")
ErrAlreadyImported = errors.New("already imported")
ErrEIP1559NotSupported = errors.New("network does not appear to support EIP-1559 (no baseFee)")
)

const (
DefaultGasLimit = 1_000_000
DefaultTipBoostPercent = 25
MinimumGasTipCap = 1_500_000_000 // 1.5 Gwei
RedistributionTipBoostPercent = 50
DefaultGasLimit = 1_000_000
DefaultTipBoostPercent = 25
)

// TxRequest describes a request for a transaction that can be executed.
Expand Down Expand Up @@ -309,7 +306,7 @@ func (t *transactionService) prepareTransaction(ctx context.Context, request *Tx
notice that gas price does not exceed 20 as defined by max fee.
*/

gasFeeCap, gasTipCap, err := t.suggestedFeeAndTip(ctx, request.GasPrice, boostPercent)
gasFeeCap, gasTipCap, err := t.backend.SuggestedFeeAndTip(ctx, request.GasPrice, boostPercent)
if err != nil {
return nil, err
}
Expand All @@ -326,51 +323,6 @@ func (t *transactionService) prepareTransaction(ctx context.Context, request *Tx
}), nil
}

func (t *transactionService) suggestedFeeAndTip(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
gasTipCap, err := t.backend.SuggestGasTipCap(ctx)
if err != nil {
return nil, nil, err
}

multiplier := big.NewInt(int64(boostPercent) + 100)
gasTipCap = new(big.Int).Div(new(big.Int).Mul(gasTipCap, multiplier), big.NewInt(100))

minimumTip := big.NewInt(MinimumGasTipCap)
if gasTipCap.Cmp(minimumTip) < 0 {
gasTipCap = new(big.Int).Set(minimumTip)
}

var gasFeeCap *big.Int

if gasPrice == nil {
latestBlockHeader, err := t.backend.HeaderByNumber(ctx, nil)
if err != nil {
return nil, nil, fmt.Errorf("failed to get latest block: %w", err)
}

if latestBlockHeader.BaseFee == nil {
return nil, nil, ErrEIP1559NotSupported
}

// gasFeeCap = (2 * baseFee) + gasTipCap
gasFeeCap = new(big.Int).Add(
new(big.Int).Mul(latestBlockHeader.BaseFee, big.NewInt(2)),
gasTipCap,
)
} else {
gasFeeCap = new(big.Int).Set(gasPrice)
}

if gasTipCap.Cmp(gasFeeCap) > 0 {
t.logger.Warning("gas tip cap is higher than gas fee cap, using gas fee cap as gas tip cap", "gas_tip_cap", gasTipCap, "gas_fee_cap", gasFeeCap)
gasTipCap = new(big.Int).Set(gasFeeCap)
}

t.logger.Debug("prepare transaction", "gas_max_fee", gasFeeCap, "gas_max_tip", gasTipCap)

return gasFeeCap, gasTipCap, nil
}

func storedTransactionKey(txHash common.Hash) string {
return fmt.Sprintf("%s%x", storedTransactionPrefix, txHash)
}
Expand All @@ -394,7 +346,7 @@ func (t *transactionService) nextNonce(ctx context.Context) (uint64, error) {

// PendingNonceAt returns the nonce we should use, but we will
// compare this to our pending tx list, therefore the -1.
var maxNonce = onchainNonce - 1
maxNonce := onchainNonce - 1
for _, txHash := range pendingTxs {
trx, _, err := t.backend.TransactionByHash(ctx, txHash)
if err != nil {
Expand Down Expand Up @@ -441,7 +393,7 @@ func (t *transactionService) WatchSentTransaction(txHash common.Hash) (<-chan ty
}

func (t *transactionService) PendingTransactions() ([]common.Hash, error) {
var txHashes = make([]common.Hash, 0)
txHashes := make([]common.Hash, 0)
err := t.store.Iterate(pendingTransactionPrefix, func(key, value []byte) (stop bool, err error) {
txHash := common.HexToHash(strings.TrimPrefix(string(key), pendingTransactionPrefix))
txHashes = append(txHashes, txHash)
Expand Down Expand Up @@ -491,7 +443,7 @@ func (t *transactionService) ResendTransaction(ctx context.Context, txHash commo
return err
}

gasFeeCap, gasTipCap, err := t.suggestedFeeAndTip(ctx, sctx.GetGasPrice(ctx), storedTransaction.GasTipBoost)
gasFeeCap, gasTipCap, err := t.backend.SuggestedFeeAndTip(ctx, sctx.GetGasPrice(ctx), storedTransaction.GasTipBoost)
if err != nil {
return err
}
Expand Down Expand Up @@ -531,7 +483,7 @@ func (t *transactionService) CancelTransaction(ctx context.Context, originalTxHa
return common.Hash{}, err
}

gasFeeCap, gasTipCap, err := t.suggestedFeeAndTip(ctx, sctx.GetGasPrice(ctx), 0)
gasFeeCap, gasTipCap, err := t.backend.SuggestedFeeAndTip(ctx, sctx.GetGasPrice(ctx), 0)
if err != nil {
return common.Hash{}, err
}
Expand Down
32 changes: 30 additions & 2 deletions pkg/transaction/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ import (
"github.com/ethersphere/bee/v2/pkg/transaction"
"github.com/ethersphere/bee/v2/pkg/transaction/backendmock"
"github.com/ethersphere/bee/v2/pkg/transaction/monitormock"
"github.com/ethersphere/bee/v2/pkg/transaction/wrapped"
"github.com/ethersphere/bee/v2/pkg/util/abiutil"
"github.com/ethersphere/bee/v2/pkg/util/testutil"
)

var (
minimumTip = big.NewInt(transaction.MinimumGasTipCap)
minimumTip = big.NewInt(wrapped.MinimumGasTipCap)
baseFee = big.NewInt(3_000_000_000)
)

Expand Down Expand Up @@ -136,6 +137,9 @@ func TestTransactionSend(t *testing.T) {
backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) {
return &types.Header{BaseFee: baseFee}, nil
}),
backendmock.WithSuggestedFeeAndTipFunc(func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
return gasFeeCap, suggestedGasTip, nil
}),
),
signerMockForTransaction(t, signedTx, sender, chainID),
store,
Expand Down Expand Up @@ -243,6 +247,9 @@ func TestTransactionSend(t *testing.T) {
backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) {
return &types.Header{BaseFee: baseFee}, nil
}),
backendmock.WithSuggestedFeeAndTipFunc(func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
return gasFeeCap, suggestedGasTip, nil
}),
),
signerMockForTransaction(t, signedTx, sender, chainID),
store,
Expand Down Expand Up @@ -359,6 +366,9 @@ func TestTransactionSend(t *testing.T) {
backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) {
return &types.Header{BaseFee: baseFee}, nil
}),
backendmock.WithSuggestedFeeAndTipFunc(func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
return gasFeeCapWithBoost, suggestedGasTip, nil
}),
),
signerMockForTransaction(t, signedTx, sender, chainID),
store,
Expand Down Expand Up @@ -471,6 +481,9 @@ func TestTransactionSend(t *testing.T) {
backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) {
return &types.Header{BaseFee: baseFee}, nil
}),
backendmock.WithSuggestedFeeAndTipFunc(func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
return gasFeeCap, suggestedGasTip, nil
}),
),
signerMockForTransaction(t, signedTx, sender, chainID),
store,
Expand Down Expand Up @@ -539,6 +552,9 @@ func TestTransactionSend(t *testing.T) {
backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) {
return &types.Header{BaseFee: baseFee}, nil
}),
backendmock.WithSuggestedFeeAndTipFunc(func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
return gasFeeCap, suggestedGasTip, nil
}),
),
signerMockForTransaction(t, signedTx, sender, chainID),
store,
Expand Down Expand Up @@ -608,6 +624,9 @@ func TestTransactionSend(t *testing.T) {
backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) {
return &types.Header{BaseFee: baseFee}, nil
}),
backendmock.WithSuggestedFeeAndTipFunc(func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
return customGasFeeCap, customGasFeeCap, nil
}),
),
signerMockForTransaction(t, signedTx, sender, chainID),
store,
Expand Down Expand Up @@ -754,6 +773,9 @@ func TestTransactionResend(t *testing.T) {
backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) {
return &types.Header{BaseFee: baseFee}, nil
}),
backendmock.WithSuggestedFeeAndTipFunc(func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
return gasFeeCap, gasTip, nil
}),
),
signerMockForTransaction(t, signedTx, recipient, chainID),
store,
Expand Down Expand Up @@ -844,6 +866,9 @@ func TestTransactionCancel(t *testing.T) {
backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) {
return &types.Header{BaseFee: baseFee}, nil
}),
backendmock.WithSuggestedFeeAndTipFunc(func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
return fee, minimumTip, nil
}),
),
signerMockForTransaction(t, cancelTx, recipient, chainID),
store,
Expand Down Expand Up @@ -879,7 +904,7 @@ func TestTransactionCancel(t *testing.T) {
Value: big.NewInt(0),
Gas: 21000,
GasFeeCap: gasFeeCap,
GasTipCap: gasTip,
GasTipCap: gasTipCap,
Data: []byte{},
})

Expand All @@ -894,6 +919,9 @@ func TestTransactionCancel(t *testing.T) {
backendmock.WithSuggestGasTipCapFunc(func(ctx context.Context) (*big.Int, error) {
return gasTip, nil
}),
backendmock.WithSuggestedFeeAndTipFunc(func(ctx context.Context, gasPrice *big.Int, boostPercent int) (*big.Int, *big.Int, error) {
return gasFee, gasTip, nil
}),
),
signerMockForTransaction(t, cancelTx, recipient, chainID),
store,
Expand Down
Loading
Loading