Skip to content

Commit 14ffa05

Browse files
the rest
1 parent 09deeaf commit 14ffa05

File tree

3 files changed

+97
-14
lines changed

3 files changed

+97
-14
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## Unreleased
1010

11+
* Added `wasm_memory_threshold` field to `CanisterSettings`.
12+
1113
## [0.39.2] - 2024-12-20
1214

1315
* Bumped `ic-certification` to `3.0.0`.

Diff for: ic-utils/src/interfaces/management_canister/builders.rs

+91-14
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ pub struct CanisterSettings {
7676
/// Must be a number between 0 and 2^48^ (i.e 256TB), inclusively.
7777
pub wasm_memory_limit: Option<Nat>,
7878

79+
/// A threshold on the remaining Wasm memory of the canister.
80+
///
81+
/// When the remaining memory drops below this threshold, its
82+
/// `on_low_wasm_memory` hook will be invoked. This enables it
83+
/// to self-optimize or raise an alert or otherwise attempt to
84+
/// prevent itself from reaching `wasm_memory_limit`.
85+
pub wasm_memory_threshold: Option<Nat>,
86+
7987
/// The canister log visibility of the canister.
8088
///
8189
/// If unspecified and a canister is being created with these settings, defaults to `Controllers`, i.e. private by default.
@@ -93,6 +101,7 @@ pub struct CreateCanisterBuilder<'agent, 'canister: 'agent> {
93101
freezing_threshold: Option<Result<FreezingThreshold, AgentError>>,
94102
reserved_cycles_limit: Option<Result<ReservedCyclesLimit, AgentError>>,
95103
wasm_memory_limit: Option<Result<WasmMemoryLimit, AgentError>>,
104+
wasm_memory_threshold: Option<Result<WasmMemoryLimit, AgentError>>,
96105
log_visibility: Option<Result<LogVisibility, AgentError>>,
97106
is_provisional_create: bool,
98107
amount: Option<u128>,
@@ -111,6 +120,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
111120
freezing_threshold: None,
112121
reserved_cycles_limit: None,
113122
wasm_memory_limit: None,
123+
wasm_memory_threshold: None,
114124
log_visibility: None,
115125
is_provisional_create: false,
116126
amount: None,
@@ -161,7 +171,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
161171
}
162172
}
163173

164-
/// Pass in an optional controller for the canister. If this is [None],
174+
/// Pass in an optional controller for the canister. If this is [`None`],
165175
/// it will revert the controller to default.
166176
pub fn with_optional_controller<C, E>(self, controller: Option<C>) -> Self
167177
where
@@ -199,7 +209,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
199209
self.with_optional_controller(Some(controller))
200210
}
201211

202-
/// Pass in a compute allocation optional value for the canister. If this is [None],
212+
/// Pass in a compute allocation optional value for the canister. If this is [`None`],
203213
/// it will revert the compute allocation to default.
204214
pub fn with_optional_compute_allocation<C, E>(self, compute_allocation: Option<C>) -> Self
205215
where
@@ -224,7 +234,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
224234
self.with_optional_compute_allocation(Some(compute_allocation))
225235
}
226236

227-
/// Pass in a memory allocation optional value for the canister. If this is [None],
237+
/// Pass in a memory allocation optional value for the canister. If this is [`None`],
228238
/// it will revert the memory allocation to default.
229239
pub fn with_optional_memory_allocation<E, C>(self, memory_allocation: Option<C>) -> Self
230240
where
@@ -249,7 +259,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
249259
self.with_optional_memory_allocation(Some(memory_allocation))
250260
}
251261

252-
/// Pass in a freezing threshold optional value for the canister. If this is [None],
262+
/// Pass in a freezing threshold optional value for the canister. If this is [`None`],
253263
/// it will revert the freezing threshold to default.
254264
pub fn with_optional_freezing_threshold<E, C>(self, freezing_threshold: Option<C>) -> Self
255265
where
@@ -283,7 +293,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
283293
self.with_optional_reserved_cycles_limit(Some(limit))
284294
}
285295

