<!-- canonical: https://0xsimao.com/findings/fuji-finance-recording-transactions-nonce-loop -->

# When recording failed transactions in ConnextHandler, getting the next Nonce involves an unnecessary for loop

Low/Info · Three Sigma · Lending aggregator · 6th May, 2023

Finding 3S-FUJI-N06 of the Fuji Finance security review.

- Report: /reports/fuji-finance
- Source: https://cdn.sanity.io/files/qoqld077/staging/32181a28eac3175d15fb8924d249bb0d91ca350c.pdf

---

### Description

In ConnextHandler.sol, in order to record a failed transaction it is necessary to know the
next nonce in the mapping, this involves a for cycle with a storage read that expends gas.
Since it only adds at the end of the list and access is always done using the nonce, the
variable _failedTxns could be changed from a mapping of a mapping to the mapping of an
array, this way in order to add a transaction push() could be used and to be able to know
the nonce the length of the array would suffice. This way the for cycle wouldn't be needed
and it would save on gas.

### Recommendation

```solidity
New declaration: mapping(bytes32 => FailedTxn[]) private _failedTxns;
Fetching the nonce: uint128 nextNonce = _failedTxns[transferId].length;
And adding to the list: _failedTxns[transferId].push(...);
```

### Status

Addressed here: [Fujicracy/fuji-v2#616](https://github.com/Fujicracy/fuji-v2/pull/616)
