Certified Queries on ICP: How to Make Fast Reads You Can Actually Trust
Thomas ·
In the previous guides, I covered persistent state across upgrades, multi-canister communication, and Internet Identity. There is another ICP-specific boundary that becomes important once an application starts presenting important information to users: a normal query is fast, but its result is not backed by subnet consensus. That distinction is easy to miss because this looks completely ordinary: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff public query func getProposal (id : Nat ) : async ? Proposal { Map . get (proposals, Nat . compare , id); }; The result really did come from your canister interface, but a normal query is executed by one replica rather than replicated across the subnet. For something harmless like a profile bio, that may be an acceptable tradeoff. For a governance proposal, account balance, authorization record, exchange rate, or anything a user will act on, blindly trusting that result can be the wrong security model. ICP has a useful middle ground: certified data . You can retain query-like latency while giving the client cryptographic evidence that the returned information corresponds to state committed by the subnet. This guide explains where certification actually belongs, builds a certified Motoko value, and then goes beyond the basic example into the problems that matter in a real application: collections, stale certificates, upgrades, derived query results, HTTP certification, and deciding when certification is unnecessary complexity. Query and update do not mean "read" and "write" It is tempting to mentally translate ICP calls into: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff query = SELECT update = INSERT / UPDATE / DELETE That is incomplete. The more useful distinction is: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff QUERY = client > one replica > result fast read-only not executed through subnet consensus versus: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff UPDATE client > subnet consensus > replicated execution > certified result slower can modify state consensus-backed Current ICP documentation puts typical query latency around 100–200 ms, while updates generally take around one to two seconds depending on the subnet and network conditions. Queries are one of the reasons ICP applications can feel responsive despite executing backend logic on a replicated network. The security tradeoff is that one replica answers the query. Replica-signed is not the same as subnet-certified The current JavaScript and Rust agents enable replica-signed queries by default. That protects against certain attacks, particularly a boundary node modifying the response after receiving it from the replica. It does not turn the query into consensus. There is an important difference between: "This replica says the answer is X." and: "The subnet previously agreed to state whose cryptographic commitment proves that the answer is X." Certified data gives you the second property. For security-sensitive reads, that is the distinction that matters. The problem certification solves Imagine a governance application displaying: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff Proposal 721 Transfer 10 ICP to Alice The actual proposal stored by the governance canister says: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff Transfer 100,000 ICP to Mallory A compromised replica serving an ordinary query could theoretically return the harmless-looking version. The user then signs a vote based on information they never should have trusted. DFINITY uses essentially this governance example in its security guidance because it demonstrates why query integrity is not merely an academic cryptography problem. The same principle applies to: token balances permissions transaction status governance proposals financial positions configuration users rely upon security decisions If the frontend will make an important decision based on a query response, ask how that response is authenticated. How certified data works Every canister has access to a special 32-byte certified data field . During an update call, the canister can set it: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff hashed_state = application state + hash subnet certified state = hashed_state + 32-byte certified_data The subnet includes that value in its certified state tree. Later, during a query, the canister can retrieve a certificate proving the current certified value. For one small value, the process is: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff UPDATE value = "ICP" │ SHA-256 │ ▼ 32-byte hash │ CertifiedData.set() │ ▼ subnet-certified state Then: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff QUERY value certificate │ ▼ client verification For a collection, we add another layer: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff Merkle tree / | \ Alice Bob Carol │ │ │ └───────┼────────┘ ▼ root hash │ CertifiedData.set() The canister only needs to certify the root. A query can then return: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff Bob's value + Merkle witness for Bob + subnet certificate The client verifies that Bob's value belongs to the tree represented by the certified root. That is how 32 bytes can authenticate a dataset vastly larger than 32 bytes. Start with one certified value For the smallest useful example, install the SHA-256 package used by the current Motoko certification guidance: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff mops add sha2 I would also keep the Motoko compiler pinned through Mops rather than relying on whatever compiler happens to exist globally: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff mops toolchain use moc latest Mops supports pinning the selected compiler directly into mops.toml , which is useful for keeping builds reproducible. The current Motoko compiler changelog lists 1.14.1 , so this is another area where old snippets can diverge substantially from the current toolchain. Now create: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff import CertifiedData "mo:core/CertifiedData" ; import Runtime "mo:core/Runtime" ; import Sha256 "mo:sha2/Sha256" ; import Text "mo:core/Text" ; persistent actor Config { var networkName : Text = "ICP" ; func hashValue (value : Text ) : Blob { Sha256 . fromBlob ( #sha256, Text . encodeUtf8 (value), ); }; func recertify () { CertifiedData . set ( hashValue (networkName)); }; public func setNetworkName (value : Text ) : async () { networkName := value; recertify (); }; public query func getNetworkName () : async { value : Text ; certificate : Blob ; } { let ?certificate = CertifiedData . getCertificate () else { Runtime . trap ( "No data certificate available" ); }; { value = networkName; certificate; }; }; }; There are two pieces worth understanding separately. This: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff networkName := value; changes application state. This: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff CertifiedData . set ( hashValue (networkName)); changes the cryptographic commitment the subnet will certify. If you perform the first and forget the second, your state and your certification diverge. CertifiedData.set() does not certify arbitrary bytes This is worth stating explicitly because the API can initially give the wrong impression. You are not asking the subnet to store your application data. The certified field has a maximum size of 32 bytes. So this architecture is wrong: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff CertifiedData.set(entireDatabase) Instead: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff database │ deterministic authenticated structure │ root hash │ CertifiedData.set(rootHash) The canister retains the actual state. The certified field contains only a commitment to that state via the hash. Certification must be updated when state changes Suppose we write: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff public func setNetworkName (value : Text ) : async () { networkName := value; }; and forget: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff recertify (); Now: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff actual state: networkName = "Internet Computer" certified hash: SHA256 ( "ICP" ) The query can still return: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff Internet Computer but a correctly implemented verifier will reject it because the returned value no longer matches the certified commitment. That failure is a feature. Certification does not make incorrect application logic disappear. It makes inconsistencies detectable. A pattern I prefer in this situation is centralizing state mutations: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff func setValue ( value : Text ) { networkName := value; recertify (); }; Then all update methods call setValue() rather than modifying the certified field independently. The larger the application becomes, the more valuable that discipline is. The certificate only exists in the correct execution context CertifiedData.getCertificate() is not a general-purpose "give me the certificate whenever I want" API. The certificate is available when the method is executing as the target of a non-replicated query. The IC system interface explicitly says it is unavailable during update execution, replicated query execution, and certain composite-query callback situations. That is why this makes sense: Plain text Bash / Shell C C++ C# CSS Candid Go HTML / XML Java JavaScript JSON Kotlin Markdown Motoko PHP Python Ruby Rust SQL Swift TypeScript YAML Diff public query func getCertifiedValue () : async CertifiedValue while trying to build the same response inside an ord