Skip to main content

fakecloud_managedblockchain/
state.rs

1//! Account-partitioned, serializable state for Amazon Managed Blockchain
2//! (`managedblockchain`).
3//!
4//! Every resource is stored as its already-output-valid wire JSON object so
5//! reads echo exactly what writes persisted. All map keys are plain `String`s
6//! (resource id or resource ARN), so the snapshot never depends on the
7//! tuple-key serde adapter that has silently broken snapshot serialization on
8//! other services.
9
10use std::collections::BTreeMap;
11use std::sync::Arc;
12
13use parking_lot::RwLock;
14use serde::{Deserialize, Serialize};
15use serde_json::Value;
16
17use fakecloud_core::multi_account::{AccountState, MultiAccountState};
18
19pub const MANAGEDBLOCKCHAIN_SNAPSHOT_SCHEMA_VERSION: u32 = 1;
20
21/// Per-account Amazon Managed Blockchain state.
22#[derive(Debug, Clone, Default, Serialize, Deserialize)]
23pub struct ManagedBlockchainData {
24    /// Networks keyed by `Id`, stored as their `Network` wire object.
25    #[serde(default)]
26    pub networks: BTreeMap<String, Value>,
27    /// Members keyed by `Id`, stored as their `Member` wire object.
28    #[serde(default)]
29    pub members: BTreeMap<String, Value>,
30    /// Nodes keyed by `Id`, stored as their `Node` wire object.
31    #[serde(default)]
32    pub nodes: BTreeMap<String, Value>,
33    /// Proposals keyed by `ProposalId`, stored as their `Proposal` wire object.
34    #[serde(default)]
35    pub proposals: BTreeMap<String, Value>,
36    /// Recorded votes keyed by `ProposalId`, each a `VoteSummary` wire object.
37    #[serde(default)]
38    pub votes: BTreeMap<String, Vec<Value>>,
39    /// Pending invitations keyed by `InvitationId`, stored as their `Invitation`
40    /// wire object.
41    #[serde(default)]
42    pub invitations: BTreeMap<String, Value>,
43    /// Token accessors keyed by `Id`, stored as their `Accessor` wire object.
44    #[serde(default)]
45    pub accessors: BTreeMap<String, Value>,
46    /// Tags keyed by resource ARN.
47    #[serde(default)]
48    pub tags: BTreeMap<String, BTreeMap<String, String>>,
49}
50
51impl AccountState for ManagedBlockchainData {
52    fn new_for_account(_account_id: &str, _region: &str, _endpoint: &str) -> Self {
53        Self::default()
54    }
55}
56
57pub type SharedManagedBlockchainState = Arc<RwLock<MultiAccountState<ManagedBlockchainData>>>;
58
59#[derive(Debug, Serialize, Deserialize)]
60pub struct ManagedBlockchainSnapshot {
61    pub schema_version: u32,
62    pub accounts: MultiAccountState<ManagedBlockchainData>,
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn new_account_is_empty() {
71        let data = ManagedBlockchainData::new_for_account("000000000000", "us-east-1", "");
72        assert!(data.networks.is_empty());
73        assert!(data.members.is_empty());
74        assert!(data.accessors.is_empty());
75    }
76}