286-
/// Pass in a reserved cycles limit optional value for the canister. If this is [None],
296+
/// Pass in a reserved cycles limit optional value for the canister. If this is [`None`],
287297
/// it will create the canister with the default limit.
288298
pub fn with_optional_reserved_cycles_limit<E, C>(self, limit: Option<C>) -> Self
289299
where
@@ -309,7 +319,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
309319
self.with_optional_wasm_memory_limit(Some(wasm_memory_limit))
310320
}
311321

312-
/// Pass in a Wasm memory limit optional value for the canister. If this is [None],
322+
/// Pass in a Wasm memory limit optional value for the canister. If this is [`None`],
313323
/// it will revert the Wasm memory limit to default.
314324
pub fn with_optional_wasm_memory_limit<E, C>(self, wasm_memory_limit: Option<C>) -> Self
315325
where
@@ -326,6 +336,32 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
326336
}
327337
}
328338

339+
/// Pass in a Wasm memory threshold value for the canister.
340+
pub fn with_wasm_memory_threshold<C, E>(self, wasm_memory_threshold: C) -> Self
341+
where
342+
E: std::fmt::Display,
343+
C: TryInto<WasmMemoryLimit, Error = E>,
344+
{
345+
self.with_optional_wasm_memory_threshold(Some(wasm_memory_threshold))
346+
}
347+
348+
/// Pass in a Wasm memory threshold optional value for the canister. If this is [`None`],
349+
/// it will revert the Wasm memory threshold to default.
350+
pub fn with_optional_wasm_memory_threshold<E, C>(self, wasm_memory_threshold: Option<C>) -> Self
351+
where
352+
E: std::fmt::Display,
353+
C: TryInto<WasmMemoryLimit, Error = E>,
354+
{
355+
Self {
356+
wasm_memory_threshold: wasm_memory_threshold.map(|limit| {
357+
limit
358+
.try_into()
359+
.map_err(|e| AgentError::MessageError(format!("{e}")))
360+
}),
361+
..self
362+
}
363+
}
364+
329365
/// Pass in a log visibility setting for the canister.
330366
pub fn with_log_visibility<C, E>(self, log_visibility: C) -> Self
331367
where
@@ -335,7 +371,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
335371
self.with_optional_log_visibility(Some(log_visibility))
336372
}
337373

338-
/// Pass in a log visibility optional setting for the canister. If this is [None],
374+
/// Pass in a log visibility optional setting for the canister. If this is [`None`],
339375
/// it will revert the log visibility to default.
340376
pub fn with_optional_log_visibility<E, C>(self, log_visibility: Option<C>) -> Self
341377
where
@@ -385,6 +421,11 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
385421
Some(Ok(x)) => Some(Nat::from(u64::from(x))),
386422
None => None,
387423
};
424+
let wasm_memory_threshold = match self.wasm_memory_threshold {
425+
Some(Err(x)) => return Err(AgentError::MessageError(format!("{x}"))),
426+
Some(Ok(x)) => Some(Nat::from(u64::from(x))),
427+
None => None,
428+
};
388429
let log_visibility = match self.log_visibility {
389430
Some(Err(x)) => return Err(AgentError::MessageError(format!("{x}"))),
390431
Some(Ok(x)) => Some(x),
@@ -412,6 +453,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
412453
freezing_threshold,
413454
reserved_cycles_limit,
414455
wasm_memory_limit,
456+
wasm_memory_threshold,
415457
log_visibility,
416458
},
417459
specified_id: self.specified_id,
@@ -430,6 +472,7 @@ impl<'agent, 'canister: 'agent> CreateCanisterBuilder<'agent, 'canister> {
430472
freezing_threshold,
431473
reserved_cycles_limit,
432474
wasm_memory_limit,
475+
wasm_memory_threshold,
433476
log_visibility,
434477
})
435478
.with_effective_canister_id(self.effective_canister_id)
@@ -956,6 +999,7 @@ pub struct UpdateCanisterBuilder<'agent, 'canister: 'agent> {
956999
freezing_threshold: Option<Result<FreezingThreshold, AgentError>>,
9571000
reserved_cycles_limit: Option<Result<ReservedCyclesLimit, AgentError>>,
9581001
wasm_memory_limit: Option<Result<WasmMemoryLimit, AgentError>>,
1002+
wasm_memory_threshold: Option<Result<WasmMemoryLimit, AgentError>>,
9591003
log_visibility: Option<Result<LogVisibility, AgentError>>,
9601004
}
9611005

