Skip to content

Commit 64e27dc

Browse files
author
Sandy Zhou
authored
Merge pull request #218 from meshplus/fix/adapt-to-bitxhub
fix(*): fix bugs that do not fit the BitXHub
2 parents 211c5d0 + c01a28c commit 64e27dc

File tree

6 files changed

+174
-161
lines changed

6 files changed

+174
-161
lines changed

cmd/pier/appchain_bxh.go

Lines changed: 81 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,22 @@ var appchainBxhCMD = cli.Command{
2121
Name: "appchain",
2222
Usage: "Command about appchain in bitxhub",
2323
Subcommands: []cli.Command{
24-
methodCommand,
24+
//methodCommand,
2525
serviceCommand,
2626
didCommand,
2727
{
2828
Name: "register",
29-
Usage: "Register pier to bitxhub",
29+
Usage: "Register appchain to bitxhub",
3030
Flags: []cli.Flag{
31-
methodFlag,
31+
adminKeyPathFlag,
32+
appchainIdFlag,
33+
appchainNameFlag,
34+
appchainTypeFlag,
35+
appchainDescFlag,
36+
appchainVersionFlag,
37+
appchainValidatorFlag,
38+
appchainConsensusFlag,
39+
governanceReasonFlag,
3240
},
3341
Action: registerPier,
3442
},
@@ -39,19 +47,9 @@ var appchainBxhCMD = cli.Command{
3947
adminKeyPathFlag,
4048
cli.StringFlag{
4149
Name: "id",
42-
Usage: "Specify appchain id(did)",
50+
Usage: "Specify appchain id",
4351
Required: true,
4452
},
45-
cli.StringFlag{
46-
Name: "doc-addr",
47-
Usage: "Specify appchain did doc addr",
48-
Required: false,
49-
},
50-
cli.StringFlag{
51-
Name: "doc-hash",
52-
Usage: "Specify appchain did doc hash",
53-
Required: false,
54-
},
5553
cli.StringFlag{
5654
Name: "name",
5755
Usage: "Specify appchain name",
@@ -82,6 +80,7 @@ var appchainBxhCMD = cli.Command{
8280
Usage: "Specify appchain consensus type",
8381
Required: false,
8482
},
83+
governanceReasonFlag,
8584
},
8685
Action: updateAppchain,
8786
},
@@ -92,7 +91,7 @@ var appchainBxhCMD = cli.Command{
9291
adminKeyPathFlag,
9392
cli.StringFlag{
9493
Name: "id",
95-
Usage: "Specify appchain id(did)",
94+
Usage: "Specify appchain id",
9695
Required: true,
9796
},
9897
},
@@ -105,7 +104,7 @@ var appchainBxhCMD = cli.Command{
105104
adminKeyPathFlag,
106105
cli.StringFlag{
107106
Name: "id",
108-
Usage: "Specify appchain id(did)",
107+
Usage: "Specify appchain id",
109108
Required: true,
110109
},
111110
},
@@ -118,9 +117,10 @@ var appchainBxhCMD = cli.Command{
118117
adminKeyPathFlag,
119118
cli.StringFlag{
120119
Name: "id",
121-
Usage: "Specify appchain id(did)",
120+
Usage: "Specify appchain id",
122121
Required: true,
123122
},
123+
governanceReasonFlag,
124124
},
125125
Action: logoutAppchain,
126126
},
@@ -131,7 +131,7 @@ var appchainBxhCMD = cli.Command{
131131
adminKeyPathFlag,
132132
cli.StringFlag{
133133
Name: "id",
134-
Usage: "Specify appchain id(did)",
134+
Usage: "Specify appchain id",
135135
Required: true,
136136
},
137137
},
@@ -141,21 +141,64 @@ var appchainBxhCMD = cli.Command{
141141
}
142142

143143
func registerPier(ctx *cli.Context) error {
144-
// todo: add register pier logic
144+
id := ctx.String("appchain-id")
145+
chainAdminKeyPath := ctx.String("admin-key")
146+
name := ctx.String("name")
147+
typ := ctx.String("type")
148+
desc := ctx.String("desc")
149+
version := ctx.String("version")
150+
validatorsPath := ctx.String("validators")
151+
consensus := ctx.String("consensus")
152+
reason := ctx.String("reason")
153+
validatorData, err := ioutil.ReadFile(validatorsPath)
154+
if err != nil {
155+
return fmt.Errorf("read validators file: %w", err)
156+
}
157+
158+
// get repo public key
159+
pubKey, err := getPubKey(chainAdminKeyPath)
160+
if err != nil {
161+
return fmt.Errorf("get public key: %w", err)
162+
}
163+
client, _, err := initClientWithKeyPath(ctx, chainAdminKeyPath)
164+
if err != nil {
165+
return err
166+
}
167+
168+
receipt, err := client.InvokeBVMContract(
169+
constant.AppchainMgrContractAddr.Address(),
170+
"Register", nil,
171+
rpcx.String(id),
172+
rpcx.String(""), rpcx.String(""),
173+
rpcx.String(string(validatorData)), rpcx.String(consensus), rpcx.String(typ),
174+
rpcx.String(name), rpcx.String(desc), rpcx.String(version),
175+
rpcx.String(pubKey),
176+
rpcx.String(reason),
177+
)
178+
if err != nil {
179+
return fmt.Errorf("invoke bvm contract: %w", err)
180+
}
181+
if !receipt.IsSuccess() {
182+
return fmt.Errorf("register method info faild: %s", string(receipt.Ret))
183+
}
184+
ret := &GovernanceResult{}
185+
if err := json.Unmarshal(receipt.Ret, ret); err != nil {
186+
return err
187+
}
188+
fmt.Printf("Register appchain for %s successfully, wait for proposal %s to finish.\n", string(ret.Extra), ret.ProposalID)
145189
return nil
146190
}
147191

148192
func updateAppchain(ctx *cli.Context) error {
149193
chainAdminKeyPath := ctx.String("admin-key")
150194
id := ctx.String("id")
151-
docAddr := ctx.String("doc-addr")
152-
docHash := ctx.String("doc-hash")
153195
name := ctx.String("name")
154196
typ := ctx.String("type")
155197
desc := ctx.String("desc")
156198
version := ctx.String("version")
157199
validatorsPath := ctx.String("validators")
158200
consensusType := ctx.String("consensus-type")
201+
reason := ctx.String("reason")
159202

160203
client, _, err := initClientWithKeyPath(ctx, chainAdminKeyPath)
161204
if err != nil {
@@ -178,12 +221,6 @@ func updateAppchain(ctx *cli.Context) error {
178221
if err = json.Unmarshal(receipt.Ret, &appchainInfo); err != nil {
179222
return err
180223
}
181-
if docAddr == "" {
182-
docAddr = appchainInfo.DidDocAddr
183-
}
184-
if docHash == "" {
185-
docHash = appchainInfo.DidDocHash
186-
}
187224
if name == "" {
188225
name = appchainInfo.Name
189226
}
@@ -219,15 +256,16 @@ func updateAppchain(ctx *cli.Context) error {
219256
constant.AppchainMgrContractAddr.Address(),
220257
"UpdateAppchain", nil,
221258
rpcx.String(id),
222-
rpcx.String(docAddr),
223-
rpcx.String(docHash),
259+
rpcx.String(""),
260+
rpcx.String(""),
224261
rpcx.String(validators),
225262
rpcx.String(consensusType),
226263
rpcx.String(typ),
227264
rpcx.String(name),
228265
rpcx.String(desc),
229266
rpcx.String(version),
230267
rpcx.String(string(pubKey)),
268+
rpcx.String(reason),
231269
)
232270
if err != nil {
233271
return fmt.Errorf("invoke bvm contract: %w", err)
@@ -258,7 +296,7 @@ func freezeAppchain(ctx *cli.Context) error {
258296

259297
receipt, err := client.InvokeBVMContract(
260298
constant.AppchainMgrContractAddr.Address(),
261-
"FreezeAppchain", nil, rpcx.String(id),
299+
"FreezeAppchain", nil, rpcx.String(id), rpcx.String(""),
262300
)
263301
if err != nil {
264302
return fmt.Errorf("invoke bvm contract: %w", err)
@@ -269,7 +307,11 @@ func freezeAppchain(ctx *cli.Context) error {
269307
}
270308

271309
proposalId := gjson.Get(string(receipt.Ret), "proposal_id").String()
272-
fmt.Printf("the freeze request was submitted successfully, proposal id is %s\n", proposalId)
310+
if proposalId != "" {
311+
fmt.Printf("the freeze request was submitted successfully, proposal id is %s\n", proposalId)
312+
} else {
313+
fmt.Printf("the freeze request was submitted successfully\n")
314+
}
273315

274316
return nil
275317
}
@@ -285,7 +327,7 @@ func activateAppchain(ctx *cli.Context) error {
285327

286328
receipt, err := client.InvokeBVMContract(
287329
constant.AppchainMgrContractAddr.Address(),
288-
"ActivateAppchain", nil, rpcx.String(id),
330+
"ActivateAppchain", nil, rpcx.String(id), rpcx.String(""),
289331
)
290332
if err != nil {
291333
return fmt.Errorf("invoke bvm contract: %w", err)
@@ -296,14 +338,19 @@ func activateAppchain(ctx *cli.Context) error {
296338
}
297339

298340
proposalId := gjson.Get(string(receipt.Ret), "proposal_id").String()
299-
fmt.Printf("the activate request was submitted successfully, proposal id is %s\n", proposalId)
341+
if proposalId != "" {
342+
fmt.Printf("the activate request was submitted successfully, proposal id is %s\n", proposalId)
343+
} else {
344+
fmt.Printf("the activate request was submitted successfully\n")
345+
}
300346

301347
return nil
302348
}
303349

304350
func logoutAppchain(ctx *cli.Context) error {
305351
chainAdminKeyPath := ctx.String("admin-key")
306352
id := ctx.String("id")
353+
reason := ctx.String("reason")
307354

308355
client, _, err := initClientWithKeyPath(ctx, chainAdminKeyPath)
309356
if err != nil {
@@ -312,7 +359,7 @@ func logoutAppchain(ctx *cli.Context) error {
312359

313360
receipt, err := client.InvokeBVMContract(
314361
constant.AppchainMgrContractAddr.Address(),
315-
"LogoutAppchain", nil, rpcx.String(id),
362+
"LogoutAppchain", nil, rpcx.String(id), rpcx.String(reason),
316363
)
317364
if err != nil {
318365
return fmt.Errorf("invoke bvm contract: %w", err)

cmd/pier/flags.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ var (
4242
}
4343

4444
// appchain info related flags
45+
appchainIdFlag = cli.StringFlag{
46+
Name: "appchain-id",
47+
Usage: "Specify appchain id",
48+
Required: true,
49+
}
50+
4551
appchainNameFlag = cli.StringFlag{
4652
Name: "name",
4753
Usage: "Specific appchain name",
@@ -72,4 +78,9 @@ var (
7278
Usage: "Specific appchain consensus type",
7379
Required: true,
7480
}
81+
governanceReasonFlag = cli.StringFlag{
82+
Name: "reason",
83+
Usage: "Specify governance reason",
84+
Required: false,
85+
}
7586
)

cmd/pier/governance.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var governanceCMD = cli.Command{
2222
Usage: "proposal id",
2323
Required: true,
2424
},
25+
governanceReasonFlag,
2526
},
2627
Action: withdraw,
2728
},
@@ -31,6 +32,7 @@ var governanceCMD = cli.Command{
3132
func withdraw(ctx *cli.Context) error {
3233
chainAdminKeyPath := ctx.String("admin-key")
3334
id := ctx.String("id")
35+
reason := ctx.String("reason")
3436

3537
client, _, err := initClientWithKeyPath(ctx, chainAdminKeyPath)
3638
if err != nil {
@@ -39,7 +41,7 @@ func withdraw(ctx *cli.Context) error {
3941

4042
receipt, err := client.InvokeBVMContract(
4143
constant.GovernanceContractAddr.Address(),
42-
"WithdrawProposal", nil, rpcx.String(id),
44+
"WithdrawProposal", nil, rpcx.String(id), rpcx.String(reason),
4345
)
4446
if err != nil {
4547
return fmt.Errorf("invoke bvm contract: %w", err)

0 commit comments

Comments
 (0)