/
tx.go
215 lines (173 loc) · 4.83 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
package cli
import (
"sort"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/ovrclk/akash/sdkutil"
akashtypes "github.com/ovrclk/akash/types/v1beta2"
atypes "github.com/ovrclk/akash/types/v1beta2"
types "github.com/ovrclk/akash/x/audit/types/v1beta2"
ptypes "github.com/ovrclk/akash/x/provider/types/v1beta2"
)
// 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: client.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 errors.Errorf("attributes must be provided as pairs")
}
providerAddress, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
attr, err := readAttributes(cmd, clientCtx, providerAddress.String(), args[1:])
if err != nil {
return err
}
if len(attr) == 0 {
return errors.Errorf("no attributes provided|found")
}
msg := &types.MsgSignProviderAttributes{
Auditor: clientCtx.GetFromAddress().String(),
Owner: providerAddress.String(),
Attributes: attr,
}
if err = msg.ValidateBasic(); err != nil {
return err
}
return sdkutil.BroadcastTX(cmd.Context(), clientCtx, cmd.Flags(), msg)
},
}
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 {
providerAddress, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
keys, err := readKeys(args[1:])
if err != nil {
return err
}
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg := &types.MsgDeleteProviderAttributes{
Auditor: clientCtx.GetFromAddress().String(),
Owner: providerAddress.String(),
Keys: keys,
}
if err = msg.ValidateBasic(); err != nil {
return err
}
return sdkutil.BroadcastTX(cmd.Context(), clientCtx, cmd.Flags(), msg)
},
}
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 client.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, errors.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, errors.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
}