/
rule.go
189 lines (171 loc) · 4.7 KB
/
rule.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
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"github.com/fatih/color"
"github.com/meshplus/bitxhub-model/constant"
rpcx "github.com/meshplus/go-bitxhub-client"
"github.com/meshplus/pier/internal/repo"
"github.com/tidwall/gjson"
"github.com/urfave/cli"
)
var ruleCMD = cli.Command{
Name: "rule",
Usage: "Command about rule",
Subcommands: cli.Commands{
{
Name: "deploy",
Usage: "Deploy validation rule",
Flags: []cli.Flag{
cli.StringFlag{
Name: "path",
Usage: "Specify rule path",
Required: true,
},
},
Action: deployRule,
},
{
Name: "register",
Usage: "Register validation rule",
Flags: []cli.Flag{
appchainIdFlag,
appchainRuleFlag,
appchainRuleUrlFlag,
},
Action: registerRule,
},
{
Name: "update",
Usage: "update master rule",
Flags: []cli.Flag{
cli.StringFlag{
Name: "addr",
Usage: "Specify rule addr",
Required: true,
},
appchainIdFlag,
governanceReasonFlag,
},
Action: updateMasterRule,
},
{
Name: "logout",
Usage: "logout validation rule",
Flags: []cli.Flag{
cli.StringFlag{
Name: "addr",
Usage: "Specify rule addr",
Required: true,
},
appchainIdFlag,
},
Action: logoutRule,
},
},
}
func deployRule(ctx *cli.Context) error {
repoRoot, err := repo.PathRootWithDefault(ctx.GlobalString("repo"))
if err != nil {
return err
}
rulePath := ctx.String("path")
keyPath := filepath.Join(repoRoot, "key.json")
client, _, err := initClientWithKeyPath(ctx, keyPath)
if err != nil {
return fmt.Errorf("Load client: %w", err)
}
contract, err := ioutil.ReadFile(rulePath)
if err != nil {
return err
}
contractAddr, err := client.DeployContract(contract, nil)
if err != nil {
return fmt.Errorf("Deploy rule error: %w", err)
} else {
color.Green(fmt.Sprintf("Deploy rule successfully: %s", contractAddr.String()))
return nil
}
}
func registerRule(ctx *cli.Context) error {
repoRoot, err := repo.PathRootWithDefault(ctx.GlobalString("repo"))
if err != nil {
return err
}
appchainID := ctx.String("appchain-id")
rule_addr := ctx.String("rule")
rule_url := ctx.String("rule-url")
keyPath := filepath.Join(repoRoot, "key.json")
client, _, err := initClientWithKeyPath(ctx, keyPath)
if err != nil {
return fmt.Errorf("Load client: %w", err)
}
receipt, err := client.InvokeBVMContract(
constant.RuleManagerContractAddr.Address(),
"RegisterRule", nil,
rpcx.String(appchainID), rpcx.String(rule_addr), rpcx.String(rule_url))
if err != nil {
return fmt.Errorf("Register rule: %w", err)
}
if !receipt.IsSuccess() {
return fmt.Errorf("Register rule to bitxhub for appchain %s error: %s", appchainID, string(receipt.Ret))
} else {
color.Green(fmt.Sprintf("Register rule to bitxhub for appchain %s successfully.", appchainID))
return nil
}
}
func updateMasterRule(ctx *cli.Context) error {
repoRoot, err := repo.PathRootWithDefault(ctx.GlobalString("repo"))
if err != nil {
return err
}
ruleAddr := ctx.String("addr")
appchainID := ctx.String("appchain-id")
reason := ctx.String("reason")
keyPath := filepath.Join(repoRoot, "key.json")
client, _, err := initClientWithKeyPath(ctx, keyPath)
if err != nil {
return fmt.Errorf("Load client: %w", err)
}
receipt, err := client.InvokeBVMContract(
constant.RuleManagerContractAddr.Address(),
"UpdateMasterRule", nil,
rpcx.String(appchainID), rpcx.String(ruleAddr), rpcx.String(reason))
if err != nil {
return fmt.Errorf("Update master rule: %w", err)
}
if !receipt.IsSuccess() {
return fmt.Errorf("Update master rule to bitxhub for appchain %s error: %s", appchainID, string(receipt.Ret))
} else {
proposalId := gjson.Get(string(receipt.Ret), "proposal_id").String()
color.Green(fmt.Sprintf("Update master rule to bitxhub for appchain %s successfully, wait for proposal %s to finish.", appchainID, proposalId))
return nil
}
}
func logoutRule(ctx *cli.Context) error {
ruleAddr := ctx.String("addr")
appchainID := ctx.String("appchain-id")
repoRoot, err := repo.PathRootWithDefault(ctx.GlobalString("repo"))
if err != nil {
return err
}
keyPath := filepath.Join(repoRoot, "key.json")
client, _, err := initClientWithKeyPath(ctx, keyPath)
if err != nil {
return fmt.Errorf("Load client: %w", err)
}
receipt, err := client.InvokeBVMContract(
constant.RuleManagerContractAddr.Address(),
"LogoutRule", nil,
rpcx.String(appchainID), rpcx.String(ruleAddr))
if err != nil {
return fmt.Errorf("Logout rule: %w", err)
}
if !receipt.IsSuccess() {
return fmt.Errorf("Logout rule to bitxhub for appchain %s error: %s", appchainID, string(receipt.Ret))
} else {
color.Green("The logout request was submitted successfully\n")
return nil
}
}