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

fix: canisterStatus throws if root key is not fetched #600

Merged
merged 2 commits into from Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/generated/changelog.html
Expand Up @@ -15,6 +15,7 @@ <h2>Version 0.12.2</h2>
<li>
Support for the SubtleCrypto interface in @dfinity/identity using the new ECDSAKeyIdentity
</li>
<li>CanisterStatus no longer suppresses rootKey errors</li>
</ul>
<h2>Version 0.12.1</h2>
<ul>
Expand Down
40 changes: 40 additions & 0 deletions e2e/node/basic/canisterStatus.test.ts
@@ -0,0 +1,40 @@
import { CanisterStatus, HttpAgent } from '@dfinity/agent';
import { Principal } from '@dfinity/principal';
import counter from '../canisters/counter';

jest.setTimeout(30_000);
describe.only('canister status', () => {
it('should fetch successfully', async () => {
const counterObj = await (await counter)();
const agent = new HttpAgent({ host: `http://localhost:${process.env.REPLICA_PORT}` });
await agent.fetchRootKey();
const request = await CanisterStatus.request({
canisterId: Principal.from(counterObj.canisterId),
agent,
paths: ['controllers'],
});

expect(Array.isArray(request.get('controllers'))).toBe(true);
});
it('should throw an error if fetchRootKey has not been called', async () => {
const counterObj = await (await counter)();
const agent = new HttpAgent({ host: `http://localhost:${process.env.REPLICA_PORT}` });
const shouldThrow = async () => {
// eslint-disable-next-line no-useless-catch
try {
const request = await CanisterStatus.request({
canisterId: Principal.from(counterObj.canisterId),
agent,
paths: ['controllers'],
}).catch(error => {
throw error;
});
console.log(request);
} catch (error) {
throw error;
}
};

expect(shouldThrow).rejects.toThrow();
});
});
17 changes: 7 additions & 10 deletions e2e/node/basic/counter.test.ts
Expand Up @@ -34,15 +34,12 @@ describe('counter', () => {
expect(set1.size < values.length || set2.size < values2.length).toBe(true);
});
it('should increment', async () => {
const { actor: counter } = await counterCanister();
try {
expect(Number(await counter.read())).toEqual(0);
await counter.inc();
expect(Number(await counter.read())).toEqual(1);
await counter.inc();
expect(Number(await counter.read())).toEqual(2);
} catch (error) {
console.error(error);
}
const { actor: counter } = await noncelessCanister();

expect(Number(await counter.read())).toEqual(0);
await counter.inc();
expect(Number(await counter.read())).toEqual(1);
await counter.inc();
expect(Number(await counter.read())).toEqual(2);
});
});
6 changes: 3 additions & 3 deletions e2e/node/test-setup.ts
Expand Up @@ -7,12 +7,12 @@
// Note that we can use webpack configuration to make some features available to

// Node.js in a similar way.
// import { TextEncoder, TextDecoder } from 'text-encoding'; // eslint-disable-line
import { TextEncoder, TextDecoder } from 'text-encoding'; // eslint-disable-line
// import fetch from 'isomorphic-fetch';
// global.crypto = crypto as unknown as Crypto;
// console.log('subtle', crypto['subtle']); // eslint-disable-line
// global.TextDecoder = TextDecoder; // eslint-disable-line
// global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder; // eslint-disable-line
global.TextEncoder = TextEncoder;

// global.TextDecoder = TextDecoder; // eslint-disable-line
// (global.fetch as any) = fetch;
Expand Down
11 changes: 9 additions & 2 deletions packages/agent/src/canisterStatus/index.ts
Expand Up @@ -2,7 +2,8 @@

import { lebDecode, PipeArrayBuffer } from '@dfinity/candid';
import { Principal } from '@dfinity/principal';
import { HttpAgent, HttpAgentOptions, Cbor, Certificate, toHex } from '..';
import { AgentError } from '../errors';
import { HttpAgent, Cbor, Certificate, toHex } from '..';

/**
* Types of an entry on the canisterStatus map.
Expand Down Expand Up @@ -148,13 +149,19 @@ export const request = async (options: {
}
}
} catch (error) {
error;
// Break on signature verification errors
if ((error as AgentError)?.message?.includes('Invalid certificate')) {
throw new AgentError((error as AgentError).message);
}
if (typeof path !== 'string' && 'key' in path && 'path' in path) {
status.set(path.key, null);
} else {
status.set(path, null);
}
console.group();
console.warn(`Expected to find result for path ${path}, but instead found nothing.`);
console.warn(error);
console.groupEnd();
}
})();
});
Expand Down