Skip to content

Commit

Permalink
Merge branch 'master' into db/chore/optimise-balance-queries
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbate authored Oct 9, 2024
2 parents 0ad52f7 + 74991ad commit 039c993
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 61 deletions.
5 changes: 5 additions & 0 deletions .changeset/filthy-bacteria-multiply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/account": patch
---

chore: merge `chain` and `nodeInfo` queries into one
9 changes: 9 additions & 0 deletions packages/account/src/providers/operations.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,15 @@ query getChain {
}
}

query getChainAndNodeInfo {
chain {
...chainInfoFragment
}
nodeInfo {
...nodeInfoFragment
}
}

query getTransaction($transactionId: TransactionId!) {
transaction(id: $transactionId) {
...transactionFragment
Expand Down
63 changes: 7 additions & 56 deletions packages/account/src/providers/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1067,36 +1067,31 @@ describe('Provider', () => {
const { provider } = launched;

expect(spyFetchChainAndNodeInfo).toHaveBeenCalledTimes(1);
expect(spyFetchChain).toHaveBeenCalledTimes(1);
expect(spyFetchNode).toHaveBeenCalledTimes(1);

provider.getChain();
provider.getNode();

expect(spyFetchChainAndNodeInfo).toHaveBeenCalledTimes(1);
expect(spyFetchChain).toHaveBeenCalledTimes(1);
expect(spyFetchNode).toHaveBeenCalledTimes(1);
expect(spyFetchChain).toHaveBeenCalledTimes(0);
expect(spyFetchNode).toHaveBeenCalledTimes(0);
});

it('should ensure fetchChainAndNodeInfo always fetch new data', async () => {
Provider.clearChainAndNodeCaches();

const spyFetchChainAndNodeInfo = vi.spyOn(Provider.prototype, 'fetchChainAndNodeInfo');
const spyFetchChain = vi.spyOn(Provider.prototype, 'fetchChain');
const spyFetchNode = vi.spyOn(Provider.prototype, 'fetchNode');

using launched = await setupTestProviderAndWallets();
const { provider } = launched;

expect(spyFetchChainAndNodeInfo).toHaveBeenCalledTimes(1);
expect(spyFetchChain).toHaveBeenCalledTimes(1);
expect(spyFetchNode).toHaveBeenCalledTimes(1);

const { provider } = launched;

const spyOperation = vi.spyOn(provider.operations, 'getChainAndNodeInfo');

await provider.fetchChainAndNodeInfo();

expect(spyFetchChainAndNodeInfo).toHaveBeenCalledTimes(2);
expect(spyFetchChain).toHaveBeenCalledTimes(2);
expect(spyFetchNode).toHaveBeenCalledTimes(2);
expect(spyOperation).toHaveBeenCalledTimes(1);
});

it('should ensure getGasConfig return essential gas related data', async () => {
Expand Down Expand Up @@ -1204,50 +1199,6 @@ Supported fuel-core version: ${mock.supportedVersion}.`
);
});

it('should ensure fuel node version warning is shown before chain incompatibility error', async () => {
const { FUEL_CORE } = versions;
const [major, minor, patch] = FUEL_CORE.split('.');
const majorMismatch = major === '0' ? 1 : parseInt(patch, 10) - 1;

const mock = {
isMajorSupported: false,
isMinorSupported: true,
isPatchSupported: true,
supportedVersion: `${majorMismatch}.${minor}.${patch}`,
};

if (mock.supportedVersion === FUEL_CORE) {
throw new Error();
}

const spy = vi.spyOn(fuelTsVersionsMod, 'checkFuelCoreVersionCompatibility');
spy.mockImplementationOnce(() => mock);

const consoleWarnSpy = vi.spyOn(console, 'warn');

const graphQLDummyError = `Unknown field "height" on type "Block".
Unknown field "version" on type "ScriptParameters".
Unknown field "version" on type "ConsensusParameters".`;

const fuelError = new FuelError(ErrorCode.INVALID_REQUEST, graphQLDummyError);

const fetchChainSpy = vi
.spyOn(Provider.prototype, 'fetchChain')
.mockImplementationOnce(async () => Promise.reject(fuelError));

await expectToThrowFuelError(() => setupTestProviderAndWallets(), fuelError);

expect(consoleWarnSpy).toHaveBeenCalledOnce();
expect(consoleWarnSpy).toHaveBeenCalledWith(
`The Fuel Node that you are trying to connect to is using fuel-core version ${FUEL_CORE},
which is not supported by the version of the TS SDK that you are using.
Things may not work as expected.
Supported fuel-core version: ${mock.supportedVersion}.`
);

expect(fetchChainSpy).toHaveBeenCalledOnce();
});

it('An invalid subscription request throws a FuelError and does not hold the test runner (closes all handles)', async () => {
using launched = await setupTestProviderAndWallets();
const { provider } = launched;
Expand Down
23 changes: 18 additions & 5 deletions packages/account/src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,13 +616,26 @@ export default class Provider {
* @returns A promise that resolves to the Chain and NodeInfo.
*/
async fetchChainAndNodeInfo() {
const nodeInfo = await this.fetchNode();
Provider.ensureClientVersionIsSupported(nodeInfo);
const chain = await this.fetchChain();
const { nodeInfo, chain } = await this.operations.getChainAndNodeInfo();

const processedNodeInfo: NodeInfo = {
maxDepth: bn(nodeInfo.maxDepth),
maxTx: bn(nodeInfo.maxTx),
nodeVersion: nodeInfo.nodeVersion,
utxoValidation: nodeInfo.utxoValidation,
vmBacktrace: nodeInfo.vmBacktrace,
};

Provider.ensureClientVersionIsSupported(processedNodeInfo);

const processedChain = processGqlChain(chain);

Provider.chainInfoCache[this.urlWithoutAuth] = processedChain;
Provider.nodeInfoCache[this.urlWithoutAuth] = processedNodeInfo;

return {
chain,
nodeInfo,
chain: processedChain,
nodeInfo: processedNodeInfo,
};
}

Expand Down

0 comments on commit 039c993

Please sign in to comment.