-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommitment.go
98 lines (76 loc) · 2.79 KB
/
commitment.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
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/commitment"
"github.com/code-payments/code-server/pkg/code/data/fulfillment"
)
// todo: commitment state is highly correlated with fulfillment submission, so we manage
// it here, at least for now
func markCommitmentPayingDestination(ctx context.Context, data code_data.Provider, intentId string, actionId uint32) error {
commitmentRecord, err := data.GetCommitmentByAction(ctx, intentId, actionId)
if err != nil {
return err
}
if commitmentRecord.State == commitment.StatePayingDestination {
return nil
}
if commitmentRecord.State != commitment.StateUnknown {
return errors.New("commitment in invalid state")
}
commitmentRecord.State = commitment.StatePayingDestination
return data.SaveCommitment(ctx, commitmentRecord)
}
func markCommitmentReadyToOpen(ctx context.Context, data code_data.Provider, intentId string, actionId uint32) error {
commitmentRecord, err := data.GetCommitmentByAction(ctx, intentId, actionId)
if err != nil {
return err
}
if commitmentRecord.State == commitment.StateReadyToOpen {
return nil
}
if commitmentRecord.State != commitment.StatePayingDestination {
return errors.New("commitment in invalid state")
}
commitmentRecord.State = commitment.StateReadyToOpen
return data.SaveCommitment(ctx, commitmentRecord)
}
// Opening is something managed externally
func markCommitmentOpen(ctx context.Context, data code_data.Provider, intentId string, actionId uint32) error {
commitmentRecord, err := data.GetCommitmentByAction(ctx, intentId, actionId)
if err != nil {
return err
}
if commitmentRecord.State == commitment.StateOpen {
return nil
}
if commitmentRecord.State != commitment.StateOpening {
return errors.New("commitment in invalid state")
}
fulfillmentRecords, err := data.GetAllFulfillmentsByTypeAndAction(ctx, fulfillment.TemporaryPrivacyTransferWithAuthority, intentId, actionId)
if err != nil {
return err
}
err = markFulfillmentAsActivelyScheduled(ctx, data, fulfillmentRecords[0])
if err != nil {
return err
}
commitmentRecord.State = commitment.StateOpen
return data.SaveCommitment(ctx, commitmentRecord)
}
// Closing is likely something managed externally
func markCommitmentClosed(ctx context.Context, data code_data.Provider, intentId string, actionId uint32) error {
commitmentRecord, err := data.GetCommitmentByAction(ctx, intentId, actionId)
if err != nil {
return err
}
if commitmentRecord.State == commitment.StateClosed {
return nil
}
if commitmentRecord.State != commitment.StateClosing {
return errors.New("commitment in invalid state")
}
commitmentRecord.State = commitment.StateClosed
return data.SaveCommitment(ctx, commitmentRecord)
}