-
Notifications
You must be signed in to change notification settings - Fork 296
/
lazy_tree_conversion.rs
741 lines (667 loc) · 23.7 KB
/
lazy_tree_conversion.rs
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
//! Conversion from `ReplicatedState` to `LazyTree`.
use crate::{
encoding::{
encode_controllers, encode_message, encode_metadata, encode_stream_header,
encode_subnet_canister_ranges, encode_subnet_metrics,
},
CertificationVersion, MAX_SUPPORTED_CERTIFICATION_VERSION,
};
use ic_canonical_state_tree_hash::lazy_tree::{blob, fork, num, string, Lazy, LazyFork, LazyTree};
use ic_crypto_tree_hash::Label;
use ic_error_types::ErrorCode;
use ic_error_types::RejectCode;
use ic_registry_routing_table::RoutingTable;
use ic_replicated_state::{
canister_state::CanisterState,
metadata_state::{
IngressHistoryState, StreamMap, SubnetMetrics, SubnetTopology, SystemMetadata,
},
replicated_state::ReplicatedStateMessageRouting,
ExecutionState, ReplicatedState,
};
use ic_types::{
ingress::{IngressState, IngressStatus, WasmResult},
messages::{MessageId, EXPECTED_MESSAGE_ID_LENGTH},
xnet::{StreamHeader, StreamIndex, StreamIndexedQueue},
CanisterId, NodeId, PrincipalId, SubnetId,
};
use std::collections::BTreeMap;
use std::convert::{AsRef, TryFrom, TryInto};
use std::iter::once;
use std::sync::Arc;
use LazyTree::Blob;
/// A simple map from a label to a tree. It should be mostly used for static
/// subtrees where all the labels are known in advance.
#[derive(Default)]
struct FiniteMap<'a>(BTreeMap<Label, Lazy<'a, LazyTree<'a>>>);
impl<'a> FiniteMap<'a> {
/// Adds a function returning a subtree with the specified label to this map.
pub fn with<B, T>(mut self, blob: B, func: T) -> Self
where
B: AsRef<[u8]>,
T: Fn() -> LazyTree<'a> + 'a + Send + Sync,
{
self.0.insert(Label::from(blob), Lazy::Func(Arc::new(func)));
self
}
/// Adds a subtree with the specified label to this map.
pub fn with_tree<B: AsRef<[u8]>>(mut self, label: B, tree: LazyTree<'a>) -> Self {
self.0.insert(Label::from(label), Lazy::Value(tree));
self
}
/// If condition is true, adds a function returning a subtree with the specified label to this map.
/// Otherwise does nothing.
pub fn with_if<B, T>(mut self, condition: bool, blob: B, func: T) -> Self
where
B: AsRef<[u8]>,
T: Fn() -> LazyTree<'a> + 'a + Send + Sync,
{
if condition {
self.0.insert(Label::from(blob), Lazy::Func(Arc::new(func)));
}
self
}
/// If condition is true, adds a new subtree to this map.
/// Otherwise does nothing.
pub fn with_tree_if<B: AsRef<[u8]>>(
mut self,
condition: bool,
label: B,
tree: LazyTree<'a>,
) -> Self {
if condition {
self.0.insert(Label::from(label), Lazy::Value(tree));
}
self
}
}
impl<'a> LazyFork<'a> for FiniteMap<'a> {
fn edge(&self, label: &Label) -> Option<LazyTree<'a>> {
self.0.get(label).map(|lazy| lazy.force())
}
fn labels(&self) -> Box<dyn Iterator<Item = Label> + '_> {
Box::new(self.0.keys().cloned())
}
fn children(&self) -> Box<dyn Iterator<Item = (Label, LazyTree<'a>)> + '_> {
Box::new(self.0.iter().map(|(l, lazy)| (l.clone(), lazy.force())))
}
fn len(&self) -> usize {
self.0.len()
}
}
/// LabelLike defines a (partial) conversion between a type and a label.
pub trait LabelLike: Sized {
fn to_label(&self) -> Label;
fn from_label(label: &[u8]) -> Option<Self>;
}
impl LabelLike for u64 {
fn to_label(&self) -> Label {
Label::from(self.to_be_bytes())
}
fn from_label(label: &[u8]) -> Option<Self> {
if label.len() != 8 {
return None;
}
let be_bytes: [u8; 8] = label.try_into().ok()?;
Some(u64::from_be_bytes(be_bytes))
}
}
impl LabelLike for String {
fn to_label(&self) -> Label {
Label::from(self.as_bytes())
}
fn from_label(label: &[u8]) -> Option<Self> {
String::from_utf8(Vec::from(label)).ok()
}
}
impl<T: LabelLike, Tag> LabelLike for phantom_newtype::Id<Tag, T> {
fn to_label(&self) -> Label {
self.get_ref().to_label()
}
fn from_label(label: &[u8]) -> Option<Self> {
T::from_label(label).map(Self::new)
}
}
impl<T: LabelLike + Copy, Tag> LabelLike for phantom_newtype::AmountOf<Tag, T> {
fn to_label(&self) -> Label {
self.get().to_label()
}
fn from_label(label: &[u8]) -> Option<Self> {
T::from_label(label).map(Self::new)
}
}
impl LabelLike for PrincipalId {
fn to_label(&self) -> Label {
Label::from(self.as_slice())
}
fn from_label(label: &[u8]) -> Option<Self> {
PrincipalId::try_from(label).ok()
}
}
impl LabelLike for CanisterId {
fn to_label(&self) -> Label {
self.get_ref().to_label()
}
fn from_label(label: &[u8]) -> Option<Self> {
PrincipalId::from_label(label).map(|principal| Self::unchecked_from_principal(principal))
}
}
/// A type of fork that constructs a lazy tree view of a typed Map without
/// copying the underlying data.
#[derive(Clone)]
struct MapTransformFork<'a, K, V, F>
where
F: Fn(K, &'a V, CertificationVersion) -> LazyTree<'a>,
{
map: &'a BTreeMap<K, V>,
certification_version: CertificationVersion,
mk_tree: F,
}
impl<'a, K, V, F> LazyFork<'a> for MapTransformFork<'a, K, V, F>
where
K: Ord + LabelLike + Clone + Send + Sync,
F: Fn(K, &'a V, CertificationVersion) -> LazyTree<'a> + Send + Sync,
V: Send + Sync,
{
fn edge(&self, label: &Label) -> Option<LazyTree<'a>> {
let k = K::from_label(label.as_bytes())?;
self.map
.get(&k)
.map(move |v| (self.mk_tree)(k, v, self.certification_version))
}
fn labels(&self) -> Box<dyn Iterator<Item = Label> + '_> {
Box::new(self.map.keys().map(|l| l.to_label()))
}
fn children(&self) -> Box<dyn Iterator<Item = (Label, LazyTree<'a>)> + '_> {
Box::new(self.map.iter().map(move |(k, v)| {
(
k.to_label(),
(self.mk_tree)(k.clone(), v, self.certification_version),
)
}))
}
fn len(&self) -> usize {
self.map.len()
}
}
/// A special type of fork that describes a stream-indexed queue.
#[derive(Clone)]
struct StreamQueueFork<'a, T>
where
T: Send + Sync,
{
queue: &'a StreamIndexedQueue<T>,
certification_version: CertificationVersion,
mk_tree: fn(StreamIndex, &'a T, CertificationVersion) -> LazyTree<'a>,
}
impl<'a, T: Send + Sync> LazyFork<'a> for StreamQueueFork<'a, T> {
fn edge(&self, label: &Label) -> Option<LazyTree<'a>> {
let idx = StreamIndex::from_label(label.as_bytes())?;
self.queue
.get(idx)
.map(move |v| (self.mk_tree)(idx, v, self.certification_version))
}
fn labels(&self) -> Box<dyn Iterator<Item = Label> + '_> {
Box::new((self.queue.begin().get()..self.queue.end().get()).map(|i| i.to_label()))
}
fn children(&self) -> Box<dyn Iterator<Item = (Label, LazyTree<'a>)> + '_> {
Box::new(self.queue.iter().map(move |(idx, v)| {
(
idx.to_label(),
(self.mk_tree)(idx, v, self.certification_version),
)
}))
}
fn len(&self) -> usize {
self.queue.len()
}
}
fn invert_routing_table(
routing_table: &RoutingTable,
) -> BTreeMap<SubnetId, Vec<(PrincipalId, PrincipalId)>> {
let mut inverse_map: BTreeMap<SubnetId, Vec<_>> = BTreeMap::new();
for (range, subnet_id) in routing_table.iter() {
inverse_map
.entry(*subnet_id)
.or_default()
.push((range.start.get(), range.end.get()));
}
inverse_map
}
/// Converts replicated state into a lazy tree.
pub fn replicated_state_as_lazy_tree(state: &ReplicatedState) -> LazyTree<'_> {
let certification_version = state.metadata.certification_version;
assert!(
certification_version <= MAX_SUPPORTED_CERTIFICATION_VERSION,
"Unable to certify state with version {:?}. Maximum supported certification version is {:?}",
certification_version,
MAX_SUPPORTED_CERTIFICATION_VERSION
);
fork(
FiniteMap::default()
.with("metadata", move || {
system_metadata_as_tree(&state.metadata, certification_version)
})
.with("streams", move || {
streams_as_tree(state.streams(), certification_version)
})
.with("canister", move || {
canisters_as_tree(&state.canister_states, certification_version)
})
.with_tree(
"request_status",
fork(IngressHistoryFork(
&state.metadata.ingress_history,
certification_version,
)),
)
.with("subnet", move || {
let inverted_routing_table = Arc::new(invert_routing_table(
&state.metadata.network_topology.routing_table,
));
let own_subnet_id = state.metadata.own_subnet_id;
subnets_as_tree(
&state.metadata.network_topology.subnets,
own_subnet_id,
&state.metadata.node_public_keys,
inverted_routing_table,
&state.metadata.subnet_metrics,
certification_version,
)
})
.with_tree(
"time",
num(state.metadata.batch_time.as_nanos_since_unix_epoch()),
),
)
}
fn streams_as_tree(
streams: &StreamMap,
certification_version: CertificationVersion,
) -> LazyTree<'_> {
fork(MapTransformFork {
map: streams,
certification_version,
mk_tree: |_subnet_id, stream, certification_version| {
fork(
FiniteMap::default()
.with_tree(
"header",
blob(move || {
let stream_header: StreamHeader = stream.header();
encode_stream_header(&stream_header, certification_version)
}),
)
.with_tree(
"messages",
fork(StreamQueueFork {
queue: stream.messages(),
certification_version,
mk_tree: |_idx, msg, certification_version| {
blob(move || encode_message(msg, certification_version))
},
}),
),
)
},
})
}
fn system_metadata_as_tree(
m: &SystemMetadata,
certification_version: CertificationVersion,
) -> LazyTree<'_> {
blob(move || encode_metadata(m, certification_version))
}
struct IngressHistoryFork<'a>(&'a IngressHistoryState, CertificationVersion);
impl<'a> LazyFork<'a> for IngressHistoryFork<'a> {
fn edge(&self, label: &Label) -> Option<LazyTree<'a>> {
let byte_array: [u8; EXPECTED_MESSAGE_ID_LENGTH] = label.as_bytes().try_into().ok()?;
let id = MessageId::from(byte_array);
self.0.get(&id).map(|status| status_to_tree(status, self.1))
}
fn labels(&self) -> Box<dyn Iterator<Item = Label> + '_> {
Box::new(self.0.statuses().map(|(id, _)| Label::from(id.as_bytes())))
}
fn children(&self) -> Box<dyn Iterator<Item = (Label, LazyTree<'a>)> + '_> {
Box::new(
self.0
.statuses()
.map(|(id, status)| (Label::from(id.as_bytes()), status_to_tree(status, self.1))),
)
}
fn len(&self) -> usize {
self.0.len()
}
}
const ERROR_CODE_LABEL: &[u8] = b"error_code";
const REJECT_CODE_LABEL: &[u8] = b"reject_code";
const REJECT_MESSAGE_LABEL: &[u8] = b"reject_message";
const REPLY_LABEL: &[u8] = b"reply";
const STATUS_LABEL: &[u8] = b"status";
struct OnlyStatus<'a>(&'a str);
impl<'a> LazyFork<'a> for OnlyStatus<'a> {
fn edge(&self, label: &Label) -> Option<LazyTree<'a>> {
(label.as_bytes() == STATUS_LABEL).then_some(string(self.0))
}
fn labels(&self) -> Box<dyn Iterator<Item = Label> + 'a> {
Box::new(once(Label::from(STATUS_LABEL)))
}
fn children(&self) -> Box<dyn Iterator<Item = (Label, LazyTree<'a>)> + 'a> {
Box::new(once((Label::from(STATUS_LABEL), string(self.0))))
}
fn len(&self) -> usize {
1
}
}
const REPLY_STATUS_LABELS: [&[u8]; 2] = [REPLY_LABEL, STATUS_LABEL];
#[derive(Clone)]
struct ReplyStatus<'a>(&'a [u8]);
impl<'a> LazyFork<'a> for ReplyStatus<'a> {
fn edge(&self, label: &Label) -> Option<LazyTree<'a>> {
match label.as_bytes() {
STATUS_LABEL => Some(string("replied")),
REPLY_LABEL => Some(Blob(self.0, None)),
_ => None,
}
}
fn labels(&self) -> Box<dyn Iterator<Item = Label> + 'a> {
Box::new(REPLY_STATUS_LABELS.iter().map(Label::from))
}
fn children(&self) -> Box<dyn Iterator<Item = (Label, LazyTree<'a>)> + 'a> {
let status = self.clone();
Box::new(REPLY_STATUS_LABELS.iter().filter_map(move |label| {
let label = Label::from(label);
let fork = status.edge(&label)?;
Some((label, fork))
}))
}
fn len(&self) -> usize {
REPLY_STATUS_LABELS.len()
}
}
const REJECT_STATUS_LABELS: [(&[u8], CertificationVersion); 4] = [
(ERROR_CODE_LABEL, CertificationVersion::V11),
(REJECT_CODE_LABEL, CertificationVersion::V0),
(REJECT_MESSAGE_LABEL, CertificationVersion::V0),
(STATUS_LABEL, CertificationVersion::V0),
];
#[derive(Clone)]
struct RejectStatus<'a> {
reject_code: u64,
error_code: Option<ErrorCode>,
message: &'a str,
version: CertificationVersion,
}
impl<'a> LazyFork<'a> for RejectStatus<'a> {
fn edge(&self, label: &Label) -> Option<LazyTree<'a>> {
match label.as_bytes() {
ERROR_CODE_LABEL => self
.error_code
.map(|code| blob(move || code.to_string().into_bytes())),
REJECT_CODE_LABEL => Some(num::<'a>(self.reject_code)),
REJECT_MESSAGE_LABEL => Some(string(self.message)),
STATUS_LABEL => Some(string("rejected")),
_ => None,
}
}
fn labels(&self) -> Box<dyn Iterator<Item = Label> + 'a> {
let version = self.version;
Box::new(
REJECT_STATUS_LABELS
.iter()
.filter_map(move |(label, v)| (*v <= version).then_some(Label::from(label))),
)
}
fn children(&self) -> Box<dyn Iterator<Item = (Label, LazyTree<'a>)> + 'a> {
let status = self.clone();
Box::new(REJECT_STATUS_LABELS.iter().filter_map(move |(label, v)| {
if *v > status.version {
return None;
}
let label = Label::from(label);
let fork = status.edge(&label)?;
Some((label, fork))
}))
}
fn len(&self) -> usize {
REJECT_STATUS_LABELS
.iter()
.filter(|(_, v)| *v <= self.version)
.count()
}
}
fn status_to_tree(status: &IngressStatus, version: CertificationVersion) -> LazyTree<'_> {
match status {
IngressStatus::Known { state, .. } => match state {
IngressState::Completed(WasmResult::Reply(b)) => fork(ReplyStatus(b)),
IngressState::Completed(WasmResult::Reject(s)) => fork(RejectStatus {
reject_code: RejectCode::CanisterReject as u64,
error_code: (version >= CertificationVersion::V11)
.then_some(ErrorCode::CanisterRejectedMessage),
message: s,
version,
}),
IngressState::Failed(error) => fork(RejectStatus {
reject_code: error.reject_code() as u64,
error_code: (version >= CertificationVersion::V11).then_some(error.code()),
message: error.description(),
version,
}),
IngressState::Processing | IngressState::Received | IngressState::Done => {
fork(OnlyStatus(status.as_str()))
}
},
IngressStatus::Unknown => fork(OnlyStatus(status.as_str())),
}
}
const CERTIFIED_DATA_LABEL: &[u8] = b"certified_data";
const CONTROLLER_LABEL: &[u8] = b"controller";
const CONTROLLERS_LABEL: &[u8] = b"controllers";
const METADATA_LABEL: &[u8] = b"metadata";
const MODULE_HASH_LABEL: &[u8] = b"module_hash";
const CANISTER_LABELS: [(&[u8], CertificationVersion, CertificationVersion); 5] = [
(
CERTIFIED_DATA_LABEL,
CertificationVersion::V0,
MAX_SUPPORTED_CERTIFICATION_VERSION,
),
(
CONTROLLER_LABEL,
CertificationVersion::V1,
CertificationVersion::V12,
),
(
CONTROLLERS_LABEL,
CertificationVersion::V2,
MAX_SUPPORTED_CERTIFICATION_VERSION,
),
(
METADATA_LABEL,
CertificationVersion::V6,
MAX_SUPPORTED_CERTIFICATION_VERSION,
),
(
MODULE_HASH_LABEL,
CertificationVersion::V1,
MAX_SUPPORTED_CERTIFICATION_VERSION,
),
];
const CANISTER_NO_MODULE_LABELS: [(&[u8], CertificationVersion, CertificationVersion); 2] = [
(
CONTROLLER_LABEL,
CertificationVersion::V1,
CertificationVersion::V12,
),
(
CONTROLLERS_LABEL,
CertificationVersion::V2,
MAX_SUPPORTED_CERTIFICATION_VERSION,
),
];
#[derive(Clone)]
struct CanisterFork<'a> {
canister: &'a CanisterState,
version: CertificationVersion,
}
impl<'a> CanisterFork<'a> {
/// Like `edge`, but skips the version check on every call.
fn edge_no_checks(&self, label: &[u8]) -> Option<LazyTree<'a>> {
let canister = self.canister;
match canister.execution_state.as_ref() {
Some(execution_state) => match label {
CERTIFIED_DATA_LABEL => Some(Blob(&canister.system_state.certified_data[..], None)),
CONTROLLER_LABEL => Some(Blob(canister.system_state.controller().as_slice(), None)),
CONTROLLERS_LABEL => Some(blob(move || {
encode_controllers(&canister.system_state.controllers)
})),
METADATA_LABEL => Some(canister_metadata_as_tree(execution_state, self.version)),
MODULE_HASH_LABEL => Some(blob(move || {
execution_state.wasm_binary.binary.module_hash().to_vec()
})),
_ => None,
},
None => match label {
CONTROLLER_LABEL => Some(Blob(canister.system_state.controller().as_slice(), None)),
CONTROLLERS_LABEL => Some(blob(move || {
encode_controllers(&canister.system_state.controllers)
})),
_ => None,
},
}
}
}
impl<'a> LazyFork<'a> for CanisterFork<'a> {
fn edge(&self, label: &Label) -> Option<LazyTree<'a>> {
CANISTER_LABELS.iter().find(|(l, minv, maxv)| {
l == &label.as_bytes() && *minv <= self.version && self.version <= *maxv
})?;
self.edge_no_checks(label.as_bytes())
}
fn labels(&self) -> Box<dyn Iterator<Item = Label> + 'a> {
let version = self.version;
if self.canister.execution_state.is_some() {
Box::new(
CANISTER_LABELS
.iter()
.filter_map(move |(label, minv, maxv)| {
(*minv <= version && version <= *maxv).then_some(Label::from(label))
}),
)
} else {
Box::new(
CANISTER_NO_MODULE_LABELS
.iter()
.filter_map(move |(label, minv, maxv)| {
(*minv <= version && version <= *maxv).then_some(Label::from(label))
}),
)
}
}
fn children(&self) -> Box<dyn Iterator<Item = (Label, LazyTree<'a>)> + 'a> {
let canister = self.clone();
Box::new(
CANISTER_LABELS
.iter()
.filter_map(move |(label, minv, maxv)| {
if !(*minv <= canister.version && canister.version <= *maxv) {
return None;
}
Some((Label::from(label), canister.edge_no_checks(label)?))
}),
)
}
fn len(&self) -> usize {
let version = self.version;
if self.canister.execution_state.is_some() {
CANISTER_LABELS
.iter()
.filter(move |(_, minv, maxv)| *minv <= version && version <= *maxv)
.count()
} else {
CANISTER_NO_MODULE_LABELS
.iter()
.filter(move |(_, minv, maxv)| *minv <= version && version <= *maxv)
.count()
}
}
}
fn canisters_as_tree(
canisters: &BTreeMap<CanisterId, CanisterState>,
certification_version: CertificationVersion,
) -> LazyTree<'_> {
fork(MapTransformFork {
map: canisters,
certification_version,
mk_tree: |_canister_id, canister, certification_version| {
fork(CanisterFork {
canister,
version: certification_version,
})
},
})
}
fn subnets_as_tree<'a>(
subnets: &'a BTreeMap<SubnetId, SubnetTopology>,
own_subnet_id: SubnetId,
own_subnet_node_public_keys: &'a BTreeMap<NodeId, Vec<u8>>,
inverted_routing_table: Arc<BTreeMap<SubnetId, Vec<(PrincipalId, PrincipalId)>>>,
metrics: &'a SubnetMetrics,
certification_version: CertificationVersion,
) -> LazyTree<'a> {
fork(MapTransformFork {
map: subnets,
certification_version,
mk_tree: move |subnet_id, subnet_topology, certification_version| {
fork(
FiniteMap::default()
.with_tree("public_key", Blob(&subnet_topology.public_key[..], None))
.with_tree_if(
certification_version > CertificationVersion::V2,
"canister_ranges",
blob({
let inverted_routing_table = Arc::clone(&inverted_routing_table);
move || {
encode_subnet_canister_ranges(
inverted_routing_table.get(&subnet_id),
)
}
}),
)
.with_if(
certification_version > CertificationVersion::V11
&& subnet_id == own_subnet_id,
"node",
move || nodes_as_tree(own_subnet_node_public_keys, certification_version),
)
.with_tree_if(
certification_version >= CertificationVersion::V15
&& subnet_id == own_subnet_id,
"metrics",
blob(move || encode_subnet_metrics(metrics, certification_version)),
),
)
},
})
}
fn nodes_as_tree(
own_subnet_node_public_keys: &BTreeMap<NodeId, Vec<u8>>,
certification_version: CertificationVersion,
) -> LazyTree<'_> {
fork(MapTransformFork {
map: own_subnet_node_public_keys,
certification_version,
mk_tree: |_node_id, public_key, _version| {
fork(FiniteMap::default().with_tree("public_key", Blob(&public_key[..], None)))
},
})
}
fn canister_metadata_as_tree(
execution_state: &ExecutionState,
certification_version: CertificationVersion,
) -> LazyTree<'_> {
fork(MapTransformFork {
map: execution_state.metadata.custom_sections(),
certification_version,
mk_tree: |_name, section, _version| Blob(section.content(), Some(section.hash())),
})
}