stable-memory
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseStable Memory & Canister Upgrades
稳定内存与Canister升级
What This Is
内容概述
Stable memory is persistent storage on Internet Computer that survives canister upgrades. Heap memory (regular variables) is wiped on every upgrade. Any data you care about MUST be in stable memory, or it will be lost the next time the canister is deployed.
稳定内存是Internet Computer上的持久化存储,可在Canister升级后保留数据。堆内存(常规变量)会在每次升级时被清空。所有需要保留的数据都必须存储在稳定内存中,否则在下次部署Canister时会丢失。
Prerequisites
前置条件
- For Motoko: mops with in mops.toml
core = "2.0.0" - For Rust: in Cargo.toml
ic-stable-structures = "0.7"
- 对于Motoko:mops工具,且mops.toml中配置
core = "2.0.0" - 对于Rust:Cargo.toml中添加依赖
ic-stable-structures = "0.7"
Canister IDs
Canister ID
No external canister dependencies. Stable memory is a local canister feature.
无外部Canister依赖。稳定内存是Canister的本地特性。
Mistakes That Break Your Build
导致构建失败的常见错误
-
Usingfor user data (Rust) -- This is heap memory. It is wiped on every canister upgrade. All user data, balances, settings stored this way will vanish after
thread_local! { RefCell<T> }. Useicp deployinstead.StableBTreeMap -
Forgettinghandler (Rust) -- Without a
#[post_upgrade]function, the canister may silently reset state or behave unexpectedly after upgrade. Always define bothpost_upgradeand#[init].#[post_upgrade] -
Usingkeyword in persistent actors (Motoko) -- In mo:core
stable, allpersistent actorandletdeclarations are automatically stable. Writingvarproduces warning M0218 andstable letis redundant. Just usestable varandlet.var -
Confusing heap memory limits with stable memory limits (Rust) -- Heap (Wasm linear) memory is limited to 4GB for wasm32 and 6GB for wasm64. Stable memory can grow up to hundreds of GB (the subnet storage limit). The real danger: if you use/
pre_upgradehooks to serialize heap data to stable memory and deserialize it back, you are limited by the heap memory size AND by the instruction limit for upgrade hooks. Large datasets will trap during upgrade, bricking the canister. The solution is to use stable structures (post_upgrade,StableBTreeMap, etc.) that read/write directly to stable memory, bypassing the heap entirely. UseStableCellto partition stable memory into virtual memories so multiple structures can coexist without overwriting each other.MemoryManager -
Changing record field types between upgrades (Motoko) -- Altering the type of a persistent field (e.g.,to
Nat, or renaming a record field) will trap on upgrade and data is unrecoverable. Only ADD new optional fields. Never remove or rename existing ones.Int -
Serializing large data in pre_upgrade (Rust) --has a fixed instruction limit. If you serialize a large HashMap to stable memory in pre_upgrade, it will hit the limit and trap, bricking the canister. Use
pre_upgradewhich writes directly to stable memory and needs no serialization step.StableBTreeMap -
Usinginstead of
actor { }(Motoko) -- Plainpersistent actor { }in mo:core requires explicitactorannotations and pre/post_upgrade hooks.stablemakes everything stable by default. Always usepersistent actor.persistent actor
-
在Rust中使用存储用户数据——这属于堆内存,会在每次Canister升级时被清空。通过这种方式存储的所有用户数据、余额、设置都会在执行
thread_local! { RefCell<T> }后丢失。请改用icp deploy。StableBTreeMap -
在Rust中忘记定义处理器——如果没有
#[post_upgrade]函数,Canister在升级后可能会静默重置状态或出现异常行为。请始终同时定义post_upgrade和#[init]。#[post_upgrade] -
在Motoko的persistent actor中使用关键字——在mo:core的
stable中,所有persistent actor和let声明都会自动变为稳定存储。编写var会触发M0218警告,stable let则完全冗余。直接使用stable var和let即可。var -
混淆Rust中的堆内存限制与稳定内存限制——堆内存(Wasm线性内存)对于wasm32限制为4GB,wasm64限制为6GB。而稳定内存可扩展至数百GB(受子网存储限制)。真正的风险在于:如果使用/
pre_upgrade钩子将堆数据序列化到稳定内存再反序列化回来,会同时受限于堆内存大小和升级钩子的指令限制。大型数据集会在升级时触发陷阱,导致Canister无法正常工作。解决方案是使用直接读写稳定内存的稳定结构(如post_upgrade、StableBTreeMap等),完全绕过堆内存。使用StableCell将稳定内存划分为虚拟内存,让多个结构可以共存而不会互相覆盖。MemoryManager -
在Motoko升级时修改持久化字段的类型——修改持久化字段的类型(例如将改为
Nat,或重命名记录字段)会在升级时触发陷阱,导致数据无法恢复。只能添加新的可选字段,绝不能删除或重命名现有字段。Int -
在Rust的pre_upgrade中序列化大型数据——有固定的指令限制。如果在pre_upgrade中将大型HashMap序列化到稳定内存,会触发指令限制陷阱,导致Canister无法正常工作。请使用
pre_upgrade,它直接写入稳定内存,无需序列化步骤。StableBTreeMap -
在Motoko中使用而非
actor { }——mo:core中的普通persistent actor { }需要显式的actor注解和pre/post_upgrade钩子。stable默认让所有数据变为稳定存储。请始终使用persistent actor。persistent actor
Implementation
实现方案
Motoko
Motoko
With mo:core 2.0, makes stable storage trivial. All and declarations inside the actor body are automatically persisted across upgrades.
persistent actorletvarmotoko
import Map "mo:core/Map";
import List "mo:core/List";
import Nat "mo:core/Nat";
import Text "mo:core/Text";
import Time "mo:core/Time";
persistent actor {
// Types -- must be inside actor body
type User = {
id : Nat;
name : Text;
created : Int;
};
// These survive upgrades automatically -- no "stable" keyword needed
let users = Map.empty<Nat, User>();
var userCounter : Nat = 0;
let tags = List.empty<Text>();
// Transient data -- reset to initial value on every upgrade
transient var requestCount : Nat = 0;
public func addUser(name : Text) : async Nat {
let id = userCounter;
Map.add(users, Nat.compare, id, {
id;
name;
created = Time.now();
});
userCounter += 1;
requestCount += 1;
id
};
public query func getUser(id : Nat) : async ?User {
Map.get(users, Nat.compare, id)
};
public query func getUserCount() : async Nat {
Map.size(users)
};
// requestCount resets to 0 after every upgrade
public query func getRequestCount() : async Nat {
requestCount
};
}Key rules for Motoko persistent actors:
- for Map, List, Set, Queue -- auto-persisted, no serialization
let - for simple values (Nat, Text, Bool) -- auto-persisted
var - for caches, counters that should reset on upgrade
transient var - NO /
pre_upgradeneeded -- the runtime handles itpost_upgrade - NO keyword -- it is redundant and produces warnings
stable
在mo:core 2.0中,让稳定存储变得简单。Actor体内所有和声明的变量都会在升级后自动持久化。
persistent actorletvarmotoko
import Map "mo:core/Map";
import List "mo:core/List";
import Nat "mo:core/Nat";
import Text "mo:core/Text";
import Time "mo:core/Time";
persistent actor {
// Types -- must be inside actor body
type User = {
id : Nat;
name : Text;
created : Int;
};
// These survive upgrades automatically -- no "stable" keyword needed
let users = Map.empty<Nat, User>();
var userCounter : Nat = 0;
let tags = List.empty<Text>();
// Transient data -- reset to initial value on every upgrade
transient var requestCount : Nat = 0;
public func addUser(name : Text) : async Nat {
let id = userCounter;
Map.add(users, Nat.compare, id, {
id;
name;
created = Time.now();
});
userCounter += 1;
requestCount += 1;
id
};
public query func getUser(id : Nat) : async ?User {
Map.get(users, Nat.compare, id)
};
public query func getUserCount() : async Nat {
Map.size(users)
};
// requestCount resets to 0 after every upgrade
public query func getRequestCount() : async Nat {
requestCount
};
}Motoko persistent actor的核心规则:
- 声明的Map、List、Set、Queue——自动持久化,无需序列化
let - 声明的简单值(Nat、Text、Bool等)——自动持久化
var - 用于缓存、计数器等需要在升级时重置的内容
transient var - 无需/
pre_upgrade——运行时会自动处理post_upgrade - 不要使用关键字——冗余且会产生警告
stable
mops.toml
mops.toml
toml
[package]
name = "my-project"
version = "0.1.0"
[dependencies]
core = "2.0.0"toml
[package]
name = "my-project"
version = "0.1.0"
[dependencies]
core = "2.0.0"Rust
Rust
Rust canisters use for persistent storage. The partitions stable memory (up to hundreds of GB, limited by subnet storage) into virtual memories, each backing a different data structure.
ic-stable-structuresMemoryManagerRust Canister使用实现持久化存储。将稳定内存(最大可达数百GB,受子网存储限制)划分为虚拟内存,每个虚拟内存对应不同的数据结构。
ic-stable-structuresMemoryManagerCargo.toml
Cargo.toml
toml
[package]
name = "stable_memory_backend"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
ic-cdk = "0.19"
ic-stable-structures = "0.7"
candid = "0.10"
serde = { version = "1", features = ["derive"] }
ciborium = "0.2"toml
[package]
name = "stable_memory_backend"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
ic-cdk = "0.19"
ic-stable-structures = "0.7"
candid = "0.10"
serde = { version = "1", features = ["derive"] }
ciborium = "0.2"Single Stable Structure (Simple Case)
单个稳定结构(简单场景)
rust
use ic_stable_structures::{
memory_manager::{MemoryId, MemoryManager, VirtualMemory},
storable::{Bound, Storable},
DefaultMemoryImpl, StableBTreeMap,
};
use ic_cdk::{init, post_upgrade, query, update};
use candid::CandidType;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::cell::RefCell;
type Memory = VirtualMemory<DefaultMemoryImpl>;
// -- Implement Storable for custom types --
// StableBTreeMap keys need Storable + Ord, values need Storable.
// Storable defines how a type is serialized to/from bytes in stable memory.
// Use CBOR (via ciborium) for serialization -- compact binary format, faster than candid.
#[derive(CandidType, Serialize, Deserialize, Clone)]
struct User {
id: u64,
name: String,
created: u64,
}
impl Storable for User {
// Recommended: prefer Unbounded to avoid backwards compatibility issues when adding new fields.
// Bounded requires a fixed max_size -- adding a field that increases the size will break existing data.
const BOUND: Bound = Bound::Unbounded;
fn to_bytes(&self) -> Cow<'_, [u8]> {
let mut buf = vec![];
ciborium::into_writer(self, &mut buf).expect("Failed to encode User");
Cow::Owned(buf)
}
fn into_bytes(self) -> Vec<u8> {
let mut buf = vec![];
ciborium::into_writer(&self, &mut buf).expect("Failed to encode User");
buf
}
fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
ciborium::from_reader(bytes.as_ref()).expect("Failed to decode User")
}
}
// Bound::Bounded { max_size, is_fixed_size: true } exists for fixed-size types but is NOT
// recommended -- adding a new field later will exceed max_size and break deserialization.
// Stable storage -- survives upgrades
thread_local! {
static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> =
RefCell::new(MemoryManager::init(DefaultMemoryImpl::default()));
static USERS: RefCell<StableBTreeMap<u64, User, Memory>> =
RefCell::new(StableBTreeMap::init(
MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(0)))
));
// Counter stored in stable memory via StableCell
static COUNTER: RefCell<ic_stable_structures::StableCell<u64, Memory>> =
RefCell::new(ic_stable_structures::StableCell::init(
MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(1))),
0u64,
));
}
#[init]
fn init() {
// Any one-time initialization
}
#[post_upgrade]
fn post_upgrade() {
// Stable structures auto-restore -- no deserialization needed
// Re-init timers or other transient state here
}
#[update]
fn add_user(name: String) -> u64 {
let id = COUNTER.with(|c| {
let mut cell = c.borrow_mut();
let current = *cell.get();
cell.set(current + 1);
current
});
let user = User {
id,
name,
created: ic_cdk::api::time(),
};
USERS.with(|users| {
users.borrow_mut().insert(id, user);
});
id
}
#[query]
fn get_user(id: u64) -> Option<User> {
USERS.with(|users| users.borrow().get(&id))
}
#[query]
fn get_user_count() -> u64 {
USERS.with(|users| users.borrow().len())
}
ic_cdk::export_candid!();rust
use ic_stable_structures::{
memory_manager::{MemoryId, MemoryManager, VirtualMemory},
storable::{Bound, Storable},
DefaultMemoryImpl, StableBTreeMap,
};
use ic_cdk::{init, post_upgrade, query, update};
use candid::CandidType;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::cell::RefCell;
type Memory = VirtualMemory<DefaultMemoryImpl>;
// -- Implement Storable for custom types --
// StableBTreeMap keys need Storable + Ord, values need Storable.
// Storable defines how a type is serialized to/from bytes in stable memory.
// Use CBOR (via ciborium) for serialization -- compact binary format, faster than candid.
#[derive(CandidType, Serialize, Deserialize, Clone)]
struct User {
id: u64,
name: String,
created: u64,
}
impl Storable for User {
// Recommended: prefer Unbounded to avoid backwards compatibility issues when adding new fields.
// Bounded requires a fixed max_size -- adding a field that increases the size will break existing data.
const BOUND: Bound = Bound::Unbounded;
fn to_bytes(&self) -> Cow<'_, [u8]> {
let mut buf = vec![];
ciborium::into_writer(self, &mut buf).expect("Failed to encode User");
Cow::Owned(buf)
}
fn into_bytes(self) -> Vec<u8> {
let mut buf = vec![];
ciborium::into_writer(&self, &mut buf).expect("Failed to encode User");
buf
}
fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
ciborium::from_reader(bytes.as_ref()).expect("Failed to decode User");
}
}
// Bound::Bounded { max_size, is_fixed_size: true } exists for fixed-size types but is NOT
// recommended -- adding a new field later will exceed max_size and break deserialization.
// Stable storage -- survives upgrades
thread_local! {
static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> =
RefCell::new(MemoryManager::init(DefaultMemoryImpl::default()));
static USERS: RefCell<StableBTreeMap<u64, User, Memory>> =
RefCell::new(StableBTreeMap::init(
MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(0)))
));
// Counter stored in stable memory via StableCell
static COUNTER: RefCell<ic_stable_structures::StableCell<u64, Memory>> =
RefCell::new(ic_stable_structures::StableCell::init(
MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(1))),
0u64,
));
}
#[init]
fn init() {
// Any one-time initialization
}
#[post_upgrade]
fn post_upgrade() {
// Stable structures auto-restore -- no deserialization needed
// Re-init timers or other transient state here
}
#[update]
fn add_user(name: String) -> u64 {
let id = COUNTER.with(|c| {
let mut cell = c.borrow_mut();
let current = *cell.get();
cell.set(current + 1);
current
});
let user = User {
id,
name,
created: ic_cdk::api::time(),
};
USERS.with(|users| {
users.borrow_mut().insert(id, user);
});
id
}
#[query]
fn get_user(id: u64) -> Option<User> {
USERS.with(|users| users.borrow().get(&id))
}
#[query]
fn get_user_count() -> u64 {
USERS.with(|users| users.borrow().len())
}
ic_cdk::export_candid!();Multiple Stable Structures with MemoryManager
使用MemoryManager管理多个稳定结构
rust
use ic_stable_structures::{
memory_manager::{MemoryId, MemoryManager, VirtualMemory},
DefaultMemoryImpl, StableBTreeMap, StableCell, StableLog,
};
use std::cell::RefCell;
type Memory = VirtualMemory<DefaultMemoryImpl>;
// Each structure gets its own MemoryId -- NEVER reuse IDs
const USERS_MEM_ID: MemoryId = MemoryId::new(0);
const POSTS_MEM_ID: MemoryId = MemoryId::new(1);
const COUNTER_MEM_ID: MemoryId = MemoryId::new(2);
const LOG_INDEX_MEM_ID: MemoryId = MemoryId::new(3);
const LOG_DATA_MEM_ID: MemoryId = MemoryId::new(4);
thread_local! {
static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> =
RefCell::new(MemoryManager::init(DefaultMemoryImpl::default()));
static USERS: RefCell<StableBTreeMap<u64, Vec<u8>, Memory>> =
RefCell::new(StableBTreeMap::init(
MEMORY_MANAGER.with(|m| m.borrow().get(USERS_MEM_ID))
));
static POSTS: RefCell<StableBTreeMap<u64, Vec<u8>, Memory>> =
RefCell::new(StableBTreeMap::init(
MEMORY_MANAGER.with(|m| m.borrow().get(POSTS_MEM_ID))
));
static COUNTER: RefCell<StableCell<u64, Memory>> =
RefCell::new(StableCell::init(
MEMORY_MANAGER.with(|m| m.borrow().get(COUNTER_MEM_ID)),
0u64,
));
static AUDIT_LOG: RefCell<StableLog<Vec<u8>, Memory, Memory>> =
RefCell::new(StableLog::init(
MEMORY_MANAGER.with(|m| m.borrow().get(LOG_INDEX_MEM_ID)),
MEMORY_MANAGER.with(|m| m.borrow().get(LOG_DATA_MEM_ID)),
));
}Key rules for Rust stable structures:
- partitions stable memory -- each structure gets a unique
MemoryManagerMemoryId - NEVER reuse a for two different structures -- they will corrupt each other
MemoryId - keys must implement
StableBTreeMap+Storable, values must implementOrdStorable - Implement for custom types: define
Storable,BOUND,to_bytes, andinto_bytes. Usefrom_bytes/ciborium::into_writerfor CBOR serialization (compact, fast). Preferciborium::from_reader-- it avoids backwards compatibility breakage when adding new fields.Bound::Unboundedexists but is not recommended because exceedingBound::Boundedafter a schema change breaks deserializationmax_size - Primitive types (,
u64,bool, etc.),f64,String, andVec<u8>already implementPrincipal-- no manual impl neededStorable - for single values (counters, config)
StableCell - for append-only logs (needs two memory regions: index + data)
StableLog - is the correct pattern -- the RefCell wraps the stable structure, not a heap HashMap
thread_local! { RefCell<StableBTreeMap<...>> } - No /
pre_upgradeserialization needed -- data is already in stable memorypost_upgrade
rust
use ic_stable_structures::{
memory_manager::{MemoryId, MemoryManager, VirtualMemory},
DefaultMemoryImpl, StableBTreeMap, StableCell, StableLog,
};
use std::cell::RefCell;
type Memory = VirtualMemory<DefaultMemoryImpl>;
// Each structure gets its own MemoryId -- NEVER reuse IDs
const USERS_MEM_ID: MemoryId = MemoryId::new(0);
const POSTS_MEM_ID: MemoryId = MemoryId::new(1);
const COUNTER_MEM_ID: MemoryId = MemoryId::new(2);
const LOG_INDEX_MEM_ID: MemoryId = MemoryId::new(3);
const LOG_DATA_MEM_ID: MemoryId = MemoryId::new(4);
thread_local! {
static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> =
RefCell::new(MemoryManager::init(DefaultMemoryImpl::default()));
static USERS: RefCell<StableBTreeMap<u64, Vec<u8>, Memory>> =
RefCell::new(StableBTreeMap::init(
MEMORY_MANAGER.with(|m| m.borrow().get(USERS_MEM_ID))
));
static POSTS: RefCell<StableBTreeMap<u64, Vec<u8>, Memory>> =
RefCell::new(StableBTreeMap::init(
MEMORY_MANAGER.with(|m| m.borrow().get(POSTS_MEM_ID))
));
static COUNTER: RefCell<StableCell<u64, Memory>> =
RefCell::new(StableCell::init(
MEMORY_MANAGER.with(|m| m.borrow().get(COUNTER_MEM_ID)),
0u64,
));
static AUDIT_LOG: RefCell<StableLog<Vec<u8>, Memory, Memory>> =
RefCell::new(StableLog::init(
MEMORY_MANAGER.with(|m| m.borrow().get(LOG_INDEX_MEM_ID)),
MEMORY_MANAGER.with(|m| m.borrow().get(LOG_DATA_MEM_ID)),
));
}Rust稳定结构的核心规则:
- 用于划分稳定内存——每个结构对应唯一的
MemoryManagerMemoryId - 绝不能为两个不同的结构重用——会导致数据互相损坏
MemoryId - 的键必须实现
StableBTreeMap+Storable,值必须实现OrdStorable - 为自定义类型实现:定义
Storable、BOUND、to_bytes和into_bytes。使用from_bytes/ciborium::into_writer进行CBOR序列化(紧凑、快速)。优先使用ciborium::from_reader——可避免添加新字段时的向后兼容性问题。Bound::Unbounded虽然存在,但不推荐使用,因为模式修改后超过Bound::Bounded会导致反序列化失败max_size - 原始类型(、
u64、bool等)、f64、String和Vec<u8>已实现Principal——无需手动实现Storable - 用于存储单个值(计数器、配置)
StableCell - 用于追加式日志(需要两个内存区域:索引+数据)
StableLog - 正确的模式是——RefCell包裹的是稳定结构,而非堆HashMap
thread_local! { RefCell<StableBTreeMap<...>> } - 无需在/
pre_upgrade中进行序列化——数据已存储在稳定内存中post_upgrade
Deploy & Test
部署与测试
Motoko: Verify Persistence Across Upgrades
Motoko:验证跨升级的持久化效果
bash
undefinedbash
undefinedStart local replica
Start local replica
icp network start -d
icp network start -d
Deploy
Deploy
icp deploy backend
icp deploy backend
Add data
Add data
icp canister call backend addUser '("Alice")'
icp canister call backend addUser '("Alice")'
Expected: (0 : nat)
Expected: (0 : nat)
icp canister call backend addUser '("Bob")'
icp canister call backend addUser '("Bob")'
Expected: (1 : nat)
Expected: (1 : nat)
Verify data exists
Verify data exists
icp canister call backend getUserCount '()'
icp canister call backend getUserCount '()'
Expected: (2 : nat)
Expected: (2 : nat)
icp canister call backend getUser '(0)'
icp canister call backend getUser '(0)'
Expected: (opt record { id = 0 : nat; name = "Alice"; created = ... })
Expected: (opt record { id = 0 : nat; name = "Alice"; created = ... })
Now upgrade the canister (simulates code change + redeploy)
Now upgrade the canister (simulates code change + redeploy)
icp deploy backend
icp deploy backend
Verify data survived the upgrade
Verify data survived the upgrade
icp canister call backend getUserCount '()'
icp canister call backend getUserCount '()'
Expected: (2 : nat) -- STILL 2, not 0
Expected: (2 : nat) -- STILL 2, not 0
icp canister call backend getUser '(1)'
icp canister call backend getUser '(1)'
Expected: (opt record { id = 1 : nat; name = "Bob"; created = ... })
Expected: (opt record { id = 1 : nat; name = "Bob"; created = ... })
undefinedundefinedRust: Verify Persistence Across Upgrades
Rust:验证跨升级的持久化效果
bash
icp network start -d
icp deploy backend
icp canister call backend add_user '("Alice")'bash
icp network start -d
icp deploy backend
icp canister call backend add_user '("Alice")'Expected: (0 : nat64)
Expected: (0 : nat64)
icp canister call backend get_user_count '()'
icp canister call backend get_user_count '()'
Expected: (1 : nat64)
Expected: (1 : nat64)
Upgrade
Upgrade
icp deploy backend
icp deploy backend
Verify persistence
Verify persistence
icp canister call backend get_user_count '()'
icp canister call backend get_user_count '()'
Expected: (1 : nat64) -- data survived
Expected: (1 : nat64) -- data survived
icp canister call backend get_user '(0)'
icp canister call backend get_user '(0)'
Expected: (opt record { id = 0 : nat64; name = "Alice"; created = ... })
Expected: (opt record { id = 0 : nat64; name = "Alice"; created = ... })
undefinedundefinedVerify It Works
验证功能正常
The definitive test for stable memory: data survives upgrade.
bash
undefined稳定内存的终极测试:数据在升级后仍然保留。
bash
undefined1. Deploy and add data
1. Deploy and add data
icp deploy backend
icp canister call backend addUser '("TestUser")'
icp deploy backend
icp canister call backend addUser '("TestUser")'
2. Record the count
2. Record the count
icp canister call backend getUserCount '()'
icp canister call backend getUserCount '()'
Note the number
Note the number
3. Upgrade (redeploy)
3. Upgrade (redeploy)
icp deploy backend
icp deploy backend
4. Check count again -- must be identical
4. Check count again -- must be identical
icp canister call backend getUserCount '()'
icp canister call backend getUserCount '()'
Must match step 2
Must match step 2
5. Verify transient data DID reset
5. Verify transient data DID reset
icp canister call backend getRequestCount '()'
icp canister call backend getRequestCount '()'
Expected: (0 : nat) -- transient var resets on upgrade
Expected: (0 : nat) -- transient var resets on upgrade
If the count drops to 0 after step 3, your data is NOT in stable memory. Review your storage declarations.
如果步骤3后计数变为0,说明你的数据未存储在稳定内存中。请检查你的存储声明。