Skip to content

Commit abd8312

Browse files
committed
feat: flexible canister http outcalls
1 parent 9eb2c44 commit abd8312

4 files changed

Lines changed: 168 additions & 8 deletions

File tree

docs/references/ic-interface-spec/abstract-behavior.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5486,6 +5486,11 @@ ic0.subnet_self_copy<es>(dst : I, offset : I, size : I) =
54865486
if es.context = s then Trap {cycles_used = es.cycles_used;}
54875487
copy_to_canister<es>(dst, offset, size, es.params.sysenv.subnet_id)
54885488

5489+
I ∈ {i32, i64}
5490+
ic0.subnet_self_node_count<es>() : I =
5491+
if es.context = s then Trap {cycles_used = es.cycles_used;}
5492+
return es.params.sysenv.subnet_size
5493+
54895494
ic0.canister_cycle_balance<es>() : i64 =
54905495
if es.context = s then Trap {cycles_used = es.cycles_used;}
54915496
if es.balance >= 2^64 then Trap {cycles_used = es.cycles_used;}
@@ -5747,6 +5752,10 @@ I ∈ {i32, i64}
57475752
ic0.cost_http_request<es>(request_size: i64, max_res_bytes: i64, dst: I) : () =
57485753
copy_cycles_to_canister<es>(dst, arbitrary())
57495754

5755+
I ∈ {i32, i64}
5756+
ic0.cost_http_request_v2<es>(params_src : I, params_size : I, dst : I) : ()=
5757+
copy_cycles_to_canister<es>(dst, arbitrary())
5758+
57505759
I ∈ {i32, i64}
57515760
ic0.cost_sign_with_ecdsa<es>(src: I, size: I, ecdsa_curve: i32, dst: I) : i32 =
57525761
known_keys = arbitrary()

