-
Notifications
You must be signed in to change notification settings - Fork 178
/
node_info.go
144 lines (119 loc) · 4.48 KB
/
node_info.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
package utils
import (
"fmt"
"os"
"path/filepath"
model "github.com/onflow/flow-go/model/bootstrap"
"github.com/onflow/flow-go/model/encodable"
"github.com/onflow/flow-go/model/flow"
io "github.com/onflow/flow-go/utils/io"
"github.com/onflow/flow-go/utils/unittest"
)
// WritePartnerFiles writes the all partner public node info into `bootDir/partners/public-root-information/`
// also writes a map containing each of the nodes weights mapped by NodeID
func WritePartnerFiles(nodeInfos []model.NodeInfo, bootDir string) (string, string, error) {
// convert to public nodeInfos and map stkes
nodePubInfos := make([]model.NodeInfoPub, len(nodeInfos))
weights := make(map[flow.Identifier]uint64)
for i, node := range nodeInfos {
nodePubInfos[i] = node.Public()
weights[node.NodeID] = node.Weight
}
// write node public infos to partner dir
partnersDir := filepath.Join(bootDir, "partners")
err := os.MkdirAll(filepath.Join(bootDir, partnersDir), os.ModePerm)
if err != nil {
return "", "", fmt.Errorf("could not create partner node info directory: %w", err)
}
// write each node info into partners dir
for _, node := range nodePubInfos {
nodePubInfosPath := filepath.Join(partnersDir, fmt.Sprintf(model.PathNodeInfoPub, node.NodeID.String()))
err := io.WriteJSON(nodePubInfosPath, node)
if err != nil {
return "", "", fmt.Errorf("could not write partner node info: %w", err)
}
}
// write partner weights
weightsPath := filepath.Join(bootDir, model.FileNamePartnerWeights)
err = io.WriteJSON(weightsPath, weights)
if err != nil {
return "", "", fmt.Errorf("could not write partner weights info: %w", err)
}
return filepath.Join(partnersDir, model.DirnamePublicBootstrap), weightsPath, nil
}
// WriteInternalFiles writes the internal private node info into `bootDir/private-root-information/`
// also writes a map containing each of the nodes weights mapped by the node's networking address
func WriteInternalFiles(nodeInfos []model.NodeInfo, bootDir string) (string, string, error) {
// convert to private nodeInfos and node configuration map
nodePrivInfos := make([]model.NodeInfoPriv, len(nodeInfos))
configs := make([]model.NodeConfig, len(nodeInfos))
for i, node := range nodeInfos {
netPriv := unittest.NetworkingPrivKeyFixture()
stakePriv := unittest.StakingPrivKeyFixture()
nodePrivInfos[i] = model.NodeInfoPriv{
Role: node.Role,
Address: node.Address,
NodeID: node.NodeID,
NetworkPrivKey: encodable.NetworkPrivKey{
PrivateKey: netPriv,
},
StakingPrivKey: encodable.StakingPrivKey{
PrivateKey: stakePriv,
},
}
configs[i] = model.NodeConfig{
Role: node.Role,
Address: node.Address,
Weight: node.Weight,
}
}
// write config
configPath := filepath.Join(bootDir, "node-internal-infos.pub.json")
err := io.WriteJSON(configPath, configs)
if err != nil {
return "", "", fmt.Errorf("could not write internal node configuration: %w", err)
}
// write node private infos to internal priv dir
for _, node := range nodePrivInfos {
internalPrivPath := fmt.Sprintf(model.PathNodeInfoPriv, node.NodeID)
err = io.WriteJSON(filepath.Join(bootDir, internalPrivPath), node)
if err != nil {
return "", "", fmt.Errorf("could not write internal node info: %w", err)
}
}
return bootDir, configPath, nil
}
func GenerateNodeInfos(consensus, collection, execution, verification, access int) []model.NodeInfo {
nodes := make([]model.NodeInfo, 0)
// CONSENSUS = 1
consensusNodes := unittest.NodeInfosFixture(consensus,
unittest.WithRole(flow.RoleConsensus),
unittest.WithInitialWeight(flow.DefaultInitialWeight),
)
nodes = append(nodes, consensusNodes...)
// COLLECTION = 1
collectionNodes := unittest.NodeInfosFixture(collection,
unittest.WithRole(flow.RoleCollection),
unittest.WithInitialWeight(flow.DefaultInitialWeight),
)
nodes = append(nodes, collectionNodes...)
// EXECUTION = 1
executionNodes := unittest.NodeInfosFixture(execution,
unittest.WithRole(flow.RoleExecution),
unittest.WithInitialWeight(flow.DefaultInitialWeight),
)
nodes = append(nodes, executionNodes...)
// VERIFICATION = 1
verificationNodes := unittest.NodeInfosFixture(verification,
unittest.WithRole(flow.RoleVerification),
unittest.WithInitialWeight(flow.DefaultInitialWeight),
)
nodes = append(nodes, verificationNodes...)
// ACCESS = 1
accessNodes := unittest.NodeInfosFixture(access,
unittest.WithRole(flow.RoleAccess),
unittest.WithInitialWeight(flow.DefaultInitialWeight),
)
nodes = append(nodes, accessNodes...)
return nodes
}