Skip to content

Commit

Permalink
- optimized code, added protection & fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianpascalau committed Dec 21, 2023
1 parent 7a682b5 commit 725bb5a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
9 changes: 3 additions & 6 deletions interactors/nonceHandlerV2/addressNonceHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,9 @@ func (anh *addressNonceHandler) getOlderTxWithSameNonce(tx *transaction.Frontend
anh.mut.RLock()
defer anh.mut.RUnlock()

for _, oldTx := range anh.transactions {
if oldTx.Nonce == tx.Nonce {
return oldTx
}
}
return nil
olderTx, _ := anh.transactions[tx.Nonce]

return olderTx
}

// IsInterfaceNil returns true if there is no value under the interface
Expand Down
8 changes: 6 additions & 2 deletions interactors/nonceHandlerV2/nonceTransactionsHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ func (nth *nonceTransactionsHandlerV2) SendTransaction(ctx context.Context, tx *
return "", interactors.ErrNilTransaction
}

addrAsBech32 := tx.Sender
// Work with a full copy of the provided transaction so the provided one can change without affecting this component.
// Abnormal and unpredictable behaviors due to the resending mechanism are prevented this way
txCopy := *tx

addrAsBech32 := txCopy.Sender
address, err := data.NewAddressFromBech32String(addrAsBech32)
if err != nil {
return "", fmt.Errorf("%w while creating address handler for string %s", err, addrAsBech32)
Expand All @@ -136,7 +140,7 @@ func (nth *nonceTransactionsHandlerV2) SendTransaction(ctx context.Context, tx *
return "", err
}

sentHash, err := anh.SendTransaction(ctx, tx)
sentHash, err := anh.SendTransaction(ctx, &txCopy)
if err != nil {
return "", fmt.Errorf("%w while sending transaction for address %s", err, addrAsBech32)
}
Expand Down
15 changes: 8 additions & 7 deletions interactors/nonceHandlerV2/nonceTransactionsHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ func TestNonceTransactionsHandlerV2_SendTransactionsWithGetNonce(t *testing.T) {
}

func TestNonceTransactionsHandlerV2_SendDuplicateTransactions(t *testing.T) {
currentNonce := uint64(664)
initialNonce := uint64(664)
currentNonce := initialNonce

numCalls := 0

Expand All @@ -383,7 +384,7 @@ func TestNonceTransactionsHandlerV2_SendDuplicateTransactions(t *testing.T) {
},
SendTransactionCalled: func(tx *transaction.FrontendTransaction) (string, error) {
require.LessOrEqual(t, numCalls, 1)
currentNonce++
atomic.AddUint64(&currentNonce, 1)
return "", nil
},
}
Expand All @@ -409,15 +410,15 @@ func TestNonceTransactionsHandlerV2_SendDuplicateTransactions(t *testing.T) {
require.True(t, ok)

// after sending first tx, nonce shall increase
require.Equal(t, accWithPrivateAccess.computedNonce+1, currentNonce)
require.Equal(t, atomic.LoadUint64(&currentNonce), accWithPrivateAccess.computedNonce+1)

// trying to apply nonce for the same tx, NonceTransactionHandler shall return ErrTxAlreadySent
// and computedNonce shall not increase
tx.Nonce = 0
tx.Nonce = initialNonce
err = nth.ApplyNonceAndGasPrice(context.Background(), testAddress, tx)
require.Equal(t, err, interactors.ErrTxWithSameNonceAndGasPriceAlreadySent)
require.Equal(t, tx.Nonce, uint64(0))
require.Equal(t, accWithPrivateAccess.computedNonce+1, currentNonce)
require.Equal(t, interactors.ErrTxWithSameNonceAndGasPriceAlreadySent, err)
require.Equal(t, initialNonce, tx.Nonce)
require.Equal(t, currentNonce, accWithPrivateAccess.computedNonce+1)
}

func createMockTransactionsWithGetNonce(
Expand Down

0 comments on commit 725bb5a

Please sign in to comment.