/
generator.go
203 lines (162 loc) · 4.92 KB
/
generator.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package tendermint
import (
"encoding/base64"
"fmt"
"log"
"os"
"path"
"path/filepath"
"code.vegaprotocol.io/vegacapsule/config"
"code.vegaprotocol.io/vegacapsule/types"
"code.vegaprotocol.io/vegacapsule/utils"
tmconfig "github.com/cometbft/cometbft/config"
tmp2p "github.com/cometbft/cometbft/p2p"
"github.com/cometbft/cometbft/privval"
tmtypes "github.com/cometbft/cometbft/types"
)
const (
nodeDirPerm = 0755
)
type Peer struct {
Index int
ID string
}
type node struct {
name string
groupName string
id string
index int
}
type ConfigGenerator struct {
conf *config.Config
homeDir string
genValidators []tmtypes.GenesisValidator
nodes []node
}
func newGenValidator(nodeDir string, config *tmconfig.Config) (*tmtypes.GenesisValidator, error) {
pv := privval.LoadFilePV(config.BaseConfig.PrivValidatorKeyFile(), config.BaseConfig.PrivValidatorStateFile())
pubKey, err := pv.GetPubKey()
if err != nil {
return nil, err
}
return &tmtypes.GenesisValidator{
Address: pubKey.Address(),
PubKey: pubKey,
Power: 1,
Name: nodeDir,
}, nil
}
func NewConfigGenerator(conf *config.Config, generatedNodeSets []types.NodeSet) (*ConfigGenerator, error) {
homeDir, err := filepath.Abs(path.Join(*conf.OutputDir, conf.TendermintNodePrefix))
if err != nil {
return nil, err
}
nodes := make([]node, 0, len(generatedNodeSets))
genValidators := make([]tmtypes.GenesisValidator, 0, len(generatedNodeSets))
for _, tn := range generatedNodeSets {
nodes = append(nodes, node{
name: tn.Tendermint.Name,
groupName: tn.GroupName,
id: tn.Tendermint.NodeID,
index: tn.Index,
})
if tn.Mode != types.NodeModeValidator {
continue
}
config := tmconfig.DefaultConfig()
config.SetRoot(tn.Tendermint.HomeDir)
genValidator, err := newGenValidator(tn.Tendermint.HomeDir, config)
if err != nil {
return nil, err
}
genValidators = append(genValidators, *genValidator)
}
return &ConfigGenerator{
conf: conf,
homeDir: homeDir,
nodes: nodes,
genValidators: genValidators,
}, nil
}
func (tg ConfigGenerator) HomeDir() string {
return tg.homeDir
}
func (tg *ConfigGenerator) Initiate(index int, mode, groupName string) (*types.TendermintNode, error) {
nodeDir := tg.nodeDir(index)
if err := os.MkdirAll(nodeDir, os.ModePerm); err != nil {
return nil, err
}
if err := os.MkdirAll(filepath.Join(nodeDir, "config"), nodeDirPerm); err != nil {
_ = os.RemoveAll(*tg.conf.OutputDir)
return nil, err
}
if err := os.MkdirAll(filepath.Join(nodeDir, "data"), nodeDirPerm); err != nil {
_ = os.RemoveAll(*tg.conf.OutputDir)
return nil, err
}
args := []string{"tm", "init", mode, "--home", nodeDir}
log.Printf("Initiating Tendermint node %q with: %s %v", mode, *tg.conf.VegaBinary, args)
b, err := utils.ExecuteBinary(*tg.conf.VegaBinary, args, nil)
if err != nil {
return nil, err
}
log.Println(string(b))
confFilePath := ConfigFilePath(nodeDir)
origConFilePath := originalConfigFilePath(nodeDir)
if err := utils.CopyFile(confFilePath, origConFilePath); err != nil {
return nil, fmt.Errorf("failed to copy initiated config from %q to %q: %w", confFilePath, origConFilePath, err)
}
config := tmconfig.DefaultConfig()
config.SetRoot(nodeDir)
nodeKey, err := tmp2p.LoadNodeKey(config.NodeKeyFile())
if err != nil {
return nil, fmt.Errorf("failed to get node key: %w", err)
}
nodeID := string(nodeKey.ID())
nodeName := fmt.Sprintf("tendermint-%s-%d", mode, index)
tg.nodes = append(tg.nodes, node{
name: nodeName,
groupName: groupName,
id: nodeID,
index: index,
})
initNode := &types.TendermintNode{
GeneratedService: types.GeneratedService{
Name: nodeName,
HomeDir: nodeDir,
ConfigFilePath: confFilePath,
},
NodeID: nodeID,
GenesisFilePath: config.BaseConfig.GenesisFile(),
BinaryPath: *tg.conf.VegaBinary,
}
if mode != string(types.NodeModeValidator) {
return initNode, nil
}
genValidator, err := newGenValidator(nodeDir, config)
if err != nil {
return nil, err
}
initNode.ValidatorPublicKey = base64.StdEncoding.EncodeToString(genValidator.PubKey.Bytes())
tg.genValidators = append(tg.genValidators, *genValidator)
return initNode, nil
}
func (tg ConfigGenerator) GenesisValidators() []tmtypes.GenesisValidator {
return tg.genValidators
}
func (tg ConfigGenerator) nodeDir(i int) string {
nodeDirName := fmt.Sprintf("%s%d", tg.conf.NodeDirPrefix, i)
return filepath.Join(tg.homeDir, nodeDirName)
}
func ConfigFilePath(nodeDir string) string {
return filepath.Join(nodeDir, "config", "config.toml")
}
func originalConfigFilePath(nodeDir string) string {
return filepath.Join(nodeDir, "config", "config-original.toml")
}
func NodeKeyFilePath(nodeDir string) string {
return filepath.Join(nodeDir, "config", "node_key.json")
}
func PrivValidatorFilePath(nodeDir string) string {
return filepath.Join(nodeDir, "config", "priv_validator_key.json")
}