-
Notifications
You must be signed in to change notification settings - Fork 36
/
action_builder.go
273 lines (239 loc) · 7.52 KB
/
action_builder.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
var (
_ ActionBuilder = &TransferActionBuilder{}
_ ActionBuilder = &DelegateActionBuilder{}
_ ActionBuilder = &VoteActionBuilder{}
)
// ActionBuilder defines functions used to collect events to check against specific actions.
type ActionBuilder interface {
// GetEventCriteria returns the event criteria for this action.
GetEventCriteria() *EventCriteria
// AddEvent adds an event to this builder in preparation for checking events against the criteria.
AddEvent(eventType string, attributes *map[string][]byte) error
// CanBuild returns true if this builder has enough event information for an evaluation result.
CanBuild() bool
// BuildAction builds the action and returns an EvaluationResult.
// This should only be called when CanBuild returns true.
// This should return an error if CanBuild is false or some other error is encountered.
BuildAction() (EvaluationResult, error)
// Reset clears out any previous event data, returning the builder to it's initial state.
Reset()
}
type TransferActionBuilder struct {
Sender sdk.AccAddress
Action string
Recipient sdk.AccAddress
}
func (b *TransferActionBuilder) GetEventCriteria() *EventCriteria {
return NewEventCriteria([]ABCIEvent{
{
Type: sdk.EventTypeMessage,
Attributes: map[string][]byte{
sdk.AttributeKeyAction: []byte(sdk.MsgTypeURL(&banktypes.MsgSend{})),
},
},
{
Type: banktypes.EventTypeTransfer,
Attributes: map[string][]byte{},
},
})
}
func (b *TransferActionBuilder) AddEvent(eventType string, attributes *map[string][]byte) error {
switch eventType {
case sdk.EventTypeMessage:
// Update the result with the senders address
address := (*attributes)[banktypes.AttributeKeySender]
action := (*attributes)[sdk.AttributeKeyAction]
if address != nil {
address, err := sdk.AccAddressFromBech32(string(address))
if err != nil {
return err
}
b.Sender = address
}
if action != nil {
b.Action = string(action)
}
case banktypes.EventTypeTransfer:
// Update the result with the senders address
address := (*attributes)[banktypes.AttributeKeySender]
addressSenderAddr, err := sdk.AccAddressFromBech32(string(address))
if err != nil {
return err
}
b.Sender = addressSenderAddr
addressRecipient := (*attributes)[banktypes.AttributeKeyRecipient]
addressRecipientAddr, errFromParsingRecipientAddress := sdk.AccAddressFromBech32(string(addressRecipient))
if errFromParsingRecipientAddress != nil {
return errFromParsingRecipientAddress
}
b.Recipient = addressRecipientAddr
}
return nil
}
func (b *TransferActionBuilder) CanBuild() bool {
return !b.Sender.Empty() && !(len(b.Action) == 0) && !b.Recipient.Empty()
}
func (b *TransferActionBuilder) BuildAction() (EvaluationResult, error) {
if !b.CanBuild() {
return EvaluationResult{}, fmt.Errorf("missing sender or recipient or action from transfer action")
}
result := EvaluationResult{
Shares: 1,
Address: b.Sender,
Recipient: b.Recipient,
}
return result, nil
}
func (b *TransferActionBuilder) Reset() {
b.Sender = sdk.AccAddress{}
}
type DelegateActionBuilder struct {
Validator sdk.ValAddress
Delegator sdk.AccAddress
}
func (b *DelegateActionBuilder) GetEventCriteria() *EventCriteria {
return NewEventCriteria([]ABCIEvent{
{
Type: sdk.EventTypeMessage,
Attributes: map[string][]byte{
sdk.AttributeKeyModule: []byte(stakingtypes.ModuleName),
},
},
{
Type: stakingtypes.EventTypeDelegate,
Attributes: map[string][]byte{},
},
{
Type: stakingtypes.EventTypeCreateValidator,
Attributes: map[string][]byte{},
},
})
}
func (b *DelegateActionBuilder) AddEvent(eventType string, attributes *map[string][]byte) error {
switch eventType {
case stakingtypes.EventTypeDelegate:
address := (*attributes)[stakingtypes.AttributeKeyValidator]
validator, err := sdk.ValAddressFromBech32(string(address))
if err != nil {
return err
}
b.Validator = validator
case stakingtypes.EventTypeCreateValidator:
address := (*attributes)[stakingtypes.AttributeKeyValidator]
validator, err := sdk.ValAddressFromBech32(string(address))
if err != nil {
return err
}
b.Validator = validator
case sdk.EventTypeMessage:
// Update the last result to have the delegator's address
address := (*attributes)[banktypes.AttributeKeySender]
address, err := sdk.AccAddressFromBech32(string(address))
if err != nil {
return err
}
b.Delegator = address
}
return nil
}
func (b *DelegateActionBuilder) CanBuild() bool {
return !b.Validator.Empty() && !b.Delegator.Empty()
}
func (b *DelegateActionBuilder) BuildAction() (EvaluationResult, error) {
if !b.CanBuild() {
return EvaluationResult{}, fmt.Errorf("missing delegator or validator from delegate action")
}
result := EvaluationResult{
Shares: 1,
Address: b.Delegator,
Delegator: b.Delegator,
Validator: b.Validator,
}
return result, nil
}
func (b *DelegateActionBuilder) Reset() {
b.Validator = sdk.ValAddress{}
b.Delegator = sdk.AccAddress{}
}
type VoteActionBuilder struct {
Voter sdk.AccAddress
Voted bool
}
func (v *VoteActionBuilder) CanBuild() bool {
return !v.Voter.Empty() && v.Voted
}
func (v *VoteActionBuilder) BuildAction() (EvaluationResult, error) {
if !v.CanBuild() {
return EvaluationResult{}, fmt.Errorf("missing voter address from vote action")
}
result := EvaluationResult{
Shares: 1,
Address: v.Voter,
}
return result, nil
}
func (v *VoteActionBuilder) Reset() {
v.Voter = sdk.AccAddress{}
v.Voted = false
}
func (v *VoteActionBuilder) GetEventCriteria() *EventCriteria {
return NewEventCriteria([]ABCIEvent{
{
Type: sdk.EventTypeMessage,
Attributes: map[string][]byte{sdk.AttributeKeyModule: []byte(govtypes.AttributeValueCategory)},
},
{
Type: sdk.EventTypeMessage,
Attributes: map[string][]byte{sdk.AttributeKeyAction: nil},
},
})
}
// govVoteMsgURLs the MsgURLs of all the governance module's vote messages.
// Use getGovVoteMsgURLs() instead of using this variable directly.
var govVoteMsgURLs []string
// getGovVoteMsgURLs returns govVoteMsgURLs, but first sets it if it hasn't yet been set.
func getGovVoteMsgURLs() []string {
// Checking for nil here (as opposed to len == 0) because we only want to set it
// if it hasn't been set yet.
if govVoteMsgURLs == nil {
// sdk.MsgTypeURL sometimes uses reflection and/or proto registration.
// So govVoteMsgURLs is only set when it's finally needed in the hopes
// that everything's wired up as needed by then.
govVoteMsgURLs = []string{
sdk.MsgTypeURL(&govtypesv1.MsgVote{}),
sdk.MsgTypeURL(&govtypesv1.MsgVoteWeighted{}),
sdk.MsgTypeURL(&govtypesv1beta1.MsgVote{}),
sdk.MsgTypeURL(&govtypesv1beta1.MsgVoteWeighted{}),
}
}
return govVoteMsgURLs
}
func (v *VoteActionBuilder) AddEvent(_ string, attributes *map[string][]byte) error {
if _, ok := (*attributes)[sdk.AttributeKeyModule]; ok {
address := (*attributes)[banktypes.AttributeKeySender]
address, err := sdk.AccAddressFromBech32(string(address))
if err != nil {
return err
}
v.Voter = address
} else if action, ok := (*attributes)[sdk.AttributeKeyAction]; ok {
a := string(action)
for _, m := range getGovVoteMsgURLs() {
if a == m {
v.Voted = true
break
}
}
}
return nil
}