docs/references/ic-interface-spec/canister-interface.md

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ The 32-bit stable memory System API (`ic0.stable_size`, `ic0.stable_grow`, `ic0.
215215

216216
:::
217217

218+
:::note
219+
220+
The `ic0.cost_http_request` System API call is DEPRECATED. Canister developers are advised to use the `ic0.cost_http_request_v2` call instead.
221+
222+
:::
223+
218224
The following sections describe various System API functions, also referred to as system calls, which we summarize here.
219225

220226
All the following functions belong to the `ic0` module (denoted by the prefix `ic0.`).
@@ -259,6 +265,7 @@ defaulting to `I = i32` if the canister declares no memory.
259265
260266
ic0.subnet_self_size : () -> I; // *
261267
ic0.subnet_self_copy : (dst : I, offset : I, size : I) -> (); // *
268+
ic0.subnet_self_node_count : () -> i32; // *
262269
263270
ic0.msg_method_name_size : () -> I; // F
264271
ic0.msg_method_name_copy : (dst : I, offset : I, size : I) -> (); // F
@@ -301,6 +308,7 @@ defaulting to `I = i32` if the canister declares no memory.
301308
ic0.cost_call : (method_name_size: i64, payload_size : i64, dst : I) -> (); // * s
302309
ic0.cost_create_canister : (dst : I) -> (); // * s
303310
ic0.cost_http_request : (request_size : i64, max_res_bytes : i64, dst : I) -> (); // * s
311+
ic0.cost_http_request_v2 : (params_src : I, params_size : I, dst : I) -> (); // * s
304312
ic0.cost_sign_with_ecdsa : (src : I, size : I, ecdsa_curve: i32, dst : I) -> i32; // * s
305313
ic0.cost_sign_with_schnorr : (src : I, size : I, algorithm: i32, dst : I) -> i32; // * s
306314
ic0.cost_vetkd_derive_key : (src : I, size : I, vetkd_curve: i32, dst : I) -> i32; // * s
@@ -503,9 +511,9 @@ A canister can learn about its own identity:
503511

504512
A canister can learn about the subnet it is running on:
505513

506-
- `ic0.subnet_self_size : () → I` and `ic0.subnet_self_copy: (dst : I, offset : I, size : I) → ()`; `I ∈ {i32, i64}`
514+
- `ic0.subnet_self_size : () → I`, `ic0.subnet_self_copy: (dst : I, offset : I, size : I) → ()`; `I ∈ {i32, i64}`, and `ic0.subnet_self_node_count : () -> i32`
507515

508-
These functions allow the canister to query the subnet id (as a blob) of the subnet on which the canister is running.
516+
These functions allow the canister to query the subnet id (as a blob) of the subnet on which the canister is running, and to retrieve the number of nodes that are currently on the subnet.
509517

510518
### Canister status {#system-api-canister-status}
511519

@@ -910,14 +918,59 @@ These system calls return costs in Cycles, represented by 128 bits, which will b
910918

911919
- `ic0.cost_http_request(request_size : i64, max_res_bytes : i64, dst : I) -> ()`; `I ∈ {i32, i64}`
912920

913-
The cost of a canister http outcall via [`http_request`](./management-canister.md#ic-http_request). `request_size` is the sum of the byte lengths of the following components of an http request:
921+
:::note
922+
923+
The `ic0.cost_http_request` System API call is DEPRECATED. Canister developers are advised to use the `ic0.cost_http_request_v2` call instead.
924+
925+
:::
926+
927+
The cost of a canister HTTP outcall via [`http_request`](./management-canister.md#ic-http_request) with the pricing version set to `1` (currently the default). `request_size` is the sum of the byte lengths of the following components of an http request:
914928
- url
915929
- headers - i.e., the sum of the lengths of all keys and values
916930
- body
917931
- transform - i.e., the sum of the transform method name length and the length of the transform context
918932

919933
`max_res_bytes` is the maximum response length the caller wishes to accept (the caller should provide the default value of `2,000,000` if no maximum response length is provided in the actual request to the management canister).
920934

935+
- `ic0.cost_http_request_v2(params_src: I, params_size: I, dst : I) -> (); I ∈ {i32, i64}`
936+
937+
The cost of a canister HTTP outcall via [`http_request`](./management-canister.md#ic-http_request) with the pricing version set to `2`. The blob described by `params_src` and `params_size` must be a valid Candid encoding of a value of the following type:
938+
```
939+
record {
940+
request_bytes : nat64;
941+
http_roundtrip_time_ms : nat64;
942+
raw_response_bytes : nat64;
943+
transformed_response_bytes : nat64;
944+
transform_instructions: nat64;
945+
outcall_type : opt variant {
946+
fully_replicated: reserved;
947+
non_replicated: reserved;
948+
flexible: opt record {
949+
min_responses: nat32;
950+
max_responses: nat32;
951+
total_requests: nat32;
952+
}
953+
}
954+
}
955+
```
956+
957+
The function traps if `params_src` and `params_size` do not describe a valid Candid encoding of a value of the above type, or if the encoding contains additional fields other than the ones above. The function returns the cycle cost of an HTTP outcall whose execution uses up exactly the amount of resources specified by the individual fields:
958+
- `request_bytes` is the sum of the byte lengths of the following components of an HTTP request:
959+
- `url`
960+
- `headers` - i.e., the sum of the lengths of all keys and values
961+
- `body`
962+
- `transform` - i.e., the sum of the transform method name length and the length of the transform context.
963+
964+
- `http_roundtrip_time_ms` is the amount of time between the time when the HTTP request starts being sent to the remote server and the time that the HTTP response is fully received (in milliseconds).
965+
966+
- `raw_response_bytes` is the length of the HTTP response.
967+
968+
- `transformed_response_bytes` is the length of the HTTP response after transformation.
969+
970+
- `transform_instructions` is the number of instructions the transform function takes.
971+
972+
- `outcall_type` is the type of HTTP outcall issued: a fully replicated call (made through the `http_request` endpoint with `is_replicated` set to `null` or `opt false`), non-replicated (made through `http_request` with `is_replicated` set to `opt true`), or flexible (made through the `flexible_http_request` endpoint). When the `flexible` outcall variant is selected, it can optionally be supplemented with the `min_responses`, `max_responses`, and `total_requests` parameters provided to the endpoint.
973+
921974
- `ic0.cost_sign_with_ecdsa(src : I, size : I, ecdsa_curve: i32, dst : I) -> i32`; `I ∈ {i32, i64}`
922975
923976
- `ic0.cost_sign_with_schnorr(src : I, size : I, algorithm: i32, dst : I) -> i32`; `I ∈ {i32, i64}`

docs/references/ic-interface-spec/management-canister.md

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ The following parameters should be supplied for the call:
646646

647647
- `url` - the requested URL. The URL must be valid according to [RFC-3986](https://www.ietf.org/rfc/rfc3986.txt), it might contain non-ASCII characters according to [RFC-3987](https://www.ietf.org/rfc/rfc3987.txt), and its length must not exceed `8192`. The URL may specify a custom port number.
648648

649-
- `max_response_bytes` - optional, specifies the maximal size of the response in bytes. If provided, the value must not exceed `2MB` (`2,000,000B`). The call will be charged based on this parameter. If not provided, the maximum of `2MB` will be used.
649+
- `max_response_bytes` - optional, specifies the maximal size of the response in bytes. If provided, the value must not exceed `2MB` (`2,000,000B`). If not provided, the maximum of `2MB` will be used. When the `pricing_version` is set to `1`, the call will be charged based on this parameter. When the `pricing_version` is set to `2`, this field is ignored.
650650

651651
- `method` - currently, `GET`, `HEAD`, and `POST` are supported. Additionally, `PUT` and `DELETE` are supported in non-replicated mode only.
652652

@@ -658,13 +658,17 @@ The following parameters should be supplied for the call:
658658

659659
- `is_replicated` - optional, selecting between replicated and non-replicated modes.
660660

661-
:::note
661+
:::note
662662

663-
The `is_replicated` field is considered EXPERIMENTAL.
663+
The `is_replicated` field is considered EXPERIMENTAL.
664664

665-
:::
665+
:::
666666

667-
Cycles to pay for the call must be explicitly transferred with the call, i.e., they are not automatically deducted from the caller's balance implicitly (e.g., as for inter-canister calls).
667+
- `pricing_version` - the version of the pricing mechanism for HTTP outcalls that should be applied to this call; it can be either `1` or `2`. For compatibility reasons, the default is `1`; however, version `1` is deprecated.
668+
669+
Cycles to pay for the call must be explicitly transferred with the call, i.e., they are not automatically deducted from the caller's balance implicitly (e.g., as for inter-canister calls). Extraneous cycles are refunded:
670+
- with pricing version `1`, the difference between the attached cycles and the cost returned by the `ic0.cost_http_request` API with the appropriate parameters
671+
- with pricing version `2`, any attached cycles exceeding those used by the outcall execution.
668672

669673
The returned response (and the response provided to the `transform` function, if specified) contains the following fields:
670674

@@ -703,6 +707,54 @@ If you do not specify the `max_response_bytes` parameter, the maximum of a `2MB`
703707

704708
:::
705709

710+
### IC method `flexible_http_request` {#ic-flexible_http_request}
711+
712+
This is a variant of the [`http_request`](#ic-http_request) method where nodes return their individual HTTP responses to the caller instead of trying to reach consensus on the response, letting the caller do its own HTTP response processing. Use cases include calling HTTP endpoints that provide rapidly changing information (where achieving consensus is unlikely) and letting the user pick a trade-off between cheaper calls (fewer replicas requesting/responding) and stronger integrity guarantees (more replicas requesting/responding).
713+
714+
The arguments of the call are as for `http_request`, except that:
715+
716+
- there is an additional optional argument `replication`. When set, the caller can specify how many nodes should issue an HTTP outcall, the minimum number of HTTP responses from nodes in order for the outcall to succeed (`min_responses`), and the maximum number of HTTP responses the caller is willing to receive as the result of the outcall (`max_responses`). That is, a successful HTTP outcall is guaranteed to return between `min_responses` and `max_responses`. If `replication` is set, then the caller must ensure that `0 <= min_responses <= max_responses <= total_requests` and `1 <= total_requests <= N`, where `N` is the number of the nodes on the caller's subnet, otherwise the call will fail. The caller may use the `ic0.subnet_self_node_count` System API call to determine `N`. If `replication` is not provided, the defaults of `floor(2 / 3 * N) + 1`, `N` and `N` are used for `min_responses`, `max_responses` and `total_requests`.
717+
718+
- the deprecated `max_response_bytes` argument is not supported.
719+
720+
The other arguments, `url`, `method`, `headers`, `body`, and `transform` are the same as for `http_request`. The result is a vector of responses, with each individual response having the same structure as a `http_request` response, providing `status`, `headers`, and `body` fields.
721+
722+
As for `http_request`, the endpoint specified by the provided `url` should be idempotent. The one exception is when `total_requests` is set to 1 in `replication`. The request restrictions are also the same as for the `http_request` method:
723+
724+
- The total number of bytes in the request must not exceed `2MB` (`2,000,000`) bytes.
725+
726+
- Only the `GET`, `HEAD`, and `POST` methods are supported.
727+
728+
- The number of headers must not exceed `64`.
729+
730+
- The number of bytes representing a header name or value must not exceed `8KiB`.
731+
732+
- The total number of bytes representing the header names and values must not exceed `48KiB`.
733+
734+
The response from the remote server must not exceed `2MB`. Moreover, the total size of the result, that is, the sum of the responses returned by the different replicas (possibly after the transform function), must also not exceed 2MB.
735+
736+
Cycles to pay for the call must be explicitly transferred with the call, i.e., they are not automatically deducted from the caller's balance implicitly (e.g., as for inter-canister calls). The unused cycles are then refunded to the caller.
737+
738+
The method may return an error of the `flexible_http_request_err` type. The error includes a textual error message, an optional global error code, and a vector of resource reports from individual nodes.
739+
740+
The `global_error` field describes why the aggregate call failed to meet the requirements:
741+
742+
- `timeout`, meaning that less than `min_responses` from the nodes have been collected before some system-defined timeout.
743+
744+
- `out_of_cycles` indicating that the attached cycles were not enough to cover the processing of at least `min_responses`.
745+
746+
- `responses_too_large`: indicating that no combination of at least `min_responses` available responses could fit into the 2MB total limit.
747+
748+
- `too_many_rejects`: indicating that more than `total_requests - min_responses` nodes returned reject responses, so at least `min_responses` successful responses can never be collected.
749+
750+
The `node_details` vector provides visibility into the execution on specific nodes. Each entry contains:
751+
752+
- `node_id`.
753+
754+
- `report`: A detailed accounting of resources (bytes, instructions, time, and cycles) used by the node. Note: If a node fails due to a resource limit or running out of cycles, the corresponding field in this report will be set to `exceeded` rather than `used`.
755+
756+
- `error`: An optional record containing a `code` and `message`. This is populated only when the node encounters a functional failure.
757+
706758
### IC method `node_metrics_history` {#ic-node_metrics_history}
707759

708760
This method can only be called by canisters, i.e., it cannot be called by external users via ingress messages.

public/references/ic.did

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,34 @@ type http_request_result = record {
112112
body : blob;
113113
};
114114

115+
type http_request_resource_report = record {
116+
raw_response_bytes: opt variant { used: nat64; exceeded: reserved };
117+
http_roundtrip_time_ms: opt variant { used: nat64; exceeded: reserved };
118+
transform_instructions: opt variant { used: nat64; exceeded: reserved };
119+
transformed_response_bytes: opt variant { used: nat64; exceeded: reserved };
120+
cycles: opt variant { used: nat; exceeded: reserved };
121+
};
122+
123+
type flexible_http_request_err = record {
124+
global_error: opt variant {
125+
timeout : reserved;
126+
out_of_cycles : reserved;
127+
responses_too_large : reserved;
128+
too_many_rejects : reserved;
129+
};
130+
node_details : vec record {
131+
node_id: principal;
132+
report: http_request_resource_report;
133+
error: opt record { code: text; message: text };
134+
};
135+
message: text;
136+
};
137+
138+
type flexible_http_request_result = variant {
139+
ok: vec http_request_result;
140+
err: flexible_http_request_err;
141+
};
142+
115143
type ecdsa_curve = variant {
116144
secp256k1;
117145
};
@@ -352,6 +380,23 @@ type http_request_args = record {
352380
context : blob;
353381
};
354382
is_replicated : opt bool;
383+
pricing_version : opt nat32;
384+
};
385+
386+
type flexible_http_request_args = record {
387+
url : text;
388+
method : variant { get; head; post };
389+
headers : vec http_header;
390+
body : opt blob;
391+
transform : opt record {
392+
function : func(record { response : http_request_result; context : blob }) -> (http_request_result) query;
393+
context : blob;
394+
};
395+
replication: opt record {
396+
min_responses: nat32;
397+
max_responses: nat32;
398+
total_requests: nat32;
399+
};
355400
};
356401

357402
type ecdsa_public_key_args = record {
@@ -646,6 +691,7 @@ service ic : {
646691
deposit_cycles : (deposit_cycles_args) -> ();
647692
raw_rand : () -> (raw_rand_result);
648693
http_request : (http_request_args) -> (http_request_result);
694+
flexible_http_request : (flexible_http_request_args) -> (flexible_http_request_result);
649695

650696
// Public canister data
651697
canister_info : (canister_info_args) -> (canister_info_result);

0 commit comments

Comments
 (0)