@@ -971,11 +1015,12 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
9711015
freezing_threshold: None,
9721016
reserved_cycles_limit: None,
9731017
wasm_memory_limit: None,
1018+
wasm_memory_threshold: None,
9741019
log_visibility: None,
9751020
}
9761021
}
9771022

978-
/// Pass in an optional controller for the canister. If this is [None],
1023+
/// Pass in an optional controller for the canister. If this is [`None`],
9791024
/// it will revert the controller to default.
9801025
pub fn with_optional_controller<C, E>(self, controller: Option<C>) -> Self
9811026
where
@@ -1014,7 +1059,7 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
10141059
self.with_optional_controller(Some(controller))
10151060
}
10161061

1017-
/// Pass in a compute allocation optional value for the canister. If this is [None],
1062+
/// Pass in a compute allocation optional value for the canister. If this is [`None`],
10181063
/// it will revert the compute allocation to default.
10191064
pub fn with_optional_compute_allocation<C, E>(self, compute_allocation: Option<C>) -> Self
10201065
where
@@ -1039,7 +1084,7 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
10391084
self.with_optional_compute_allocation(Some(compute_allocation))
10401085
}
10411086

1042-
/// Pass in a memory allocation optional value for the canister. If this is [None],
1087+
/// Pass in a memory allocation optional value for the canister. If this is [`None`],
10431088
/// it will revert the memory allocation to default.
10441089
pub fn with_optional_memory_allocation<E, C>(self, memory_allocation: Option<C>) -> Self
10451090
where
@@ -1064,7 +1109,7 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
10641109
self.with_optional_memory_allocation(Some(memory_allocation))
10651110
}
10661111

1067-
/// Pass in a freezing threshold optional value for the canister. If this is [None],
1112+
/// Pass in a freezing threshold optional value for the canister. If this is [`None`],
10681113
/// it will revert the freezing threshold to default.
10691114
pub fn with_optional_freezing_threshold<E, C>(self, freezing_threshold: Option<C>) -> Self
10701115
where
@@ -1099,7 +1144,7 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
10991144
}
11001145

11011146
/// Pass in a reserved cycles limit optional value for the canister.
1102-
/// If this is [None], leaves the reserved cycles limit unchanged.
1147+
/// If this is [`None`], leaves the reserved cycles limit unchanged.
11031148
pub fn with_optional_reserved_cycles_limit<E, C>(self, limit: Option<C>) -> Self
11041149
where
11051150
E: std::fmt::Display,
@@ -1123,7 +1168,7 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
11231168
self.with_optional_wasm_memory_limit(Some(wasm_memory_limit))
11241169
}
11251170

1126-
/// Pass in a Wasm memory limit optional value for the canister. If this is [None],
1171+
/// Pass in a Wasm memory limit optional value for the canister. If this is [`None`],
11271172
/// leaves the Wasm memory limit unchanged.
11281173
pub fn with_optional_wasm_memory_limit<E, C>(self, wasm_memory_limit: Option<C>) -> Self
11291174
where
@@ -1140,6 +1185,32 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
11401185
}
11411186
}
11421187

