-
Notifications
You must be signed in to change notification settings - Fork 210
/
tx.go
252 lines (200 loc) · 5.47 KB
/
tx.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
package cli
import (
"fmt"
"sort"
cltypes "github.com/akash-network/akash-api/go/node/client/types"
"github.com/spf13/cobra"
sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
types "github.com/akash-network/akash-api/go/node/audit/v1beta3"
ptypes "github.com/akash-network/akash-api/go/node/provider/v1beta3"
akashtypes "github.com/akash-network/akash-api/go/node/types/v1beta3"
atypes "github.com/akash-network/akash-api/go/node/types/v1beta3"
aclient "github.com/akash-network/node/client"
)
// GetTxCmd returns the transaction commands for audit module
func GetTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: "Audit transaction subcommands",
SuggestionsMinimumDistance: 2,
RunE: sdkclient.ValidateCmd,
}
cmd.AddCommand(
cmdAttributes(),
)
return cmd
}
func cmdAttributes() *cobra.Command {
cmd := &cobra.Command{
Use: "attr",
Short: "Manage provider attributes",
}
cmd.AddCommand(
cmdCreateProviderAttributes(),
cmdDeleteProviderAttributes(),
)
return cmd
}
func cmdCreateProviderAttributes() *cobra.Command {
cmd := &cobra.Command{
Use: "create [provider]",
Short: "Create/update provider attributes",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if ((len(args) - 1) % 2) != 0 {
return fmt.Errorf("attributes must be provided as pairs")
}
ctx := cmd.Context()
cctx, err := sdkclient.GetClientTxContext(cmd)
if err != nil {
return err
}
opts, err := cltypes.ClientOptionsFromFlags(cmd.Flags())
if err != nil {
return err
}
cl, err := aclient.DiscoverClient(ctx, cctx, opts...)
if err != nil {
return err
}
providerAddress, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
attr, err := readAttributes(cmd, cctx, providerAddress.String(), args[1:])
if err != nil {
return err
}
if len(attr) == 0 {
return fmt.Errorf("no attributes provided|found")
}
msg := &types.MsgSignProviderAttributes{
Auditor: cctx.GetFromAddress().String(),
Owner: providerAddress.String(),
Attributes: attr,
}
if err = msg.ValidateBasic(); err != nil {
return err
}
resp, err := cl.Tx().Broadcast(ctx, []sdk.Msg{msg})
if err != nil {
return err
}
return cl.PrintMessage(resp)
},
}
setCmdProviderFlags(cmd)
return cmd
}
func cmdDeleteProviderAttributes() *cobra.Command {
cmd := &cobra.Command{
Use: "delete [provider]",
Short: "Delete provider attributes",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
cctx, err := sdkclient.GetClientTxContext(cmd)
if err != nil {
return err
}
opts, err := cltypes.ClientOptionsFromFlags(cmd.Flags())
if err != nil {
return err
}
cl, err := aclient.DiscoverClient(ctx, cctx, opts...)
if err != nil {
return err
}
providerAddress, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
keys, err := readKeys(args[1:])
if err != nil {
return err
}
msg := &types.MsgDeleteProviderAttributes{
Auditor: cctx.GetFromAddress().String(),
Owner: providerAddress.String(),
Keys: keys,
}
if err = msg.ValidateBasic(); err != nil {
return err
}
resp, err := cl.Tx().Broadcast(ctx, []sdk.Msg{msg})
if err != nil {
return err
}
return cl.PrintMessage(resp)
},
}
setCmdProviderFlags(cmd)
return cmd
}
func setCmdProviderFlags(cmd *cobra.Command) {
flags.AddTxFlagsToCmd(cmd)
if err := cmd.MarkFlagRequired(flags.FlagFrom); err != nil {
panic(err.Error())
}
}
// readAttributes try read attributes from both cobra arguments or query
// if no arguments were provided then query provider and sign all found
// read from stdin uses trick to check if it's file descriptor is a pipe
// which happens when some data is piped for example cat attr.yaml | akash ...
func readAttributes(cmd *cobra.Command, cctx sdkclient.Context, provider string, args []string) (akashtypes.Attributes, error) {
var attr akashtypes.Attributes
if len(args) != 0 {
for i := 0; i < len(args); i += 2 {
attr = append(attr, atypes.Attribute{
Key: args[i],
Value: args[i+1],
})
}
} else {
resp, err := ptypes.NewQueryClient(cctx).Provider(cmd.Context(), &ptypes.QueryProviderRequest{Owner: provider})
if err != nil {
return nil, err
}
attr = append(attr, resp.Provider.Attributes...)
}
sort.SliceStable(attr, func(i, j int) bool {
return attr[i].Key < attr[j].Value
})
if checkAttributeDuplicates(attr) {
return nil, fmt.Errorf("supplied attributes with duplicate keys")
}
return attr, nil
}
func readKeys(args []string) ([]string, error) {
sort.SliceStable(args, func(i, j int) bool {
return args[i] < args[j]
})
if checkKeysDuplicates(args) {
return nil, fmt.Errorf("supplied attributes with duplicate keys")
}
return args, nil
}
func checkAttributeDuplicates(attr akashtypes.Attributes) bool {
keys := make(map[string]bool)
for _, entry := range attr {
if _, value := keys[entry.Key]; !value {
keys[entry.Key] = true
} else {
return true
}
}
return false
}
func checkKeysDuplicates(k []string) bool {
keys := make(map[string]bool)
for _, entry := range k {
if _, value := keys[entry]; !value {
keys[entry] = true
} else {
return true
}
}
return false
}