Skip to main content

weirdnet_update/
state.rs

1//! Client anti-rollback state, persisted by the app.
2
3use crate::envelope::Signed;
4use crate::{Hash32, Result, cbor};
5use semver::Version;
6use serde::{Deserialize, Serialize};
7use std::collections::{BTreeMap, BTreeSet};
8
9/// Highest seq and manifest hash seen for one app and channel.
10#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
11pub struct Seen {
12    /// Seq.
13    pub seq: u64,
14    /// Payload hash of that manifest.
15    pub manifest_hash: Hash32,
16}
17
18/// What the client remembers between runs. Store it in the app's private storage.
19#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
20pub struct ClientState {
21    /// The trusted root chain, oldest first; re-verified from the pin on load.
22    pub roots: Vec<Signed>,
23    /// Last manifest seen per `app/channel`.
24    pub seen: BTreeMap<String, Seen>,
25    /// Installed version per `app/channel/kind`.
26    pub installed: BTreeMap<String, Version>,
27    /// Updates that failed their boot check here.
28    pub bad: BTreeSet<String>,
29}
30
31impl ClientState {
32    /// CBOR encoding.
33    pub fn to_bytes(&self) -> Result<Vec<u8>> {
34        cbor::to_vec(self)
35    }
36
37    /// Parses saved state.
38    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
39        cbor::from_slice(bytes)
40    }
41}
42
43pub(crate) fn channel_key(app: &str, channel: &str) -> String {
44    format!("{app}/{channel}")
45}
46
47pub(crate) fn installed_key(app: &str, channel: &str, kind: &str) -> String {
48    format!("{app}/{channel}/{kind}")
49}