/
metadata_state.rs
1564 lines (1400 loc) · 59.6 KB
/
metadata_state.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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
pub mod subnet_call_context_manager;
#[cfg(test)]
mod tests;
use crate::metadata_state::subnet_call_context_manager::SubnetCallContextManager;
use ic_base_types::CanisterId;
use ic_btc_types::Network as BitcoinNetwork;
use ic_btc_types_internal::BlockBlob;
use ic_certification_version::{CertificationVersion, CURRENT_CERTIFICATION_VERSION};
use ic_constants::MAX_INGRESS_TTL;
use ic_ic00_types::EcdsaKeyId;
use ic_protobuf::{
proxy::{try_from_option_field, ProxyDecodeError},
registry::subnet::v1 as pb_subnet,
state::{
ingress::v1 as pb_ingress,
queues::v1 as pb_queues,
system_metadata::v1::{self as pb_metadata},
},
types::v1 as pb_types,
};
use ic_registry_routing_table::{
canister_id_into_u64, difference, intersection, CanisterIdRanges, CanisterMigrations,
RoutingTable, CANISTER_IDS_PER_SUBNET,
};
use ic_registry_subnet_features::{BitcoinFeature, BitcoinFeatureStatus, SubnetFeatures};
use ic_registry_subnet_type::SubnetType;
use ic_types::nominal_cycles::NominalCycles;
use ic_types::{
crypto::CryptoHash,
ingress::{IngressState, IngressStatus},
messages::{MessageId, RequestOrResponse},
node_id_into_protobuf, node_id_try_from_protobuf, subnet_id_into_protobuf,
subnet_id_try_from_protobuf,
time::{Time, UNIX_EPOCH},
xnet::{StreamHeader, StreamIndex, StreamIndexedQueue, StreamSlice},
CountBytes, CryptoHashOfPartialState, NodeId, NumBytes, PrincipalId, SubnetId,
};
use ic_wasm_types::WasmHash;
use serde::{Deserialize, Serialize};
use std::ops::Bound::{Included, Unbounded};
use std::{
collections::{BTreeMap, BTreeSet, VecDeque},
convert::{From, TryFrom, TryInto},
mem::size_of,
sync::Arc,
};
/// `BTreeMap` of streams by destination `SubnetId`.
pub type StreamMap = BTreeMap<SubnetId, Stream>;
/// Replicated system metadata. Used primarily for inter-canister messaging and
/// history queries.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SystemMetadata {
/// History of ingress messages as they traversed through the
/// system.
pub ingress_history: IngressHistoryState,
/// XNet stream state indexed by the _destination_ subnet id.
pub(super) streams: Arc<Streams>,
/// The canister ID ranges from which this subnet generates canister IDs.
canister_allocation_ranges: CanisterIdRanges,
/// The last generated canister ID; or `None` if this subnet has not
/// generated any canister IDs yet.
///
/// If present, must be within the first `CanisterIdRange` in
/// `canister_allocation_ranges` (and the latter may not be empty).
last_generated_canister_id: Option<CanisterId>,
/// The hash of the previous partial canonical state.
/// The initial state doesn't have any previous state.
pub prev_state_hash: Option<CryptoHashOfPartialState>,
/// The Consensus-determined time this batch was created.
/// NOTE: this time is monotonically increasing (and not strictly
/// increasing).
pub batch_time: Time,
pub network_topology: NetworkTopology,
pub own_subnet_id: SubnetId,
pub own_subnet_type: SubnetType,
pub own_subnet_features: SubnetFeatures,
/// Asynchronously handled subnet messages.
pub subnet_call_context_manager: SubnetCallContextManager,
/// The version of StateSync protocol that should be used to compute
/// manifest of this state.
pub state_sync_version: u32,
/// The version of certification procedure that should be used for this
/// state.
pub certification_version: CertificationVersion,
/// When canisters execute and modify their heap, we track the actual delta
/// they produced. From time to time, when consensus tells us that it is
/// fine to drop older states, the respective deltas are dropped. This field
/// tracks a deterministic estimate of the size of all the deltas that we
/// are currently maintaining.
///
/// The reason this field cannot track the actual delta precisely is because
/// consensus signals the StateManager asynchronously when it can drop older
/// states and hence the signal is handled in a non-deterministic fashion by
/// different nodes on the subnet.
///
/// We know that after MR has processed a batch with
/// "requires_full_state_hash" set, consensus will eventually deliver a
/// signal to the StateManager to drop states below that batches' height and
/// this signal will be sent at the latest before consensus sends another
/// batch with "requires_full_state_hash" set.
///
/// We also use this field to limit further execution in the scheduler when
/// the canisters have produced more delta than the subnet can handle given
/// the hardware specs of the subnet. The scheduler's configuration contains
/// relevant settings for the maximum delta capacity of the subnet.
///
/// Therefore, if we reset this field to 0 in MR when processing a batch
/// with "requires_full_state_hash" set after the canisters have executed
/// then the actual total for all the deltas that we are maintaining should
/// always be <= this field + (the maximum delta capacity of the subnet /
/// 2).
pub heap_delta_estimate: NumBytes,
pub subnet_metrics: SubnetMetrics,
/// The set of WASM modules we expect to be present in the [`Hypervisor`]'s
/// compilation cache. This allows us to deterministically decide when we
/// expect a compilation to be fast and ignore the compilation cost when
/// considering the round instruction limit.
///
/// Each time a canister is installed, its WASM is inserted and the set is
/// cleared at each checkpoint.
pub expected_compiled_wasms: BTreeSet<WasmHash>,
/// Responses to `BitcoinGetSuccessors` can be larger than the max inter-canister
/// response limit. To work around this limitation, large responses are paginated
/// and are stored here temporarily until they're fetched by the calling canister.
pub bitcoin_get_successors_follow_up_responses: BTreeMap<CanisterId, Vec<BlockBlob>>,
}
/// Full description of the IC network toplogy.
///
/// Contains [`Arc`] references, so it is only safe to serialize for read-only
/// use.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct NetworkTopology {
pub subnets: BTreeMap<SubnetId, SubnetTopology>,
#[serde(serialize_with = "ic_utils::serde_arc::serialize_arc")]
#[serde(deserialize_with = "ic_utils::serde_arc::deserialize_arc")]
pub routing_table: Arc<RoutingTable>,
#[serde(serialize_with = "ic_utils::serde_arc::serialize_arc")]
#[serde(deserialize_with = "ic_utils::serde_arc::deserialize_arc")]
pub canister_migrations: Arc<CanisterMigrations>,
pub nns_subnet_id: SubnetId,
/// Mapping from ECDSA key_id to a list of subnets which can sign with the
/// given key. Keys without any signing subnets are not included in the map.
pub ecdsa_signing_subnets: BTreeMap<EcdsaKeyId, Vec<SubnetId>>,
/// The ID of the canister to forward bitcoin testnet requests to.
pub bitcoin_testnet_canister_id: Option<CanisterId>,
/// The ID of the canister to forward bitcoin mainnet requests to.
pub bitcoin_mainnet_canister_id: Option<CanisterId>,
}
impl Default for NetworkTopology {
fn default() -> Self {
Self {
subnets: Default::default(),
routing_table: Default::default(),
canister_migrations: Default::default(),
nns_subnet_id: SubnetId::new(PrincipalId::new_anonymous()),
ecdsa_signing_subnets: Default::default(),
bitcoin_testnet_canister_id: None,
bitcoin_mainnet_canister_id: None,
}
}
}
impl NetworkTopology {
/// Returns a list of subnets where the bitcoin testnet feature is enabled.
pub fn bitcoin_testnet_subnets(&self) -> Vec<SubnetId> {
self.subnets
.iter()
.filter(|(_, subnet_topology)| {
subnet_topology.subnet_features.bitcoin()
== BitcoinFeature {
network: BitcoinNetwork::Testnet,
status: BitcoinFeatureStatus::Enabled,
}
})
.map(|(subnet_id, _)| *subnet_id)
.collect()
}
/// Returns a list of subnets where the ecdsa feature is enabled.
pub fn ecdsa_signing_subnets(&self, key_id: &EcdsaKeyId) -> &[SubnetId] {
self.ecdsa_signing_subnets
.get(key_id)
.map(|ids| &ids[..])
.unwrap_or(&[])
}
/// Returns the size of the given subnet.
pub fn get_subnet_size(&self, subnet_id: &SubnetId) -> Option<usize> {
self.subnets
.get(subnet_id)
.map(|subnet_topology| subnet_topology.nodes.len())
}
}
impl From<&NetworkTopology> for pb_metadata::NetworkTopology {
fn from(item: &NetworkTopology) -> Self {
Self {
subnets: item
.subnets
.iter()
.map(|(subnet_id, subnet_topology)| pb_metadata::SubnetsEntry {
subnet_id: Some(subnet_id_into_protobuf(*subnet_id)),
subnet_topology: Some(subnet_topology.into()),
})
.collect(),
routing_table: Some(item.routing_table.as_ref().into()),
nns_subnet_id: Some(subnet_id_into_protobuf(item.nns_subnet_id)),
canister_migrations: Some(item.canister_migrations.as_ref().into()),
ecdsa_signing_subnets: item
.ecdsa_signing_subnets
.iter()
.map(|(key_id, subnet_ids)| {
let subnet_ids = subnet_ids
.iter()
.map(|id| subnet_id_into_protobuf(*id))
.collect();
pb_metadata::EcdsaKeyEntry {
key_id: Some(key_id.into()),
subnet_ids,
}
})
.collect(),
bitcoin_testnet_canister_ids: match item.bitcoin_testnet_canister_id {
Some(c) => vec![pb_types::CanisterId::from(c)],
None => vec![],
},
bitcoin_mainnet_canister_ids: match item.bitcoin_mainnet_canister_id {
Some(c) => vec![pb_types::CanisterId::from(c)],
None => vec![],
},
}
}
}
impl TryFrom<pb_metadata::NetworkTopology> for NetworkTopology {
type Error = ProxyDecodeError;
fn try_from(item: pb_metadata::NetworkTopology) -> Result<Self, Self::Error> {
let mut subnets = BTreeMap::new();
for entry in item.subnets {
subnets.insert(
subnet_id_try_from_protobuf(try_from_option_field(
entry.subnet_id,
"NetworkTopology::subnets::K",
)?)?,
try_from_option_field(entry.subnet_topology, "NetworkTopology::subnets::V")?,
);
}
// NetworkTopology.nns_subnet_id will be removed in the following PR
// Currently, initialise nns_subnet_id with dummy value in case not found
let nns_subnet_id =
match try_from_option_field(item.nns_subnet_id, "NetworkTopology::nns_subnet_id") {
Ok(subnet_id) => subnet_id_try_from_protobuf(subnet_id)?,
Err(_) => SubnetId::new(PrincipalId::new_anonymous()),
};
let mut ecdsa_signing_subnets = BTreeMap::new();
for entry in item.ecdsa_signing_subnets {
let mut subnet_ids = vec![];
for subnet_id in entry.subnet_ids {
subnet_ids.push(subnet_id_try_from_protobuf(subnet_id)?);
}
ecdsa_signing_subnets.insert(
try_from_option_field(entry.key_id, "EcdsaKeyEntry::key_id")?,
subnet_ids,
);
}
let bitcoin_testnet_canister_id = match item.bitcoin_testnet_canister_ids.first() {
Some(canister) => Some(CanisterId::try_from(canister.clone())?),
None => None,
};
let bitcoin_mainnet_canister_id = match item.bitcoin_mainnet_canister_ids.first() {
Some(canister) => Some(CanisterId::try_from(canister.clone())?),
None => None,
};
Ok(Self {
subnets,
routing_table: try_from_option_field(
item.routing_table,
"NetworkTopology::routing_table",
)
.map(Arc::new)?,
// `None` value needs to be allowed here because all the existing states don't have this field yet.
canister_migrations: item
.canister_migrations
.map(CanisterMigrations::try_from)
.transpose()?
.unwrap_or_default()
.into(),
nns_subnet_id,
ecdsa_signing_subnets,
bitcoin_testnet_canister_id,
bitcoin_mainnet_canister_id,
})
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct SubnetTopology {
/// The public key of the subnet (a DER-encoded BLS key, see
/// https://sdk.dfinity.org/docs/interface-spec/index.html#certification)
pub public_key: Vec<u8>,
pub nodes: BTreeMap<NodeId, NodeTopology>,
pub subnet_type: SubnetType,
pub subnet_features: SubnetFeatures,
/// ECDSA keys held by this subnet. Just because a subnet holds an ECDSA key
/// doesn't mean the subnet has been enabled to sign with that key. This
/// will happen when a key is shared with a second subnet which holds it as
/// a backup. An additional NNS proposal will be needed to allow the subnet
/// holding the key as backup to actually produce signatures.
pub ecdsa_keys_held: BTreeSet<EcdsaKeyId>,
}
impl From<&SubnetTopology> for pb_metadata::SubnetTopology {
fn from(item: &SubnetTopology) -> Self {
Self {
public_key: item.public_key.clone(),
nodes: item
.nodes
.iter()
.map(|(node_id, node_toplogy)| pb_metadata::SubnetTopologyEntry {
node_id: Some(node_id_into_protobuf(*node_id)),
node_topology: Some(node_toplogy.into()),
})
.collect(),
subnet_type: i32::from(item.subnet_type),
subnet_features: Some(pb_subnet::SubnetFeatures::from(item.subnet_features)),
ecdsa_keys_held: item.ecdsa_keys_held.iter().map(|k| k.into()).collect(),
}
}
}
impl TryFrom<pb_metadata::SubnetTopology> for SubnetTopology {
type Error = ProxyDecodeError;
fn try_from(item: pb_metadata::SubnetTopology) -> Result<Self, Self::Error> {
let mut nodes = BTreeMap::<NodeId, NodeTopology>::new();
for entry in item.nodes {
nodes.insert(
node_id_try_from_protobuf(try_from_option_field(
entry.node_id,
"SubnetTopology::nodes::K",
)?)?,
try_from_option_field(entry.node_topology, "SubnetTopology::nodes::V")?,
);
}
let mut ecdsa_keys_held = BTreeSet::new();
for key in item.ecdsa_keys_held {
ecdsa_keys_held.insert(EcdsaKeyId::try_from(key)?);
}
Ok(Self {
public_key: item.public_key,
nodes,
// It is fine to use an arbitrary value here. We always reset the
// field before we actually use it. We pick the value of least
// privilege just to be sure.
subnet_type: SubnetType::try_from(item.subnet_type).unwrap_or(SubnetType::Application),
subnet_features: item
.subnet_features
.map(SubnetFeatures::from)
.unwrap_or_default(),
ecdsa_keys_held,
})
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct NodeTopology {
pub ip_address: String,
pub http_port: u16,
}
impl From<&NodeTopology> for pb_metadata::NodeTopology {
fn from(item: &NodeTopology) -> Self {
Self {
ip_address: item.ip_address.clone(),
http_port: item.http_port as u32,
}
}
}
impl TryFrom<pb_metadata::NodeTopology> for NodeTopology {
type Error = ProxyDecodeError;
fn try_from(item: pb_metadata::NodeTopology) -> Result<Self, Self::Error> {
Ok(Self {
ip_address: item.ip_address,
http_port: item.http_port as u16,
})
}
}
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct SubnetMetrics {
pub consumed_cycles_by_deleted_canisters: NominalCycles,
pub consumed_cycles_http_outcalls: NominalCycles,
pub consumed_cycles_ecdsa_outcalls: NominalCycles,
pub ecdsa_signature_agreements: u64,
}
impl From<&SubnetMetrics> for pb_metadata::SubnetMetrics {
fn from(item: &SubnetMetrics) -> Self {
Self {
consumed_cycles_by_deleted_canisters: Some(
(&item.consumed_cycles_by_deleted_canisters).into(),
),
consumed_cycles_http_outcalls: Some((&item.consumed_cycles_http_outcalls).into()),
consumed_cycles_ecdsa_outcalls: Some((&item.consumed_cycles_ecdsa_outcalls).into()),
ecdsa_signature_agreements: Some(item.ecdsa_signature_agreements),
}
}
}
impl TryFrom<pb_metadata::SubnetMetrics> for SubnetMetrics {
type Error = ProxyDecodeError;
fn try_from(item: pb_metadata::SubnetMetrics) -> Result<Self, Self::Error> {
Ok(Self {
consumed_cycles_by_deleted_canisters: try_from_option_field(
item.consumed_cycles_by_deleted_canisters,
"SubnetMetrics::consumed_cycles_by_deleted_canisters",
)?,
consumed_cycles_http_outcalls: try_from_option_field(
item.consumed_cycles_http_outcalls,
"SubnetMetrics::consumed_cycles_http_outcalls",
)
.unwrap_or_else(|_| NominalCycles::from(0_u128)),
consumed_cycles_ecdsa_outcalls: try_from_option_field(
item.consumed_cycles_ecdsa_outcalls,
"SubnetMetrics::consumed_cycles_ecdsa_outcalls",
)
.unwrap_or_else(|_| NominalCycles::from(0_u128)),
ecdsa_signature_agreements: item.ecdsa_signature_agreements.unwrap_or_default(),
})
}
}
impl From<&SystemMetadata> for pb_metadata::SystemMetadata {
fn from(item: &SystemMetadata) -> Self {
// We do not store the subnet type when we serialize SystemMetadata. We rely on
// `load_checkpoint()` to properly set this value.
Self {
own_subnet_id: Some(subnet_id_into_protobuf(item.own_subnet_id)),
canister_allocation_ranges: Some(item.canister_allocation_ranges.clone().into()),
last_generated_canister_id: item.last_generated_canister_id.map(Into::into),
prev_state_hash: item
.prev_state_hash
.clone()
.map(|prev_hash| prev_hash.get().0),
batch_time_nanos: item.batch_time.as_nanos_since_unix_epoch(),
ingress_history: Some((&item.ingress_history).into()),
streams: item
.streams
.iter()
.map(|(subnet_id, stream)| pb_queues::StreamEntry {
subnet_id: Some(subnet_id_into_protobuf(*subnet_id)),
subnet_stream: Some(stream.into()),
})
.collect(),
network_topology: Some((&item.network_topology).into()),
subnet_call_context_manager: Some((&item.subnet_call_context_manager).into()),
state_sync_version: item.state_sync_version,
certification_version: item.certification_version as u32,
heap_delta_estimate: item.heap_delta_estimate.get(),
own_subnet_features: Some(item.own_subnet_features.into()),
subnet_metrics: Some((&item.subnet_metrics).into()),
bitcoin_get_successors_follow_up_responses: item
.bitcoin_get_successors_follow_up_responses
.clone()
.into_iter()
.map(
|(sender, payloads)| pb_metadata::BitcoinGetSuccessorsFollowUpResponses {
sender: Some(sender.into()),
payloads,
},
)
.collect(),
}
}
}
impl TryFrom<pb_metadata::SystemMetadata> for SystemMetadata {
type Error = ProxyDecodeError;
fn try_from(item: pb_metadata::SystemMetadata) -> Result<Self, Self::Error> {
let mut streams = BTreeMap::<SubnetId, Stream>::new();
for entry in item.streams {
streams.insert(
subnet_id_try_from_protobuf(try_from_option_field(
entry.subnet_id,
"SystemMetadata::streams::K",
)?)?,
try_from_option_field(entry.subnet_stream, "SystemMetadata::streams::V")?,
);
}
let certification_version = item.certification_version;
let canister_allocation_ranges: CanisterIdRanges = match item.canister_allocation_ranges {
Some(canister_allocation_ranges) => canister_allocation_ranges.try_into()?,
None => Default::default(),
};
let last_generated_canister_id = item
.last_generated_canister_id
.map(TryInto::try_into)
.transpose()?;
// Validate that `last_generated_canister_id` (if not `None`) is within the
// first `canister_allocation_ranges` range.
if let Some(last_generated_canister_id) = last_generated_canister_id {
match canister_allocation_ranges.iter().next() {
Some(first_allocation_range)
if first_allocation_range.contains(&last_generated_canister_id) => {}
_ => return Err(ProxyDecodeError::Other(format!(
"SystemMetadata::last_generated_canister_id ({}) not in the first SystemMetadata::canister_allocation_ranges range ({:?})",
last_generated_canister_id, canister_allocation_ranges
))),
}
}
let mut bitcoin_get_successors_follow_up_responses = BTreeMap::new();
for response in item.bitcoin_get_successors_follow_up_responses {
let sender_pb: pb_types::CanisterId = try_from_option_field(
response.sender,
"BitcoinGetSuccessorsFollowUpResponses::sender",
)?;
let sender = CanisterId::try_from(sender_pb)?;
bitcoin_get_successors_follow_up_responses.insert(sender, response.payloads);
}
let batch_time = Time::from_nanos_since_unix_epoch(item.batch_time_nanos);
Ok(Self {
own_subnet_id: subnet_id_try_from_protobuf(try_from_option_field(
item.own_subnet_id,
"SystemMetadata::own_subnet_id",
)?)?,
// WARNING! Setting to the default value which can be incorrect. We do not store the
// actual value when we serialize SystemMetadata. We rely on `load_checkpoint()` to
// properly set this value.
own_subnet_type: SubnetType::default(),
own_subnet_features: item.own_subnet_features.unwrap_or_default().into(),
canister_allocation_ranges,
last_generated_canister_id,
prev_state_hash: item.prev_state_hash.map(|b| CryptoHash(b).into()),
batch_time,
ingress_history: try_from_option_field(
item.ingress_history,
"SystemMetadata::ingress_history",
)?,
streams: Arc::new(Streams {
responses_size_bytes: Streams::calculate_stats(&streams),
streams,
}),
network_topology: try_from_option_field(
item.network_topology,
"SystemMetadata::network_topology",
)?,
state_sync_version: item.state_sync_version,
certification_version: item.certification_version.try_into().map_err(|_| {
ProxyDecodeError::UnknownCertificationVersion(certification_version)
})?,
subnet_call_context_manager: match item.subnet_call_context_manager {
Some(manager) => SubnetCallContextManager::try_from((batch_time, manager))?,
None => Default::default(),
},
heap_delta_estimate: NumBytes::from(item.heap_delta_estimate),
subnet_metrics: match item.subnet_metrics {
Some(subnet_metrics) => subnet_metrics.try_into()?,
None => SubnetMetrics::default(),
},
expected_compiled_wasms: BTreeSet::new(),
bitcoin_get_successors_follow_up_responses,
})
}
}
impl SystemMetadata {
/// Creates a new empty system metadata state.
pub fn new(own_subnet_id: SubnetId, own_subnet_type: SubnetType) -> Self {
Self {
own_subnet_id,
own_subnet_type,
ingress_history: Default::default(),
streams: Default::default(),
canister_allocation_ranges: Default::default(),
last_generated_canister_id: None,
batch_time: UNIX_EPOCH,
network_topology: Default::default(),
subnet_call_context_manager: Default::default(),
own_subnet_features: SubnetFeatures::default(),
// StateManager populates proper values of these fields before
// committing each state.
prev_state_hash: Default::default(),
state_sync_version: 0,
// NB. State manager relies on the root hash of the hash tree
// corresponding to the initial state to be a constant. Thus we fix
// the certification version that we use for the initial state. If
// we used CURRENT_CERTIFICATION_VERSION here, the state hash would
// NOT guaranteed to be constant, potentially leading to
// hard-to-track bugs in state manager.
certification_version: CertificationVersion::V0,
heap_delta_estimate: NumBytes::from(0),
subnet_metrics: Default::default(),
expected_compiled_wasms: BTreeSet::new(),
bitcoin_get_successors_follow_up_responses: BTreeMap::default(),
}
}
pub fn time(&self) -> Time {
self.batch_time
}
/// Returns a reference to the streams.
pub fn streams(&self) -> &Streams {
&self.streams
}
/// One-off initialization: populate `canister_allocation_ranges` with the only
/// `[N * 2^20, (N+1) * 2^20 - 1]` range fully hosted by the subnet as per the
/// routing table; and initialize `last_generated_canister_id` based on
/// `generated_id_counter`.
///
/// This is done under the assumption that the registry always assigns exactly
/// 2^20 canister IDs to every newly created subnet (and at this point in time
/// no canisters have yet been migrated).
///
/// Canister ID allocation range assignment will be made explicit in a follow-up
/// change.
///
/// Returns `Ok` if `canister_allocation_ranges` is not empty (whether it was
/// populated by this call or not); `Err` if empty (and the subnet is unable
/// to generate new canister IDs).
pub fn init_allocation_ranges_if_empty(&mut self) -> Result<(), String> {
if !self.canister_allocation_ranges.is_empty() {
return Ok(());
}
let routing_table_ranges = self
.network_topology
.routing_table
.ranges(self.own_subnet_id);
for range in routing_table_ranges.iter().rev() {
let start = canister_id_into_u64(range.start);
let end = canister_id_into_u64(range.end);
if start % CANISTER_IDS_PER_SUBNET == 0 && end == start + CANISTER_IDS_PER_SUBNET - 1 {
// Found the `[N * 2^20, (N+1) * 2^20 - 1]` (sub)range, use it as allocation
// range.
//
// Unwrapping is safe because the only reason why we would fail to convert is if
// we provided set of ranges that was not well formed. This is not the case
// here, as we are creating a `CanisterIdRanges` out of one non-empty range.
self.canister_allocation_ranges = vec![*range].try_into().unwrap();
break;
}
}
if self.canister_allocation_ranges.is_empty() {
return Err("No range of length CANISTER_IDS_PER_SUBNET in routing table".into());
}
Ok(())
}
/// Generates a new canister ID.
///
/// If a canister ID from a second canister allocation range is generated, the
/// first range is dropped. The last canister allocation range is never dropped.
///
/// Returns `Err` iff no more canister IDs can be generated.
pub fn generate_new_canister_id(&mut self) -> Result<CanisterId, String> {
// Start off with
// (canister_allocation_ranges
// ∩ routing_table.ranges(own_subnet_id))
// \ canister_migrations.ranges()
let own_subnet_ranges = self
.network_topology
.routing_table
.ranges(self.own_subnet_id);
let canister_allocation_ranges = intersection(
self.canister_allocation_ranges.iter(),
own_subnet_ranges.iter(),
)
.map_err(|err| {
format!(
"intersection({:?}, {:?}) is not well formed: {:?}",
self.canister_allocation_ranges, own_subnet_ranges, err
)
})?;
let canister_allocation_ranges = difference(
canister_allocation_ranges.iter(),
self.network_topology.canister_migrations.ranges(),
)
.map_err(|err| {
format!(
"difference({:?}, {:?}) is not well formed: {:?}",
canister_allocation_ranges, self.network_topology.canister_migrations, err
)
})?;
let res = canister_allocation_ranges.generate_canister_id(self.last_generated_canister_id);
if let Some(res) = &res {
self.last_generated_canister_id = Some(*res);
while self.canister_allocation_ranges.len() > 1
&& !self
.canister_allocation_ranges
.iter()
.next()
.unwrap()
.contains(res)
{
// Drop the first canister allocation range iff consumed and more allocation
// ranges are available.
self.canister_allocation_ranges.drop_first();
}
}
res.ok_or_else(|| "Canister ID allocation was consumed".into())
}
/// Returns the number of canister IDs that can still be generated.
pub fn available_canister_ids(&self) -> u64 {
let generated_canister_ids = match (
self.canister_allocation_ranges.start(),
self.last_generated_canister_id,
) {
(Some(start), Some(last)) => {
canister_id_into_u64(last) + 1 - canister_id_into_u64(start)
}
_ => 0,
};
self.canister_allocation_ranges.total_count() as u64 - generated_canister_ids
}
}
/// Stream is the state of bi-directional communication session with a remote
/// subnet. It contains outgoing messages having that subnet as their
/// destination and signals for inducted messages received from that subnet.
///
/// Conceptually we use a gap-free queue containing one signal for each inducted
/// message; but because most signals are `Accept` we represent that queue as a
/// combination of `signals_end` (pointing just beyond the last signal) plus a
/// collection of `reject_signals`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Stream {
/// Indexed queue of outgoing messages.
messages: StreamIndexedQueue<RequestOrResponse>,
/// Index of the next expected reverse stream message.
///
/// Conceptually we use a gap-free queue containing one signal for each
/// inducted message; but because these signals are all "Accept" (as we
/// generate responses when rejecting messages), that queue can be safely
/// represented by its end index (pointing just beyond the last signal).
signals_end: StreamIndex,
/// Stream indices of rejected messages, in ascending order.
reject_signals: VecDeque<StreamIndex>,
/// Estimated byte size of `self.messages`.
messages_size_bytes: usize,
}
impl Default for Stream {
fn default() -> Self {
let messages = Default::default();
let signals_end = Default::default();
let reject_signals = VecDeque::default();
let messages_size_bytes = Self::size_bytes(&messages);
Self {
messages,
signals_end,
reject_signals,
messages_size_bytes,
}
}
}
impl From<&Stream> for pb_queues::Stream {
fn from(item: &Stream) -> Self {
let reject_signals = item.reject_signals.iter().map(|i| i.get()).collect();
Self {
messages_begin: item.messages.begin().get(),
messages: item
.messages
.iter()
.map(|(_, req_or_resp)| req_or_resp.into())
.collect(),
signals_end: item.signals_end.get(),
reject_signals,
}
}
}
impl TryFrom<pb_queues::Stream> for Stream {
type Error = ProxyDecodeError;
fn try_from(item: pb_queues::Stream) -> Result<Self, Self::Error> {
let mut messages = StreamIndexedQueue::with_begin(item.messages_begin.into());
for req_or_resp in item.messages {
messages.push(req_or_resp.try_into()?);
}
let messages_size_bytes = Self::size_bytes(&messages);
let reject_signals = item
.reject_signals
.iter()
.map(|i| StreamIndex::new(*i))
.collect();
Ok(Self {
messages,
signals_end: item.signals_end.into(),
reject_signals,
messages_size_bytes,
})
}
}
impl Stream {
/// Creates a new `Stream` with the given `messages` and `signals_end`.
pub fn new(messages: StreamIndexedQueue<RequestOrResponse>, signals_end: StreamIndex) -> Self {
let messages_size_bytes = Self::size_bytes(&messages);
Self {
messages,
signals_end,
reject_signals: VecDeque::new(),
messages_size_bytes,
}
}
/// Creates a new `Stream` with the given `messages` and `signals_end`.
pub fn with_signals(
messages: StreamIndexedQueue<RequestOrResponse>,
signals_end: StreamIndex,
reject_signals: VecDeque<StreamIndex>,
) -> Self {
let messages_size_bytes = Self::size_bytes(&messages);
Self {
messages,
signals_end,
reject_signals,
messages_size_bytes,
}
}
/// Creates a slice starting from index `from` and containing at most
/// `count` messages from this stream.
pub fn slice(&self, from: StreamIndex, count: Option<usize>) -> StreamSlice {
let messages = self.messages.slice(from, count);
StreamSlice::new(self.header(), messages)
}
/// Creates a header for this stream.
pub fn header(&self) -> StreamHeader {
StreamHeader {
begin: self.messages.begin(),
end: self.messages.end(),
signals_end: self.signals_end,
reject_signals: self.reject_signals.clone(),
}
}
/// Returns a reference to the message queue.
pub fn messages(&self) -> &StreamIndexedQueue<RequestOrResponse> {
&self.messages
}
/// Returns the stream's begin index.
pub fn messages_begin(&self) -> StreamIndex {
self.messages.begin()
}
/// Returns the stream's end index.
pub fn messages_end(&self) -> StreamIndex {
self.messages.end()
}
/// Appends the given message to the tail of the stream.
pub fn push(&mut self, message: RequestOrResponse) {
self.messages_size_bytes += message.count_bytes();
self.messages.push(message);
debug_assert_eq!(Self::size_bytes(&self.messages), self.messages_size_bytes);
}
/// Garbage collects messages before `new_begin`, collecting and returning all
/// messages for which a reject signal was received.
pub fn discard_messages_before(
&mut self,
new_begin: StreamIndex,
reject_signals: &VecDeque<StreamIndex>,
) -> Vec<RequestOrResponse> {
assert!(
new_begin >= self.messages.begin(),
"Begin index ({}) has already advanced past requested begin index ({})",
self.messages.begin(),
new_begin
);
assert!(
new_begin <= self.messages.end(),
"Cannot advance begin index ({}) beyond end index ({})",
new_begin,
self.messages.end()
);
// Skip any reject signals before `self.messages.begin()`.
//
// This may happen legitimately if the remote subnet has not yet GC-ed a signal
// because it has not yet seen our `messages.begin()` advance past it.
let messages_begin = self.messages.begin();
let mut reject_signals = reject_signals
.iter()
.skip_while(|&reject_signal| reject_signal < &messages_begin);
let mut next_reject_signal = reject_signals.next().unwrap_or(&new_begin);
// Garbage collect all messages up to `new_begin`.
let mut rejected_messages = Vec::new();
while self.messages.begin() < new_begin {
let (index, msg) = self.messages.pop().unwrap();
// Deduct every discarded message from the stream's byte size.
self.messages_size_bytes -= msg.count_bytes();
debug_assert_eq!(Self::size_bytes(&self.messages), self.messages_size_bytes);
// If we received a reject signal for this message, collect it in
// `rejected_messages`.
if next_reject_signal == &index {
rejected_messages.push(msg);
next_reject_signal = reject_signals.next().unwrap_or(&new_begin);
}
}
rejected_messages
}
/// Garbage collects signals before `new_signals_begin`.
pub fn discard_signals_before(&mut self, new_signals_begin: StreamIndex) {
while let Some(signal_index) = self.reject_signals.front() {
if *signal_index < new_signals_begin {
self.reject_signals.pop_front();
} else {
break;
}
}
}
/// Returns a reference to the reject signals.
pub fn reject_signals(&self) -> &VecDeque<StreamIndex> {
&self.reject_signals
}
/// Returns the index just beyond the last sent signal.
pub fn signals_end(&self) -> StreamIndex {
self.signals_end
}
/// Increments the index of the last sent signal.
pub fn increment_signals_end(&mut self) {
self.signals_end.inc_assign()
}
/// Appends the given reject signal to the tail of the reject signals.
pub fn push_reject_signal(&mut self, index: StreamIndex) {
assert_eq!(index, self.signals_end);
if let Some(&last_signal) = self.reject_signals.back() {
assert!(
last_signal < index,
"The signal to be pushed ({}) should be larger than the last signal ({})",
index,
last_signal
);
}
self.reject_signals.push_back(index)
}
/// Calculates the estimated byte size of the given messages.
fn size_bytes(messages: &StreamIndexedQueue<RequestOrResponse>) -> usize {
messages.iter().map(|(_, m)| m.count_bytes()).sum()