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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
extern crate futures;
extern crate serde_json;
use action::{Action, ActionWrapper};
use agent;
use context::Context;
use futures::{future, Async, Future};
use holochain_core_types::{
cas::{content::AddressableContent, storage::ContentAddressableStorage},
chain_header::ChainHeader,
entry::Entry,
error::HolochainError,
validation::{ValidationPackage, ValidationPackageDefinition::*},
};
use nucleus::ribosome::callback::{self, CallbackResult};
use snowflake;
use std::{sync::Arc, thread};
pub fn build_validation_package(
entry: &Entry,
context: &Arc<Context>,
) -> Box<dyn Future<Item = ValidationPackage, Error = HolochainError>> {
let id = snowflake::ProcessUniqueId::new();
match context
.state()
.unwrap()
.nucleus()
.dna()
.unwrap()
.get_zome_name_for_entry_type(entry.entry_type().as_str())
{
None => {
return Box::new(future::err(HolochainError::ValidationFailed(format!(
"Unknown entry type: '{}'",
entry.entry_type().as_str()
))));;
}
Some(_) => {
let id = id.clone();
let entry = entry.clone();
let context = context.clone();
let entry_header = chain_header(entry.clone(), &context).unwrap_or(
agent::state::create_new_chain_header(&entry, &*context.state().unwrap().agent()),
);
thread::spawn(move || {
let maybe_callback_result =
callback::validation_package::get_validation_package_definition(
entry.entry_type().clone(),
context.clone(),
);
let maybe_validation_package = maybe_callback_result
.and_then(|callback_result| match callback_result {
CallbackResult::Fail(error_string) => {
Err(HolochainError::ErrorGeneric(error_string))
}
CallbackResult::ValidationPackageDefinition(def) => Ok(def),
CallbackResult::NotImplemented => {
Err(HolochainError::ErrorGeneric(format!(
"ValidationPackage callback not implemented for {:?}",
entry.entry_type().clone()
)))
}
_ => unreachable!(),
})
.and_then(|package_definition| {
Ok(match package_definition {
Entry => ValidationPackage::only_header(entry_header),
ChainEntries => {
let mut package = ValidationPackage::only_header(entry_header);
package.source_chain_entries =
Some(all_public_chain_entries(&context));
package
}
ChainHeaders => {
let mut package = ValidationPackage::only_header(entry_header);
package.source_chain_headers =
Some(all_public_chain_headers(&context));
package
}
ChainFull => {
let mut package = ValidationPackage::only_header(entry_header);
package.source_chain_entries =
Some(all_public_chain_entries(&context));
package.source_chain_headers =
Some(all_public_chain_headers(&context));
package
}
Custom(string) => {
let mut package = ValidationPackage::only_header(entry_header);
package.custom = Some(string);
package
}
})
});
context
.action_channel
.send(ActionWrapper::new(Action::ReturnValidationPackage((
id,
maybe_validation_package,
))))
.expect("action channel to be open in reducer");
});
}
};
Box::new(ValidationPackageFuture {
context: context.clone(),
key: id,
})
}
fn chain_header(entry: Entry, context: &Arc<Context>) -> Option<ChainHeader> {
let chain = context.state().unwrap().agent().chain();
let top_header = context.state().unwrap().agent().top_chain_header();
chain
.iter(&top_header)
.find(|ref header| *header.entry_address() == entry.address())
}
fn all_public_chain_entries(context: &Arc<Context>) -> Vec<Entry> {
let chain = context.state().unwrap().agent().chain();
let top_header = context.state().unwrap().agent().top_chain_header();
chain
.iter(&top_header)
.filter(|ref chain_header| chain_header.entry_type().can_publish())
.map(|chain_header| {
let entry: Option<Entry> = chain
.content_storage()
.fetch(chain_header.entry_address())
.expect("Could not fetch from CAS");
entry.expect("Could not find entry in CAS for existing chain header")
})
.collect::<Vec<_>>()
}
fn all_public_chain_headers(context: &Arc<Context>) -> Vec<ChainHeader> {
let chain = context.state().unwrap().agent().chain();
let top_header = context.state().unwrap().agent().top_chain_header();
chain
.iter(&top_header)
.filter(|ref chain_header| chain_header.entry_type().can_publish())
.collect::<Vec<_>>()
}
pub struct ValidationPackageFuture {
context: Arc<Context>,
key: snowflake::ProcessUniqueId,
}
impl Future for ValidationPackageFuture {
type Item = ValidationPackage;
type Error = HolochainError;
fn poll(
&mut self,
cx: &mut futures::task::Context<'_>,
) -> Result<Async<Self::Item>, Self::Error> {
cx.waker().wake();
if let Some(state) = self.context.state() {
match state.nucleus().validation_packages.get(&self.key) {
Some(Ok(validation_package)) => {
Ok(futures::Async::Ready(validation_package.clone()))
}
Some(Err(error)) => Err(error.clone()),
None => Ok(futures::Async::Pending),
}
} else {
Ok(futures::Async::Pending)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use nucleus::actions::tests::*;
use futures::executor::block_on;
use holochain_core_types::validation::ValidationPackage;
#[test]
fn test_building_validation_package_entry() {
let (_instance, context) = instance();
commit(test_entry_package_chain_entries(), &context);
commit(test_entry_package_chain_full(), &context);
let chain_header = commit(test_entry_package_entry(), &context);
let maybe_validation_package = block_on(build_validation_package(
&test_entry_package_entry(),
&context.clone(),
));
println!("{:?}", maybe_validation_package);
assert!(maybe_validation_package.is_ok());
let expected = ValidationPackage {
chain_header: Some(chain_header),
source_chain_entries: None,
source_chain_headers: None,
custom: None,
};
assert_eq!(maybe_validation_package.unwrap(), expected);
}
#[test]
fn test_building_validation_package_chain_entries() {
let (_instance, context) = instance();
commit(test_entry_package_chain_entries(), &context);
commit(test_entry_package_chain_full(), &context);
let chain_header = commit(test_entry_package_chain_entries(), &context);
let maybe_validation_package = block_on(build_validation_package(
&test_entry_package_chain_entries(),
&context.clone(),
));
assert!(maybe_validation_package.is_ok());
let expected = ValidationPackage {
chain_header: Some(chain_header),
source_chain_entries: Some(all_public_chain_entries(&context)),
source_chain_headers: None,
custom: None,
};
assert_eq!(maybe_validation_package.unwrap(), expected);
}
#[test]
fn test_building_validation_package_chain_headers() {
let (_instance, context) = instance();
commit(test_entry_package_chain_entries(), &context);
commit(test_entry_package_chain_full(), &context);
let chain_header = commit(test_entry_package_chain_headers(), &context);
let maybe_validation_package = block_on(build_validation_package(
&test_entry_package_chain_headers(),
&context.clone(),
));
assert!(maybe_validation_package.is_ok());
let expected = ValidationPackage {
chain_header: Some(chain_header),
source_chain_entries: None,
source_chain_headers: Some(all_public_chain_headers(&context)),
custom: None,
};
assert_eq!(maybe_validation_package.unwrap(), expected);
}
#[test]
fn test_building_validation_package_chain_full() {
let (_instance, context) = instance();
commit(test_entry_package_chain_entries(), &context);
commit(test_entry_package_entry(), &context);
let chain_header = commit(test_entry_package_chain_full(), &context);
let maybe_validation_package = block_on(build_validation_package(
&test_entry_package_chain_full(),
&context.clone(),
));
assert!(maybe_validation_package.is_ok());
let expected = ValidationPackage {
chain_header: Some(chain_header),
source_chain_entries: Some(all_public_chain_entries(&context)),
source_chain_headers: Some(all_public_chain_headers(&context)),
custom: None,
};
assert_eq!(maybe_validation_package.unwrap(), expected);
}
}