-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.go
215 lines (173 loc) · 6.23 KB
/
utils.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 async_sequencer
import (
"context"
"github.com/pkg/errors"
"github.com/code-payments/code-server/pkg/solana"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/fulfillment"
"github.com/code-payments/code-server/pkg/code/data/nonce"
"github.com/code-payments/code-server/pkg/code/data/transaction"
)
func (p *service) validateFulfillmentState(record *fulfillment.Record, states ...fulfillment.State) error {
for _, validState := range states {
if record.State == validState {
return nil
}
}
return ErrInvalidFulfillmentStateTransition
}
func (p *service) markFulfillmentPending(ctx context.Context, record *fulfillment.Record) error {
err := p.validateFulfillmentState(record, fulfillment.StateUnknown)
if err != nil {
return err
}
record.State = fulfillment.StatePending
return p.data.UpdateFulfillment(ctx, record)
}
func (p *service) markFulfillmentConfirmed(ctx context.Context, record *fulfillment.Record) error {
err := p.validateFulfillmentState(record, fulfillment.StatePending)
if err != nil {
return err
}
err = p.markNonceReleasedDueToSubmittedTransaction(ctx, record)
if err != nil {
return err
}
record.State = fulfillment.StateConfirmed
record.Data = nil
return p.data.UpdateFulfillment(ctx, record)
}
func (p *service) markFulfillmentFailed(ctx context.Context, record *fulfillment.Record) error {
err := p.validateFulfillmentState(record, fulfillment.StatePending)
if err != nil {
return err
}
err = p.markNonceReleasedDueToSubmittedTransaction(ctx, record)
if err != nil {
return err
}
record.State = fulfillment.StateFailed
record.Data = nil
return p.data.UpdateFulfillment(ctx, record)
}
func (p *service) markFulfillmentRevoked(ctx context.Context, fulfillmentRecord *fulfillment.Record, nonceUsed bool) error {
err := p.validateFulfillmentState(fulfillmentRecord, fulfillment.StateUnknown)
if err != nil {
return err
}
// We'll only mark the nonce as available when the fulfillment is in an unknown state
// and we know we haven't used the nonce. Otherwise, there's a chance it was submitted
// and could have been used. A human is needed to resolve it.
//
// Note: We opt to manage the nonce here because the nonce worker can't be aware
// of how we got to the revoked state. There are important distinctions between
// the various use cases.
if !nonceUsed && fulfillmentRecord.State == fulfillment.StateUnknown {
err = p.markNonceAvailableDueToRevokedFulfillment(ctx, fulfillmentRecord)
if err != nil {
return err
}
}
fulfillmentRecord.State = fulfillment.StateRevoked
fulfillmentRecord.Data = nil
return p.data.UpdateFulfillment(ctx, fulfillmentRecord)
}
func markFulfillmentAsActivelyScheduled(ctx context.Context, data code_data.Provider, fulfillmentRecord *fulfillment.Record) error {
if fulfillmentRecord.Id == 0 {
return nil
}
if !fulfillmentRecord.DisableActiveScheduling {
return nil
}
if fulfillmentRecord.State != fulfillment.StateUnknown {
return nil
}
// Note: different than Save, since we don't have distributed locks
return data.MarkFulfillmentAsActivelyScheduled(ctx, fulfillmentRecord.Id)
}
func (p *service) sendToBlockchain(ctx context.Context, record *fulfillment.Record) error {
var stx solana.Transaction
var err error
err = stx.Unmarshal(record.Data)
if err != nil {
return err
}
_, err = p.data.SubmitBlockchainTransaction(ctx, &stx)
if err != nil {
return err
}
return nil
}
func (p *service) getTransaction(ctx context.Context, record *fulfillment.Record) (*transaction.Record, error) {
if record.Signature == nil || len(*record.Signature) == 0 {
return nil, transaction.ErrNotFound
}
if p.conf.enableCachedTransactionLookup.Get(ctx) {
return p.data.GetTransaction(ctx, *record.Signature)
}
return p.getTransactionFromBlockchain(ctx, record)
}
func (p *service) getTransactionFromBlockchain(ctx context.Context, record *fulfillment.Record) (*transaction.Record, error) {
stx, err := p.data.GetBlockchainTransaction(ctx, *record.Signature, solana.CommitmentFinalized)
if err == solana.ErrSignatureNotFound {
return nil, transaction.ErrNotFound
}
if err != nil {
return nil, err
}
tx, err := transaction.FromConfirmedTransaction(stx)
if err != nil {
return nil, err
}
return tx, nil
}
// Important Note: Do NOT call this if the fulfillment being revoked is due to
// transactions having shared nonce blockhashes!
func (p *service) markNonceAvailableDueToRevokedFulfillment(ctx context.Context, fulfillmentToRevoke *fulfillment.Record) error {
// We'll only automatically manage the nonce state if the fulfillment is in
// an unknown state. Otherwise, there's a chance it was submitted and could
// have been used. A human is needed to resolve it.
if fulfillmentToRevoke.State != fulfillment.StateUnknown {
return errors.New("fulfillment is in dangerous state to manage nonce state")
}
// Transaction doesn't have an assigned nonce
if fulfillmentToRevoke.Nonce == nil {
return nil
}
nonceRecord, err := p.data.GetNonce(ctx, *fulfillmentToRevoke.Nonce)
if err != nil {
return err
}
if *fulfillmentToRevoke.Signature != nonceRecord.Signature {
return errors.New("unexpected nonce signature")
}
if *fulfillmentToRevoke.Blockhash != nonceRecord.Blockhash {
return errors.New("unexpected nonce blockhash")
}
if nonceRecord.State != nonce.StateReserved {
return errors.New("unexpected nonce state")
}
nonceRecord.State = nonce.StateAvailable
nonceRecord.Signature = ""
return p.data.SaveNonce(ctx, nonceRecord)
}
func (p *service) markNonceReleasedDueToSubmittedTransaction(ctx context.Context, fulfillmentRecord *fulfillment.Record) error {
if fulfillmentRecord.State != fulfillment.StatePending {
return errors.New("fulfillment is in unexpected state")
}
nonceRecord, err := p.data.GetNonce(ctx, *fulfillmentRecord.Nonce)
if err != nil {
return err
}
if *fulfillmentRecord.Signature != nonceRecord.Signature {
return errors.New("unexpected nonce signature")
}
if *fulfillmentRecord.Blockhash != nonceRecord.Blockhash {
return errors.New("unexpected nonce blockhash")
}
if nonceRecord.State != nonce.StateReserved {
return errors.New("unexpected nonce state")
}
nonceRecord.State = nonce.StateReleased
return p.data.SaveNonce(ctx, nonceRecord)
}