/
participants_processor.go
295 lines (246 loc) · 7.84 KB
/
participants_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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//lint:file-ignore U1001 Ignore all unused code, staticcheck doesn't understand testify/suite
package processors
import (
"context"
"github.com/TosinShada/monorepo/ingest"
"github.com/TosinShada/monorepo/services/horizon/internal/db2/history"
"github.com/TosinShada/monorepo/support/errors"
"github.com/TosinShada/monorepo/toid"
"github.com/TosinShada/monorepo/xdr"
)
// ParticipantsProcessor is a processor which ingests various participants
// from different sources (transactions, operations, etc)
type ParticipantsProcessor struct {
participantsQ history.QParticipants
sequence uint32
participantSet map[string]participant
}
func NewParticipantsProcessor(participantsQ history.QParticipants, sequence uint32) *ParticipantsProcessor {
return &ParticipantsProcessor{
participantsQ: participantsQ,
sequence: sequence,
participantSet: map[string]participant{},
}
}
type participant struct {
accountID int64
transactionSet map[int64]struct{}
operationSet map[int64]struct{}
}
func (p *participant) addTransactionID(id int64) {
if p.transactionSet == nil {
p.transactionSet = map[int64]struct{}{}
}
p.transactionSet[id] = struct{}{}
}
func (p *participant) addOperationID(id int64) {
if p.operationSet == nil {
p.operationSet = map[int64]struct{}{}
}
p.operationSet[id] = struct{}{}
}
func (p *ParticipantsProcessor) loadAccountIDs(ctx context.Context, participantSet map[string]participant) error {
addresses := make([]string, 0, len(participantSet))
for address := range participantSet {
addresses = append(addresses, address)
}
addressToID, err := p.participantsQ.CreateAccounts(ctx, addresses, maxBatchSize)
if err != nil {
return errors.Wrap(err, "Could not create account ids")
}
for _, address := range addresses {
id, ok := addressToID[address]
if !ok {
return errors.Errorf("no id found for account address %s", address)
}
participantForAddress := participantSet[address]
participantForAddress.accountID = id
participantSet[address] = participantForAddress
}
return nil
}
func participantsForChanges(
changes xdr.LedgerEntryChanges,
) ([]xdr.AccountId, error) {
var participants []xdr.AccountId
for _, c := range changes {
var participant *xdr.AccountId
switch c.Type {
case xdr.LedgerEntryChangeTypeLedgerEntryCreated:
participant = participantsForLedgerEntry(c.MustCreated())
case xdr.LedgerEntryChangeTypeLedgerEntryRemoved:
participant = participantsForLedgerKey(c.MustRemoved())
case xdr.LedgerEntryChangeTypeLedgerEntryUpdated:
participant = participantsForLedgerEntry(c.MustUpdated())
case xdr.LedgerEntryChangeTypeLedgerEntryState:
participant = participantsForLedgerEntry(c.MustState())
default:
return nil, errors.Errorf("Unknown change type: %s", c.Type)
}
if participant != nil {
participants = append(participants, *participant)
}
}
return participants, nil
}
func participantsForLedgerEntry(le xdr.LedgerEntry) *xdr.AccountId {
if le.Data.Type != xdr.LedgerEntryTypeAccount {
return nil
}
aid := le.Data.MustAccount().AccountId
return &aid
}
func participantsForLedgerKey(lk xdr.LedgerKey) *xdr.AccountId {
if lk.Type != xdr.LedgerEntryTypeAccount {
return nil
}
aid := lk.MustAccount().AccountId
return &aid
}
func participantsForMeta(
meta xdr.TransactionMeta,
) ([]xdr.AccountId, error) {
var participants []xdr.AccountId
if meta.Operations == nil {
return participants, nil
}
for _, op := range *meta.Operations {
var accounts []xdr.AccountId
accounts, err := participantsForChanges(op.Changes)
if err != nil {
return nil, err
}
participants = append(participants, accounts...)
}
return participants, nil
}
func (p *ParticipantsProcessor) addTransactionParticipants(
participantSet map[string]participant,
sequence uint32,
transaction ingest.LedgerTransaction,
) error {
transactionID := toid.New(int32(sequence), int32(transaction.Index), 0).ToInt64()
transactionParticipants, err := ParticipantsForTransaction(
sequence,
transaction,
)
if err != nil {
return errors.Wrap(err, "Could not determine participants for transaction")
}
for _, participant := range transactionParticipants {
address := participant.Address()
entry := participantSet[address]
entry.addTransactionID(transactionID)
participantSet[address] = entry
}
return nil
}
func (p *ParticipantsProcessor) addOperationsParticipants(
participantSet map[string]participant,
sequence uint32,
transaction ingest.LedgerTransaction,
) error {
participants, err := operationsParticipants(transaction, sequence)
if err != nil {
return errors.Wrap(err, "could not determine operation participants")
}
for operationID, p := range participants {
for _, participant := range p {
address := participant.Address()
entry := participantSet[address]
entry.addOperationID(operationID)
participantSet[address] = entry
}
}
return nil
}
func (p *ParticipantsProcessor) insertDBTransactionParticipants(ctx context.Context, participantSet map[string]participant) error {
batch := p.participantsQ.NewTransactionParticipantsBatchInsertBuilder(maxBatchSize)
for _, entry := range participantSet {
for transactionID := range entry.transactionSet {
if err := batch.Add(ctx, transactionID, entry.accountID); err != nil {
return errors.Wrap(err, "Could not insert transaction participant in db")
}
}
}
if err := batch.Exec(ctx); err != nil {
return errors.Wrap(err, "Could not flush transaction participants to db")
}
return nil
}
func (p *ParticipantsProcessor) insertDBOperationsParticipants(ctx context.Context, participantSet map[string]participant) error {
batch := p.participantsQ.NewOperationParticipantBatchInsertBuilder(maxBatchSize)
for _, entry := range participantSet {
for operationID := range entry.operationSet {
if err := batch.Add(ctx, operationID, entry.accountID); err != nil {
return errors.Wrap(err, "could not insert operation participant in db")
}
}
}
if err := batch.Exec(ctx); err != nil {
return errors.Wrap(err, "could not flush operation participants to db")
}
return nil
}
func (p *ParticipantsProcessor) ProcessTransaction(ctx context.Context, transaction ingest.LedgerTransaction) (err error) {
err = p.addTransactionParticipants(p.participantSet, p.sequence, transaction)
if err != nil {
return err
}
err = p.addOperationsParticipants(p.participantSet, p.sequence, transaction)
if err != nil {
return err
}
return nil
}
func (p *ParticipantsProcessor) Commit(ctx context.Context) (err error) {
if len(p.participantSet) > 0 {
if err = p.loadAccountIDs(ctx, p.participantSet); err != nil {
return err
}
if err = p.insertDBTransactionParticipants(ctx, p.participantSet); err != nil {
return err
}
if err = p.insertDBOperationsParticipants(ctx, p.participantSet); err != nil {
return err
}
}
return err
}
func ParticipantsForTransaction(
sequence uint32,
transaction ingest.LedgerTransaction,
) ([]xdr.AccountId, error) {
participants := []xdr.AccountId{
transaction.Envelope.SourceAccount().ToAccountId(),
}
if transaction.Envelope.IsFeeBump() {
participants = append(participants, transaction.Envelope.FeeBumpAccount().ToAccountId())
}
p, err := participantsForMeta(transaction.UnsafeMeta)
if err != nil {
return nil, err
}
participants = append(participants, p...)
p, err = participantsForChanges(transaction.FeeChanges)
if err != nil {
return nil, err
}
participants = append(participants, p...)
for opi, op := range transaction.Envelope.Operations() {
operation := transactionOperationWrapper{
index: uint32(opi),
transaction: transaction,
operation: op,
ledgerSequence: sequence,
}
p, err := operation.Participants()
if err != nil {
return nil, errors.Wrapf(
err, "could not determine operation %v participants", operation.ID(),
)
}
participants = append(participants, p...)
}
return dedupeParticipants(participants), nil
}