-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfulfillment.go
296 lines (247 loc) · 8 KB
/
fulfillment.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
293
294
295
296
package fulfillment
import (
"time"
"github.com/pkg/errors"
"github.com/code-payments/code-server/pkg/phone"
"github.com/code-payments/code-server/pkg/pointer"
"github.com/code-payments/code-server/pkg/code/data/action"
"github.com/code-payments/code-server/pkg/code/data/intent"
)
var (
ErrFulfillmentNotFound = errors.New("no records could be found")
ErrFulfillmentExists = errors.New("fulfillment exists")
ErrInvalidFulfillment = errors.New("invalid fulfillment")
)
type Type uint8
const (
UnknownType Type = iota
InitializeLockedTimelockAccount
NoPrivacyTransferWithAuthority
NoPrivacyWithdraw
TemporaryPrivacyTransferWithAuthority
PermanentPrivacyTransferWithAuthority
TransferWithCommitment
CloseEmptyTimelockAccount
CloseDormantTimelockAccount
SaveRecentRoot
InitializeCommitmentProof
UploadCommitmentProof
VerifyCommitmentProof // Deprecated, since we bundle verification with OpenCommitmentVault
OpenCommitmentVault
CloseCommitmentVault
)
type State uint8
const (
StateUnknown State = iota // not scheduled
StatePending // submitted to the solana network
StateRevoked // tx not submitted and revoked
StateConfirmed // tx confirmed
StateFailed // tx failed
)
type Record struct {
Id uint64
Intent string
IntentType intent.Type
ActionId uint32
ActionType action.Type
FulfillmentType Type
Data []byte
Signature *string
Nonce *string
Blockhash *string
Source string // Source token account involved in the transaction
Destination *string // Destination token account involved in the transaction, when it makes sense (eg. transfers)
// Metadata required to pre-sort fulfillments for scheduling
//
// This is a 3-tiered sorting heurstic. At each tier, f1 < f2 when index1 < index2.
// We move down in tiers when the current level tier matches. The order of tiers is
// intent, then action, then fullfillment.
IntentOrderingIndex uint64 // Typically, but not always, the FK to the intent ID
ActionOrderingIndex uint32 // Typically, but not always, the FK of the action ID
FulfillmentOrderingIndex uint32
// Does the fulfillment worker poll for this record? If true, it's up to other
// systems to hint as to when polling can occur. This is primarily an optimization
// to reduce redundant processing. This doesn't affect correctness of scheduling
// (eg. depedencies), so accidentally making some actively scheduled is ok.
DisableActiveScheduling bool
// Metadata required to help make antispam decisions
InitiatorPhoneNumber *string
State State
CreatedAt time.Time
}
type BySchedulingOrder []*Record
func (a BySchedulingOrder) Len() int { return len(a) }
func (a BySchedulingOrder) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a BySchedulingOrder) Less(i, j int) bool { return a[i].ScheduledBefore(a[j]) }
func (r *Record) IsFulfilled() bool {
return r.State == StateConfirmed
}
func (r *Record) ScheduledBefore(other *Record) bool {
if other == nil {
return true
}
if r.Id == other.Id {
return false
}
if r.IntentOrderingIndex > other.IntentOrderingIndex {
return false
} else if r.IntentOrderingIndex < other.IntentOrderingIndex {
return true
}
if r.ActionOrderingIndex > other.ActionOrderingIndex {
return false
} else if r.ActionOrderingIndex < other.ActionOrderingIndex {
return true
}
return r.FulfillmentOrderingIndex < other.FulfillmentOrderingIndex
}
func (r *Record) Clone() Record {
var data []byte
if r.Data != nil {
data = make([]byte, len(r.Data))
copy(data, r.Data)
}
return Record{
Id: r.Id,
Intent: r.Intent,
IntentType: r.IntentType,
ActionId: r.ActionId,
ActionType: r.ActionType,
FulfillmentType: r.FulfillmentType,
Data: data,
Signature: pointer.StringCopy(r.Signature),
Nonce: pointer.StringCopy(r.Nonce),
Blockhash: pointer.StringCopy(r.Blockhash),
Source: r.Source,
Destination: pointer.StringCopy(r.Destination),
IntentOrderingIndex: r.IntentOrderingIndex,
ActionOrderingIndex: r.ActionOrderingIndex,
FulfillmentOrderingIndex: r.FulfillmentOrderingIndex,
DisableActiveScheduling: r.DisableActiveScheduling,
InitiatorPhoneNumber: pointer.StringCopy(r.InitiatorPhoneNumber),
State: r.State,
CreatedAt: r.CreatedAt,
}
}
func (r *Record) CopyTo(dst *Record) {
dst.Id = r.Id
dst.Intent = r.Intent
dst.IntentType = r.IntentType
dst.ActionId = r.ActionId
dst.ActionType = r.ActionType
dst.FulfillmentType = r.FulfillmentType
dst.Data = r.Data
dst.Signature = r.Signature
dst.Nonce = r.Nonce
dst.Blockhash = r.Blockhash
dst.Source = r.Source
dst.Destination = r.Destination
dst.IntentOrderingIndex = r.IntentOrderingIndex
dst.ActionOrderingIndex = r.ActionOrderingIndex
dst.FulfillmentOrderingIndex = r.FulfillmentOrderingIndex
dst.InitiatorPhoneNumber = r.InitiatorPhoneNumber
dst.DisableActiveScheduling = r.DisableActiveScheduling
dst.State = r.State
dst.CreatedAt = r.CreatedAt
}
func (r *Record) Validate() error {
if len(r.Intent) == 0 {
return errors.New("intent is required")
}
if r.IntentType == intent.UnknownType {
return errors.New("intent type is required")
}
if r.ActionType == action.UnknownType {
return errors.New("action type is required")
}
if r.FulfillmentType == UnknownType {
return errors.New("fulfillment type is required")
}
if r.Signature != nil && len(*r.Signature) == 0 {
return errors.New("signature is required when set")
}
if len(r.Data) != 0 && r.Signature == nil {
return errors.New("signature must be set when data is present")
}
if r.Nonce != nil && len(*r.Nonce) == 0 {
return errors.New("nonce is required when set")
}
if r.Blockhash != nil && len(*r.Blockhash) == 0 {
return errors.New("blockhash is required when set")
}
if (r.Nonce == nil) != (r.Blockhash == nil) {
return errors.New("nonce and blockhash must be set or not set at the same time")
}
if len(r.Source) == 0 {
return errors.New("source token account is required")
}
if r.Destination != nil && len(*r.Destination) == 0 {
return errors.New("destination token account is empty")
}
// todo: validate intent, action and fulfillment type all align
if r.InitiatorPhoneNumber != nil && !phone.IsE164Format(*r.InitiatorPhoneNumber) {
return errors.New("initiator phone number doesn't match E.164 format")
}
return nil
}
func (s State) IsTerminal() bool {
switch s {
case StateConfirmed:
fallthrough
case StateFailed:
fallthrough
case StateRevoked:
return true
}
return false
}
func (s State) String() string {
switch s {
case StateUnknown:
return "unknown"
case StatePending:
return "pending"
case StateConfirmed:
return "confirmed"
case StateFailed:
return "failed"
case StateRevoked:
return "revoked"
}
return "unknown"
}
func (s Type) String() string {
switch s {
case UnknownType:
return "unknown"
case InitializeLockedTimelockAccount:
return "initialize_locked_timelock_account"
case NoPrivacyTransferWithAuthority:
return "no_privacy_transfer_with_authority"
case NoPrivacyWithdraw:
return "no_privacy_withdraw"
case TemporaryPrivacyTransferWithAuthority:
return "temporary_privacy_transfer_with_authority"
case PermanentPrivacyTransferWithAuthority:
return "permanent_privacy_transfer_with_authority"
case TransferWithCommitment:
return "transfer_with_commitment"
case CloseEmptyTimelockAccount:
return "close_empty_timelock_account"
case CloseDormantTimelockAccount:
return "close_dormant_timelock_account"
case SaveRecentRoot:
return "save_recent_root"
case InitializeCommitmentProof:
return "initialize_commitment_proof"
case UploadCommitmentProof:
return "upload_commitment_proof"
case VerifyCommitmentProof:
return "verify_commitment_proof"
case OpenCommitmentVault:
return "open_commitment_vault"
case CloseCommitmentVault:
return "close_commitment_vault"
}
return "unknown"
}