-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_node_raft.py
245 lines (203 loc) · 8.11 KB
/
add_node_raft.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
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
#!/Users/ariel/quorum-local/.pyenv/shims/python
import os
import sys
import json
import time
import errno
import pexpect
import subprocess
"""
建立區塊鏈節點
"""
def run_command(command):
p = subprocess.check_output(command, shell=True)
result = p.decode(sys.stdout.encoding)
return result
def change_dir(path):
if os.getcwd() != path:
print("Path before: " + os.getcwd())
os.chdir(path)
print("Path after: " + os.getcwd())
else:
print("Current path: " + os.getcwd())
def autofill(cmd):
child = pexpect.spawn(cmd)
child.expect("Password")
child.sendline("123")
child.expect("Repeat password")
child.sendline("123")
# print(child.before)
child.interact()
def add_node_cmd(node_num):
i = node_num+1
command = f"cat /Users/ariel/quorum-local/quorum/fromscratch/new-node-{i}/enode"
enode_id = run_command(command).replace("\n", "")
enode_url = (
f"enode://{enode_id}@127.0.0.1:{21000+i-1}?discport=0&raftport={50000+i-1}"
)
cmd = f"geth attach new-node-{node_num}/geth.ipc"
child = pexpect.spawn(cmd)
child.expect(">")
try:
child.sendline(f"raft.addPeer('{enode_url}')")
except OSError as exc:
if exc.errno != errno.EEXIST:
raise
child.sendline("exit")
child.interact()
def edit_genesis(keystore):
change_dir("/Users/ariel/quorum-local/quorum/fromscratch")
keystore = "0x" + keystore
print(keystore)
try:
with open(f"genesis.json", "r", encoding="utf8") as jfile:
jdata = json.load(jfile)
print(type(jdata["alloc"]))
print(jdata["alloc"])
alloc = jdata["alloc"]
with open(f"genesis.json", "w", encoding="utf8") as jfile:
d1 = {keystore: {"balance": "2000000000000000000000000000"}}
alloc.update(d1)
json.dump(jdata, jfile, indent=4)
# print("> ls\n" + run_command("ls"))
except OSError:
print("genesis.json not exist.")
def gen_key(i):
change_dir("/Users/ariel/quorum-local/quorum/fromscratch")
os.system(f"bootnode --genkey=nodekey{i}")
os.system(f"mv nodekey{i} new-node-{i}/nodekey")
os.system(
f"bootnode --nodekey=new-node-{i}/nodekey --writeaddress > new-node-{i}/enode"
)
# 先改 後複製
# 抓命令行輸出的結果
command = f"cat new-node-{i}/enode"
enode_id = run_command(command).replace("\n", "")
print(enode_id)
with open(f"new-node-{i-1}/static-nodes.json", "r", encoding="utf8") as jfile:
jdata = json.load(jfile)
jdata.append(
f"enode://{enode_id}@127.0.0.1:{21000+i-1}?discport=0&raftport={50000+i-1}"
)
print(jdata)
with open(f"new-node-{i}/static-nodes.json", "w", encoding="utf8") as jfile:
json.dump(jdata, jfile, indent=4)
# os.system(f"cp static-nodes.json new-node-{i}")
enode_url = (
f"enode://{enode_id}@127.0.0.1:{21000+i-1}?discport=0&raftport={50000+i-1}"
)
return enode_url
def init_node(i):
change_dir("/Users/ariel/quorum-local/quorum/fromscratch")
try:
os.system(f"geth --datadir new-node-{i} init genesis.json")
except OSError:
print("Init new node failed.")
def init():
"""
建立初始節點
"""
os.chdir("/Users/ariel/quorum-local")
os.system("git clone https://github.com/ConsenSys/quorum.git")
os.chdir(os.getcwd() + "/quorum/")
# os.chdir("~/quorum/")
os.system("make all")
# NOTE: Change PATH (先直接下指令改路徑)
path = R"${pwd}/build/bin:$PATH"
os.path.expandvars(path)
# os.system("export PATH=$(pwd)/build/bin:$PATH")
os.system("mkdir fromscratch")
# subprocess.Popen(['mkdir new-node-1', cwd="~/quorum/fromscratch/"])
os.chdir(os.getcwd() + "/fromscratch/")
# os.chdir('~/quorum/fromscratch/')
# Create new account & folder
os.system("mkdir new-node-1")
cmd = "geth --datadir new-node-1 account new"
autofill(cmd)
keystore = run_command("ls new-node-1/keystore").split("-")
keystore = keystore[len(keystore) - 1].replace("\n", "")
print(type(keystore))
print("account: ", keystore)
os.system(
"cp /Users/ariel/quorum-local/quorum_template/genesis_template.json genesis.json")
edit_genesis(keystore)
# Create enode_id & Edit static-node.json
os.system(
"cp /Users/ariel/quorum-local/quorum_template/static-nodes_template.json static-nodes.json"
)
# ADD NODEKEY
os.system("bootnode --genkey=nodekey")
os.system("cp nodekey new-node-1/")
os.system(
"bootnode --nodekey=new-node-1/nodekey --writeaddress > new-node-1/enode")
command = "cat new-node-1/enode"
enode_id = run_command(command).replace("\n", "")
print(enode_id)
with open(f"static-nodes.json", "r", encoding="utf8") as jfile:
jdata = json.load(jfile)
jdata.append(
f"enode://{enode_id}@127.0.0.1:21000?discport=0&raftport=50000")
print(jdata)
with open(f"static-nodes.json", "w", encoding="utf8") as jfile:
json.dump(jdata, jfile, indent=4)
os.system(f"cp static-nodes.json new-node-1")
# INIT NEW NODE
init_node(1)
os.system("mkdir log")
# Edit startnode.sh
os.system(
"cp /Users/ariel/quorum-local/quorum_template/startnode_template.sh startnode.sh")
# print("> ls\n" + run_command("ls -al"))
os.system("chmod +x startnode.sh")
# print("> ls\n" + run_command("ls -al"))
def add_node(num):
"""添加其餘節點
Args:
num (int): 輸入總節點數
"""
change_dir("/Users/ariel/quorum-local/quorum/fromscratch")
for i in range(2, num + 1):
try:
os.system(f"mkdir new-node-{i}")
# New ethereum account
cmd = f"geth --datadir new-node-{i} account new"
autofill(cmd)
# Get new account address
keystore = run_command(f"ls new-node-{i}/keystore").split("-")
keystore = keystore[len(keystore) - 1].replace("\n", "")
print(type(keystore))
print("account: ", keystore)
# edit_genesis(keystore)
# ADD NODEKEY
enode_url = gen_key(i)
# Init new node
init_node(i)
# Edit startnode.sh
with open(f"startnode.sh", "a+", encoding="utf8") as jfile:
jfile.write("\n")
# jfile.write(f"PRIVATE_CONFIG=ignore nohup geth --datadir new-node-{i} --nodiscover --verbosity 5 --networkid 31337 --raft --raftport {50000+i-1} --raftjoinexisting {i} --rpc --rpcaddr 0.0.0.0 --rpcport {22000+i-1} --rpcapi admin,db,eth,debug,miner,net,shh,txpool,personal,web3,quorum,raft --emitcheckpoints --port {21000+i-1} 2>>node{i}.log &")
# jfile.write(f"PRIVATE_CONFIG=ignore nohup geth --datadir new-node-{i} --verbosity 5 --networkid 31337 --raft --raftport {50000+i-1} --raftjoinexisting {i} --rpc --rpcaddr 0.0.0.0 --rpcport {22000+i-1} --rpcapi admin,db,eth,debug,miner,net,shh,txpool,personal,web3,quorum,raft --bootnodes --emitcheckpoints --port {21000+i-1} 2>>node{i}.log &")
jfile.write(
f"PRIVATE_CONFIG=ignore nohup geth --datadir new-node-{i} --nodiscover --verbosity 5 --networkid 31337 --raft --raftport {50000+i-1} --raftjoinexisting {i} --rpc --rpcaddr 0.0.0.0 --rpcport {22001+i-1} --rpcapi admin,db,eth,debug,miner,net,shh,txpool,personal,web3,quorum,raft --allow-insecure-unlock --targetgaslimit '9000000000000' --emitcheckpoints --port {21000+i-1} 2>>log/node{i}.log &"
)
jfile.write("\n")
except OSError as exc:
if exc.errno != errno.EEXIST:
raise
pass
# 生成節點,並複製到datadir
if __name__ == "__main__":
init()
# # 改gaslimit後重新初始化節點
# init_node(1)
# 新增其他節點
time.sleep(1)
total_num = 13
add_node(total_num)
run_command("killall geth")
time.sleep(1)
os.popen("sh /Users/ariel/quorum-local/quorum/fromscratch/startnode.sh")
os.system("ps")
# cmd in geth & Add node to node1
for i in range(2, total_num+1):
add_node_cmd(i-1)