1188+
/// Pass in a Wasm memory limit threshold value for the canister.
1189+
pub fn with_wasm_memory_threshold<C, E>(self, wasm_memory_threshold: C) -> Self
1190+
where
1191+
E: std::fmt::Display,
1192+
C: TryInto<WasmMemoryLimit, Error = E>,
1193+
{
1194+
self.with_optional_wasm_memory_threshold(Some(wasm_memory_threshold))
1195+
}
1196+
1197+
/// Pass in a Wasm memory limit threshold value for the canister. If this is [`None`],
1198+
/// leaves the memory threshold unchanged.
1199+
pub fn with_optional_wasm_memory_threshold<E, C>(self, wasm_memory_threshold: Option<C>) -> Self
1200+
where
1201+
E: std::fmt::Display,
1202+
C: TryInto<WasmMemoryLimit, Error = E>,
1203+
{
1204+
Self {
1205+
wasm_memory_threshold: wasm_memory_threshold.map(|limit| {
1206+
limit
1207+
.try_into()
1208+
.map_err(|e| AgentError::MessageError(format!("{e}")))
1209+
}),
1210+
..self
1211+
}
1212+
}
1213+
11431214
/// Pass in a log visibility setting for the canister.
11441215
pub fn with_log_visibility<C, E>(self, log_visibility: C) -> Self
11451216
where
@@ -1149,7 +1220,7 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
11491220
self.with_optional_log_visibility(Some(log_visibility))
11501221
}
11511222

1152-
/// Pass in a log visibility optional setting for the canister. If this is [None],
1223+
/// Pass in a log visibility optional setting for the canister. If this is [`None`],
11531224
/// leaves the log visibility unchanged.
11541225
pub fn with_optional_log_visibility<E, C>(self, log_visibility: Option<C>) -> Self
11551226
where
@@ -1205,6 +1276,11 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
12051276
Some(Ok(x)) => Some(Nat::from(u64::from(x))),
12061277
None => None,
12071278
};
1279+
let wasm_memory_threshold = match self.wasm_memory_threshold {
1280+
Some(Err(x)) => return Err(AgentError::MessageError(format!("{x}"))),
1281+
Some(Ok(x)) => Some(Nat::from(u64::from(x))),
1282+
None => None,
1283+
};
12081284
let log_visibility = match self.log_visibility {
12091285
Some(Err(x)) => return Err(AgentError::MessageError(format!("{x}"))),
12101286
Some(Ok(x)) => Some(x),
@@ -1223,6 +1299,7 @@ impl<'agent, 'canister: 'agent> UpdateCanisterBuilder<'agent, 'canister> {
12231299
freezing_threshold,
12241300
reserved_cycles_limit,
12251301
wasm_memory_limit,
1302+
wasm_memory_threshold,
12261303
log_visibility,
12271304
},
12281305
})

Diff for: ic-utils/src/interfaces/wallet.rs

+4
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ impl<'agent> WalletCanister<'agent> {
673673
freezing_threshold: freezing_threshold.map(u64::from).map(Nat::from),
674674
reserved_cycles_limit: None,
675675
wasm_memory_limit: None,
676+
wasm_memory_threshold: None,
676677
log_visibility: None,
677678
};
678679

@@ -704,6 +705,7 @@ impl<'agent> WalletCanister<'agent> {
704705
freezing_threshold: freezing_threshold.map(u64::from).map(Nat::from),
705706
reserved_cycles_limit: None,
706707
wasm_memory_limit: None,
708+
wasm_memory_threshold: None,
707709
log_visibility: None,
708710
};
709711

@@ -833,6 +835,7 @@ impl<'agent> WalletCanister<'agent> {
833835
freezing_threshold: freezing_threshold.map(u64::from).map(Nat::from),
834836
reserved_cycles_limit: None,
835837
wasm_memory_limit: None,
838+
wasm_memory_threshold: None,
836839
log_visibility: None,
837840
};
838841

@@ -864,6 +867,7 @@ impl<'agent> WalletCanister<'agent> {
864867
freezing_threshold: freezing_threshold.map(u64::from).map(Nat::from),
865868
reserved_cycles_limit: None,
866869
wasm_memory_limit: None,
870+
wasm_memory_threshold: None,
867871
log_visibility: None,
868872
};
869873

0 commit comments

Comments
 (0)