Skip to content

Commit 0c64659

Browse files
authored
Merge pull request #195 from meshplus/feat/add-rule-mgr-cmd
Feat/add rule mgr cmd
2 parents 261cc3c + fcb80a9 commit 0c64659

File tree

1 file changed

+203
-15
lines changed

1 file changed

+203
-15
lines changed

cmd/pier/rule.go

Lines changed: 203 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"fmt"
55
"io/ioutil"
66

7+
"github.com/fatih/color"
78
"github.com/meshplus/bitxhub-model/constant"
89
rpcx "github.com/meshplus/go-bitxhub-client"
9-
"github.com/meshplus/pier/internal/repo"
1010
"github.com/urfave/cli"
1111
)
1212

@@ -24,55 +24,243 @@ var ruleCMD = cli.Command{
2424
Required: true,
2525
},
2626
methodFlag,
27+
adminKeyPathFlag,
2728
},
2829
Action: deployRule,
2930
},
31+
{
32+
Name: "bind",
33+
Usage: "bind validation rule",
34+
Flags: []cli.Flag{
35+
cli.StringFlag{
36+
Name: "addr",
37+
Usage: "Specific rule addr",
38+
Required: true,
39+
},
40+
methodFlag,
41+
adminKeyPathFlag,
42+
},
43+
Action: bindRule,
44+
},
45+
{
46+
Name: "unbind",
47+
Usage: "unbind validation rule",
48+
Flags: []cli.Flag{
49+
cli.StringFlag{
50+
Name: "addr",
51+
Usage: "Specific rule addr",
52+
Required: true,
53+
},
54+
methodFlag,
55+
adminKeyPathFlag,
56+
},
57+
Action: unbindRule,
58+
},
59+
{
60+
Name: "freeze",
61+
Usage: "freeze validation rule",
62+
Flags: []cli.Flag{
63+
cli.StringFlag{
64+
Name: "addr",
65+
Usage: "Specific rule addr",
66+
Required: true,
67+
},
68+
methodFlag,
69+
adminKeyPathFlag,
70+
},
71+
Action: freezeRule,
72+
},
73+
{
74+
Name: "activate",
75+
Usage: "activate validation rule",
76+
Flags: []cli.Flag{
77+
cli.StringFlag{
78+
Name: "addr",
79+
Usage: "Specific rule addr",
80+
Required: true,
81+
},
82+
methodFlag,
83+
adminKeyPathFlag,
84+
},
85+
Action: activateRule,
86+
},
87+
{
88+
Name: "logout",
89+
Usage: "logout validation rule",
90+
Flags: []cli.Flag{
91+
cli.StringFlag{
92+
Name: "addr",
93+
Usage: "Specific rule addr",
94+
Required: true,
95+
},
96+
methodFlag,
97+
adminKeyPathFlag,
98+
},
99+
Action: logoutRule,
100+
},
30101
},
31102
}
32103

