Skip to content

Commit

Permalink
Revert "feat: allow option set agent replica time (#923)"
Browse files Browse the repository at this point in the history
This reverts commit a973a4d.
  • Loading branch information
Jason authored Sep 27, 2024
1 parent 21cf470 commit 2181f4a
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 260 deletions.
80 changes: 2 additions & 78 deletions e2e/node/basic/mainnet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
fromHex,
polling,
requestIdOf,
ReplicaTimeError,
} from '@dfinity/agent';
import { IDL } from '@dfinity/candid';
import { Ed25519KeyIdentity } from '@dfinity/identity';
Expand All @@ -22,7 +21,7 @@ const createWhoamiActor = async (identity: Identity) => {
const idlFactory = () => {
return IDL.Service({
whoami: IDL.Func([], [IDL.Principal], ['query']),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) as unknown as any;
};
vi.useFakeTimers();
Expand Down Expand Up @@ -143,6 +142,7 @@ describe('call forwarding', () => {
}, 15_000);
});


test('it should allow you to set an incorrect root key', async () => {
const agent = HttpAgent.createSync({
rootKey: new Uint8Array(31),
Expand All @@ -159,79 +159,3 @@ test('it should allow you to set an incorrect root key', async () => {

expect(actor.whoami).rejects.toThrowError(`Invalid certificate:`);
});

test('it should throw an error when the clock is out of sync during a query', async () => {
const canisterId = 'ivcos-eqaaa-aaaab-qablq-cai';
const idlFactory = () => {
return IDL.Service({
whoami: IDL.Func([], [IDL.Principal], ['query']),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) as unknown as any;
};
vi.useRealTimers();

// set date to long ago
vi.spyOn(Date, 'now').mockImplementation(() => {
return new Date('2021-01-01T00:00:00Z').getTime();
});
// vi.setSystemTime(new Date('2021-01-01T00:00:00Z'));

const agent = await HttpAgent.create({ host: 'https://icp-api.io', fetch: globalThis.fetch });

const actor = Actor.createActor(idlFactory, {
agent,
canisterId,
});
try {
// should throw an error
await actor.whoami();
} catch (err) {
// handle the replica time error
if (err.name === 'ReplicaTimeError') {
const error = err as ReplicaTimeError;
// use the replica time to sync the agent
error.agent.replicaTime = error.replicaTime;
}
}
// retry the call
const result = await actor.whoami();
expect(Principal.from(result)).toBeInstanceOf(Principal);
});

test('it should throw an error when the clock is out of sync during an update', async () => {
const canisterId = 'ivcos-eqaaa-aaaab-qablq-cai';
const idlFactory = () => {
return IDL.Service({
whoami: IDL.Func([], [IDL.Principal], []),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) as unknown as any;
};
vi.useRealTimers();

// set date to long ago
vi.spyOn(Date, 'now').mockImplementation(() => {
return new Date('2021-01-01T00:00:00Z').getTime();
});
// vi.setSystemTime(new Date('2021-01-01T00:00:00Z'));

const agent = await HttpAgent.create({ host: 'https://icp-api.io', fetch: globalThis.fetch });

const actor = Actor.createActor(idlFactory, {
agent,
canisterId,
});
try {
// should throw an error
await actor.whoami();
} catch (err) {
// handle the replica time error
if (err.name === 'ReplicaTimeError') {
const error = err as ReplicaTimeError;
// use the replica time to sync the agent
error.agent.replicaTime = error.replicaTime;
// retry the call
const result = await actor.whoami();
expect(Principal.from(result)).toBeInstanceOf(Principal);
}
}
});
9 changes: 1 addition & 8 deletions packages/agent/src/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Buffer } from 'buffer/';
import {
Agent,
getDefaultAgent,
HttpAgent,
HttpDetailsResponse,
QueryResponseRejected,
QueryResponseStatus,
Expand Down Expand Up @@ -536,19 +535,13 @@ function _createActorMethod(
});
let reply: ArrayBuffer | undefined;
let certificate: Certificate | undefined;
const certTime = (agent as HttpAgent).replicaTime
? (agent as HttpAgent).replicaTime
: undefined;

certTime;

if (response.body && response.body.certificate) {
const cert = response.body.certificate;
certificate = await Certificate.create({
certificate: bufFromBufLike(cert),
rootKey: agent.rootKey,
canisterId: Principal.from(canisterId),
certTime,
blsVerify,
});
const path = [new TextEncoder().encode('request_status'), requestId];
const status = new TextDecoder().decode(
Expand Down
1 change: 0 additions & 1 deletion packages/agent/src/agent/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ export interface CallOptions {

export interface ReadStateResponse {
certificate: ArrayBuffer;
replicaTime?: Date;
}

export interface SubmitResponse {
Expand Down

This file was deleted.

7 changes: 0 additions & 7 deletions packages/agent/src/agent/http/calculateReplicaTime.test.ts

This file was deleted.

22 changes: 0 additions & 22 deletions packages/agent/src/agent/http/calculateReplicaTime.ts

This file was deleted.

19 changes: 1 addition & 18 deletions packages/agent/src/agent/http/errors.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import { HttpAgent } from '.';
import { AgentError } from '../../errors';
import { HttpDetailsResponse } from '../api';

export class AgentHTTPResponseError extends AgentError {
constructor(
message: string,
public readonly response: HttpDetailsResponse,
) {
constructor(message: string, public readonly response: HttpDetailsResponse) {
super(message);
this.name = this.constructor.name;
Object.setPrototypeOf(this, new.target.prototype);
}
}

export class ReplicaTimeError extends AgentError {
public readonly replicaTime: Date;
public readonly agent: HttpAgent;

constructor(message: string, replicaTime: Date, agent: HttpAgent) {
super(message);
this.name = 'ReplicaTimeError';
this.replicaTime = replicaTime;
this.agent = agent;
Object.setPrototypeOf(this, new.target.prototype);
}
}
4 changes: 1 addition & 3 deletions packages/agent/src/agent/http/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import { JSDOM } from 'jsdom';
import { Actor, AnonymousIdentity, SignIdentity, toHex } from '../..';
import { Ed25519KeyIdentity } from '@dfinity/identity';
import { AgentError } from '../../errors';
import { AgentHTTPResponseError, ReplicaTimeError } from './errors';
import { IDL } from '@dfinity/candid';
import { AgentHTTPResponseError } from './errors';
const { window } = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
window.fetch = global.fetch;
(global as any).window = window;
Expand Down Expand Up @@ -814,4 +813,3 @@ test('it should log errors to console if the option is set', async () => {
await agent.syncTime();
});

jest.setTimeout(5000);
Loading

0 comments on commit 2181f4a

Please sign in to comment.