Skip to content

Commit

Permalink
rewrite using safeExec
Browse files Browse the repository at this point in the history
  • Loading branch information
Cameron Manavian committed May 8, 2023
1 parent a95bd82 commit ef8353d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 87 deletions.
1 change: 1 addition & 0 deletions packages/providers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"lodash.clonedeep": "^4.5.0"
},
"devDependencies": {
"@fuel-ts/utils": "workspace:*",
"@graphql-codegen/cli": "^2.13.7",
"@graphql-codegen/typescript": "^2.8.0",
"@graphql-codegen/typescript-graphql-request": "^4.5.7",
Expand Down
167 changes: 81 additions & 86 deletions packages/providers/src/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { randomBytes } from '@fuel-ts/keystore';
import { BN, bn } from '@fuel-ts/math';
import type { Receipt } from '@fuel-ts/transactions';
import { InputType, ReceiptType, TransactionType } from '@fuel-ts/transactions';
import { safeExec } from '@fuel-ts/utils/test';
import * as GraphQL from 'graphql-request';

import Provider from './provider';
Expand Down Expand Up @@ -296,11 +297,10 @@ describe('Provider', () => {
const provider = new Provider('http://127.0.0.1:4000/graphql');
const transactionRequest = new ScriptTransactionRequest({});

try {
await provider.sendTransaction(transactionRequest);
} catch (e) {
expect(provider.cache).toEqual(undefined);
}
const { error } = await safeExec(() => provider.sendTransaction(transactionRequest));

expect(error).toBeTruthy();
expect(provider.cache).toEqual(undefined);
});

it('can cacheUtxo [will not cache inputs cache enabled + no coins]', async () => {
Expand All @@ -318,12 +318,11 @@ describe('Provider', () => {
inputs: [MessageInput],
});

try {
await provider.sendTransaction(transactionRequest);
} catch (e) {
expect(provider.cache).toBeTruthy();
expect(provider.cache?.getExcluded()).toStrictEqual([]);
}
const { error } = await safeExec(() => provider.sendTransaction(transactionRequest));

expect(error).toBeTruthy();
expect(provider.cache).toBeTruthy();
expect(provider.cache?.getExcluded()).toStrictEqual([]);
});

it('can cacheUtxo [will cache inputs cache enabled + coins]', async () => {
Expand Down Expand Up @@ -373,16 +372,15 @@ describe('Provider', () => {
inputs: [MessageInput, CoinInputA, CoinInputB, CoinInputC],
});

try {
await provider.sendTransaction(transactionRequest);
} catch (e) {
const EXCLUDED = provider.cache?.getExcluded() || [];
expect(EXCLUDED.length).toEqual(3);
expect(EXCLUDED.map((value) => hexlify(value))).toStrictEqual(EXPECTED);
const { error } = await safeExec(() => provider.sendTransaction(transactionRequest));

// clear cache
EXCLUDED.forEach((value) => provider.cache?.del(value));
}
expect(error).toBeTruthy();
const EXCLUDED = provider.cache?.getExcluded() || [];
expect(EXCLUDED.length).toEqual(3);
expect(EXCLUDED.map((value) => hexlify(value))).toStrictEqual(EXPECTED);

// clear cache
EXCLUDED.forEach((value) => provider.cache?.del(value));
});

it('can cacheUtxo [will cache inputs and also use in exclude list]', async () => {
Expand Down Expand Up @@ -432,31 +430,30 @@ describe('Provider', () => {
inputs: [MessageInput, CoinInputA, CoinInputB, CoinInputC],
});

try {
await provider.sendTransaction(transactionRequest);
} catch (e) {
const EXCLUDED = provider.cache?.getExcluded() || [];
expect(EXCLUDED.length).toEqual(3);
expect(EXCLUDED.map((value) => hexlify(value))).toStrictEqual(EXPECTED);

const owner = Address.fromRandom();
const resourcesToSpendMock = jest.fn(() => Promise.resolve({ resourcesToSpend: [] }));
// @ts-expect-error mock
provider.operations.getResourcesToSpend = resourcesToSpendMock;
await provider.getResourcesToSpend(owner, []);

expect(resourcesToSpendMock).toHaveBeenCalledWith({
owner: owner.toB256(),
queryPerAsset: [],
excludedIds: {
messages: [],
utxos: EXPECTED,
},
});

// clear cache
EXCLUDED.forEach((value) => provider.cache?.del(value));
}
const { error } = await safeExec(() => provider.sendTransaction(transactionRequest));

expect(error).toBeTruthy();
const EXCLUDED = provider.cache?.getExcluded() || [];
expect(EXCLUDED.length).toEqual(3);
expect(EXCLUDED.map((value) => hexlify(value))).toStrictEqual(EXPECTED);

const owner = Address.fromRandom();
const resourcesToSpendMock = jest.fn(() => Promise.resolve({ resourcesToSpend: [] }));
// @ts-expect-error mock
provider.operations.getResourcesToSpend = resourcesToSpendMock;
await provider.getResourcesToSpend(owner, []);

expect(resourcesToSpendMock).toHaveBeenCalledWith({
owner: owner.toB256(),
queryPerAsset: [],
excludedIds: {
messages: [],
utxos: EXPECTED,
},
});

// clear cache
EXCLUDED.forEach((value) => provider.cache?.del(value));
});

it('can cacheUtxo [will cache inputs cache enabled + coins]', async () => {
Expand Down Expand Up @@ -506,16 +503,15 @@ describe('Provider', () => {
inputs: [MessageInput, CoinInputA, CoinInputB, CoinInputC],
});

try {
await provider.sendTransaction(transactionRequest);
} catch (e) {
const EXCLUDED = provider.cache?.getExcluded() || [];
expect(EXCLUDED.length).toEqual(3);
expect(EXCLUDED.map((value) => hexlify(value))).toStrictEqual(EXPECTED);
const { error } = await safeExec(() => provider.sendTransaction(transactionRequest));

// clear cache
EXCLUDED.forEach((value) => provider.cache?.del(value));
}
expect(error).toBeTruthy();
const EXCLUDED = provider.cache?.getExcluded() || [];
expect(EXCLUDED.length).toEqual(3);
expect(EXCLUDED.map((value) => hexlify(value))).toStrictEqual(EXPECTED);

// clear cache
EXCLUDED.forEach((value) => provider.cache?.del(value));
});

it('can cacheUtxo [will cache inputs and also merge/de-dupe in exclude list]', async () => {
Expand Down Expand Up @@ -565,43 +561,42 @@ describe('Provider', () => {
inputs: [MessageInput, CoinInputA, CoinInputB, CoinInputC],
});

try {
await provider.sendTransaction(transactionRequest);
} catch (e) {
const EXCLUDED = provider.cache?.getExcluded() || [];
expect(EXCLUDED.length).toEqual(3);
expect(EXCLUDED.map((value) => hexlify(value))).toStrictEqual(EXPECTED);

const owner = Address.fromRandom();
const resourcesToSpendMock = jest.fn(() => Promise.resolve({ resourcesToSpend: [] }));
// @ts-expect-error mock
provider.operations.getResourcesToSpend = resourcesToSpendMock;
await provider.getResourcesToSpend(owner, [], {
const { error } = await safeExec(() => provider.sendTransaction(transactionRequest));

expect(error).toBeTruthy();
const EXCLUDED = provider.cache?.getExcluded() || [];
expect(EXCLUDED.length).toEqual(3);
expect(EXCLUDED.map((value) => hexlify(value))).toStrictEqual(EXPECTED);

const owner = Address.fromRandom();
const resourcesToSpendMock = jest.fn(() => Promise.resolve({ resourcesToSpend: [] }));
// @ts-expect-error mock
provider.operations.getResourcesToSpend = resourcesToSpendMock;
await provider.getResourcesToSpend(owner, [], {
utxos: [
'0xbc90ada45d89ec6648f8304eaf8fa2b03384d3c0efabc192b849658f4689b9c503',
'0xbc90ada45d89ec6648f8304eaf8fa2b03384d3c0efabc192b849658f4689b9c507',
'0xbc90ada45d89ec6648f8304eaf8fa2b03384d3c0efabc192b849658f4689b9c508',
],
});

expect(resourcesToSpendMock).toHaveBeenCalledWith({
owner: owner.toB256(),
queryPerAsset: [],
excludedIds: {
messages: [],
utxos: [
'0xbc90ada45d89ec6648f8304eaf8fa2b03384d3c0efabc192b849658f4689b9c503',
'0xbc90ada45d89ec6648f8304eaf8fa2b03384d3c0efabc192b849658f4689b9c507',
'0xbc90ada45d89ec6648f8304eaf8fa2b03384d3c0efabc192b849658f4689b9c508',
EXPECTED[1],
EXPECTED[2],
],
});

expect(resourcesToSpendMock).toHaveBeenCalledWith({
owner: owner.toB256(),
queryPerAsset: [],
excludedIds: {
messages: [],
utxos: [
'0xbc90ada45d89ec6648f8304eaf8fa2b03384d3c0efabc192b849658f4689b9c503',
'0xbc90ada45d89ec6648f8304eaf8fa2b03384d3c0efabc192b849658f4689b9c507',
'0xbc90ada45d89ec6648f8304eaf8fa2b03384d3c0efabc192b849658f4689b9c508',
EXPECTED[1],
EXPECTED[2],
],
},
});

// clear cache
EXCLUDED.forEach((value) => provider.cache?.del(value));
}
},
});

// clear cache
EXCLUDED.forEach((value) => provider.cache?.del(value));
});

it('can getBlocks', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/providers/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,10 @@ export default class Provider {
transactionRequestLike: TransactionRequestLike
): Promise<TransactionResponse> {
const transactionRequest = transactionRequestify(transactionRequestLike);
this.#cacheInputs(transactionRequest.inputs);
await this.estimateTxDependencies(transactionRequest);
// #endregion Provider-sendTransaction

this.#cacheInputs(transactionRequest.inputs);
const encodedTransaction = hexlify(transactionRequest.toTransactionBytes());
const { gasUsed, minGasPrice } = await this.getTransactionCost(transactionRequest, 0);

Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ef8353d

Please sign in to comment.