Skip to content

Commit

Permalink
chore: revert "feat: allow option set agent replica time" (#935)
Browse files Browse the repository at this point in the history
* Revert "feat: allow option set agent replica time (#923)"

This reverts commit a973a4d.

* chore: override rollup version to resolve vulnerability
  • Loading branch information
dfx-json authored Sep 30, 2024
1 parent 21cf470 commit fafdfa7
Show file tree
Hide file tree
Showing 18 changed files with 133 additions and 358 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);
}
}
});
2 changes: 1 addition & 1 deletion e2e/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
},
"overrides": {
"vite": {
"rollup": "npm:@rollup/wasm-node"
"rollup": "^4.22.5"
}
}
}
Loading

0 comments on commit fafdfa7

Please sign in to comment.