-
Notifications
You must be signed in to change notification settings - Fork 496
/
claimable_balances_change_processor.go
167 lines (146 loc) · 5.1 KB
/
claimable_balances_change_processor.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
package processors
import (
"context"
"github.com/stellar/go/ingest"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
type ClaimableBalancesChangeProcessor struct {
encodingBuffer *xdr.EncodingBuffer
qClaimableBalances history.QClaimableBalances
cbIDsToDelete []string
updatedBalances []history.ClaimableBalance
claimantsInsertBuilder history.ClaimableBalanceClaimantBatchInsertBuilder
claimableBalanceInsertBuilder history.ClaimableBalanceBatchInsertBuilder
}
func NewClaimableBalancesChangeProcessor(Q history.QClaimableBalances) *ClaimableBalancesChangeProcessor {
p := &ClaimableBalancesChangeProcessor{
encodingBuffer: xdr.NewEncodingBuffer(),
qClaimableBalances: Q,
}
p.reset()
return p
}
func (p *ClaimableBalancesChangeProcessor) Name() string {
return "processors.ClaimableBalancesChangeProcessor"
}
func (p *ClaimableBalancesChangeProcessor) reset() {
p.cbIDsToDelete = []string{}
p.updatedBalances = []history.ClaimableBalance{}
p.claimantsInsertBuilder = p.qClaimableBalances.NewClaimableBalanceClaimantBatchInsertBuilder()
p.claimableBalanceInsertBuilder = p.qClaimableBalances.NewClaimableBalanceBatchInsertBuilder()
}
func (p *ClaimableBalancesChangeProcessor) ProcessChange(ctx context.Context, change ingest.Change) error {
if change.Type != xdr.LedgerEntryTypeClaimableBalance {
return nil
}
switch {
case change.Pre == nil && change.Post != nil:
// Created
cb, err := p.ledgerEntryToRow(change.Post)
if err != nil {
return err
}
// Add claimable balance
if err := p.claimableBalanceInsertBuilder.Add(cb); err != nil {
return errors.Wrap(err, "error adding to ClaimableBalanceBatchInsertBuilder")
}
// Add claimants
for _, claimant := range cb.Claimants {
claimant := history.ClaimableBalanceClaimant{
BalanceID: cb.BalanceID,
Destination: claimant.Destination,
LastModifiedLedger: cb.LastModifiedLedger,
}
if err := p.claimantsInsertBuilder.Add(claimant); err != nil {
return errors.Wrap(err, "error adding to ClaimableBalanceClaimantBatchInsertBuilder")
}
}
case change.Pre != nil && change.Post == nil:
// Removed
cBalance := change.Pre.Data.MustClaimableBalance()
id, err := p.encodingBuffer.MarshalHex(cBalance.BalanceId)
if err != nil {
return err
}
p.cbIDsToDelete = append(p.cbIDsToDelete, id)
default:
// this case should only occur if the sponsor has changed in the claimable balance
// the other fields of a claimable balance are immutable
postCB, err := p.ledgerEntryToRow(change.Post)
if err != nil {
return err
}
p.updatedBalances = append(p.updatedBalances, postCB)
}
if p.claimableBalanceInsertBuilder.Len()+p.claimantsInsertBuilder.Len()+len(p.updatedBalances)+len(p.cbIDsToDelete) > maxBatchSize {
if err := p.Commit(ctx); err != nil {
return errors.Wrap(err, "error in Commit")
}
}
return nil
}
func (p *ClaimableBalancesChangeProcessor) Commit(ctx context.Context) error {
defer p.reset()
err := p.claimantsInsertBuilder.Exec(ctx)
if err != nil {
return errors.Wrap(err, "error executing ClaimableBalanceClaimantBatchInsertBuilder")
}
err = p.claimableBalanceInsertBuilder.Exec(ctx)
if err != nil {
return errors.Wrap(err, "error executing ClaimableBalanceBatchInsertBuilder")
}
if len(p.updatedBalances) > 0 {
if err = p.qClaimableBalances.UpsertClaimableBalances(ctx, p.updatedBalances); err != nil {
return errors.Wrap(err, "error updating claimable balances")
}
}
if len(p.cbIDsToDelete) > 0 {
count, err := p.qClaimableBalances.RemoveClaimableBalances(ctx, p.cbIDsToDelete)
if err != nil {
return errors.Wrap(err, "error executing removal")
}
if count != int64(len(p.cbIDsToDelete)) {
return ingest.NewStateError(errors.Errorf(
"%d rows affected when deleting %d claimable balances",
count,
len(p.cbIDsToDelete),
))
}
// Remove ClaimableBalanceClaimants
_, err = p.qClaimableBalances.RemoveClaimableBalanceClaimants(ctx, p.cbIDsToDelete)
if err != nil {
return errors.Wrap(err, "error executing removal of claimants")
}
}
return nil
}
func buildClaimants(claimants []xdr.Claimant) history.Claimants {
hClaimants := history.Claimants{}
for _, c := range claimants {
xc := c.MustV0()
hClaimants = append(hClaimants, history.Claimant{
Destination: xc.Destination.Address(),
Predicate: xc.Predicate,
})
}
return hClaimants
}
func (p *ClaimableBalancesChangeProcessor) ledgerEntryToRow(entry *xdr.LedgerEntry) (history.ClaimableBalance, error) {
cBalance := entry.Data.MustClaimableBalance()
id, err := xdr.MarshalHex(cBalance.BalanceId)
if err != nil {
return history.ClaimableBalance{}, err
}
row := history.ClaimableBalance{
BalanceID: id,
Claimants: buildClaimants(cBalance.Claimants),
Asset: cBalance.Asset,
Amount: cBalance.Amount,
Sponsor: ledgerEntrySponsorToNullString(*entry),
LastModifiedLedger: uint32(entry.LastModifiedLedgerSeq),
Flags: uint32(cBalance.Flags()),
}
return row, nil
}