Skip to content

Commit

Permalink
feat: allow for setting HttpAgent ingress expiry
Browse files Browse the repository at this point in the history
using `ingressExpiryInMinutes` option
  • Loading branch information
krpeacock committed Jul 18, 2024
1 parent fa40abe commit bb52652
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Added

- feat: allow for setting HttpAgent ingress expiry using `ingressExpiryInMinutes` option

## [2.0.0] - 2024-07-16

### Changed
Expand Down
46 changes: 46 additions & 0 deletions e2e/node/basic/mainnet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,49 @@ describe('call forwarding', () => {
reply; // ArrayBuffer
}, 15_000);
});

// TODO: change Expiry logic rounding to make <= 1 minute expiry work
test.skip('it should succeed when setting an expiry in the near future', async () => {
``;
const agent = await HttpAgent.create({
host: 'https://icp-api.io',
ingressExpiryInMinutes: 1,
});

await agent.syncTime();

expect(
agent.call('tnnnb-2yaaa-aaaab-qaiiq-cai', {
methodName: 'inc_read',
arg: fromHex('4449444c0000'),
effectiveCanisterId: 'tnnnb-2yaaa-aaaab-qaiiq-cai',
}),
).rejects.toThrowError(`Specified ingress_expiry not within expected range`);
});

test('it should succeed when setting an expiry in the future', async () => {
``;
const agent = await HttpAgent.create({
host: 'https://icp-api.io',
ingressExpiryInMinutes: 5,
});

await agent.syncTime();

expect(
agent.call('tnnnb-2yaaa-aaaab-qaiiq-cai', {
methodName: 'inc_read',
arg: fromHex('4449444c0000'),
effectiveCanisterId: 'tnnnb-2yaaa-aaaab-qaiiq-cai',
}),
).resolves.toBeDefined();
});

test('it should fail when setting an expiry in the far future', async () => {
expect(
HttpAgent.create({
host: 'https://icp-api.io',
ingressExpiryInMinutes: 100,
}),
).rejects.toThrowError(`The maximum ingress expiry time is 5 minutes`);
});
25 changes: 8 additions & 17 deletions packages/agent/src/agent/http/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -811,21 +811,12 @@ test('it should log errors to console if the option is set', async () => {
});


test.only('it should allow for configuring a max age in minutes for the ingress expiry', async () => {
const mockFetch = jest.fn();

const agent = await HttpAgent.create({
host: HTTP_AGENT_HOST,
fetch: mockFetch,
ingressExpiryInMinutes: 5,
});

await agent.syncTime();

await agent.call(Principal.managementCanister(), {
methodName: 'test',
arg: new Uint8Array().buffer,
});

const requestBody: any = cbor.decode(mockFetch.mock.calls[0][1].body);
test('it should fail when setting an expiry in the past', async () => {
expect(() =>
HttpAgent.createSync({
host: 'https://icp-api.io',
ingressExpiryInMinutes: -1,
fetch: jest.fn(),
}),
).toThrow(`Ingress expiry time must be greater than 0`);
});
5 changes: 5 additions & 0 deletions packages/agent/src/agent/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ export class HttpAgent implements Agent {
`The maximum ingress expiry time is 5 minutes. Provided ingress expiry time is ${options.ingressExpiryInMinutes} minutes.`,
);
}
if (options.ingressExpiryInMinutes && options.ingressExpiryInMinutes <= 0) {
throw new AgentError(
`Ingress expiry time must be greater than 0. Provided ingress expiry time is ${options.ingressExpiryInMinutes} minutes.`,
);
}

this.#maxIngressExpiryInMinutes = options.ingressExpiryInMinutes || 5;

Expand Down

0 comments on commit bb52652

Please sign in to comment.