/
deploy.go
496 lines (449 loc) · 15 KB
/
deploy.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package ethtxs
import (
"context"
"crypto/ecdsa"
"fmt"
"math/big"
"time"
"github.com/33cn/chain33/common/log/log15"
"github.com/33cn/plugin/plugin/dapp/x2ethereum/ebrelayer/ethcontract/generated"
"github.com/33cn/plugin/plugin/dapp/x2ethereum/ebrelayer/ethinterface"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
)
var (
deployLog = log15.New("contract deployer", "deployer")
)
//DeployResult ...
type DeployResult struct {
Address common.Address
TxHash string
}
//X2EthContracts ...
type X2EthContracts struct {
BridgeRegistry *generated.BridgeRegistry
BridgeBank *generated.BridgeBank
Chain33Bridge *generated.Chain33Bridge
Valset *generated.Valset
Oracle *generated.Oracle
}
//X2EthDeployInfo ...
type X2EthDeployInfo struct {
BridgeRegistry *DeployResult
BridgeBank *DeployResult
Chain33Bridge *DeployResult
Valset *DeployResult
Oracle *DeployResult
}
//DeployPara ...
type DeployPara struct {
DeployPrivateKey *ecdsa.PrivateKey
Deployer common.Address
Operator common.Address
InitValidators []common.Address
ValidatorPriKey []*ecdsa.PrivateKey
InitPowers []*big.Int
}
//OperatorInfo ...
type OperatorInfo struct {
PrivateKey *ecdsa.PrivateKey
Address common.Address
}
//DeployValset : 部署Valset
func DeployValset(client ethinterface.EthClientSpec, privateKey *ecdsa.PrivateKey, deployer common.Address, operator common.Address, initValidators []common.Address, initPowers []*big.Int) (*generated.Valset, *DeployResult, error) {
auth, err := PrepareAuth(client, privateKey, deployer)
if nil != err {
return nil, nil, err
}
//部署合约
addr, tx, valset, err := generated.DeployValset(auth, client, operator, initValidators, initPowers)
if err != nil {
return nil, nil, err
}
deployResult := &DeployResult{
Address: addr,
TxHash: tx.Hash().String(),
}
return valset, deployResult, nil
}
//DeployChain33Bridge : 部署Chain33Bridge
func DeployChain33Bridge(client ethinterface.EthClientSpec, privateKey *ecdsa.PrivateKey, deployer common.Address, operator, valset common.Address) (*generated.Chain33Bridge, *DeployResult, error) {
auth, err := PrepareAuth(client, privateKey, deployer)
if nil != err {
return nil, nil, err
}
//部署合约
addr, tx, chain33Bridge, err := generated.DeployChain33Bridge(auth, client, operator, valset)
if err != nil {
return nil, nil, err
}
deployResult := &DeployResult{
Address: addr,
TxHash: tx.Hash().String(),
}
return chain33Bridge, deployResult, nil
}
//DeployOracle : 部署Oracle
func DeployOracle(client ethinterface.EthClientSpec, privateKey *ecdsa.PrivateKey, deployer, operator, valset, chain33Bridge common.Address) (*generated.Oracle, *DeployResult, error) {
auth, err := PrepareAuth(client, privateKey, deployer)
if nil != err {
return nil, nil, err
}
//部署合约
addr, tx, oracle, err := generated.DeployOracle(auth, client, operator, valset, chain33Bridge)
if err != nil {
return nil, nil, err
}
deployResult := &DeployResult{
Address: addr,
TxHash: tx.Hash().String(),
}
return oracle, deployResult, nil
}
//DeployBridgeBank : 部署BridgeBank
func DeployBridgeBank(client ethinterface.EthClientSpec, privateKey *ecdsa.PrivateKey, deployer, operator, oracle, chain33Bridge common.Address) (*generated.BridgeBank, *DeployResult, error) {
auth, err := PrepareAuth(client, privateKey, deployer)
if nil != err {
return nil, nil, err
}
//部署合约
addr, tx, bridgeBank, err := generated.DeployBridgeBank(auth, client, operator, oracle, chain33Bridge)
if err != nil {
return nil, nil, err
}
deployResult := &DeployResult{
Address: addr,
TxHash: tx.Hash().String(),
}
return bridgeBank, deployResult, nil
}
//DeployBridgeRegistry : 部署BridgeRegistry
func DeployBridgeRegistry(client ethinterface.EthClientSpec, privateKey *ecdsa.PrivateKey, deployer, chain33BridgeAddr, bridgeBankAddr, oracleAddr, valsetAddr common.Address) (*generated.BridgeRegistry, *DeployResult, error) {
auth, err := PrepareAuth(client, privateKey, deployer)
if nil != err {
return nil, nil, err
}
//部署合约
addr, tx, bridgeRegistry, err := generated.DeployBridgeRegistry(auth, client, chain33BridgeAddr, bridgeBankAddr, oracleAddr, valsetAddr)
if err != nil {
return nil, nil, err
}
deployResult := &DeployResult{
Address: addr,
TxHash: tx.Hash().String(),
}
return bridgeRegistry, deployResult, nil
}
//DeployAndInit ...
func DeployAndInit(client ethinterface.EthClientSpec, para *DeployPara) (*X2EthContracts, *X2EthDeployInfo, error) {
x2EthContracts := &X2EthContracts{}
deployInfo := &X2EthDeployInfo{}
var err error
/////////////////////////////////////
sim, isSim := client.(*ethinterface.SimExtend)
if isSim {
fmt.Println("Use the simulator")
} else {
fmt.Println("Use the actual Ethereum")
}
x2EthContracts.Valset, deployInfo.Valset, err = DeployValset(client, para.DeployPrivateKey, para.Deployer, para.Operator, para.InitValidators, para.InitPowers)
if nil != err {
deployLog.Error("DeployAndInit", "failed to DeployValset due to:", err.Error())
return nil, nil, err
}
if isSim {
sim.Commit()
} else {
fmt.Println("\nDeployValset tx hash:", deployInfo.Valset.TxHash)
timeout := time.NewTimer(300 * time.Second)
oneSecondtimeout := time.NewTicker(5 * time.Second)
for {
select {
case <-timeout.C:
panic("DeployValset timeout")
case <-oneSecondtimeout.C:
_, err := client.TransactionReceipt(context.Background(), common.HexToHash(deployInfo.Valset.TxHash))
if err == ethereum.NotFound {
fmt.Println("\n No receipt received yet for DeployValset tx and continue to wait")
continue
} else if err != nil {
panic("DeployValset failed due to" + err.Error())
}
callopts := &bind.CallOpts{
Pending: true,
From: para.Deployer,
Context: context.Background(),
}
operator, err := x2EthContracts.Valset.Operator(callopts)
if nil != err {
panic(err.Error())
}
if operator.String() != para.Operator.String() {
fmt.Printf("operator queried from valset is:%s, and setted is:%s", operator.String(), para.Operator.String())
panic("operator query is not same as setted ")
}
goto deployChain33Bridge
}
}
}
deployChain33Bridge:
x2EthContracts.Chain33Bridge, deployInfo.Chain33Bridge, err = DeployChain33Bridge(client, para.DeployPrivateKey, para.Deployer, para.Operator, deployInfo.Valset.Address)
if nil != err {
deployLog.Error("DeployAndInit", "failed to DeployChain33Bridge due to:", err.Error())
return nil, nil, err
}
if isSim {
sim.Commit()
} else {
fmt.Printf("\n\nGoing to DeployChain33Bridge with valset address:%s", deployInfo.Valset.Address.String())
fmt.Println("\nDeployChain33Bridge tx hash:", deployInfo.Chain33Bridge.TxHash)
timeout := time.NewTimer(300 * time.Second)
oneSecondtimeout := time.NewTicker(5 * time.Second)
for {
select {
case <-timeout.C:
panic("DeployChain33Bridge timeout")
case <-oneSecondtimeout.C:
_, err := client.TransactionReceipt(context.Background(), common.HexToHash(deployInfo.Chain33Bridge.TxHash))
if err == ethereum.NotFound {
fmt.Println("\n No receipt received for DeployChain33Bridge tx and continue to wait")
continue
} else if err != nil {
panic("DeployChain33Bridge failed due to" + err.Error())
}
callopts := &bind.CallOpts{
Pending: true,
From: para.Deployer,
Context: context.Background(),
}
operator, err := x2EthContracts.Chain33Bridge.Operator(callopts)
if nil != err {
panic(err.Error())
}
if operator.String() != para.Operator.String() {
fmt.Printf("operator queried from valset is:%s, and setted is:%s", operator.String(), para.Operator.String())
panic("operator query is not same as setted ")
}
goto deployOracle
}
}
}
deployOracle:
x2EthContracts.Oracle, deployInfo.Oracle, err = DeployOracle(client, para.DeployPrivateKey, para.Deployer, para.Operator, deployInfo.Valset.Address, deployInfo.Chain33Bridge.Address)
if nil != err {
deployLog.Error("DeployAndInit", "failed to DeployOracle due to:", err.Error())
return nil, nil, err
}
if isSim {
sim.Commit()
} else {
fmt.Println("DeployOracle tx hash:", deployInfo.Oracle.TxHash)
timeout := time.NewTimer(300 * time.Second)
oneSecondtimeout := time.NewTicker(5 * time.Second)
for {
select {
case <-timeout.C:
panic("DeployOracle timeout")
case <-oneSecondtimeout.C:
_, err := client.TransactionReceipt(context.Background(), common.HexToHash(deployInfo.Oracle.TxHash))
if err == ethereum.NotFound {
fmt.Println("\n No receipt received for DeployOracle tx and continue to wait")
continue
} else if err != nil {
panic("DeployOracle failed due to" + err.Error())
}
callopts := &bind.CallOpts{
Pending: true,
From: para.Deployer,
Context: context.Background(),
}
operator, err := x2EthContracts.Oracle.Operator(callopts)
if nil != err {
panic(err.Error())
}
if operator.String() != para.Operator.String() {
fmt.Printf("operator queried from valset is:%s, and setted is:%s", operator.String(), para.Operator.String())
panic("operator query is not same as setted ")
}
goto deployBridgeBank
}
}
}
/////////////////////////////////////
deployBridgeBank:
x2EthContracts.BridgeBank, deployInfo.BridgeBank, err = DeployBridgeBank(client, para.DeployPrivateKey, para.Deployer, para.Operator, deployInfo.Oracle.Address, deployInfo.Chain33Bridge.Address)
if nil != err {
deployLog.Error("DeployAndInit", "failed to DeployBridgeBank due to:", err.Error())
return nil, nil, err
}
if isSim {
sim.Commit()
} else {
fmt.Println("DeployBridgeBank tx hash:", deployInfo.BridgeBank.TxHash)
timeout := time.NewTimer(300 * time.Second)
oneSecondtimeout := time.NewTicker(5 * time.Second)
for {
select {
case <-timeout.C:
panic("DeployBridgeBank timeout")
case <-oneSecondtimeout.C:
_, err := client.TransactionReceipt(context.Background(), common.HexToHash(deployInfo.BridgeBank.TxHash))
if err == ethereum.NotFound {
fmt.Println("\n No receipt received for DeployOracle tx and continue to wait")
continue
} else if err != nil {
panic("DeployBridgeBank failed due to" + err.Error())
}
callopts := &bind.CallOpts{
Pending: true,
From: para.Deployer,
Context: context.Background(),
}
operator, err := x2EthContracts.BridgeBank.Operator(callopts)
if nil != err {
panic(err.Error())
}
if operator.String() != para.Operator.String() {
fmt.Printf("operator queried from valset is:%s, and setted is:%s", operator.String(), para.Operator.String())
panic("operator query is not same as setted ")
}
goto settingBridgeBank
}
}
}
settingBridgeBank:
////////////////////////
auth, err := PrepareAuth(client, para.DeployPrivateKey, para.Deployer)
if nil != err {
return nil, nil, err
}
setBridgeBankTx, err := x2EthContracts.Chain33Bridge.SetBridgeBank(auth, deployInfo.BridgeBank.Address)
if nil != err {
deployLog.Error("DeployAndInit", "failed to SetBridgeBank due to:", err.Error())
return nil, nil, err
}
if isSim {
sim.Commit()
} else {
fmt.Println("setBridgeBankTx tx hash:", setBridgeBankTx.Hash().String())
timeout := time.NewTimer(300 * time.Second)
oneSecondtimeout := time.NewTicker(5 * time.Second)
for {
select {
case <-timeout.C:
panic("setBridgeBankTx timeout")
case <-oneSecondtimeout.C:
_, err := client.TransactionReceipt(context.Background(), setBridgeBankTx.Hash())
if err == ethereum.NotFound {
fmt.Println("\n No receipt received for setBridgeBankTx tx and continue to wait")
continue
} else if err != nil {
panic("setBridgeBankTx failed due to" + err.Error())
}
callopts := &bind.CallOpts{
Pending: true,
From: para.Deployer,
Context: context.Background(),
}
yes, err := x2EthContracts.Chain33Bridge.HasBridgeBank(callopts)
if nil != err {
panic(err.Error())
}
if !yes {
fmt.Printf("BridgeBank doesn't exist")
panic("BridgeBank doesn't exist")
}
goto setOracle
}
}
}
setOracle:
auth, err = PrepareAuth(client, para.DeployPrivateKey, para.Deployer)
if nil != err {
return nil, nil, err
}
setOracleTx, err := x2EthContracts.Chain33Bridge.SetOracle(auth, deployInfo.Oracle.Address)
if nil != err {
deployLog.Error("DeployAndInit", "failed to SetOracle due to:", err.Error())
return nil, nil, err
}
if isSim {
sim.Commit()
} else {
fmt.Println("setOracleTx tx hash:", setOracleTx.Hash().String())
timeout := time.NewTimer(300 * time.Second)
oneSecondtimeout := time.NewTicker(5 * time.Second)
for {
select {
case <-timeout.C:
panic("setBridgeBankTx timeout")
case <-oneSecondtimeout.C:
_, err := client.TransactionReceipt(context.Background(), setOracleTx.Hash())
if err == ethereum.NotFound {
fmt.Println("\n No receipt received for setOracleTx tx and continue to wait")
continue
} else if err != nil {
panic("setOracleTx failed due to" + err.Error())
}
callopts := &bind.CallOpts{
Pending: true,
From: para.Deployer,
Context: context.Background(),
}
yes, err := x2EthContracts.Chain33Bridge.HasOracle(callopts)
if nil != err {
panic(err.Error())
}
if !yes {
fmt.Printf("oracle doesn't exist")
panic("oracle doesn't exist")
}
goto deployBridgeRegistry
}
}
}
deployBridgeRegistry:
x2EthContracts.BridgeRegistry, deployInfo.BridgeRegistry, err = DeployBridgeRegistry(client, para.DeployPrivateKey, para.Deployer, deployInfo.Chain33Bridge.Address, deployInfo.BridgeBank.Address, deployInfo.Oracle.Address, deployInfo.Valset.Address)
if nil != err {
deployLog.Error("DeployAndInit", "failed to DeployBridgeBank due to:", err.Error())
return nil, nil, err
}
if isSim {
sim.Commit()
} else {
fmt.Println("DeployBridgeRegistry tx hash:", deployInfo.BridgeRegistry.TxHash)
timeout := time.NewTimer(300 * time.Second)
oneSecondtimeout := time.NewTicker(5 * time.Second)
for {
select {
case <-timeout.C:
panic("DeployBridgeRegistry timeout")
case <-oneSecondtimeout.C:
_, err := client.TransactionReceipt(context.Background(), common.HexToHash(deployInfo.BridgeRegistry.TxHash))
if err == ethereum.NotFound {
fmt.Println("\n No receipt received for DeployOracle tx and continue to wait")
continue
} else if err != nil {
panic("DeployBridgeRegistry failed due to" + err.Error())
}
callopts := &bind.CallOpts{
Pending: true,
From: para.Deployer,
Context: context.Background(),
}
oracleAddr, err := x2EthContracts.BridgeRegistry.Oracle(callopts)
if nil != err {
panic(err.Error())
}
if oracleAddr.String() != deployInfo.Oracle.Address.String() {
fmt.Printf("oracleAddr queried from BridgeRegistry is:%s, and setted is:%s", oracleAddr.String(), deployInfo.Oracle.Address.String())
panic("oracleAddr query is not same as setted ")
}
goto finished
}
}
}
finished:
return x2EthContracts, deployInfo, nil
}