-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
322 lines (272 loc) · 11.7 KB
/
main.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import os
import time
import typer
from typing_extensions import Annotated
import utils
import websockets.sync.client as wsclient
import logging
from dotenv import dotenv_values
app = typer.Typer()
state = {
"ansibleDir": "ansible-avalanche-getting-started",
"opDir": "op-integration",
"cloudProvider": "multipass",
"terraformWorkingDir": "ansible-avalanche-getting-started/terraform/multipass",
"seqDownloadAddr": "https://github.com/AnomalyFi/nodekit-seq/releases/download",
"seqVersion": "0.9.5",
"inventoryDir": "inventories/local",
"gethProxyDir": "op-geth-proxy",
"nodekitL1Dir": "nodekit-l1",
"nodekitZKDir": "nodekit-zk",
"mnenoic": "test test test test test test test test test test test junk",
"l2storage": ".l2chains",
# if manual is enabled, seq and eth ip will be overrided accordingly
"manual": False,
"ethIP": "localhost",
"seqRPCUrl": "http://127.0.0.1:41009/ext/bc/22XkPgahTQDmpe4NpNiydVH1iSp6cyyutgjSapa6wYzCEdMkLy",
}
pjoin = os.path.join
@app.command()
def deploy():
print('reset avalanche node config')
utils.setDefaultAvaNodesConfig(state['ansibleDir'], state['inventoryDir'])
print('deploying eth l1')
utils.deployEthL1(state['ansibleDir'], state['inventoryDir'])
ethL1IP = utils.getEthL1IP(state['terraformWorkingDir'])
print('bootstraping avalanchego validators')
utils.bootstrapValidators(state['ansibleDir'], state['inventoryDir'])
# wait bootstraping to be stable
print('boostraping finished, waiting 30s to let it become stable to bootstrap a subnet')
time.sleep(30)
print('creating nodekit-seq subnet, here we are not directing stdout and stderr since we need to capture output')
log = utils.createAvalancheSubnet(state['ansibleDir'], state["inventoryDir"])
chainID = utils.getChainIDFromCreationLog(log)
subnetID = utils.getSubnetIDfromCreationLog(log)
print(f'updating avalanche nodes config chainID: {chainID}, subnetID: {subnetID}, ETH L1 IP: {ethL1IP}')
utils.updateTrackedSubnetNChainConfig(state['ansibleDir'], state['inventoryDir'], subnetID, chainID, ethL1IP)
print('provision avalanche nodes to start nodekit-seq subnet')
utils.provisionAvaNodes(state['ansibleDir'], state['inventoryDir'])
validatorIPs = utils.getValidatorIPs(state['terraformWorkingDir'])
seqRPCURL = f"http://{validatorIPs[0]}:9650/ext/bc/{chainID}"
# default timeout is 10(times) x 5s
seqIsUp = utils.wait_seq(seqRPCURL)
cnt = 0
retry = 3
# must restart avalanchego at least once
while not seqIsUp:
utils.restartAvalancheGo(state['ansibleDir'], state['inventoryDir'])
seqIsUp = utils.wait_seq(seqRPCURL)
cnt += 1
if cnt >= retry:
print("cannot launch nodekit-seq")
ethL1RPC = f'http://{ethL1IP}:8545'
ethL1WS = f'ws://{ethL1IP}:8546'
# deploy zk contracts
utils.deployNodekitZKContracts(state['nodekitZKDir'], ethL1RPC, mnenoic='test test test test test test test test test test test junk')
commitmentContractAddr = utils.getNodekitZKContractAddr(state['nodekitZKDir'])
print(f'zk contract deployed addr: {commitmentContractAddr}')
utils.deployNodekitL1(state['nodekitL1Dir'], seqRPCURL, ethL1RPC, commitmentContractAddr)
print('nodekit l1 deployed')
print('deploying op contracts')
utils.deployContractsOnL1(state['opDir'], ethL1RPC)
print('launching op stack')
utils.deployOPL2(state['opDir'], ethL1RPC, ethL1WS, seqRPCURL)
@app.command()
def launch_l2(chain_id: str):
validatorIPs = utils.getValidatorIPs(state['terraformWorkingDir'])
seqRPCURL = f"http://{validatorIPs[0]}:9650/ext/bc/{chain_id}"
ethL1IP = utils.getEthL1IP(state['terraformWorkingDir'])
ethL1RPC = f'http://{ethL1IP}:8545'
ethL1WS = f'ws://{ethL1IP}:8546'
# utils.deployContractsOnL1(state['opDir'], ethL1RPC)
utils.deployNodekitL1(state['opDir'], ethL1RPC, seqRPCURL)
utils.deployOPL2(state['opDir'], ethL1RPC, ethL1WS, seqRPCURL)
@app.command()
def deploy_seq():
print('reset avalanche node config')
utils.setDefaultAvaNodesConfig(state['ansibleDir'], state['inventoryDir'])
print('deploying eth l1')
utils.deployEthL1(state['ansibleDir'], state['inventoryDir'])
ethL1IP = utils.getEthL1IP(state['terraformWorkingDir'])
print('bootstraping avalanchego validators')
utils.bootstrapValidators(state['ansibleDir'], state['inventoryDir'])
# wait bootstraping to be stable
print('boostraping finished, waiting 30s to let it become stable to bootstrap a subnet')
time.sleep(30)
print('creating nodekit-seq subnet, here we are not directing stdout and stderr since we need to capture output')
log = utils.createAvalancheSubnet(state['ansibleDir'], state["inventoryDir"])
chainID = utils.getChainIDFromCreationLog(log)
subnetID = utils.getSubnetIDfromCreationLog(log)
print(f'updating avalanche nodes config chainID: {chainID}, subnetID: {subnetID}, ETH L1 IP: {ethL1IP}')
utils.updateTrackedSubnetNChainConfig(state['ansibleDir'], state['inventoryDir'], subnetID, chainID, ethL1IP)
print('provision avalanche nodes to start nodekit-seq subnet')
utils.provisionAvaNodes(state['ansibleDir'], state['inventoryDir'])
validatorIPs = utils.getValidatorIPs(state['terraformWorkingDir'])
seqRPCURL = f"http://{validatorIPs[0]}:9650/ext/bc/{chainID}"
# default timeout is 10(times) x 5s
seqIsUp = utils.wait_seq(seqRPCURL)
cnt = 0
retry = 3
# must restart avalanchego at least once
while not seqIsUp:
utils.restartAvalancheGo(state['ansibleDir'], state['inventoryDir'])
seqIsUp = utils.wait_seq(seqRPCURL)
cnt += 1
if cnt >= retry:
print("cannot launch nodekit-seq")
print(f'Deployed L1 at IP: {ethL1IP}, with default ports 8545 and 8546')
print(f'Deployed Seq with IPs: {validatorIPs}')
@app.command()
def deploy_zk_contracts():
l1IP = getETHIP()
ethL1RPC = f'http://{l1IP}:8545'
utils.deployNodekitZKContracts(state['nodekitZKDir'], ethL1RPC, mnenoic='test test test test test test test test test test test junk')
addr = utils.getNodekitZKContractAddr(state['nodekitZKDir'])
print(f'zk deployed addr: {addr}')
@app.command()
def deploy_op_contracts(l2_chain_id='45200'):
ethL1IP = getETHIP()
ethL1RPC = f'http://{ethL1IP}:8545'
commitmentAddr = utils.getNodekitZKContractAddr(state['nodekitZKDir'])
utils.deployContractsOnL1(state['opDir'], ethL1RPC, commitmentAddr, l2ChainID=l2_chain_id)
@app.command()
def deploy_nodekit_l1():
seqRPCUrl, _ = getSeqInfo()
commitmentAddr = utils.getNodekitZKContractAddr(state['nodekitZKDir'])
ethL1IP = getETHIP()
ethL1RPC = f'http://{ethL1IP}:8545'
utils.deployNodekitL1(state['nodekitL1Dir'], seqRPCUrl, ethL1RPC, commitmentAddr)
@app.command()
def deploy_op_l2(l2_chain_id='45200'):
seqRPCURL, _ = getSeqInfo()
ethL1IP = getETHIP()
ethL1RPC = f'http://{ethL1IP}:8545'
ethL1WS = f'ws://{ethL1IP}:8546'
utils.deployOPL2(state['opDir'], ethL1RPC, ethL1WS, seqRPCURL, l2ChainID=l2_chain_id)
@app.command()
def test_op_l2(inc='0', nvmPath=''):
l1IP = getETHIP()
l1RPC = f'http://{l1IP}:8545'
utils.testOPL2(state['opDir'], state['l2storage'], l1RPC=l1RPC, additionalPath=nvmPath, portIncrement=int(inc))
@app.command()
def deploy_op_chain(inc: int = 0):
l2_chain_id = str(45200 + inc)
print(f'deploying op chain with chainID: {l2_chain_id}')
# deploy op contracts
ethL1IP = getETHIP()
ethL1RPC = f'http://{ethL1IP}:8545'
ethL1WS = f'ws://{ethL1IP}:8546'
seqRPCURL, _ = getSeqInfo()
print('deploying op contracts')
commitmentAddr = utils.getNodekitZKContractAddr(state['nodekitZKDir'])
utils.deployContractsOnL1(state['opDir'], ethL1RPC, commitmentAddr, l2ChainID=l2_chain_id)
print('deploying op l2')
utils.deployOPL2(state['opDir'], state['gethProxyDir'], ethL1RPC, ethL1WS, seqRPCURL, l2ChainID=l2_chain_id, portIncrement=inc)
utils.saveOpDevnetInfo(state['opDir'], state['l2storage'], l2_chain_id)
@app.command()
def launch_celestia_light():
utils.deployCelestiaLightNode()
@app.command()
def create_ava_subnet():
log = utils.createAvalancheSubnet(state['ansibleDir'], state['inventoryDir'])
print(log)
@app.command()
def restart_avalanchego():
utils.restartAvalancheGo(state['ansibleDir'], state['inventoryDir'])
@app.command()
def hello(name: str):
print(f"hello {name}")
@app.command()
def get_eth_l1_ip():
ip = utils.getEthL1IP(state['terraformWorkingDir'])
print(f'ip: {ip}')
@app.command()
def get_validator_ips():
ips = utils.getValidatorIPs(state['terraformWorkingDir'])
print(f"ips :{ips}")
@app.command()
def seq_info():
rpcEndpoint = 'http://{ip}:9650/ext/bc/{id}'
chainID, ips = utils.getChainInfo(state['ansibleDir'], state['inventoryDir'], state['terraformWorkingDir'])
print(f'ChainID: {chainID}')
print('api endpoints:')
for ip in ips:
print(rpcEndpoint.format(ip=ip, id=chainID))
@app.command()
def init():
utils.download_seq(state['seqDownloadAddr'], state['seqVersion'])
@app.command()
def flags():
print(state)
@app.command()
def seq_healthy(url: str):
print(utils.seq_healthy(url))
@app.callback()
def main(
manual: bool = typer.Option(
prompt="manually input seq info and eth info, usually needed when you deploy the other rollup on another machine",
prompt_required=False,
default=False),
ansibleDir: str = typer.Option(
prompt="the ansible-avalanche-getting-started repository dir",
prompt_required=False,
default="ansible-avalanche-getting-started"),
cloudProvider: str = typer.Option(
prompt="the provider that deployed the vm instances",
prompt_required=False,
default="multipass"
),
# inventoryDir: str = typer.Option(
# prompt="inventory folder of ansible configs",
# prompt_required=False,
# default="inventories/local"
# ),
seqDownloadAddr: str = typer.Option(
prompt="nodekit-seq downloading address",
prompt_required=False,
default="https://github.com/AnomalyFi/nodekit-seq/releases/download"
),
seqVersion: str = typer.Option(
prompt="nodekit-seq version",
prompt_required=False,
default="0.9.7"
),
):
if cloudProvider == "multipass":
terraformWorkingDir = pjoin(ansibleDir, "terraform/multipass")
inventoryDir = "inventories/local"
print('using multipass cloud provider')
elif cloudProvider == "aws":
terraformWorkingDir = pjoin(ansibleDir, "terraform/aws")
inventoryDir = "inventories/aws"
print('using aws cloud provider')
else:
raise Exception("unsupported cloud provider")
if manual:
conf = dict(dotenv_values('.env'))
state['ethIP'] = conf.get('ETH_IP', state['ethIP'])
state['seqRPCUrl'] = conf.get('SEQ_RPC_URL', state['seqRPCUrl'])
print(f'using config from .env: {conf}')
state["ansibleDir"] = ansibleDir
state['cloudProvider'] = cloudProvider
state["terraformWorkingDir"] = terraformWorkingDir
state['seqDownloadAddr'] = seqDownloadAddr
state['seqVersion'] = seqVersion
state['inventoryDir'] = inventoryDir
state['manual'] = manual
def getSeqInfo():
if not state['manual']:
chainID, ips = utils.getChainInfo(state['ansibleDir'], state['inventoryDir'], state['terraformWorkingDir'])
seqRPCUrl = f'http://{ips[0]}:9650/ext/bc/{chainID}'
else:
seqRPCUrl: str = state['seqRPCUrl']
chainID = seqRPCUrl.split('/')[-1]
return seqRPCUrl, chainID
def getETHIP():
if not state['manual']:
ethL1IP = utils.getEthL1IP(state['terraformWorkingDir'])
else:
ethL1IP = state['ethIP']
return ethL1IP
if __name__ == "__main__":
app()