33104
func deployRule(ctx *cli.Context) error {
34105
rulePath := ctx.String("path")
35106
method := ctx.String("method")
107+
chainAdminKeyPath := ctx.String("admin-key")
36108

37-
repoRoot, err := repo.PathRootWithDefault(ctx.GlobalString("repo"))
109+
client, _, err := initClientWithKeyPath(ctx, chainAdminKeyPath)
110+
if err != nil {
111+
return fmt.Errorf("load client: %w", err)
112+
}
113+
114+
contract, err := ioutil.ReadFile(rulePath)
38115
if err != nil {
39116
return err
40117
}
41118

42-
config, err := repo.UnmarshalConfig(repoRoot)
119+
contractAddr, err := client.DeployContract(contract, nil)
43120
if err != nil {
44-
return fmt.Errorf("init config error: %s", err)
121+
color.Red("deploy rule error: %w", err)
122+
} else {
123+
color.Green(fmt.Sprintf("Deploy rule to bitxhub for appchain %s successfully: %s", method, contractAddr.String()))
45124
}
46125

47-
client, err := loadClient(repo.KeyPath(repoRoot), config.Mode.Relay.Addrs, ctx)
126+
return nil
127+
}
128+
129+
func bindRule(ctx *cli.Context) error {
130+
ruleAddr := ctx.String("addr")
131+
method := ctx.String("method")
132+
chainAdminKeyPath := ctx.String("admin-key")
133+
134+
client, _, err := initClientWithKeyPath(ctx, chainAdminKeyPath)
48135
if err != nil {
49136
return fmt.Errorf("load client: %w", err)
50137
}
51138

52-
contract, err := ioutil.ReadFile(rulePath)
139+
appchainMethod := fmt.Sprintf("%s:%s:.", bitxhubRootPrefix, method)
140+
receipt, err := client.InvokeBVMContract(
141+
constant.RuleManagerContractAddr.Address(),
142+
"BindRule", nil,
143+
rpcx.String(appchainMethod), rpcx.String(ruleAddr))
53144
if err != nil {
54-
return err
145+
return fmt.Errorf("Bind rule: %w", err)
55146
}
56147

57-
contractAddr, err := client.DeployContract(contract, nil)
148+
if !receipt.IsSuccess() {
149+
color.Red(fmt.Sprintf("Bind rule to bitxhub for appchain %s error: %s", appchainMethod, string(receipt.Ret)))
150+
} else {
151+
color.Green(fmt.Sprintf("Bind rule to bitxhub for appchain %s successfully, wait for proposal %s to finish.", appchainMethod, string(receipt.Ret)))
152+
}
153+
154+
return nil
155+
}
156+
157+
func unbindRule(ctx *cli.Context) error {
158+
ruleAddr := ctx.String("addr")
159+
method := ctx.String("method")
160+
chainAdminKeyPath := ctx.String("admin-key")
161+
162+
client, _, err := initClientWithKeyPath(ctx, chainAdminKeyPath)
163+
if err != nil {
164+
return fmt.Errorf("load client: %w", err)
165+
}
166+
167+
appchainMethod := fmt.Sprintf("%s:%s:.", bitxhubRootPrefix, method)
168+
receipt, err := client.InvokeBVMContract(
169+
constant.RuleManagerContractAddr.Address(),
170+
"UnbindRule", nil,
171+
rpcx.String(appchainMethod), rpcx.String(ruleAddr))
172+
if err != nil {
173+
return fmt.Errorf("Unind rule: %w", err)
174+
}
175+
176+
if !receipt.IsSuccess() {
177+
color.Red(fmt.Sprintf("Unbind rule to bitxhub for appchain %s error: %s", appchainMethod, string(receipt.Ret)))
178+
} else {
179+
color.Green(fmt.Sprintf("Unbind rule to bitxhub for appchain %s successfully, wait for proposal %s to finish.", appchainMethod, string(receipt.Ret)))
180+
}
181+
182+
return nil
183+
}
184+
185+
func freezeRule(ctx *cli.Context) error {
186+
ruleAddr := ctx.String("addr")
187+
method := ctx.String("method")
188+
chainAdminKeyPath := ctx.String("admin-key")
189+
190+
client, _, err := initClientWithKeyPath(ctx, chainAdminKeyPath)
191+
if err != nil {
192+
return fmt.Errorf("load client: %w", err)
193+
}
194+
195+
appchainMethod := fmt.Sprintf("%s:%s:.", bitxhubRootPrefix, method)
196+
receipt, err := client.InvokeBVMContract(
197+
constant.RuleManagerContractAddr.Address(),
198+
"FreezeRule", nil,
199+
rpcx.String(appchainMethod), rpcx.String(ruleAddr))
200+
if err != nil {
201+
return fmt.Errorf("freeze rule: %w", err)
202+
}
203+
204+
if !receipt.IsSuccess() {
205+
color.Red(fmt.Sprintf("Freeze rule to bitxhub for appchain %s error: %s", appchainMethod, string(receipt.Ret)))
206+
} else {
207+
color.Green(fmt.Sprintf("Freeze rule to bitxhub for appchain %s successfully, wait for proposal %s to finish.", appchainMethod, string(receipt.Ret)))
208+
}
209+
210+
return nil
211+
}
212+
213+
func activateRule(ctx *cli.Context) error {
214+
ruleAddr := ctx.String("addr")
215+
method := ctx.String("method")
216+
chainAdminKeyPath := ctx.String("admin-key")
217+
218+
client, _, err := initClientWithKeyPath(ctx, chainAdminKeyPath)
58219
if err != nil {
59-
return fmt.Errorf("deploy rule: %w", err)
220+
return fmt.Errorf("load client: %w", err)
221+
}
222+
223+
appchainMethod := fmt.Sprintf("%s:%s:.", bitxhubRootPrefix, method)
224+
receipt, err := client.InvokeBVMContract(
225+
constant.RuleManagerContractAddr.Address(),
226+
"ActivateRule", nil,
227+
rpcx.String(appchainMethod), rpcx.String(ruleAddr))
228+
if err != nil {
229+
return fmt.Errorf("activate rule: %w", err)
230+
}
231+
232+
if !receipt.IsSuccess() {
233+
color.Red(fmt.Sprintf("Activate rule to bitxhub for appchain %s error: %s", appchainMethod, string(receipt.Ret)))
234+
} else {
235+
color.Green(fmt.Sprintf("Activate rule to bitxhub for appchain %s successfully, wait for proposal %s to finish.", appchainMethod, string(receipt.Ret)))
236+
}
237+
238+
return nil
239+
}
240+
241+
func logoutRule(ctx *cli.Context) error {
242+
ruleAddr := ctx.String("addr")
243+
method := ctx.String("method")
244+
chainAdminKeyPath := ctx.String("admin-key")
245+
246+
client, _, err := initClientWithKeyPath(ctx, chainAdminKeyPath)
247+
if err != nil {
248+
return fmt.Errorf("load client: %w", err)
60249
}
61250

62251
appchainMethod := fmt.Sprintf("%s:%s:.", bitxhubRootPrefix, method)
63252
receipt, err := client.InvokeBVMContract(
64253
constant.RuleManagerContractAddr.Address(),
65-
"RegisterRule", nil,
66-
rpcx.String(appchainMethod), rpcx.String(contractAddr.String()))
254+
"LogoutRule", nil,
255+
rpcx.String(appchainMethod), rpcx.String(ruleAddr))
67256
if err != nil {
68-
return fmt.Errorf("register rule")
257+
return fmt.Errorf("logout rule: %w", err)
69258
}
70259

71-
fmt.Printf("Deploy rule for appchain %s to bitxhub successfully", method)
72260
if !receipt.IsSuccess() {
73-
fmt.Println("Deploy rule to bitxhub error: " + string(receipt.Ret))
261+
color.Red(fmt.Sprintf("Logout rule to bitxhub for appchain %s error: %s", appchainMethod, string(receipt.Ret)))
74262
} else {
75-
fmt.Println("Deploy rule to bitxhub successfully")
263+
color.Green(fmt.Sprintf("Logout rule to bitxhub for appchain %s successfully, wait for proposal %s to finish.", appchainMethod, string(receipt.Ret)))
76264
}
77265

78266
return nil

0 commit comments

Comments
 (0)