-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaction_handler.go
292 lines (233 loc) · 9.16 KB
/
action_handler.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package async_sequencer
import (
"context"
"errors"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/action"
"github.com/code-payments/code-server/pkg/code/data/fulfillment"
)
var (
ErrInvalidActionStateTransition = errors.New("invalid action state transition")
)
type ActionHandler interface {
// Note: New fulfillment states are not recorded until the very end of a worker
// run, which is why we need to pass it into this function.
OnFulfillmentStateChange(ctx context.Context, fulfillmentRecord *fulfillment.Record, newState fulfillment.State) error
}
type OpenAccountActionHandler struct {
data code_data.Provider
}
func NewOpenAccountActionHandler(data code_data.Provider) ActionHandler {
return &OpenAccountActionHandler{
data: data,
}
}
func (h *OpenAccountActionHandler) OnFulfillmentStateChange(ctx context.Context, fulfillmentRecord *fulfillment.Record, newState fulfillment.State) error {
if fulfillmentRecord.FulfillmentType != fulfillment.InitializeLockedTimelockAccount {
return errors.New("unexpected fulfillment type")
}
if newState == fulfillment.StateConfirmed {
return markActionConfirmed(ctx, h.data, fulfillmentRecord.Intent, fulfillmentRecord.ActionId)
}
if newState == fulfillment.StateFailed {
return markActionFailed(ctx, h.data, fulfillmentRecord.Intent, fulfillmentRecord.ActionId)
}
return nil
}
type CloseEmptyAccountActionHandler struct {
data code_data.Provider
}
func NewCloseEmptyAccountActionHandler(data code_data.Provider) ActionHandler {
return &CloseEmptyAccountActionHandler{
data: data,
}
}
func (h *CloseEmptyAccountActionHandler) OnFulfillmentStateChange(ctx context.Context, fulfillmentRecord *fulfillment.Record, newState fulfillment.State) error {
if fulfillmentRecord.FulfillmentType != fulfillment.CloseEmptyTimelockAccount {
return errors.New("unexpected fulfillment type")
}
if newState == fulfillment.StateConfirmed {
return markActionConfirmed(ctx, h.data, fulfillmentRecord.Intent, fulfillmentRecord.ActionId)
}
if newState == fulfillment.StateFailed {
return markActionFailed(ctx, h.data, fulfillmentRecord.Intent, fulfillmentRecord.ActionId)
}
return nil
}
type CloseDormantAccountActionHandler struct {
data code_data.Provider
}
func NewCloseDormantAccountActionHandler(data code_data.Provider) ActionHandler {
return &CloseDormantAccountActionHandler{
data: data,
}
}
func (h *CloseDormantAccountActionHandler) OnFulfillmentStateChange(ctx context.Context, fulfillmentRecord *fulfillment.Record, newState fulfillment.State) error {
if fulfillmentRecord.FulfillmentType != fulfillment.CloseDormantTimelockAccount {
return errors.New("unexpected fulfillment type")
}
if newState == fulfillment.StateConfirmed {
return markActionConfirmed(ctx, h.data, fulfillmentRecord.Intent, fulfillmentRecord.ActionId)
}
if newState == fulfillment.StateFailed {
return markActionFailed(ctx, h.data, fulfillmentRecord.Intent, fulfillmentRecord.ActionId)
}
return nil
}
type NoPrivacyTransferActionHandler struct {
data code_data.Provider
}
func NewNoPrivacyTransferActionHandler(data code_data.Provider) ActionHandler {
return &NoPrivacyTransferActionHandler{
data: data,
}
}
func (h *NoPrivacyTransferActionHandler) OnFulfillmentStateChange(ctx context.Context, fulfillmentRecord *fulfillment.Record, newState fulfillment.State) error {
if fulfillmentRecord.FulfillmentType != fulfillment.NoPrivacyTransferWithAuthority {
return errors.New("unexpected fulfillment type")
}
if newState == fulfillment.StateConfirmed {
return markActionConfirmed(ctx, h.data, fulfillmentRecord.Intent, fulfillmentRecord.ActionId)
}
if newState == fulfillment.StateFailed {
return markActionFailed(ctx, h.data, fulfillmentRecord.Intent, fulfillmentRecord.ActionId)
}
return nil
}
type NoPrivacyWithdrawActionHandler struct {
data code_data.Provider
}
func NewNoPrivacyWithdrawActionHandler(data code_data.Provider) ActionHandler {
return &NoPrivacyWithdrawActionHandler{
data: data,
}
}
func (h *NoPrivacyWithdrawActionHandler) OnFulfillmentStateChange(ctx context.Context, fulfillmentRecord *fulfillment.Record, newState fulfillment.State) error {
if fulfillmentRecord.FulfillmentType != fulfillment.NoPrivacyWithdraw {
return errors.New("unexpected fulfillment type")
}
if newState == fulfillment.StateConfirmed {
return markActionConfirmed(ctx, h.data, fulfillmentRecord.Intent, fulfillmentRecord.ActionId)
}
if newState == fulfillment.StateFailed {
return markActionFailed(ctx, h.data, fulfillmentRecord.Intent, fulfillmentRecord.ActionId)
}
return nil
}
type PrivateTransferActionHandler struct {
data code_data.Provider
}
func NewPrivateTransferActionHandler(data code_data.Provider) ActionHandler {
return &PrivateTransferActionHandler{
data: data,
}
}
// There's many fulfillments for a private transfer action, so we define success
// and failure purely based on our ability to move funds to/from the user accounts.
func (h *PrivateTransferActionHandler) OnFulfillmentStateChange(ctx context.Context, fulfillmentRecord *fulfillment.Record, newState fulfillment.State) error {
switch fulfillmentRecord.FulfillmentType {
case fulfillment.TemporaryPrivacyTransferWithAuthority, fulfillment.PermanentPrivacyTransferWithAuthority:
if newState == fulfillment.StateConfirmed {
return markActionConfirmed(ctx, h.data, fulfillmentRecord.Intent, fulfillmentRecord.ActionId)
}
if newState == fulfillment.StateFailed {
return markActionFailed(ctx, h.data, fulfillmentRecord.Intent, fulfillmentRecord.ActionId)
}
case fulfillment.TransferWithCommitment:
// No handling of confirmed state, since the other end of the split transfer
// can't be run until the advance from the treasury is made to the destination.
if newState == fulfillment.StateFailed {
return markActionFailed(ctx, h.data, fulfillmentRecord.Intent, fulfillmentRecord.ActionId)
}
case fulfillment.InitializeCommitmentProof, fulfillment.UploadCommitmentProof, fulfillment.VerifyCommitmentProof, fulfillment.OpenCommitmentVault, fulfillment.CloseCommitmentVault:
// Don't care about commitment states. These are managed elsewhere.
return nil
default:
return errors.New("unexpected fulfillment type")
}
return nil
}
type SaveRecentRootActionHandler struct {
data code_data.Provider
}
func NewSaveRecentRootActionHandler(data code_data.Provider) ActionHandler {
return &SaveRecentRootActionHandler{
data: data,
}
}
func (h *SaveRecentRootActionHandler) OnFulfillmentStateChange(ctx context.Context, fulfillmentRecord *fulfillment.Record, newState fulfillment.State) error {
if fulfillmentRecord.FulfillmentType != fulfillment.SaveRecentRoot {
return errors.New("unexpected fulfillment type")
}
if newState == fulfillment.StateConfirmed {
return markActionConfirmed(ctx, h.data, fulfillmentRecord.Intent, fulfillmentRecord.ActionId)
}
if newState == fulfillment.StateFailed {
return markActionFailed(ctx, h.data, fulfillmentRecord.Intent, fulfillmentRecord.ActionId)
}
return nil
}
func validateActionState(record *action.Record, states ...action.State) error {
for _, validState := range states {
if record.State == validState {
return nil
}
}
return ErrInvalidActionStateTransition
}
func markActionConfirmed(ctx context.Context, data code_data.Provider, intentId string, actionId uint32) error {
record, err := data.GetActionById(ctx, intentId, actionId)
if err != nil {
return err
}
if record.State == action.StateConfirmed {
return nil
}
err = validateActionState(record, action.StatePending)
if err != nil {
return err
}
record.State = action.StateConfirmed
return data.UpdateAction(ctx, record)
}
func markActionFailed(ctx context.Context, data code_data.Provider, intentId string, actionId uint32) error {
record, err := data.GetActionById(ctx, intentId, actionId)
if err != nil {
return err
}
if record.State == action.StateFailed {
return nil
}
err = validateActionState(record, action.StatePending)
if err != nil {
return err
}
record.State = action.StateFailed
return data.UpdateAction(ctx, record)
}
func markActionRevoked(ctx context.Context, data code_data.Provider, intentId string, actionId uint32) error {
record, err := data.GetActionById(ctx, intentId, actionId)
if err != nil {
return err
}
if record.State == action.StateRevoked {
return nil
}
err = validateActionState(record, action.StateUnknown)
if err != nil {
return err
}
record.State = action.StateRevoked
return data.UpdateAction(ctx, record)
}
func getActionHandlers(data code_data.Provider) map[action.Type]ActionHandler {
handlersByType := make(map[action.Type]ActionHandler)
handlersByType[action.OpenAccount] = NewOpenAccountActionHandler(data)
handlersByType[action.CloseEmptyAccount] = NewCloseEmptyAccountActionHandler(data)
handlersByType[action.CloseDormantAccount] = NewCloseDormantAccountActionHandler(data)
handlersByType[action.NoPrivacyTransfer] = NewNoPrivacyTransferActionHandler(data)
handlersByType[action.NoPrivacyWithdraw] = NewNoPrivacyWithdrawActionHandler(data)
handlersByType[action.PrivateTransfer] = NewPrivateTransferActionHandler(data)
handlersByType[action.SaveRecentRoot] = NewSaveRecentRootActionHandler(data)
return handlersByType
}