-
Notifications
You must be signed in to change notification settings - Fork 1
/
make-mainnet-genesis.py
executable file
·213 lines (159 loc) · 6.39 KB
/
make-mainnet-genesis.py
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
#!/usr/bin/env python
import argparse
import json
import os
import subprocess
import sys
import time
import logging
import re
import random
# Symbols
chainID = 'kratos-mainnet'
mainChainSymbol = 'kratos'
coreCoinSymbol = 'kts'
coreCoinDenom = '%s/%s' % (mainChainSymbol, coreCoinSymbol)
cliCmd = 'kucli'
nodeCmd = 'kucd'
coinBase = 1000000000000000000
keyInit1 = 'kratos1xaaxd4p3ll0cgw3l0ujrasuz93knecaet9x9rn'
keyInit2 = 'kratos1ul7zrgzwwf90j7lxrla09hhcr456j5yae5xkml'
keyInit3 = 'kratos1v7mv73v0d4gac54pn22ja6qptuxp3u8flls09h'
# auths for test
auths = {}
pubkeys = {}
conpubkeys = {}
keyHexs = {}
# node info for test
nodes = {}
logging.basicConfig(level=logging.DEBUG)
args = None
def run(args):
logging.debug('%s', args)
if subprocess.call(args, shell=True):
logging.error('run \"%s\" error, exitting', args)
sys.exit(1)
def sleep(t):
time.sleep(t)
def run_output(args):
logging.debug('%s', args)
try:
out_bytes = subprocess.check_output(args, shell=True)
except subprocess.CalledProcessError as e:
out_bytes = e.output
out_text = out_bytes.decode('utf-8')
code = e.returncode
logging.error('run \"%s\" error by %d and %s', args, code, out_text)
sys.exit(1)
return out_bytes.decode('utf-8')
def cli(cmd, noKey = None):
cliParams = "--home %s/cli/ --keyring-backend test" % (args.home)
if noKey is not None:
cliParams = "--home %s/cli/ " % (args.home)
return run_output('%s/%s %s %s' % (args.build_path, cliCmd, cliParams, cmd))
def cliByHome(home, cmd):
cliParams = "--home %s" % (home)
return run_output('%s/%s %s %s' % (args.build_path, cliCmd, cliParams, cmd))
def getNodeHomePath(name):
return "%s/nodes/%s/" % (args.home, name)
def node(name, cmd):
cliParams = "--home %s" % (getNodeHomePath(name))
cmdRun = '%s/%s %s %s' % (args.build_path, nodeCmd, cliParams, cmd)
return run_output(cmdRun)
def nodeInBackground(name, logPath, cmd):
cliParams = "--home %s" % (getNodeHomePath(name))
cmdRun = '%s/%s %s %s' % (args.build_path, nodeCmd, cliParams, cmd)
with open(logPath, mode='w') as f:
f.write(cmdRun + '\n')
subprocess.Popen(cmdRun + ' 2>>' + logPath, shell=True)
def nodeByCli(name, cmd):
cliParams = "--home-client %s/cli/ --keyring-backend test" % (args.home)
return node(name, '%s %s' % (cliParams, cmd))
def coreCoin(amt):
return '%s%s' % (amt, coreCoinDenom)
def initWallet():
logging.debug("init wallet")
run('rm -rf %s/cli' % (args.home))
return
def genAuth(name):
addInfoJSON = cli('keys add ' + name)
infos = addInfoJSON.splitlines()
pubkey = infos[3].split(':')[1][1:]
valAuth = cli('keys show %s -a' % (name))[:-1]
keyHex = cli('parse --chain-id %s %s' % (chainID, pubkey), True)[:-1]
keyHex = keyHex.splitlines()[1].split(':')[1][1:]
keysInfos = cli('parse --chain-id %s %s' % (chainID, keyHex), True)
conpubkey = keysInfos.splitlines()[6][2:]
auths[name] = valAuth
pubkeys[name] = pubkey
conpubkeys[name] = conpubkey
keyHexs[name] = keyHex
return valAuth
def getAuth(name):
return auths[name]
def getNodeName(num):
return "validator%d" % (num)
def initGenesis(nodeNum):
# 1 kratos kratos1xaaxd4p3ll0cgw3l0ujrasuz93knecaet9x9rn 0
node(mainChainSymbol, 'genesis add-account %s %s' % ('kratos', keyInit1))
node(mainChainSymbol, 'genesis add-coin %s \"%s\"' % (coreCoin(0), "core token for kratos chain"))
node(mainChainSymbol, 'genesis add-account-coin %s %s' % ('kratos', coreCoin(25 * coinBase)))
# 2 kratos1xaaxd4p3ll0cgw3l0ujrasuz93knecaet9x9rn 50
node(mainChainSymbol, 'genesis add-address %s' % (keyInit1))
node(mainChainSymbol, 'genesis add-account-coin %s %s' % (keyInit1, coreCoin(25 * coinBase)))
# 3 initial@kratos kratos1ul7zrgzwwf90j7lxrla09hhcr456j5yae5xkml
node(mainChainSymbol, 'genesis add-account %s %s' % ('initial@kratos', keyInit2))
node(mainChainSymbol, 'genesis add-account-coin %s %s' % ('initial@kratos', coreCoin(100000000 * coinBase)))
# 4 foundation@kratos kratos1v7mv73v0d4gac54pn22ja6qptuxp3u8flls09h
node(mainChainSymbol, 'genesis add-account %s %s' % ('foundation@kratos', keyInit3))
node(mainChainSymbol, 'genesis add-account-coin %s %s' % ('foundation@kratos', coreCoin(40000000 * coinBase)))
genTx()
def modifyNodeCfg(name, key, oldValue, newValue=None):
file = "%s/config/config.toml" % (getNodeHomePath(name))
patternStr = r"^%s = .+" % (key)
newStr = '%s = %s' % (key, oldValue)
if (newValue is not None):
patternStr = r"^%s = %s" % (key, oldValue)
newStr = '%s = %s' % (key, newValue)
with open(file, "r") as f1, open("%s.bak" % file, "w") as f2:
for line in f1:
f2.write(re.sub(patternStr, newStr, line))
os.remove(file)
os.rename("%s.bak" % file, file)
def appendNodeCfg(name, key, value):
cliByHome(getNodeHomePath(name), "config %s %s" % (key, value))
def initNode(name, num):
run('rm -rf %s' % (getNodeHomePath(name)))
node(name, 'init --chain-id %s %s' % (chainID, name))
def initChain(nodeNum):
initNode(mainChainSymbol, 0)
initGenesis(nodeNum)
return
def genTx():
nodeByCli(mainChainSymbol, 'gentx %s %s --name %s ' % (mainChainSymbol, keyInit1, mainChainSymbol))
node(mainChainSymbol, 'collect-gentxs')
def message(msg, *args):
for arg in args:
print(msg + ' comes from ' + arg)
# Parse args
parser = argparse.ArgumentParser()
parser.add_argument('--build-path', metavar='', help='Kuchain build path', default='../build')
parser.add_argument('--home', metavar='', help='testnet data home path', default='./mainnet')
parser.add_argument('--trace', action='store_true', help='if --trace to kucd')
parser.add_argument('--log-level', metavar='', help='log level for kucd', default='*:info')
parser.add_argument('--node-num', type=int, metavar='', help='val node number', default=12)
parser.add_argument('--gen-auths', type=bool, metavar='', help='if use gen new auth for test', default=False)
args = parser.parse_args()
logging.debug("args %s", args)
# Start Chain
logging.info("start kuchain testnet by %s to %s", args.home, args.build_path)
run('rm -rf %s/' % (args.home))
initWallet()
if args.gen_auths:
keyInit1 = genAuth(mainChainSymbol)
keyInit2 = genAuth('a2')
keyInit3 = genAuth('a3')
initChain(int(args.node_num))
# rm tmp files
run('cp %s/nodes/kratos/config/genesis.json %s/' % (args.home, args.home))
run('cp -r %s/nodes/kratos/config/gentx %s/' % (args.home, args.home))