rgbstd/persistence/
stash.rs1use std::collections::{BTreeMap, BTreeSet};
25use std::error::Error;
26
27use amplify::confinement::TinyOrdSet;
28use commit_verify::mpc;
29use rgb::{
30 Anchor, AnchorId, BundleId, ContractId, Extension, Genesis, OpId, SchemaId, TransitionBundle,
31};
32use strict_encoding::TypeName;
33
34use crate::interface::{ContractSuppl, Iface, IfaceId, SchemaIfaces};
35
36#[derive(Debug, Display, Error, From)]
37#[display(inner)]
38pub enum StashError<E: Error> {
39 Connectivity(E),
41
42 #[from]
45 InternalInconsistency(StashInconsistency),
46}
47
48#[derive(Debug, Display, Error, From)]
49#[display(doc_comments)]
50pub enum StashInconsistency {
51 IfaceNameAbsent(TypeName),
53
54 IfaceAbsent(IfaceId),
56
57 ContractAbsent(ContractId),
59
60 SchemaAbsent(SchemaId),
65
66 IfaceImplAbsent(IfaceId, SchemaId),
68
69 OperationAbsent(OpId),
74
75 AnchorAbsent(AnchorId),
80
81 BundleAbsent(BundleId),
86}
87
88pub trait Stash {
89 type Error: Error;
91
92 fn schema_ids(&self) -> Result<BTreeSet<SchemaId>, Self::Error>;
93
94 fn ifaces(&self) -> Result<BTreeMap<IfaceId, TypeName>, Self::Error>;
95
96 fn iface_by_name(&self, name: &TypeName) -> Result<&Iface, StashError<Self::Error>>;
97
98 fn iface_by_id(&self, id: IfaceId) -> Result<&Iface, StashError<Self::Error>>;
99
100 fn schema(&self, schema_id: SchemaId) -> Result<&SchemaIfaces, StashError<Self::Error>>;
101
102 fn contract_ids(&self) -> Result<BTreeSet<ContractId>, Self::Error>;
103
104 fn contract_ids_by_iface(&self, name: &TypeName) -> Result<BTreeSet<ContractId>, Self::Error>;
105
106 fn contract_schema(
107 &self,
108 contract_id: ContractId,
109 ) -> Result<&SchemaIfaces, StashError<Self::Error>> {
110 let genesis = self.genesis(contract_id)?;
111 self.schema(genesis.schema_id)
112 }
113
114 fn contract_suppl(&self, contract_id: ContractId) -> Option<&TinyOrdSet<ContractSuppl>>;
115
116 fn genesis(&self, contract_id: ContractId) -> Result<&Genesis, StashError<Self::Error>>;
117
118 fn bundle_ids(&self) -> Result<BTreeSet<BundleId>, Self::Error>;
119
120 fn bundle(&self, bundle_id: BundleId) -> Result<&TransitionBundle, StashError<Self::Error>>;
121
122 fn extension_ids(&self) -> Result<BTreeSet<OpId>, Self::Error>;
123
124 fn extension(&self, op_id: OpId) -> Result<&Extension, StashError<Self::Error>>;
125
126 fn anchor_ids(&self) -> Result<BTreeSet<AnchorId>, Self::Error>;
127
128 fn anchor(
129 &self,
130 anchor_id: AnchorId,
131 ) -> Result<&Anchor<mpc::MerkleBlock>, StashError<Self::Error>>;
132}