Skip to content

Commit

Permalink
cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
krpeacock committed Sep 9, 2024
1 parent c45a873 commit 6e8f206
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`calculateReplicaTime 1`] = `2024-08-13T22:49:30.148Z`;
3 changes: 2 additions & 1 deletion packages/agent/src/agent/http/calculateReplicaTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import { calculateReplicaTime } from './calculateReplicaTime';
const exampleMessage = `Specified ingress_expiry not within expected range: Minimum allowed expiry: 2024-08-13 22:49:30.148075776 UTC, Maximum allowed expiry: 2024-08-13 22:55:00.148075776 UTC, Provided expiry: 2021-01-01 00:04:00 UTC`;

test('calculateReplicaTime', () => {
calculateReplicaTime(exampleMessage); //?
const parsedTime = calculateReplicaTime(exampleMessage);
expect(parsedTime).toMatchSnapshot();
});
16 changes: 3 additions & 13 deletions packages/agent/src/agent/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ export class HttpAgent implements Agent {
}

set replicaTime(replicaTime: Date) {
replicaTime;
this.#initialClientTime = new Date(Date.now());
this.#initialReplicaTime = replicaTime;
}
Expand Down Expand Up @@ -448,7 +447,6 @@ export class HttpAgent implements Agent {

let ingress_expiry = new Expiry(DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS);

this.replicaTime; //?
// If the value is off by more than 30 seconds, reconcile system time with the network
const timeDiffMsecs = this.replicaTime && this.replicaTime.getTime() - Date.now();
if (Math.abs(timeDiffMsecs) > 1_000 * 30) {
Expand Down Expand Up @@ -519,7 +517,6 @@ export class HttpAgent implements Agent {
});
};


const request = this.#requestAndRetry({
request: callSync ? requestSync : requestAsync,
backoff,
Expand Down Expand Up @@ -642,6 +639,7 @@ export class HttpAgent implements Agent {
);
}
} catch (error) {
this.log.error('Caught exception while attempting to read state', error as AgentError);
this.#handleReplicaTimeError(error as AgentError);
if (tries < this.#retryTimes) {
this.log.warn(
Expand Down Expand Up @@ -768,8 +766,6 @@ export class HttpAgent implements Agent {
});
}

#errorTimes = 0;

public async query(
canisterId: Principal | string,
fields: QueryFields,
Expand Down Expand Up @@ -860,8 +856,7 @@ export class HttpAgent implements Agent {
};
// Attempt to make the query i=retryTimes times
// Make query and fetch subnet keys in parallel
// const [queryResult, subnetStatus] = await Promise.all([makeQuery(), getSubnetStatus()]);
const [queryResult] = await Promise.all([makeQuery()]);
const [queryResult, subnetStatus] = await Promise.all([makeQuery(), getSubnetStatus()]);
const { requestDetails, query } = queryResult;

const queryWithDetails = {
Expand All @@ -876,7 +871,7 @@ export class HttpAgent implements Agent {
}

try {
return this.#verifyQueryResponse(queryWithDetails, await getSubnetStatus());
return this.#verifyQueryResponse(queryWithDetails, subnetStatus);
} catch {
// In case the node signatures have changed, refresh the subnet keys and try again
this.log.warn('Query response verification failed. Retrying with fresh subnet keys.');
Expand Down Expand Up @@ -980,9 +975,6 @@ export class HttpAgent implements Agent {
}
const sender = id?.getPrincipal() || Principal.anonymous();

// TODO: remove this any. This can be a Signed or UnSigned request.
// eslint-disable-next-line @typescript-eslint/no-explicit-any

let ingress_expiry = new Expiry(DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS);

// If the value is off by more than 30 seconds, reconcile system time with the network
Expand Down Expand Up @@ -1063,13 +1055,11 @@ export class HttpAgent implements Agent {
}
return decodedResponse;
} catch (error) {
this.#errorTimes++;
this.#handleReplicaTimeError(error as AgentError);
}
throw new AgentError('Failed to read state');
}


#handleReplicaTimeError = (error: AgentError): void => {
const message = error.message;
if (message?.includes('ingress_expiry')) {
Expand Down
5 changes: 1 addition & 4 deletions packages/agent/src/agent/http/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@ export class Expiry {
private readonly _value: bigint;

constructor(deltaInMSec: number) {
deltaInMSec;
// 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;

new Date(Number(raw_value / NANOSECONDS_PER_MILLISECONDS)); //?

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

// round down to nearest minute
const ingress_as_minutes = ingress_as_seconds / BigInt(60);

const rounded_down_nanos = ingress_as_minutes * BigInt(60) * BigInt(1_000_000_000); //?
const rounded_down_nanos = ingress_as_minutes * BigInt(60) * BigInt(1_000_000_000);

this._value = rounded_down_nanos;
}
Expand Down

0 comments on commit 6e8f206

Please sign in to comment.