Skip to content

Commit

Permalink
Merge branch 'main' into kai/SDK-1828-call-and-poll
Browse files Browse the repository at this point in the history
  • Loading branch information
krpeacock committed Oct 15, 2024
2 parents 5befbdb + 00ad493 commit 7b52f29
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
8 changes: 6 additions & 2 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

- feat: new `callAndPoll` function for an agent to call a canister and poll for the response

## [2.1.2] - 2024-09-30
- fix: revert https://github.com/dfinity/agent-js/pull/923 allow option to set agent replica time
## Changed

- fix: recalculates body to use a fresh `Expiry` when polling for `read_state` requests. This prevents the request from exceeding the `maximum_ingress_expiry` when the replica is slow to respond.
- fix: handle v3 traps correctly, pulling the reject_code and message from the certificate in the error response like v2.
Example trap error message:
```txt
Expand All @@ -21,6 +22,9 @@ AgentError: Call failed:
```
- feat: the `UpdateCallRejected` error now exposes `reject_code: ReplicaRejectCode`, `reject_message: string`, and `error_code?: string` properties directly on the error object.

## [2.1.2] - 2024-09-30
- fix: revert https://github.com/dfinity/agent-js/pull/923 allow option to set agent replica time

## [2.1.1] - 2024-09-13

### Added
Expand Down
17 changes: 10 additions & 7 deletions packages/agent/src/agent/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { Ed25519PublicKey } from '../../public_key';
import { decodeTime } from '../../utils/leb';
import { ObservableLog } from '../../observable';
import { BackoffStrategy, BackoffStrategyFactory, ExponentialBackoff } from '../../polling/backoff';
import { DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS } from '../../constants';
export * from './transforms';
export { Nonce, makeNonce } from './types';

Expand All @@ -54,9 +55,6 @@ export enum RequestStatusResponseStatus {
Done = 'done',
}

// Default delta for ingress expiry is 5 minutes.
const DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS = 5 * 60 * 1000;

// Root public key for the IC, encoded as hex
export const IC_ROOT_KEY =
'308182301d060d2b0601040182dc7c0503010201060c2b0601040182dc7c05030201036100814' +
Expand Down Expand Up @@ -785,7 +783,6 @@ export class HttpAgent implements Agent {

const requestId = await requestIdOf(request);

// TODO: remove this any. This can be a Signed or UnSigned request.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let transformedRequest: HttpAgentRequest = await this._transform({
request: {
Expand Down Expand Up @@ -952,9 +949,8 @@ 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
const transformedRequest: any = await this._transform({
const transformedRequest = await this._transform({
request: {
method: 'POST',
headers: {
Expand Down Expand Up @@ -985,7 +981,14 @@ export class HttpAgent implements Agent {
const canister = typeof canisterId === 'string' ? Principal.fromText(canisterId) : canisterId;

const transformedRequest = request ?? (await this.createReadStateRequest(fields, identity));
const body = cbor.encode(transformedRequest.body);

// With read_state, we should always use a fresh expiry, even beyond the point where the initial request would have expired
const bodyWithAdjustedExpiry = {
...transformedRequest.body,
ingress_expiry: new Expiry(DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS),
};

const body = cbor.encode(bodyWithAdjustedExpiry);

this.log.print(
`fetching "/api/v2/canister/${canister}/read_state" with request:`,
Expand Down
2 changes: 2 additions & 0 deletions packages/agent/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Default delta for ingress expiry is 5 minutes.
export const DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS = 5 * 60 * 1000;
7 changes: 6 additions & 1 deletion packages/agent/src/polling/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Principal } from '@dfinity/principal';
import { Agent, RequestStatusResponseStatus } from '../agent';
import { Agent, Expiry, RequestStatusResponseStatus } from '../agent';
import { Certificate, CreateCertificateOptions, lookupResultToBuffer } from '../certificate';
import { RequestId } from '../request_id';
import { toHex } from '../utils/buffer';

export * as strategy from './strategy';
import { defaultStrategy } from './strategy';
import { DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS } from '../constants';
export { defaultStrategy } from './strategy';
export type PollStrategy = (
canisterId: Principal,
Expand Down Expand Up @@ -38,6 +39,10 @@ export async function pollForResponse(
}> {
const path = [new TextEncoder().encode('request_status'), requestId];
const currentRequest = request ?? (await agent.createReadStateRequest?.({ paths: [path] }));

// Use a fresh expiry for the readState call.
currentRequest.body.content.ingress_expiry = new Expiry(DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS);

const state = await agent.readState(canisterId, { paths: [path] }, undefined, currentRequest);
if (agent.rootKey == null) throw new Error('Agent root key not initialized before polling');
const cert = await Certificate.create({
Expand Down

0 comments on commit 7b52f29

Please sign in to comment.