This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
claimable_balances_change_processor.go
136 lines (121 loc) · 3.51 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
package processors
import (
"context"
"github.com/xdbfoundation/go/ingest"
"github.com/xdbfoundation/go/services/frontier/internal/db2/history"
"github.com/xdbfoundation/go/support/errors"
"github.com/xdbfoundation/go/xdr"
)
type ClaimableBalancesChangeProcessor struct {
encodingBuffer *xdr.EncodingBuffer
qClaimableBalances history.QClaimableBalances
cache *ingest.ChangeCompactor
}
func NewClaimableBalancesChangeProcessor(Q history.QClaimableBalances) *ClaimableBalancesChangeProcessor {
p := &ClaimableBalancesChangeProcessor{
encodingBuffer: xdr.NewEncodingBuffer(),
qClaimableBalances: Q,
}
p.reset()
return p
}
func (p *ClaimableBalancesChangeProcessor) reset() {
p.cache = ingest.NewChangeCompactor()
}
func (p *ClaimableBalancesChangeProcessor) ProcessChange(ctx context.Context, change ingest.Change) error {
if change.Type != xdr.LedgerEntryTypeClaimableBalance {
return nil
}
err := p.cache.AddChange(change)
if err != nil {
return errors.Wrap(err, "error adding to ledgerCache")
}
if p.cache.Size() > maxBatchSize {
err = p.Commit(ctx)
if err != nil {
return errors.Wrap(err, "error in Commit")
}
p.reset()
}
return nil
}
func (p *ClaimableBalancesChangeProcessor) Commit(ctx context.Context) error {
var (
cbsToUpsert []history.ClaimableBalance
cbIDsToDelete []string
)
changes := p.cache.GetChanges()
for _, change := range changes {
switch {
case change.Pre == nil && change.Post != nil:
// Created
row, err := p.ledgerEntryToRow(change.Post)
if err != nil {
return err
}
cbsToUpsert = append(cbsToUpsert, row)
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
}
cbIDsToDelete = append(cbIDsToDelete, id)
default:
// Updated
row, err := p.ledgerEntryToRow(change.Post)
if err != nil {
return err
}
cbsToUpsert = append(cbsToUpsert, row)
}
}
if len(cbsToUpsert) > 0 {
if err := p.qClaimableBalances.UpsertClaimableBalances(ctx, cbsToUpsert); err != nil {
return errors.Wrap(err, "error executing upsert")
}
}
if len(cbIDsToDelete) > 0 {
count, err := p.qClaimableBalances.RemoveClaimableBalances(ctx, cbIDsToDelete)
if err != nil {
return errors.Wrap(err, "error executing removal")
}
if count != int64(len(cbIDsToDelete)) {
return ingest.NewStateError(errors.Errorf(
"%d rows affected when deleting %d claimable balances",
count,
len(cbIDsToDelete),
))
}
}
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
}