-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathregistry.rs
More file actions
1143 lines (1049 loc) · 45.6 KB
/
registry.rs
File metadata and controls
1143 lines (1049 loc) · 45.6 KB
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
use crate::ctx::DreContext;
use crate::{auth::AuthRequirement, exe::ExecutableCommand, exe::args::GlobalArgs};
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
use clap::Args;
use ic_canisters::IcAgentCanisterClient;
use ic_canisters::governance::GovernanceCanisterWrapper;
use ic_management_backend::{health::HealthStatusQuerier, lazy_registry::LazyRegistry};
use ic_management_types::{HealthStatus, Network, Node, Operator};
use ic_protobuf::registry::node::v1::NodeRewardType;
use ic_protobuf::registry::{
dc::v1::DataCenterRecord,
hostos_version::v1::HostosVersionRecord,
node::v1::{ConnectionEndpoint, IPv4InterfaceConfig},
replica_version::v1::ReplicaVersionRecord,
subnet::v1::{ChainKeyConfig, SubnetFeatures},
unassigned_nodes_config::v1::UnassignedNodesConfigRecord,
};
use ic_registry_subnet_type::SubnetType;
use ic_types::PrincipalId;
use icp_ledger::AccountIdentifier;
use indexmap::IndexMap;
use itertools::Itertools;
use log::{info, warn};
use prost::Message;
use regex::Regex;
use serde::Serialize;
use serde_json::Value;
use std::cmp::max;
use std::iter::{IntoIterator, Iterator};
use std::{
collections::{BTreeMap, HashMap, HashSet},
net::Ipv6Addr,
path::PathBuf,
str::FromStr,
sync::Arc,
};
#[derive(Args, Debug)]
#[clap(after_help = r#"EXAMPLES:
dre registry # Dump all contents to stdout
dre registry --filter rewards_correct!=true # Entries for which rewardable_nodes != total_up_nodes
dre registry --filter "node_type=type1" # Entries where node_type == "type1"
dre registry -o registry.json --filter "subnet_id startswith tdb26" # Write to file and filter by subnet_id
dre registry -o registry.json --filter "node_id contains h5zep" # Write to file and filter by node_id
Registry versions/records dump:
dre registry --dump-versions # Dump ALL versions (Python slicing, end-exclusive)
dre registry --dump-versions 0 # Same as above (from start to end)
dre registry --dump-versions -5 # Last 5 versions (from -5 to end)
dre registry --dump-versions -5 -1 # Last 4 versions (excludes last)
Indexing semantics (important!):
- Python slicing with end-exclusive semantics
- Positive indices are 1-based positions (since registry records are 1-based)
- 0 means start (same as omitting FROM)
- Negative indices count from the end (-1 is last)
- Reversed ranges yield empty results
Notes:
- Values are best-effort decoded by key; unknown bytes are shown as {"bytes_base64": "..."}.
- For known protobuf records, embedded byte arrays are compacted to base64 strings for readability.
"#)]
pub struct Registry {
/// Output file (default is stdout)
#[clap(short = 'o', long)]
pub output: Option<PathBuf>,
/// Filters in `key=value` format
#[clap(long, short, alias = "filter")]
pub filters: Vec<Filter>,
/// Specify the height for the registry
#[clap(long, visible_aliases = ["registry-height", "version"])]
pub height: Option<u64>,
/// Dump raw registry versions in range by index; defaults to 0 -1 if no args
#[clap(
long = "dump-versions",
num_args(0..=2),
value_names = ["FROM", "TO"],
allow_hyphen_values = true,
conflicts_with_all = ["filters"],
visible_aliases = ["dump-version", "dump-versions-json", "dump-json-versions", "json-versions"],
help = "Index-based range over available versions. Negative indexes allowed (Python-style). If omitted, defaults to 0 -1."
)]
pub dump_versions: Option<Vec<i64>>,
}
#[derive(Debug, Clone)]
pub struct Filter {
key: String,
value: Value,
comparison: Comparison,
}
impl FromStr for Filter {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
// Define the regex pattern for `key comparison value` with optional spaces
let re = Regex::new(r"^\s*(\w+)\s*\b(.+?)\b\s*(.*)$").unwrap();
// Capture key, comparison, and value
if let Some(captures) = re.captures(s) {
let key = captures[1].to_string();
let comparison_str = &captures[2];
let value_str = &captures[3];
let comparison = Comparison::from_str(comparison_str)?;
let value = serde_json::from_str(value_str).unwrap_or_else(|_| serde_json::Value::String(value_str.to_string()));
Ok(Self { key, value, comparison })
} else {
anyhow::bail!(
"Expected format: `key comparison value` (spaces around the comparison are optional, supported comparison: = != > < >= <= re contains startswith endswith), found {}",
s
);
}
}
}
impl ExecutableCommand for Registry {
fn require_auth(&self) -> AuthRequirement {
AuthRequirement::Anonymous
}
async fn execute(&self, ctx: DreContext) -> anyhow::Result<()> {
let writer: Box<dyn std::io::Write> = match &self.output {
Some(path) => {
let path = path.with_extension("json");
let file = fs_err::File::create(path)?;
info!("Writing to file: {:?}", file.path().canonicalize()?);
Box::new(std::io::BufWriter::new(file))
}
None => Box::new(std::io::stdout()),
};
// Versions/records mode
if self.dump_versions.is_some() {
let json_value = self.dump_versions_json(ctx).await?;
serde_json::to_writer_pretty(writer, &json_value)?;
return Ok(());
}
// Friendly aggregated registry view (default)
let registry = self.get_registry(ctx).await?;
let mut serde_value = serde_json::to_value(registry)?;
self.filters.iter().for_each(|filter| {
let _ = filter_json_value(&mut serde_value, &filter.key, &filter.value, &filter.comparison);
});
serde_json::to_writer_pretty(writer, &serde_value)?;
Ok(())
}
fn validate(&self, _args: &GlobalArgs, _cmd: &mut clap::Command) {}
}
impl Registry {
pub(crate) async fn dump_versions_json(&self, ctx: DreContext) -> anyhow::Result<serde_json::Value> {
use ic_registry_common_proto::pb::local_store::v1::ChangelogEntry as PbChangelogEntry;
// Ensure local registry is initialized/synced to have content available
// (no-op in offline mode)
let _ = ctx.registry_with_version(None).await;
let base_dirs = local_registry_dirs_for_ctx(&ctx)?;
// Walk all .pb files recursively across candidates; stop at first non-empty
let entries = load_first_available_entries(&base_dirs)?;
// Apply version filters
let mut entries_sorted = entries;
entries_sorted.sort_by_key(|(v, _)| *v);
let versions_sorted: Vec<u64> = entries_sorted.iter().map(|(v, _)| *v).collect();
// Select versions
let selected_versions = select_versions(self.dump_versions.clone(), &versions_sorted)?;
// Build flat list of records
let entries_map: std::collections::HashMap<u64, PbChangelogEntry> = entries_sorted.into_iter().collect();
let out = flatten_version_records(&selected_versions, &entries_map);
Ok(serde_json::to_value(out)?)
}
}
// Helper: collect candidate base dirs
fn local_registry_dirs_for_ctx(ctx: &DreContext) -> anyhow::Result<Vec<std::path::PathBuf>> {
let mut base_dirs = vec![
dirs::cache_dir()
.ok_or_else(|| anyhow::anyhow!("Couldn't find cache dir for dre-store"))?
.join("dre-store")
.join("local_registry")
.join(&ctx.network().name),
];
if let Ok(override_dir) = std::env::var("DRE_LOCAL_REGISTRY_DIR_OVERRIDE") {
base_dirs.insert(0, std::path::PathBuf::from(override_dir));
}
base_dirs.push(std::path::PathBuf::from("/tmp/dre-test-store/local_registry").join(&ctx.network().name));
Ok(base_dirs)
}
fn load_first_available_entries(
base_dirs: &[std::path::PathBuf],
) -> anyhow::Result<Vec<(u64, ic_registry_common_proto::pb::local_store::v1::ChangelogEntry)>> {
use ic_registry_common_proto::pb::local_store::v1::ChangelogEntry as PbChangelogEntry;
use std::ffi::OsStr;
let mut entries: Vec<(u64, PbChangelogEntry)> = Vec::new();
for base_dir in base_dirs.iter() {
let mut local: Vec<(u64, PbChangelogEntry)> = Vec::new();
collect_pb_files(base_dir, &mut |path| {
if path.extension() == Some(OsStr::new("pb")) {
if let Some(v) = extract_version_from_registry_path(base_dir, path) {
let bytes = std::fs::read(path).unwrap_or_else(|_| panic!("Failed reading {}", path.display()));
let entry = PbChangelogEntry::decode(bytes.as_slice()).unwrap_or_else(|_| panic!("Failed decoding {}", path.display()));
local.push((v, entry));
}
}
})?;
if !local.is_empty() {
entries = local;
break;
}
}
if entries.is_empty() {
anyhow::bail!("No registry versions found in local store");
}
Ok(entries)
}
// Slicing semantics:
// - Python-like, end-exclusive
// - Positive indices are 1-based positions (since registry records are 1-based)
// - 0 means start (same as omitting FROM)
// - Negative indices are from the end (-1 is last)
// - Reversed ranges yield empty results
fn select_versions(versions: Option<Vec<i64>>, versions_sorted: &[u64]) -> anyhow::Result<Vec<u64>> {
let n = versions_sorted.len();
let args = versions.unwrap_or_default();
let (from_opt, to_opt): (Option<i64>, Option<i64>) = match args.as_slice() {
[] => (None, None),
[from] => (Some(*from), None),
[from, to] => (Some(*from), Some(*to)),
_ => unreachable!(),
};
if n == 0 {
return Ok(vec![]);
}
let norm_index = |idx: i64| -> usize {
match idx {
i if i < 0 => {
let j = (n as i64) + i; // Python-style negative from end
j.clamp(0, n as i64) as usize
}
0 => 0,
i => {
// Treat positive indices as 1-based positions since registry records are 1-based; convert to zero-based
((i - 1) as usize).clamp(0, n)
}
}
};
let a = from_opt.map(norm_index).unwrap_or(0);
let b = to_opt.map(norm_index).unwrap_or(n);
if a >= b {
return Ok(vec![]);
}
Ok(versions_sorted[a..b].to_vec())
}
fn flatten_version_records(
selected_versions: &[u64],
entries_map: &std::collections::HashMap<u64, ic_registry_common_proto::pb::local_store::v1::ChangelogEntry>,
) -> Vec<VersionRecord> {
use ic_registry_common_proto::pb::local_store::v1::MutationType;
let mut out: Vec<VersionRecord> = Vec::new();
for v in selected_versions {
if let Some(entry) = entries_map.get(v) {
for km in entry.key_mutations.iter() {
let value_json = match km.mutation_type() {
MutationType::Unset => Value::Null,
MutationType::Set => decode_value_to_json(&km.key, &km.value),
_ => Value::Null,
};
out.push(VersionRecord {
version: *v,
key: km.key.clone(),
value: value_json,
});
}
}
}
out
}
impl Registry {
async fn get_registry(&self, ctx: DreContext) -> anyhow::Result<RegistryDump> {
let local_registry = ctx.registry_with_version(self.height).await;
let elected_guest_os_versions = get_elected_guest_os_versions(&local_registry)?;
let elected_host_os_versions = get_elected_host_os_versions(&local_registry)?;
let mut node_operators = get_node_operators(&local_registry, ctx.network()).await?;
let dcs = local_registry.get_datacenters()?;
let (subnets, nodes) = get_subnets_and_nodes(&local_registry, &node_operators, ctx.health_client()).await?;
let unassigned_nodes_config = local_registry.get_unassigned_nodes()?;
// Calculate number of rewardable nodes for node operators
for node_operator in node_operators.values_mut() {
let mut nodes_by_health = IndexMap::new();
for node_details in nodes.iter().filter(|n| n.node_operator_id == node_operator.node_operator_principal_id) {
let node_id = node_details.node_id;
let health = node_details.status.to_string();
let nodes = nodes_by_health.entry(health).or_insert_with(Vec::new);
nodes.push(node_id);
}
node_operator.computed.nodes_health = nodes_by_health;
node_operator.computed.total_up_nodes = nodes
.iter()
.filter(|n| {
n.node_operator_id == node_operator.node_operator_principal_id
&& (n.status == HealthStatus::Healthy || n.status == HealthStatus::Degraded)
})
.count() as u32;
}
let node_rewards_table = get_node_rewards_table(&local_registry, ctx.network());
let api_bns = get_api_boundary_nodes(&local_registry)?;
Ok(RegistryDump {
elected_guest_os_versions,
elected_host_os_versions,
nodes,
subnets,
unassigned_nodes_config,
dcs,
node_operators: node_operators.values().cloned().collect_vec(),
node_rewards_table,
api_bns,
node_providers: get_node_providers(&local_registry, ctx.network(), ctx.is_offline(), self.height.is_none()).await?,
})
}
}
#[derive(Debug, Serialize)]
struct VersionRecord {
version: u64,
key: String,
value: Value,
}
fn extract_version_from_registry_path(base_dir: &std::path::Path, full_path: &std::path::Path) -> Option<u64> {
// Registry path ends with .../<10 hex>/<2 hex>/<2 hex>/<5 hex>.pb
// We reconstruct the hex by concatenating the four segments (without slashes) and parse as hex u64.
let rel = full_path.strip_prefix(base_dir).ok()?;
let parts: Vec<_> = rel.iter().map(|s| s.to_string_lossy()).collect();
if parts.len() < 4 {
return None;
}
let last = parts[parts.len() - 1].trim_end_matches(".pb");
let seg3 = &parts[parts.len() - 2];
let seg2 = &parts[parts.len() - 3];
let seg1 = &parts[parts.len() - 4];
let hex = format!("{}{}{}{}", seg1, seg2, seg3, last);
u64::from_str_radix(&hex, 16).ok()
}
/// Best-effort decode of registry value bytes into JSON. Falls back to hex when unknown.
/// This can be extended to specific types in the future, if needed
fn decode_value_to_json(key: &str, bytes: &[u8]) -> Value {
// Known families where we can decode via protobuf types pulled from workspace crates.
// Use key prefixes to route decoding. Keep minimal and practical.
if key.starts_with(ic_registry_keys::DATA_CENTER_KEY_PREFIX) {
if let Ok(rec) = ic_protobuf::registry::dc::v1::DataCenterRecord::decode(bytes) {
return normalize_protobuf_json(serde_json::to_value(&rec).unwrap_or(Value::Null));
}
} else if key.starts_with(ic_registry_keys::NODE_OPERATOR_RECORD_KEY_PREFIX) {
if let Ok(rec) = ic_protobuf::registry::node_operator::v1::NodeOperatorRecord::decode(bytes) {
return normalize_protobuf_json(serde_json::to_value(&rec).unwrap_or(Value::Null));
}
} else if key.starts_with(ic_registry_keys::NODE_RECORD_KEY_PREFIX) {
if let Ok(rec) = ic_protobuf::registry::node::v1::NodeRecord::decode(bytes) {
return normalize_protobuf_json(serde_json::to_value(&rec).unwrap_or(Value::Null));
}
} else if key.starts_with(ic_registry_keys::SUBNET_RECORD_KEY_PREFIX) {
if let Ok(rec) = ic_protobuf::registry::subnet::v1::SubnetRecord::decode(bytes) {
return normalize_protobuf_json(serde_json::to_value(&rec).unwrap_or(Value::Null));
}
} else if key.starts_with(ic_registry_keys::REPLICA_VERSION_KEY_PREFIX) {
if let Ok(rec) = ic_protobuf::registry::replica_version::v1::ReplicaVersionRecord::decode(bytes) {
return normalize_protobuf_json(serde_json::to_value(&rec).unwrap_or(Value::Null));
}
} else if key.starts_with(ic_registry_keys::HOSTOS_VERSION_KEY_PREFIX) {
if let Ok(rec) = ic_protobuf::registry::hostos_version::v1::HostosVersionRecord::decode(bytes) {
return normalize_protobuf_json(serde_json::to_value(&rec).unwrap_or(Value::Null));
}
} else if key == ic_registry_keys::NODE_REWARDS_TABLE_KEY {
if let Ok(rec) = ic_protobuf::registry::node_rewards::v2::NodeRewardsTable::decode(bytes) {
return normalize_protobuf_json(serde_json::to_value(&rec).unwrap_or(Value::Null));
}
} else if key.starts_with(ic_registry_keys::API_BOUNDARY_NODE_RECORD_KEY_PREFIX) {
if let Ok(rec) = ic_protobuf::registry::api_boundary_node::v1::ApiBoundaryNodeRecord::decode(bytes) {
return normalize_protobuf_json(serde_json::to_value(&rec).unwrap_or(Value::Null));
}
} else if key == "unassigned_nodes_config" {
if let Ok(rec) = ic_protobuf::registry::unassigned_nodes_config::v1::UnassignedNodesConfigRecord::decode(bytes) {
return normalize_protobuf_json(serde_json::to_value(&rec).unwrap_or(Value::Null));
}
} else if key == "blessed_replica_versions" {
if let Ok(rec) = ic_protobuf::registry::replica_version::v1::BlessedReplicaVersions::decode(bytes) {
return normalize_protobuf_json(serde_json::to_value(&rec).unwrap_or(Value::Null));
}
}
// Fallback: base64 for compactness
let s = BASE64.encode(bytes);
if bytes.len() <= 29 {
if let Ok(p) = ic_types::PrincipalId::try_from(bytes.to_vec()) {
return serde_json::json!({ "bytes_base64": s, "principal": p.to_string() });
}
}
serde_json::json!({ "bytes_base64": s })
}
/// Recursively convert protobuf-derived JSON so byte arrays become base64 strings
fn normalize_protobuf_json(mut v: Value) -> Value {
match &mut v {
Value::Array(arr) => {
for e in arr.iter_mut() {
*e = normalize_protobuf_json(std::mem::take(e));
}
}
Value::Object(map) => {
for (_, vv) in map.iter_mut() {
*vv = normalize_protobuf_json(std::mem::take(vv));
}
}
Value::Number(_) | Value::String(_) | Value::Bool(_) | Value::Null => {}
}
// Replace array of small integers (likely bytes) with base64 when appropriate
if let Value::Array(arr) = &v {
if !arr.is_empty()
&& arr
.iter()
.all(|x| matches!(x, Value::Number(n) if n.as_u64().is_some() && n.as_u64().unwrap() <= 255))
{
let mut buf = Vec::with_capacity(arr.len());
for x in arr {
if let Value::Number(n) = x {
buf.push(n.as_u64().unwrap() as u8);
}
}
let s = BASE64.encode(&buf);
if buf.len() <= 29 {
if let Ok(p) = ic_types::PrincipalId::try_from(buf) {
return serde_json::json!({"bytes_base64": s, "principal": p.to_string()});
}
}
return serde_json::json!({ "bytes_base64": s });
}
}
v
}
fn collect_pb_files<F: FnMut(&std::path::Path)>(base: &std::path::Path, visitor: &mut F) -> anyhow::Result<()> {
if !base.exists() {
return Ok(());
}
for entry in fs_err::read_dir(base)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
collect_pb_files(&path, visitor)?;
} else {
visitor(&path);
}
}
Ok(())
}
fn get_elected_guest_os_versions(local_registry: &Arc<dyn LazyRegistry>) -> anyhow::Result<Vec<ReplicaVersionRecord>> {
local_registry.elected_guestos_records()
}
fn get_elected_host_os_versions(local_registry: &Arc<dyn LazyRegistry>) -> anyhow::Result<Vec<HostosVersionRecord>> {
local_registry.elected_hostos_records()
}
fn fetch_max_rewardable_nodes(record: &Operator, nodes_in_registry: Vec<Node>) -> BTreeMap<String, u32> {
// By default, this uses the types of nodes already registered.
// Some operators may register more nodes than are currently in the registry.
// These cases are handled separately as exceptions.
let exceptions: HashMap<PrincipalId, BTreeMap<String, u32>> = [
(
// Anonstake allowed to register up to 12 type3 nodes, 11 type3 nodes and 1 type3.1 node in the registry
// node pdtg7-ohtbm-rxbkj-m5u2y-a2xop-r3p2d-y2p54-xego2-fex5b-lam4a-kae to be removed and registered again
"eu5wc-g7r7l-zzy2o-227af-hfvin-orflw-4vdlf-j7gks-n5wrj-zezt7-tqe",
BTreeMap::from([(NodeRewardType::Type3.to_string(), 12)]),
),
(
// Power Meta Corporation allowed to register up to 5 nodes, 4 in the registry
"4lbqo-vgpoe-kemak-voout-777uo-kk74n-fnct3-pc4eg-3txi3-7cghf-rqe",
BTreeMap::from([(NodeRewardType::Type3dot1.to_string(), 5)]),
),
(
// Giant Leaf, LLC allowed to register up to 14 nodes, 0 in the registry
"bmlhw-kinr6-7cyv5-3o3v6-ic6tw-pnzk3-jycod-6d7sw-owaft-3b6k3-kqe",
BTreeMap::from([(NodeRewardType::Type1.to_string(), 14)]),
),
(
// Icaria Systems Pty Ltd allowed to register up to 18 nodes, 17 in the registry
"l5lhp-734i5-tbott-vipvc-vud2f-pfh7r-g3227-tgkdj-4rggm-abc2e-cae",
BTreeMap::from([(NodeRewardType::Type3dot1.to_string(), 18)]),
),
(
// Starbase allowed to register up to 14 nodes
// Nodes in the registry 15: Dead one to be removed
"cqjev-4qb7l-xv373-jfmhi-73n3a-cxhst-p6okb-vywwc-a4zbi-6sf3u-bae",
BTreeMap::from([(NodeRewardType::Type1dot1.to_string(), 14)]),
),
(
// InfoObjects allowed to register up to 2 nodes
// Nodes in the registry 4: Dead ones to be removed
"jtrpk-wokqg-aqmcn-esiq7-fip4e-dl7lv-iwdyj-cu5pm-wbg7i-4jl4t-wae",
BTreeMap::from([(NodeRewardType::Type3dot1.to_string(), 2)]),
),
(
// DFINITY can onboard type0 only (Not rewardables)
"xph6u-z3z2t-s7hh7-gtlxh-bbgbx-aatlm-eab4o-bsank-nqruh-3ub4q-sae",
BTreeMap::from([(NodeRewardType::Type0.to_string(), 28)]),
),
]
.into_iter()
.map(|(k, v)| (PrincipalId::from_str(k).unwrap(), v))
.collect::<HashMap<_, _>>();
if let Some(exception_max_rewardables) = exceptions.get(&record.principal) {
return exception_max_rewardables.clone();
}
let mut max_rewardable_count = BTreeMap::new();
for node in nodes_in_registry {
let node_type = node.node_reward_type.unwrap().to_string();
let type_count = max_rewardable_count.entry(node_type).or_insert(0);
*type_count += 1;
}
max_rewardable_count
}
async fn get_node_operators(local_registry: &Arc<dyn LazyRegistry>, network: &Network) -> anyhow::Result<IndexMap<PrincipalId, NodeOperator>> {
let all_nodes = local_registry.nodes().await?;
let operators = local_registry
.operators()
.await
.map_err(|e| anyhow::anyhow!("Couldn't get node operators: {:?}", e))?;
let node_operators = operators
.iter()
.map(|(k, record)| {
let node_provider_name = record.provider.name.as_ref().map_or_else(String::default, |v| {
if network.is_mainnet() && v.is_empty() {
panic!("Node provider name should not be empty for mainnet")
}
v.to_string()
});
let nodes_in_subnets = all_nodes
.iter()
.filter(|(_, value)| value.operator.principal == record.principal && value.subnet_id.is_some())
.count() as u64;
let nodes_in_registry: Vec<Node> = all_nodes
.iter()
.clone()
.filter(|(_, value)| value.operator.principal == record.principal)
.map(|(_, value)| value.clone())
.collect();
let nodes_in_registry_count = nodes_in_registry.len() as u64;
let max_rewardable_nodes_inferred = fetch_max_rewardable_nodes(record, nodes_in_registry);
let node_allowance_total = record.node_allowance + nodes_in_registry_count;
(
record.principal,
NodeOperator {
node_operator_principal_id: *k,
node_provider_principal_id: record.provider.principal,
dc_id: record.datacenter.as_ref().map(|d| d.name.to_owned()).unwrap_or_default(),
rewardable_nodes: record.rewardable_nodes.clone(),
max_rewardable_nodes: record.max_rewardable_nodes.clone(),
node_allowance: record.node_allowance,
ipv6: Some(record.ipv6.to_string()),
computed: NodeOperatorComputed {
node_provider_name,
node_allowance_remaining: record.node_allowance,
node_allowance_total,
total_up_nodes: 0,
nodes_health: Default::default(),
max_rewardable_nodes_inferred,
nodes_in_subnets,
nodes_in_registry: nodes_in_registry_count,
},
},
)
})
.collect::<IndexMap<_, _>>();
Ok(node_operators)
}
async fn get_subnets_and_nodes(
local_registry: &Arc<dyn LazyRegistry>,
node_operators: &IndexMap<PrincipalId, NodeOperator>,
health_client: Arc<dyn HealthStatusQuerier>,
) -> anyhow::Result<(Vec<SubnetRecord>, Vec<NodeDetails>)> {
let subnets = local_registry.subnets().await?;
let subnets = subnets
.iter()
.map(|(subnet_id, record)| SubnetRecord {
subnet_id: *subnet_id,
membership: record.nodes.iter().map(|n| n.principal.to_string()).collect(),
nodes: Default::default(),
max_ingress_bytes_per_message: record.max_ingress_bytes_per_message,
max_ingress_messages_per_block: record.max_ingress_messages_per_block,
max_block_payload_size: record.max_block_payload_size,
unit_delay_millis: record.unit_delay_millis,
initial_notary_delay_millis: record.initial_notary_delay_millis,
replica_version_id: record.replica_version.clone(),
dkg_interval_length: record.dkg_interval_length,
start_as_nns: record.start_as_nns,
subnet_type: record.subnet_type,
features: record.features.unwrap_or_default(),
max_number_of_canisters: record.max_number_of_canisters,
ssh_readonly_access: record.ssh_readonly_access.clone(),
ssh_backup_access: record.ssh_backup_access.clone(),
dkg_dealings_per_block: record.dkg_dealings_per_block,
is_halted: record.is_halted,
halt_at_cup_height: record.halt_at_cup_height,
chain_key_config: record.chain_key_config.clone(),
})
.collect::<Vec<_>>();
let nodes = _get_nodes(local_registry, node_operators, &subnets, health_client).await?;
let subnets = subnets
.into_iter()
.map(|subnet| {
let nodes = nodes
.iter()
.filter(|n| subnet.membership.contains(&n.node_id.to_string()))
.cloned()
.map(|n| (n.node_id, n))
.collect::<IndexMap<_, _>>();
SubnetRecord { nodes, ..subnet }
})
.collect();
Ok((subnets, nodes))
}
async fn _get_nodes(
local_registry: &Arc<dyn LazyRegistry>,
node_operators: &IndexMap<PrincipalId, NodeOperator>,
subnets: &[SubnetRecord],
health_client: Arc<dyn HealthStatusQuerier>,
) -> anyhow::Result<Vec<NodeDetails>> {
let nodes_health = health_client.nodes().await?;
// Rewardable nodes for all node operators
let mut rewardable_nodes: IndexMap<PrincipalId, BTreeMap<String, u32>> = node_operators
.iter()
.map(|(k, v)| (*k, v.computed.max_rewardable_nodes_inferred.clone()))
.collect();
let nodes = local_registry.nodes().await.map_err(|e| anyhow::anyhow!("Couldn't get nodes: {:?}", e))?;
for (_, record) in nodes.iter() {
let node_operator_id = record.operator.principal;
let rewardable_nodes = rewardable_nodes.get_mut(&node_operator_id);
if let Some(rewardable_nodes) = rewardable_nodes {
rewardable_nodes
.entry(record.node_reward_type.unwrap().to_string())
.and_modify(|count| *count = count.saturating_sub(1));
}
}
let nodes = nodes
.iter()
.map(|(k, record)| {
let node_operator_id = record.operator.principal;
let node_reward_type = record.node_reward_type.unwrap_or(NodeRewardType::Unspecified);
let rewardable_nodes = rewardable_nodes.get_mut(&node_operator_id);
let node_reward_type = if node_reward_type != NodeRewardType::Unspecified {
node_reward_type.as_str_name().to_string()
} else {
match rewardable_nodes {
Some(rewardable_nodes) => match rewardable_nodes.len() {
0 => "unknown:no_rewardable_nodes_found".to_string(),
1 => {
let (k, mut v) = rewardable_nodes.pop_first().unwrap();
v = v.saturating_sub(1);
if v != 0 {
rewardable_nodes.insert(k.clone(), v);
}
format!("estimated:{}", k)
}
_ => "unknown:multiple_rewardable_nodes".to_string(),
},
None => "unknown".to_string(),
}
};
let subnet = subnets.iter().find(|subnet| subnet.membership.contains(&k.to_string()));
NodeDetails {
node_id: *k,
xnet: Some(ConnectionEndpoint {
ip_addr: record.ip_addr.unwrap_or(Ipv6Addr::LOCALHOST).to_string(),
port: 2497,
}),
http: Some(ConnectionEndpoint {
ip_addr: record.ip_addr.unwrap_or(Ipv6Addr::LOCALHOST).to_string(),
port: 8080,
}),
node_operator_id,
chip_id: record.chip_id.clone(),
hostos_version_id: Some(record.hostos_version.clone()),
public_ipv4_config: record.public_ipv4_config.clone(),
node_provider_id: match node_operators.get(&node_operator_id) {
Some(no) => no.node_provider_principal_id,
None => PrincipalId::new_anonymous(),
},
subnet_id: subnet.map(|subnet| subnet.subnet_id),
dc_id: match node_operators.get(&node_operator_id) {
Some(no) => no.dc_id.clone(),
None => "".to_string(),
},
status: nodes_health.get(k).unwrap_or(&ic_management_types::HealthStatus::Unknown).clone(),
node_reward_type,
dc_owner: record.operator.datacenter.clone().map(|dc| dc.owner.name).unwrap_or_default(),
guestos_version_id: subnet.map(|sr| sr.replica_version_id.to_string()),
country: record.operator.datacenter.clone().map(|dc| dc.country).unwrap_or_default(),
}
})
.collect::<Vec<_>>();
Ok(nodes)
}
fn get_node_rewards_table(local_registry: &Arc<dyn LazyRegistry>, network: &Network) -> NodeRewardsTableFlattened {
let rewards_table_bytes = local_registry.get_node_rewards_table();
let mut rewards_table = match rewards_table_bytes {
Ok(r) => r,
Err(_) => {
if network.is_mainnet() {
panic!("Failed to get Node Rewards Table for mainnet")
} else {
warn!("Failed to get Node Rewards Table for {}", network.name);
IndexMap::new()
}
}
};
let table = match rewards_table.first_entry() {
Some(f) => f.get().table.clone(),
None => {
warn!("Failed to get Node Rewards Table for {}", network.name);
BTreeMap::new()
}
};
NodeRewardsTableFlattened {
table: table
.iter()
.map(|(k, v)| {
(
k.clone(),
NodeRewardRatesFlattened {
rates: v
.rates
.iter()
.map(|(rate_key, rate_val)| {
(
rate_key.clone(),
NodeRewardRateFlattened {
xdr_permyriad_per_node_per_month: rate_val.xdr_permyriad_per_node_per_month,
reward_coefficient_percent: rate_val.reward_coefficient_percent,
},
)
})
.collect(),
},
)
})
.collect(),
}
}
fn get_api_boundary_nodes(local_registry: &Arc<dyn LazyRegistry>) -> anyhow::Result<Vec<ApiBoundaryNodeDetails>> {
let api_bns = local_registry
.get_api_boundary_nodes()
.map_err(|e| anyhow::anyhow!("Couldn't get api boundary nodes: {:?}", e))?
.into_iter()
.map(|(k, record)| {
let principal = PrincipalId::from_str(k.as_str()).expect("Couldn't parse principal id");
ApiBoundaryNodeDetails {
principal,
version: record.version,
}
})
.collect();
Ok(api_bns)
}
async fn get_node_providers(
local_registry: &Arc<dyn LazyRegistry>,
network: &Network,
offline: bool,
latest_height: bool,
) -> anyhow::Result<Vec<NodeProvider>> {
let all_nodes = local_registry.nodes().await?;
// Get the node providers from the node operator records, and from the governance canister, and merge them
let nns_urls = network.get_nns_urls();
let url = nns_urls.first().ok_or(anyhow::anyhow!("No NNS URLs provided"))?.to_owned();
let canister_client = IcAgentCanisterClient::from_anonymous(url)?;
let gov = GovernanceCanisterWrapper::from(canister_client);
let gov_node_providers: HashMap<PrincipalId, String> = if !offline {
gov.get_node_providers()
.await?
.iter()
.map(|p| {
(
p.id.unwrap_or(PrincipalId::new_anonymous()),
match &p.reward_account {
Some(account) => AccountIdentifier::from_slice(&account.hash).unwrap().to_string(),
None => "".to_string(),
},
)
})
.collect()
} else {
HashMap::new()
};
let mut reg_node_providers = local_registry
.operators()
.await?
.values()
.map(|operator| operator.provider.clone())
.collect_vec();
let reg_provider_ids = reg_node_providers.iter().map(|provider| provider.principal).collect::<HashSet<_>>();
// Governance canister doesn't have the mechanism to retrieve node providers on a certain height
// meaning that merging the lists on arbitrary heights wouldn't make sense.
if latest_height {
for principal in gov_node_providers.keys() {
if !reg_provider_ids.contains(principal) {
reg_node_providers.push(ic_management_types::Provider {
principal: *principal,
name: None,
website: None,
});
}
}
}
let reg_node_providers = reg_node_providers
.into_iter()
.sorted_by_key(|provider| provider.principal)
.dedup_by(|x, y| x.principal == y.principal)
.collect_vec();
Ok(reg_node_providers
.iter()
.map(|provider| {
let provider_nodes = all_nodes.values().filter(|node| node.operator.provider.principal == provider.principal);
NodeProvider {
principal: provider.principal,
reward_account: gov_node_providers.get(&provider.principal).cloned().unwrap_or_default(),
total_nodes: provider_nodes.clone().count(),
nodes_in_subnet: provider_nodes.clone().filter(|node| node.subnet_id.is_some()).count(),
nodes_per_dc: provider_nodes
.map(|node| match &node.operator.datacenter {
Some(dc) => dc.name.clone(),
None => "Unknown".to_string(),
})
.counts_by(|dc_name| dc_name)
.into_iter()
.collect(),
name: provider.name.clone().unwrap_or("Unknown".to_string()),
}
})
.collect())
}
#[derive(Debug, Serialize)]
struct RegistryDump {
subnets: Vec<SubnetRecord>,
nodes: Vec<NodeDetails>,
unassigned_nodes_config: Option<UnassignedNodesConfigRecord>,
dcs: Vec<DataCenterRecord>,
node_operators: Vec<NodeOperator>,
node_rewards_table: NodeRewardsTableFlattened,
api_bns: Vec<ApiBoundaryNodeDetails>,
elected_guest_os_versions: Vec<ReplicaVersionRecord>,
elected_host_os_versions: Vec<HostosVersionRecord>,
node_providers: Vec<NodeProvider>,
}
#[derive(Clone, Debug, Serialize)]
struct ApiBoundaryNodeDetails {
principal: PrincipalId,
version: String,
}
#[derive(Debug, Serialize, Clone)]
struct NodeDetails {
node_id: PrincipalId,
xnet: Option<ConnectionEndpoint>,
http: Option<ConnectionEndpoint>,
node_operator_id: PrincipalId,
chip_id: Option<Vec<u8>>,
hostos_version_id: Option<String>,
public_ipv4_config: Option<IPv4InterfaceConfig>,
subnet_id: Option<PrincipalId>,
dc_id: String,
node_provider_id: PrincipalId,
status: HealthStatus,
node_reward_type: String,
dc_owner: String,
guestos_version_id: Option<String>,
country: String,
}
/// User-friendly representation of a SubnetRecord. For instance,
/// the `membership` field is a `Vec<String>` to pretty-print the node IDs.
#[derive(Debug, Default, Serialize, Clone)]
struct SubnetRecord {
subnet_id: PrincipalId,
membership: Vec<String>,
nodes: IndexMap<PrincipalId, NodeDetails>,
max_ingress_bytes_per_message: u64,
max_ingress_messages_per_block: u64,
max_block_payload_size: u64,
unit_delay_millis: u64,
initial_notary_delay_millis: u64,
replica_version_id: String,
dkg_interval_length: u64,
dkg_dealings_per_block: u64,
start_as_nns: bool,
subnet_type: SubnetType,
features: SubnetFeatures,
max_number_of_canisters: u64,
ssh_readonly_access: Vec<String>,
ssh_backup_access: Vec<String>,
is_halted: bool,
halt_at_cup_height: bool,
chain_key_config: Option<ChainKeyConfig>,
}
#[derive(Clone, Debug, Serialize)]
struct NodeOperator {
node_operator_principal_id: PrincipalId,
node_provider_principal_id: PrincipalId,
dc_id: String,
rewardable_nodes: BTreeMap<String, u32>,
max_rewardable_nodes: BTreeMap<String, u32>,
node_allowance: u64,
ipv6: Option<String>,
computed: NodeOperatorComputed,
}
#[derive(Clone, Debug, Serialize)]
struct NodeOperatorComputed {
node_provider_name: String,
node_allowance_remaining: u64,
node_allowance_total: u64,
total_up_nodes: u32,
nodes_health: IndexMap<String, Vec<PrincipalId>>,
max_rewardable_nodes_inferred: BTreeMap<String, u32>,
nodes_in_subnets: u64,
nodes_in_registry: u64,
}
// We re-create the rewards structs here in order to convert the output of get-rewards-table into the format
// that can also be parsed by propose-to-update-node-rewards-table.
// This is a bit of a hack, but it's the easiest way to get the desired output.
// A more proper way would be to adjust the upstream structs to flatten the "rates" and "table" fields
// directly, but this breaks some of the candid encoding and decoding and also some of the tests.
// Make sure to keep these structs in sync with the upstream ones.
#[derive(serde::Serialize, PartialEq, ::prost::Message)]
pub struct NodeRewardRateFlattened {
#[prost(uint64, tag = "1")]
pub xdr_permyriad_per_node_per_month: u64,
#[prost(int32, optional, tag = "2")]
#[serde(skip_serializing_if = "Option::is_none")]
pub reward_coefficient_percent: Option<i32>,
}
#[derive(serde::Serialize, PartialEq, ::prost::Message)]