Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: consider message on resources cache #2872

Merged
merged 34 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9642661
add txID property to global resource cache
Torres-ssf Jul 31, 2024
eaf33c3
implement method delByTxID
Torres-ssf Jul 31, 2024
35ce52a
using TX ID when setting the cache
Torres-ssf Jul 31, 2024
421260c
unsettig cached resources when TX fails or squeezed out
Torres-ssf Jul 31, 2024
cd0aea8
update memory cache test
Torres-ssf Aug 1, 2024
6529f61
add unit test
Torres-ssf Aug 1, 2024
84c5cf8
Merge branch 'master' into st/fix/unset-cached-resource-for-failed-tx
Torres-ssf Aug 1, 2024
6bdbb3b
add test case for memory cache
Torres-ssf Aug 2, 2024
414e603
refact test suites
Torres-ssf Aug 2, 2024
ca102eb
add changeset
Torres-ssf Aug 2, 2024
3413b65
ajust test description
Torres-ssf Aug 2, 2024
ff0dc53
implement ResourceCache class
Torres-ssf Aug 5, 2024
7eeb61f
refact code to use ResourceCache
Torres-ssf Aug 5, 2024
93ccb88
remove old MemoryCache
Torres-ssf Aug 5, 2024
a1c9ac1
add new changeset
Torres-ssf Aug 5, 2024
2751d92
rename cacheUtxo flag to resourceCacheTTL
Torres-ssf Aug 5, 2024
62ab680
Merge branch 'master' into st/fix/unset-cached-resource-for-failed-tx
Torres-ssf Aug 5, 2024
8aca9c2
fix comment
Torres-ssf Aug 5, 2024
09526f0
add method clear to resource cache
Torres-ssf Aug 5, 2024
1a8ab1a
improve docs
Torres-ssf Aug 5, 2024
cc818e6
edit provider options docs
Torres-ssf Aug 5, 2024
7e89b4a
fix constant name
Torres-ssf Aug 5, 2024
b26de3a
Merge branch 'master' into st/fix/unset-cached-resource-for-failed-tx
Torres-ssf Aug 5, 2024
207c73e
Merge branch 'master' into st/fix/unset-cached-resource-for-failed-tx
Torres-ssf Aug 6, 2024
c3d20a8
Merge branch 'master' into st/fix/unset-cached-resource-for-failed-tx
Torres-ssf Aug 6, 2024
9c3eb66
Merge branch 'master' into st/fix/unset-cached-resource-for-failed-tx
Torres-ssf Aug 6, 2024
6ab9f2b
Update packages/account/src/providers/provider.test.ts
Torres-ssf Aug 7, 2024
99e8e3c
Update packages/account/src/providers/provider.test.ts
Torres-ssf Aug 7, 2024
e05c96c
Update packages/account/src/providers/provider.test.ts
Torres-ssf Aug 7, 2024
78c3794
Merge branch 'master' into st/fix/unset-cached-resource-for-failed-tx
Torres-ssf Aug 7, 2024
0ddad8e
Merge branch 'master' into st/fix/unset-cached-resource-for-failed-tx
Torres-ssf Aug 8, 2024
db75955
Update packages/account/src/providers/provider.test.ts
Torres-ssf Aug 8, 2024
318fa60
lintfix
Torres-ssf Aug 8, 2024
6e6ba87
Merge branch 'master' into st/fix/unset-cached-resource-for-failed-tx
Torres-ssf Aug 9, 2024
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
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
Torres-ssf marked this conversation as resolved.
Show resolved Hide resolved
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
Loading