Skip to content

Commit

Permalink
feat: Expiry rounds to nearest second if delta is under 90 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
krpeacock committed Jul 18, 2024
1 parent bb52652 commit cf742f6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
4 changes: 2 additions & 2 deletions e2e/node/basic/mainnet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe('call forwarding', () => {
});

// 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 () => {
test('it should succeed when setting an expiry in the near future', async () => {
``;
const agent = await HttpAgent.create({
host: 'https://icp-api.io',
Expand All @@ -157,7 +157,7 @@ test.skip('it should succeed when setting an expiry in the near future', async (
arg: fromHex('4449444c0000'),
effectiveCanisterId: 'tnnnb-2yaaa-aaaab-qaiiq-cai',
}),
).rejects.toThrowError(`Specified ingress_expiry not within expected range`);
).resolves.toBeDefined();
});

test('it should succeed when setting an expiry in the future', async () => {
Expand Down
4 changes: 2 additions & 2 deletions e2e/node/integration/actor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ test("Legacy Agent interface should be accepted by Actor's createActor", async (
);

// Verify that update calls work
await actor.write(8n); //?
await actor.write(8n);
// Verify that query calls work
const count = await actor.read(); //?
const count = await actor.read();
expect(count).toBe(8n);
}, 15_000);
// TODO: tests for rejected, unknown time out
4 changes: 2 additions & 2 deletions packages/agent/src/actor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ describe('makeActor', () => {
expect(reply).toEqual(canisterDecodedReturnValue);
expect(replyUpdate).toEqual(canisterDecodedReturnValue);
expect(replyWithHttpDetails.result).toEqual(canisterDecodedReturnValue);
replyWithHttpDetails.httpDetails['requestDetails']; //?
replyWithHttpDetails.httpDetails['requestDetails'];
expect(replyWithHttpDetails.httpDetails).toMatchInlineSnapshot(`
{
"headers": [],
Expand Down Expand Up @@ -330,7 +330,7 @@ describe('makeActor', () => {
`);
expect(replyUpdateWithHttpDetails.result).toEqual(canisterDecodedReturnValue);

replyUpdateWithHttpDetails.httpDetails['requestDetails']['nonce'] = new Uint8Array(); //?
replyUpdateWithHttpDetails.httpDetails['requestDetails']['nonce'] = new Uint8Array();

expect(replyUpdateWithHttpDetails.httpDetails).toMatchSnapshot();
});
Expand Down
13 changes: 13 additions & 0 deletions packages/agent/src/agent/http/transforms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ test('it should round down to the nearest minute', () => {
).toISOString();
expect(expiry_as_date_string).toBe('2021-04-26T17:51:00.000Z');
});

test('it should round down to the nearest second if less than 90 seconds', () => {
// 2021-04-26T17:47:11.314Z - high precision
jest.setSystemTime(new Date(1619459231314));

const expiry = new Expiry(89 * 1000);
expect(expiry['_value']).toEqual(BigInt(1619459320000000000n));

const expiry_as_date_string = new Date(
Number(expiry['_value'] / BigInt(1_000_000)),
).toISOString();
expect(expiry_as_date_string).toBe('2021-04-26T17:48:40.000Z');
});
11 changes: 10 additions & 1 deletion packages/agent/src/agent/http/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ export class Expiry {
private readonly _value: bigint;

constructor(deltaInMSec: number) {
// if ingress as seconds is less than 90, round to nearest second
if (deltaInMSec < 90 * 1_000) {
// Raw value without subtraction of REPLICA_PERMITTED_DRIFT_MILLISECONDS
const raw_value = BigInt(Date.now() + deltaInMSec) * NANOSECONDS_PER_MILLISECONDS;
const ingress_as_seconds = raw_value / BigInt(1_000_000_000);
this._value = ingress_as_seconds * BigInt(1_000_000_000);
return;
}

// Use bigint because it can overflow the maximum number allowed in a double float.
const raw_value =
BigInt(Math.floor(Date.now() + deltaInMSec - REPLICA_PERMITTED_DRIFT_MILLISECONDS)) *
NANOSECONDS_PER_MILLISECONDS;

// round down to the nearest second
// round down to the nearest second (since )
const ingress_as_seconds = raw_value / BigInt(1_000_000_000);

// round down to nearest minute
Expand Down

0 comments on commit cf742f6

Please sign in to comment.