Skip to content

Commit

Permalink
feat!: consider message on resources cache (#2872)
Browse files Browse the repository at this point in the history
  • Loading branch information
Torres-ssf committed Aug 9, 2024
1 parent 1d2abd7 commit c7e01b4
Show file tree
Hide file tree
Showing 13 changed files with 475 additions and 340 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-lobsters-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/account": minor
---

feat!: consider message on resources cache
4 changes: 2 additions & 2 deletions apps/docs-snippets/src/guide/provider/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ describe('Provider', () => {
// #endregion options-fetch
});

it('options: cacheUtxo', async () => {
it('options: resourceCacheTTL', async () => {
// #region options-cache-utxo
const provider = await Provider.create(FUEL_NETWORK_URL, {
cacheUtxo: 5000, // cache UTXO for 5 seconds
resourceCacheTTL: 5000, // cache resources (Coin's and Message's) for 5 seconds
});
// #endregion options-cache-utxo

Expand Down
18 changes: 11 additions & 7 deletions apps/docs/src/guide/provider/provider-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,30 @@ _Note: If defined, `requestMiddleware`, `timeout` and `retryOptions` are applied

<<< @/../../docs-snippets/src/guide/provider/provider.test.ts#options-fetch{ts:line-numbers}

### `cacheUtxo`
### `resourceCacheTTL`

When using the SDK, it may be necessary to submit multiple transactions from the same account in a short period. In such cases, the SDK creates and funds these transactions, then submits them to the node.

However, if a second transaction is created before the first one is processed, there is a chance of using the same UTXO(s) for both transactions. This happens because the UTXO(s) used in the first transaction are still unspent until the transaction is fully processed.
However, if a second transaction is created before the first one is processed, there is a chance of using the same resources (UTXOs or Messages) for both transactions. This happens because the resources used in the first transaction are still unspent until the transaction is fully processed.

If the second transaction attempts to use the same UTXO(s) that the first transaction has already spent, it will result in the following error:
If the second transaction attempts to use the same resources that the first transaction has already spent, it will result in one of the following error:

```console
Transaction is not inserted. UTXO does not exist: 0xf5...
Transaction is not inserted. Hash is already known

Transaction is not inserted. UTXO does not exist: {{utxoID}}

Transaction is not inserted. A higher priced tx {{txID}} is already spending this message: {{messageNonce}}
```

This error indicates that the UTXO(s) used by the second transaction no longer exist, as the first transaction already spent them.
This error indicates that the resources used by the second transaction no longer exist, as the first transaction already spent them.

To prevent this issue, the SDK sets a default cache for UTXO(s) to 20 seconds. This default caching mechanism ensures that UTXO(s) used in a submitted transaction are not reused in subsequent transactions within the specified time. You can control the duration of this cache using the `cacheUtxo` flag. If you would like to disable caching, you can pass a value of `-1` to the `cacheUtxo` parameter.
To prevent this issue, the SDK sets a default cache for resources to 20 seconds. This default caching mechanism ensures that resources used in a submitted transaction are not reused in subsequent transactions within the specified time. You can control the duration of this cache using the `resourceCacheTTL` flag. If you would like to disable caching, you can pass a value of `-1` to the `resourceCacheTTL` parameter.

<<< @/../../docs-snippets/src/guide/provider/provider.test.ts#options-cache-utxo{ts:line-numbers}

**Note:**

If you would like to submit multiple transactions without waiting for each transaction to be completed, your account must have multiple UTXOs available. If you only have one UTXO, the first transaction will spend it, and any remaining amount will be converted into a new UTXO with a different ID.

By ensuring your account has multiple UTXOs, you can effectively use the `cacheUtxo` flag to manage transactions without conflicts. For more information on UTXOs, refer to the [UTXOs guide](../the-utxo-model/index.md).
By ensuring your account has multiple UTXOs, you can effectively use the `resourceCacheTTL` flag to manage transactions without conflicts. For more information on UTXOs, refer to the [UTXOs guide](../the-utxo-model/index.md).
174 changes: 0 additions & 174 deletions packages/account/src/providers/memory-cache.test.ts

This file was deleted.

78 changes: 0 additions & 78 deletions packages/account/src/providers/memory-cache.ts

This file was deleted.

Loading

0 comments on commit c7e01b4

Please sign in to comment.