Skip to content

Commit

Permalink
- fixes after review + further cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianpascalau committed Dec 21, 2023
1 parent 1c8977f commit 7a682b5
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 385 deletions.
3 changes: 3 additions & 0 deletions interactors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ var ErrNilTransaction = errors.New("nil transaction")
// ErrTxAlreadySent signals that a transaction was already sent
var ErrTxAlreadySent = errors.New("transaction already sent")

// ErrTxWithSameNonceAndGasPriceAlreadySent signals that a transaction with the same nonce & gas price was already sent
var ErrTxWithSameNonceAndGasPriceAlreadySent = errors.New("transaction with the same nonce & gas price was already sent")

// ErrGapNonce signals that a gap nonce between the lowest nonce of the transactions from the cache and the blockchain nonce has been detected
var ErrGapNonce = errors.New("gap nonce detected")
24 changes: 10 additions & 14 deletions interactors/nonceHandlerV2/addressNonceHandler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nonceHandlerV2

import (
"bytes"
"context"
"sync"

Expand Down Expand Up @@ -48,9 +47,9 @@ func NewAddressNonceHandler(proxy interactors.Proxy, address sdkCore.AddressHand

// ApplyNonceAndGasPrice will apply the computed nonce to the given FrontendTransaction
func (anh *addressNonceHandler) ApplyNonceAndGasPrice(ctx context.Context, tx *transaction.FrontendTransaction) error {
oldTx, alreadyExists := anh.isTxAlreadySent(tx)
if alreadyExists {
err := anh.handleTxAlreadyExists(oldTx, tx)
oldTx := anh.getOlderTxWithSameNonce(tx)
if oldTx != nil {
err := anh.handleTxWithSameNonce(oldTx, tx)
if err != nil {
return err
}
Expand All @@ -67,7 +66,7 @@ func (anh *addressNonceHandler) ApplyNonceAndGasPrice(ctx context.Context, tx *t
return nil
}

func (anh *addressNonceHandler) handleTxAlreadyExists(oldTx *transaction.FrontendTransaction, tx *transaction.FrontendTransaction) error {
func (anh *addressNonceHandler) handleTxWithSameNonce(oldTx *transaction.FrontendTransaction, tx *transaction.FrontendTransaction) error {
if oldTx.GasPrice < tx.GasPrice {
return nil
}
Expand All @@ -76,7 +75,7 @@ func (anh *addressNonceHandler) handleTxAlreadyExists(oldTx *transaction.Fronten
return nil
}

return interactors.ErrTxAlreadySent
return interactors.ErrTxWithSameNonceAndGasPriceAlreadySent
}

func (anh *addressNonceHandler) fetchGasPriceIfRequired(ctx context.Context, nonce uint64) {
Expand Down Expand Up @@ -186,19 +185,16 @@ func (anh *addressNonceHandler) DropTransactions() {
anh.mut.Unlock()
}

func (anh *addressNonceHandler) isTxAlreadySent(tx *transaction.FrontendTransaction) (*transaction.FrontendTransaction, bool) {
func (anh *addressNonceHandler) getOlderTxWithSameNonce(tx *transaction.FrontendTransaction) *transaction.FrontendTransaction {
anh.mut.RLock()
defer anh.mut.RUnlock()

for _, oldTx := range anh.transactions {
isTheSameReceiverDataValue := tx.Nonce == oldTx.Nonce &&
oldTx.Receiver == tx.Receiver &&
bytes.Equal(oldTx.Data, tx.Data) &&
oldTx.Value == tx.Value
if isTheSameReceiverDataValue {
return oldTx, true
if oldTx.Nonce == tx.Nonce {
return oldTx
}
}
return nil, false
return nil
}

// IsInterfaceNil returns true if there is no value under the interface
Expand Down
23 changes: 22 additions & 1 deletion interactors/nonceHandlerV2/addressNonceHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,15 @@ func TestAddressNonceHandler_ApplyNonceAndGasPrice(t *testing.T) {
anh, err := NewAddressNonceHandlerWithPrivateAccess(&testsCommon.ProxyStub{}, testAddress)
require.Nil(t, err)

err = anh.ApplyNonceAndGasPrice(context.Background(), &tx)
require.Nil(t, err)

_, err = anh.SendTransaction(context.Background(), &tx)
require.Nil(t, err)

anh.gasPrice = tx.GasPrice
err = anh.ApplyNonceAndGasPrice(context.Background(), &tx)
require.Equal(t, interactors.ErrTxAlreadySent, err)
require.Equal(t, interactors.ErrTxWithSameNonceAndGasPriceAlreadySent, err)
})
t.Run("tx already sent; oldTx.GasPrice < txArgs.GasPrice", func(t *testing.T) {
t.Parallel()
Expand All @@ -75,12 +78,18 @@ func TestAddressNonceHandler_ApplyNonceAndGasPrice(t *testing.T) {
anh, err := NewAddressNonceHandlerWithPrivateAccess(&testsCommon.ProxyStub{}, testAddress)
require.Nil(t, err)

err = anh.ApplyNonceAndGasPrice(context.Background(), &tx)
require.Nil(t, err)

_, err = anh.SendTransaction(context.Background(), &tx)
require.Nil(t, err)

anh.gasPrice = initialGasPrice
err = anh.ApplyNonceAndGasPrice(context.Background(), &tx)
require.Nil(t, err)

_, err = anh.SendTransaction(context.Background(), &tx)
require.Nil(t, err)
})
t.Run("oldTx.GasPrice == txArgs.GasPrice && oldTx.GasPrice < anh.gasPrice", func(t *testing.T) {
t.Parallel()
Expand All @@ -89,12 +98,18 @@ func TestAddressNonceHandler_ApplyNonceAndGasPrice(t *testing.T) {
anh, err := NewAddressNonceHandlerWithPrivateAccess(&testsCommon.ProxyStub{}, testAddress)
require.Nil(t, err)

err = anh.ApplyNonceAndGasPrice(context.Background(), &tx)
require.Nil(t, err)

_, err = anh.SendTransaction(context.Background(), &tx)
require.Nil(t, err)

anh.gasPrice = tx.GasPrice + 1
err = anh.ApplyNonceAndGasPrice(context.Background(), &tx)
require.Nil(t, err)

_, err = anh.SendTransaction(context.Background(), &tx)
require.Nil(t, err)
})
t.Run("same transaction but with different nonce should work", func(t *testing.T) {
t.Parallel()
Expand All @@ -106,11 +121,17 @@ func TestAddressNonceHandler_ApplyNonceAndGasPrice(t *testing.T) {
anh, err := NewAddressNonceHandlerWithPrivateAccess(&testsCommon.ProxyStub{}, testAddress)
require.Nil(t, err)

err = anh.ApplyNonceAndGasPrice(context.Background(), &tx1)
require.Nil(t, err)

_, err = anh.SendTransaction(context.Background(), &tx1)
require.Nil(t, err)

err = anh.ApplyNonceAndGasPrice(context.Background(), &tx2)
require.Nil(t, err)

_, err = anh.SendTransaction(context.Background(), &tx2)
require.Nil(t, err)
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func TestNonceTransactionsHandlerV2_SendDuplicateTransactions(t *testing.T) {
// and computedNonce shall not increase
tx.Nonce = 0
err = nth.ApplyNonceAndGasPrice(context.Background(), testAddress, tx)
require.Equal(t, err, interactors.ErrTxAlreadySent)
require.Equal(t, err, interactors.ErrTxWithSameNonceAndGasPriceAlreadySent)
require.Equal(t, tx.Nonce, uint64(0))
require.Equal(t, accWithPrivateAccess.computedNonce+1, currentNonce)
}
Expand Down
125 changes: 0 additions & 125 deletions interactors/nonceHandlerV2/singleTransactionAddressNonceHandler.go

This file was deleted.

Loading

0 comments on commit 7a682b5

Please sign in to comment.