Skip to content

Commit

Permalink
chore: dont use replica time in initial request. retry up to retryTimes
Browse files Browse the repository at this point in the history
  • Loading branch information
dfx-json committed Sep 27, 2024
1 parent 256be8f commit 3b58609
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
2 changes: 0 additions & 2 deletions packages/agent/src/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,6 @@ function _createActorMethod(
? (agent as HttpAgent).replicaTime
: undefined;

certTime;

if (response.body && response.body.certificate) {
const cert = response.body.certificate;
certificate = await Certificate.create({
Expand Down
19 changes: 9 additions & 10 deletions packages/agent/src/agent/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ export class HttpAgent implements Agent {
arg: ArrayBuffer;
effectiveCanisterId?: Principal | string;
callSync?: boolean;
tries?: number;
systemTime?: number
},
identity?: Identity | Promise<Identity>,
): Promise<SubmitResponse> {
Expand All @@ -445,13 +447,7 @@ export class HttpAgent implements Agent {

const sender: Principal = id.getPrincipal() || Principal.anonymous();

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
const timeDiffMsecs = this.replicaTime && this.replicaTime.getTime() - Date.now();
if (Math.abs(timeDiffMsecs) > 1_000 * 30) {
ingress_expiry = new Expiry(DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS + timeDiffMsecs);
}
const ingress_expiry = new Expiry(DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS, options.systemTime);

const submit: CallRequest = {
request_type: SubmitRequestType.Call,
Expand Down Expand Up @@ -567,12 +563,15 @@ export class HttpAgent implements Agent {
// If the error is due to the ingress expiry being misconfigured,
// sync this instance's replica time using the value provided in
// the error response and retry
if ((error as ReplicaTimeError).message.includes('ingress_expiry')) {
if ((error as ReplicaTimeError).message.includes('ingress_expiry') && (options.tries ?? 1) < this.#retryTimes) {
this.log.warn('Agent time out of sync. Updating and retrying...');
this.replicaTime = (error as ReplicaTimeError).replicaTime;
return this.call(
canisterId,
options,
{
...options,
tries: (options.tries ?? 1) + 1,
systemTime: (error as ReplicaTimeError).replicaTime.getTime(),
},
identity,
);
}
Expand Down
6 changes: 2 additions & 4 deletions packages/agent/src/agent/http/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ import {

const NANOSECONDS_PER_MILLISECONDS = BigInt(1_000_000);

const REPLICA_PERMITTED_DRIFT_MILLISECONDS = 60 * 1000;

export class Expiry {
private readonly _value: bigint;

constructor(deltaInMSec: number) {
constructor(deltaInMSec: number, systemTime: number = Date.now()) {
// 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)) *
BigInt(Math.floor(systemTime + deltaInMSec)) *
NANOSECONDS_PER_MILLISECONDS;

// round down to the nearest second
Expand Down

0 comments on commit 3b58609

Please sign in to comment.