1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use super::zome;
use crate::prelude::*;
#[cfg(feature = "full-dna-def")]
use crate::zome::error::ZomeError;
#[cfg(feature = "full-dna-def")]
use holo_hash::*;
pub type IntegrityZomes = Vec<(ZomeName, zome::IntegrityZomeDef)>;
pub type CoordinatorZomes = Vec<(ZomeName, zome::CoordinatorZomeDef)>;
pub type Uid = String;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, SerializedBytes)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "full-dna-def", derive(derive_builder::Builder))]
#[cfg_attr(feature = "full-dna-def", builder(public))]
pub struct DnaDef {
#[cfg_attr(
feature = "full-dna-def",
builder(default = "\"Generated DnaDef\".to_string()")
)]
pub name: String,
pub uid: String,
#[cfg_attr(feature = "full-dna-def", builder(default = "().try_into().unwrap()"))]
pub properties: SerializedBytes,
#[cfg_attr(feature = "full-dna-def", builder(default = "Timestamp::now()"))]
pub origin_time: Timestamp,
pub integrity_zomes: IntegrityZomes,
pub coordinator_zomes: CoordinatorZomes,
}
#[derive(Serialize, Debug, PartialEq, Eq)]
struct DnaDefHash<'a> {
name: &'a String,
uid: &'a String,
properties: &'a SerializedBytes,
integrity_zomes: &'a IntegrityZomes,
}
#[cfg(feature = "test_utils")]
impl DnaDef {
pub fn unique_from_zomes(
integrity: Vec<IntegrityZome>,
coordinator: Vec<CoordinatorZome>,
) -> DnaDef {
let integrity = integrity.into_iter().map(|z| z.into_inner()).collect();
let coordinator = coordinator.into_iter().map(|z| z.into_inner()).collect();
DnaDefBuilder::default()
.integrity_zomes(integrity)
.coordinator_zomes(coordinator)
.random_uid()
.build()
.unwrap()
}
}
impl DnaDef {
pub fn all_zomes(&self) -> impl Iterator<Item = (&ZomeName, &zome::ZomeDef)> {
self.integrity_zomes
.iter()
.map(|(n, def)| (n, def.as_any_zome_def()))
.chain(
self.coordinator_zomes
.iter()
.map(|(n, def)| (n, def.as_any_zome_def())),
)
}
}
#[cfg(feature = "full-dna-def")]
impl DnaDef {
pub fn get_integrity_zome(
&self,
zome_name: &ZomeName,
) -> Result<zome::IntegrityZome, ZomeError> {
self.integrity_zomes
.iter()
.find(|(name, _)| name == zome_name)
.cloned()
.map(|(name, def)| IntegrityZome::new(name, def))
.ok_or_else(|| ZomeError::ZomeNotFound(format!("Zome '{}' not found", &zome_name,)))
}
pub fn is_integrity_zome(&self, zome_name: &ZomeName) -> bool {
self.integrity_zomes
.iter()
.any(|(name, _)| name == zome_name)
}
pub fn get_coordinator_zome(
&self,
zome_name: &ZomeName,
) -> Result<zome::CoordinatorZome, ZomeError> {
self.coordinator_zomes
.iter()
.find(|(name, _)| name == zome_name)
.cloned()
.map(|(name, def)| CoordinatorZome::new(name, def))
.ok_or_else(|| ZomeError::ZomeNotFound(format!("Zome '{}' not found", &zome_name,)))
}
pub fn get_zome(&self, zome_name: &ZomeName) -> Result<zome::Zome, ZomeError> {
self.integrity_zomes
.iter()
.find(|(name, _)| name == zome_name)
.cloned()
.map(|(name, def)| Zome::new(name, def.erase_type()))
.or_else(|| {
self.coordinator_zomes
.iter()
.find(|(name, _)| name == zome_name)
.cloned()
.map(|(name, def)| Zome::new(name, def.erase_type()))
})
.ok_or_else(|| ZomeError::ZomeNotFound(format!("Zome '{}' not found", &zome_name,)))
}
pub fn get_all_coordinators(&self) -> Vec<zome::CoordinatorZome> {
self.coordinator_zomes
.iter()
.cloned()
.map(|(name, def)| CoordinatorZome::new(name, def))
.collect()
}
pub fn get_wasm_zome(&self, zome_name: &ZomeName) -> Result<&zome::WasmZome, ZomeError> {
self.all_zomes()
.find(|(name, _)| *name == zome_name)
.map(|(_, def)| def)
.ok_or_else(|| ZomeError::ZomeNotFound(format!("Zome '{}' not found", &zome_name,)))
.and_then(|def| {
if let ZomeDef::Wasm(wasm_zome) = def {
Ok(wasm_zome)
} else {
Err(ZomeError::NonWasmZome(zome_name.clone()))
}
})
}
pub fn modify_phenotype(&self, uid: Uid, properties: SerializedBytes) -> Self {
let mut clone = self.clone();
clone.properties = properties;
clone.uid = uid;
clone
}
pub fn topology(&self, cutoff: std::time::Duration) -> kitsune_p2p_dht::spacetime::Topology {
kitsune_p2p_dht::spacetime::Topology::standard(self.origin_time, cutoff)
}
}
#[cfg(feature = "full-dna-def")]
pub fn random_uid() -> String {
nanoid::nanoid!()
}
#[cfg(feature = "full-dna-def")]
impl DnaDefBuilder {
pub fn random_uid(&mut self) -> &mut Self {
self.uid = Some(random_uid());
self
}
}
#[cfg(feature = "full-dna-def")]
pub type DnaDefHashed = HoloHashed<DnaDef>;
#[cfg(feature = "full-dna-def")]
impl HashableContent for DnaDef {
type HashType = holo_hash::hash_type::Dna;
fn hash_type(&self) -> Self::HashType {
holo_hash::hash_type::Dna::new()
}
fn hashable_content(&self) -> HashableContentBytes {
let hash = DnaDefHash {
name: &self.name,
uid: &self.uid,
properties: &self.properties,
integrity_zomes: &self.integrity_zomes,
};
HashableContentBytes::Content(
holochain_serialized_bytes::UnsafeBytes::from(
holochain_serialized_bytes::encode(&hash)
.expect("Could not serialize HashableContent"),
)
.into(),
)
}
}