-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.go
89 lines (70 loc) · 2.57 KB
/
util.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
package async_commitment
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"
)
// Every other state is currently managed after successful fulfillment submission
func markCommitmentAsOpening(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.StateOpening {
return nil
}
if commitmentRecord.State != commitment.StateReadyToOpen {
return errors.New("commitment in invalid state")
}
commitmentRecord.State = commitment.StateOpening
return data.SaveCommitment(ctx, commitmentRecord)
}
func markCommitmentAsClosing(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.StateClosing {
return nil
}
if commitmentRecord.State != commitment.StateOpen {
return errors.New("commitment in invalid state")
}
fulfillmentRecords, err := data.GetAllFulfillmentsByTypeAndAction(ctx, fulfillment.CloseCommitmentVault, intentId, actionId)
if err != nil {
return err
}
err = markFulfillmentAsActivelyScheduled(ctx, data, fulfillmentRecords[0])
if err != nil {
return err
}
commitmentRecord.State = commitment.StateClosing
return data.SaveCommitment(ctx, commitmentRecord)
}
func markCommitmentReadyForGC(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.StateReadyToRemoveFromMerkleTree {
return nil
}
if commitmentRecord.State != commitment.StateReadyToOpen && commitmentRecord.State != commitment.StateClosed {
return errors.New("commitment in invalid state")
}
commitmentRecord.State = commitment.StateReadyToRemoveFromMerkleTree
return data.SaveCommitment(ctx, commitmentRecord)
}
func markTreasuryAsRepaid(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.TreasuryRepaid {
return nil
}
commitmentRecord.TreasuryRepaid = true
return data.SaveCommitment(ctx, commitmentRecord)
}