-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
230 lines (181 loc) · 5.15 KB
/
main.go
File metadata and controls
230 lines (181 loc) · 5.15 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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
edkeys "github.com/modulrcloud/net-spawner/ed25519"
)
var (
showHelp = flag.Bool("help", false, "Show help and exit")
showH = flag.Bool("h", false, "Show help (shorthand)")
)
type Config struct {
CorePath string `json:"corePath"` // absolute to the Go node binary
NetMode string `json:"netMode"` // e.g., TESTNET_2V, TESTNET_5V, TESTNET_21V
}
func usage() {
fmt.Fprintf(os.Stderr, `NetSpawner — local blockchain network launcher
Usage:
netspawner [flags] <command>
Commands:
resume Resume network from the same point
reset Reset and start the network from init (progress drop)
keygen Generate an Ed25519 key pair as JSON
help Show this help
Flags:
-h, -help Show help and exit
Examples:
netspawner resume
netspawner reset
netspawner -h
`)
}
func main() {
flag.Usage = usage
flag.Parse()
if *showHelp || *showH {
usage()
return
}
if flag.NArg() == 0 {
usage()
os.Exit(2)
}
switch strings.ToLower(flag.Arg(0)) {
case "help":
usage()
case "resume":
if err := resumeNetwork(); err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
case "reset":
if err := resetNetwork(); err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
case "keygen":
if err := runKeygen(flag.Args()[1:]); err != nil {
if err == flag.ErrHelp {
return
}
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
default:
fmt.Fprintf(os.Stderr, "unknown command: %q\n", flag.Arg(0))
os.Exit(2)
}
}
func resumeNetwork() error {
cfg, dir, err := readConfig()
if err != nil {
return err
}
baseDir := filepath.Join(dir, "X"+cfg.NetMode)
dirs := CreateDirsForNodes(cfg, baseDir)
var procs []*exec.Cmd
for _, nd := range dirs {
cmd, err := RunCoreProcess(nd, cfg.CorePath)
if err != nil {
return fmt.Errorf("spawn for %s: %w", nd, err)
}
procs = append(procs, cmd)
}
// Block until all children exit.
for _, p := range procs {
_ = p.Wait()
}
return nil
}
func resetNetwork() error {
cfg, netSpawnerDir, err := readConfig()
if err != nil {
return err
}
rootPathForAllNodesChaindata := "X" + cfg.NetMode
dirForTestnet := filepath.Join(netSpawnerDir, rootPathForAllNodesChaindata)
srcFilesDir := filepath.Join(netSpawnerDir, "files", "testnets", cfg.NetMode)
numNodes, err := parseNodesCount(cfg.NetMode)
if err != nil {
return err
}
if err := ensureDir(dirForTestnet); err != nil {
return err
}
for i := 1; i <= numNodes; i++ {
nodeDir := filepath.Join(dirForTestnet, "V"+strconv.Itoa(i))
if err := ensureDir(nodeDir); err != nil {
return err
}
// Copy per-node files into the node root:
// 1) genesis.json (shared)
if err := copyFile(
filepath.Join(srcFilesDir, "genesis.json"),
filepath.Join(nodeDir, "genesis.json"),
); err != nil {
return fmt.Errorf("copy genesis for V%d: %w", i, err)
}
// 2) Copy configs_for_nodes/config_i.json to V_i/configs.json
srcNodeCfg := filepath.Join(srcFilesDir, "configs_for_nodes", fmt.Sprintf("config_%d.json", i))
if err := copyFile(srcNodeCfg, filepath.Join(nodeDir, "configs.json")); err != nil {
return fmt.Errorf("copy node config for V%d: %w", i, err)
}
}
fmt.Printf("Directories setup complete for network size %s\n", cfg.NetMode)
nowMs := time.Now().UnixMilli()
for i := 1; i <= numNodes; i++ {
nodeDir := filepath.Join(dirForTestnet, "V"+strconv.Itoa(i))
genesisPath := filepath.Join(nodeDir, "genesis.json")
chainDataDir := filepath.Join(nodeDir, "CHAINDATA")
if fileExists(genesisPath) {
if err := updateGenesisTimestamp(genesisPath, nowMs); err != nil {
return fmt.Errorf("update timestamp for %s: %w", genesisPath, err)
}
fmt.Printf("Updated timestamp in %s\n", genesisPath)
}
if dirExists(chainDataDir) {
if err := os.RemoveAll(chainDataDir); err != nil {
return fmt.Errorf("remove CHAINDATA in %s: %w", nodeDir, err)
}
fmt.Printf("Deleted CHAINDATA directory in %s\n", nodeDir)
}
}
fmt.Println("Timestamps updated and CHAINDATA directories deleted")
return resumeNetwork()
}
func runKeygen(args []string) error {
fs := flag.NewFlagSet("keygen", flag.ContinueOnError)
mnemonic := fs.String("mnemonic", "", "Existing BIP39 mnemonic. If empty, a new 24-word phrase will be generated")
passphrase := fs.String("passphrase", "", "Optional mnemonic password")
derivationPath := fs.String("path", "", "BIP44 derivation path numbers separated by '/' (default 44/7337/0/0)")
if err := fs.Parse(args); err != nil {
return err
}
var bip44Path []uint32
if strings.TrimSpace(*derivationPath) != "" {
parts := strings.Split(*derivationPath, "/")
bip44Path = make([]uint32, 0, len(parts))
for _, part := range parts {
part = strings.TrimSpace(part)
if part == "" {
continue
}
val, err := strconv.ParseUint(part, 10, 32)
if err != nil {
return fmt.Errorf("invalid BIP44 path component %q: %w", part, err)
}
bip44Path = append(bip44Path, uint32(val))
}
}
box := edkeys.GenerateKeyPair(*mnemonic, *passphrase, bip44Path)
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
return enc.Encode(box)
}