-
Notifications
You must be signed in to change notification settings - Fork 14
/
subnet.go
181 lines (166 loc) · 4.99 KB
/
subnet.go
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
package agent
import (
"fmt"
"github.com/aviate-labs/agent-go/certification"
"github.com/aviate-labs/agent-go/certification/hashtree"
"github.com/aviate-labs/agent-go/principal"
"github.com/fxamacker/cbor/v2"
"math/big"
)
func (a Agent) GetSubnetMetrics(subnetID principal.Principal) (*SubnetMetrics, error) {
path := []hashtree.Label{hashtree.Label("subnet"), subnetID.Raw, hashtree.Label("metrics")}
cert, err := a.readSubnetStateCertificate(subnetID, [][]hashtree.Label{path})
if err != nil {
return nil, err
}
rawMetrics, err := cert.Tree.Lookup(path...)
if err != nil {
return nil, err
}
var metrics SubnetMetrics
if err := cbor.Unmarshal(rawMetrics, &metrics); err != nil {
return nil, err
}
return &metrics, nil
}
func (a Agent) GetSubnets() ([]principal.Principal, error) {
path := []hashtree.Label{hashtree.Label("subnet")}
cert, err := a.readSubnetStateCertificate(principal.MustDecode(certification.RootSubnetID), [][]hashtree.Label{path})
if err != nil {
return nil, err
}
subnets, err := cert.Tree.LookupSubTree(path...)
if err != nil {
return nil, err
}
children, err := hashtree.AllChildren(subnets)
if err != nil {
return nil, err
}
var subnetsList []principal.Principal
for _, p := range children {
subnetsList = append(subnetsList, principal.Principal{Raw: p.Path[0]})
}
return subnetsList, nil
}
func (a Agent) GetSubnetsInfo() ([]SubnetInfo, error) {
rootSubnetID := principal.MustDecode(certification.RootSubnetID)
path := []hashtree.Label{hashtree.Label("subnet")}
cert, err := a.readSubnetStateCertificate(rootSubnetID, [][]hashtree.Label{path})
if err != nil {
return nil, err
}
subnets, err := cert.Tree.LookupSubTree(path...)
if err != nil {
return nil, err
}
children, err := hashtree.AllChildren(subnets)
if err != nil {
return nil, err
}
var subnetsInfo []SubnetInfo
for _, p := range children {
subnetID := principal.Principal{Raw: p.Path[0]}
publicKey, err := hashtree.Lookup(p.Value, hashtree.Label("public_key"))
if err != nil {
return nil, err
}
rawCanisterRanges, err := hashtree.Lookup(p.Value, hashtree.Label("canister_ranges"))
if err != nil {
return nil, err
}
var canisterRanges certification.CanisterRanges
if err := cbor.Unmarshal(rawCanisterRanges, &canisterRanges); err != nil {
return nil, err
}
nodes, err := cert.Tree.LookupSubTree(hashtree.Label("subnet"), subnetID.Raw, hashtree.Label("node"))
if err != nil {
path = []hashtree.Label{hashtree.Label("subnet"), subnetID.Raw, hashtree.Label("node")}
nodesCert, err := a.readSubnetStateCertificate(subnetID, [][]hashtree.Label{path})
if err != nil {
return nil, err
}
nodes, err = nodesCert.Tree.LookupSubTree(path...)
if err != nil {
return nil, err
}
}
nodeChildren, err := hashtree.AllChildren(nodes)
if err != nil {
return nil, err
}
var nodesInfo []NodeInfo
for _, node := range nodeChildren {
nodeID := principal.Principal{Raw: node.Path[0]}
nodePublicKey, err := hashtree.Lookup(node.Value, hashtree.Label("public_key"))
if err != nil {
return nil, err
}
nodesInfo = append(
nodesInfo,
NodeInfo{
NodeID: nodeID,
PublicKey: nodePublicKey,
},
)
}
subnetsInfo = append(subnetsInfo, SubnetInfo{
SubnetID: subnetID,
PublicKey: publicKey,
CanisterRanges: canisterRanges,
Nodes: nodesInfo,
})
}
return subnetsInfo, nil
}
type NodeInfo struct {
NodeID principal.Principal
PublicKey []byte
}
type SubnetInfo struct {
SubnetID principal.Principal
PublicKey []byte
CanisterRanges certification.CanisterRanges
Nodes []NodeInfo
}
type SubnetMetrics struct {
NumCanisters uint64
CanisterStateBytes uint64
ConsumedCyclesTotal big.Int
UpdateTransactionsTotal uint64
}
func (m *SubnetMetrics) UnmarshalCBOR(bytes []byte) error {
var metricsMap map[int]any
if err := cbor.Unmarshal(bytes, &metricsMap); err != nil {
return err
}
var ok bool
m.NumCanisters, ok = metricsMap[0].(uint64)
if !ok {
return fmt.Errorf("unexpected type for NumCanisters: %T", metricsMap[0])
}
m.CanisterStateBytes, ok = metricsMap[1].(uint64)
if !ok {
return fmt.Errorf("unexpected type for CanisterStateBytes: %T", metricsMap[1])
}
rawConsumedCyclesTotal, ok := metricsMap[2].(map[any]any)
if !ok {
return fmt.Errorf("unexpected type for ConsumedCyclesTotal: %T", metricsMap[2])
}
rawLow, ok := rawConsumedCyclesTotal[uint64(0)].(uint64)
if !ok {
return fmt.Errorf("unexpected type for low: %T", rawConsumedCyclesTotal[0])
}
rawHigh, ok := rawConsumedCyclesTotal[uint64(1)].(uint64)
if !ok {
return fmt.Errorf("unexpected type for high: %T", rawConsumedCyclesTotal[1])
}
m.ConsumedCyclesTotal.SetUint64(rawHigh)
m.ConsumedCyclesTotal.Lsh(&m.ConsumedCyclesTotal, 64)
m.ConsumedCyclesTotal.Add(&m.ConsumedCyclesTotal, new(big.Int).SetUint64(rawLow))
m.UpdateTransactionsTotal, ok = metricsMap[3].(uint64)
if !ok {
return fmt.Errorf("unexpected type for UpdateTransactionsTotal: %T", metricsMap[3])
}
return nil
}