forked from sedaprotocol/seda-networks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
298 lines (253 loc) · 8.17 KB
/
main.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
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
package main
import (
"bytes"
"encoding/json"
"flag"
"io"
"log"
"math/big"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
)
var (
/* =================================================== */
/* Change as needed */
/* =================================================== */
CHAIN_ID = "seda-1"
WORKING_DIR = "./mainnet"
BINARY_URL = "https://github.com/sedaprotocol/seda-chain/releases/download/v0.1.0/sedad-amd64"
/* =================================================== */
/* The followings should rarely change */
/* =================================================== */
BINARY = "sedad"
BINARY_PATH = "./" + BINARY
GENTX_DIR = filepath.Join(WORKING_DIR, "gentx")
SEDA_HOME = filepath.Join(os.Getenv("HOME"), ".sedad")
PREFIX = "seda"
DENOM = "aseda"
GENESIS_ALLOCATION = "5000000000000000000000" + DENOM // 5000 SEDA
DEFAULT_BOND = "5000000000000000000000" + DENOM // 5000 SEDA
MAXBOND = "5000000000000000000000" // 5000 SEDA
)
func main() {
generate := flag.Bool("generate-final-genesis", false, "a boolean flag to enable final genesis generation mode")
flag.Parse()
if *generate {
log.Println("Final genesis generation mode enabled - Using local binary")
_, err := os.Stat(BINARY_PATH)
if err != nil {
if os.IsNotExist(err) {
log.Println("Binary does not exist at", BINARY_PATH)
} else {
log.Println("Error finding binary:", err)
}
}
} else {
log.Println("Downloading binary...")
err := downloadFile(BINARY_PATH, BINARY_URL)
if err != nil {
log.Fatal("Error downloading binary: ", err)
}
}
// make binary executable
err := os.Chmod(BINARY_PATH, 0755)
if err != nil {
log.Fatal("Error making binary executable: ", err)
}
log.Println("Removing existing seda home directory...")
_, err = exec.Command("rm", "-rf", SEDA_HOME).Output()
if err != nil {
log.Fatal("Error removing existing seda home directory: ", err)
}
log.Println("Setting chain id...")
_, err = exec.Command(BINARY_PATH, "config", "set", "client", "chain-id", CHAIN_ID).Output()
if err != nil {
log.Fatal("Error setting chain id: ", err)
}
log.Println("Setting keyring backend...")
_, err = exec.Command(BINARY_PATH, "config", "set", "client", "keyring-backend", "test").Output()
if err != nil {
log.Fatal("Error setting keyring backend: ", err)
}
log.Println("Initializing node...")
_, err = exec.Command(BINARY_PATH, "init", "node", "--default-denom", DENOM).Output()
if err != nil {
log.Fatal("Error initializing node: ", err)
}
log.Println("Removing existing genesis file...")
err = os.Remove(SEDA_HOME + "/config/genesis.json")
if err != nil {
log.Fatal("Error removing existing genesis file: ", err)
}
// replace with pre-genesis.json
log.Println("Replacing genesis.json...")
err = copyFile(WORKING_DIR+"/pre-genesis.json", SEDA_HOME+"/config/genesis.json")
if err != nil {
log.Fatal("Error replacing default genesis.json: ", err)
}
// modify genesis time to be in the past
log.Println("Modifying genesis time...")
originalGenesisTime, err := modifyGenesisTime(SEDA_HOME+"/config/genesis.json", "2024-01-01T18:00:00Z")
if err != nil {
log.Fatal("Error modifying genesis time: ", err)
}
// perform validation
log.Println("Validation began...")
gentxFiles, err := filepath.Glob(WORKING_DIR + "/gentx/*.json")
if err != nil {
log.Fatal("Error reading gentx files: ", err)
}
gentxDir := filepath.Join(SEDA_HOME, "config/gentx")
if _, err := os.Stat(gentxDir); os.IsNotExist(err) {
err := os.Mkdir(gentxDir, 0755)
if err != nil {
log.Println("Error creating directory:", err)
return
}
}
for _, file := range gentxFiles {
gentxFile, err := os.ReadFile(file)
if err != nil {
log.Fatal("Error reading gentx file: ", err)
}
var gentx map[string]interface{}
err = json.Unmarshal(gentxFile, &gentx)
if err != nil {
log.Fatal("Error unmarshalling gentx file: ", err)
}
body := gentx["body"].(map[string]interface{})
messages := body["messages"].([]interface{})
message := messages[0].(map[string]interface{})
validatorAddress := message["validator_address"].(string)
value := message["value"].(map[string]interface{})
denom := value["denom"].(string)
amount, ok := new(big.Int).SetString(value["amount"].(string), 10)
if !ok {
log.Fatal("Error converting bond amount to big.Int")
}
_, err = exec.Command(BINARY_PATH, "debug", "addr", validatorAddress).Output()
if err != nil {
log.Fatal("Error debugging validator address: ", err)
}
if denom != DENOM {
log.Fatal("invalid denomination")
}
maxBond, ok := new(big.Int).SetString(MAXBOND, 10)
if !ok {
log.Fatal("Error converting max bonding amount to big.Int")
}
if amount.Cmp(maxBond) == 1 {
log.Fatalf("Bonded stake exceeds limit: %d > %d", amount, maxBond)
}
// add genesis account, if it hasn't already been added
addrBytes, err := sdk.GetFromBech32(validatorAddress, "sedavaloper")
if err != nil {
log.Fatalf("Error converting validator address to bytes: %s", err)
}
delegatorAddr, err := sdk.Bech32ifyAddressBytes("seda", addrBytes)
if err != nil {
log.Fatalf("Error converting address bytes to delegator address: %s", err)
}
log.Println("Adding genesis account:", delegatorAddr)
log.Println("Amount:", GENESIS_ALLOCATION)
var stderr bytes.Buffer
cmd := exec.Command(BINARY_PATH, "add-genesis-account", delegatorAddr, GENESIS_ALLOCATION, "--keyring-backend", "test")
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
if strings.Contains(stderr.String(), "cannot add account at existing address") {
log.Printf("genesis account %s has already been added", delegatorAddr)
} else {
log.Fatalf("Error adding genesis account: %s", err)
}
}
// copy the gentx file to the node directory
err = copyFile(file, filepath.Join(gentxDir, filepath.Base(file)))
if err != nil {
log.Fatalf("Error copying gentx file to node directory: %s", err)
}
}
log.Println("Validating finished...")
log.Println("Collecting gentx...")
_, err = exec.Command(BINARY_PATH, "collect-gentxs").Output()
if err != nil {
log.Fatal("Error collecting gentxs: ", err)
}
log.Println("Validating gentx...")
_, err = exec.Command(BINARY_PATH, "validate-genesis").Output()
if err != nil {
log.Fatal("Error validating gentxs: ", err)
}
log.Println("Starting localnet...")
cmd := exec.Command(BINARY_PATH, "start")
err = cmd.Start()
if err != nil {
log.Fatal("Error starting localnet: ", err)
}
// wait for the chain to start
time.Sleep(10 * time.Second)
log.Println("Checking node status...")
cmd = exec.Command(BINARY_PATH, "status")
output, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("Error checking node status: %v, output: %s", err, output)
}
// revert the genesis time adjustment in the final genesis
if *generate {
log.Println("Reverting genesis time adjustment...")
_, err = modifyGenesisTime(SEDA_HOME+"/config/genesis.json", originalGenesisTime)
if err != nil {
log.Fatal("Error modifying genesis time: ", err)
}
}
log.Println("✅ ✅ ✅ Gentx validation passed successfully...")
}
func downloadFile(filepath string, url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
return err
}
func copyFile(src, dst string) error {
data, err := os.ReadFile(src)
if err != nil {
return err
}
err = os.WriteFile(dst, data, 0644)
if err != nil {
return err
}
return nil
}
func modifyGenesisTime(genesisFileNamePath, timestamp string) (string, error) {
genesisFile, err := os.ReadFile(genesisFileNamePath)
if err != nil {
log.Fatal(err)
}
var genesis map[string]interface{}
err = json.Unmarshal(genesisFile, &genesis)
if err != nil {
log.Fatal(err)
}
oldTimestamp := genesis["genesis_time"].(string)
genesis["genesis_time"] = timestamp
newGenesisFile, err := json.MarshalIndent(genesis, "", " ")
if err != nil {
log.Fatal(err)
}
err = os.WriteFile(genesisFileNamePath, newGenesisFile, 0644)
return oldTimestamp, err
}