forked from stellar/go
/
postgres.go
476 lines (395 loc) · 13.9 KB
/
postgres.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package database
import (
"database/sql"
"strconv"
"strings"
"time"
"github.com/leevlad/go/services/bifrost/queue"
"github.com/leevlad/go/services/bifrost/sse"
"github.com/leevlad/go/support/db"
"github.com/leevlad/go/support/errors"
"github.com/lib/pq"
)
const (
schemaVersionKey = "schema_version"
ethereumAddressIndexKey = "ethereum_address_index"
ethereumLastBlockKey = "ethereum_last_block"
bitcoinAddressIndexKey = "bitcoin_address_index"
bitcoinLastBlockKey = "bitcoin_last_block"
addressAssociationTableName = "address_association"
broadcastedEventTableName = "broadcasted_event"
keyValueStoreTableName = "key_value_store"
processedTransactionTableName = "processed_transaction"
transactionsQueueTableName = "transactions_queue"
recoveryTransactionTableName = "recovery_transaction"
)
type keyValueStoreRow struct {
Key string `db:"key"`
Value string `db:"value"`
}
type broadcastedEventRow struct {
ID int64 `db:"id"`
Address string `db:"address"`
Event string `db:"event"`
Data string `db:"data"`
}
func (b *broadcastedEventRow) toSSE() sse.Event {
return sse.Event{
Address: b.Address,
Event: sse.AddressEvent(b.Event),
Data: b.Data,
}
}
type transactionsQueueRow struct {
TransactionID string `db:"transaction_id"`
AssetCode queue.AssetCode `db:"asset_code"`
Amount string `db:"amount"`
StellarPublicKey string `db:"stellar_public_key"`
}
type processedTransactionRow struct {
Chain Chain `db:"chain"`
TransactionID string `db:"transaction_id"`
ReceivingAddress string `db:"receiving_address"`
CreatedAt time.Time `db:"created_at"`
}
type recoveryTransactionRow struct {
Source string `db:"source"`
EnvelopeXDR string `db:"envelope_xdr"`
}
func fromQueueTransaction(tx queue.Transaction) *transactionsQueueRow {
return &transactionsQueueRow{
TransactionID: tx.TransactionID,
AssetCode: tx.AssetCode,
Amount: tx.Amount,
StellarPublicKey: tx.StellarPublicKey,
}
}
func isDuplicateError(err error) bool {
return strings.Contains(err.Error(), "duplicate key value violates unique constraint")
}
func (r *transactionsQueueRow) toQueueTransaction() *queue.Transaction {
return &queue.Transaction{
TransactionID: r.TransactionID,
AssetCode: r.AssetCode,
Amount: r.Amount,
StellarPublicKey: r.StellarPublicKey,
}
}
func (d *PostgresDatabase) Open(dsn string) error {
var err error
d.session, err = db.Open("postgres", dsn)
if err != nil {
return err
}
return nil
}
// Import imports DB schema
func (d *PostgresDatabase) Import() error {
return d.session.ExecAll(schema)
}
// GetSchemaVersion returns schema version of Bifrost DB. Returns 0.
func (d *PostgresDatabase) GetSchemaVersion() (uint32, error) {
keyValueStore := d.getTable(keyValueStoreTableName, nil)
row := keyValueStoreRow{}
err := keyValueStore.Get(&row, map[string]interface{}{"key": schemaVersionKey}).Exec()
if err != nil {
if pqErr, ok := errors.Cause(err).(*pq.Error); ok && pqErr.Code == "42P01" {
// Relation does not exist - DB is probably clean. If not, Import will return error.
return 0, nil
}
if errors.Cause(err) == sql.ErrNoRows {
// schemaVersionKey key not found - schema is old (return 1 because schemaVersionKey was set to 2 when created)
return 1, nil
}
return 0, errors.Wrap(err, "Error getting `"+schemaVersionKey+"` from DB")
}
version, err := strconv.ParseUint(row.Value, 10, 32)
if err != nil {
return 0, errors.Wrap(err, "Error converting `"+schemaVersionKey+"` value to uint32")
}
return uint32(version), nil
}
func (d *PostgresDatabase) getTable(name string, session *db.Session) *db.Table {
if session == nil {
session = d.session
}
return &db.Table{
Name: name,
Session: session,
}
}
func (d *PostgresDatabase) CreateAddressAssociation(chain Chain, stellarAddress, address string, addressIndex uint32) error {
addressAssociationTable := d.getTable(addressAssociationTableName, nil)
association := &AddressAssociation{
Chain: chain,
AddressIndex: addressIndex,
Address: address,
StellarPublicKey: stellarAddress,
CreatedAt: time.Now(),
}
_, err := addressAssociationTable.Insert(association).Exec()
return err
}
func (d *PostgresDatabase) GetAssociationByChainAddress(chain Chain, address string) (*AddressAssociation, error) {
addressAssociationTable := d.getTable(addressAssociationTableName, nil)
row := &AddressAssociation{}
where := map[string]interface{}{"address": address, "chain": chain}
err := addressAssociationTable.Get(row, where).Exec()
if err != nil {
switch errors.Cause(err) {
case sql.ErrNoRows:
return nil, nil
default:
return nil, errors.Wrap(err, "Error getting addressAssociation from DB")
}
}
return row, nil
}
func (d *PostgresDatabase) GetAssociationByStellarPublicKey(stellarPublicKey string) (*AddressAssociation, error) {
addressAssociationTable := d.getTable(addressAssociationTableName, nil)
row := &AddressAssociation{}
where := map[string]interface{}{"stellar_public_key": stellarPublicKey}
err := addressAssociationTable.Get(row, where).Exec()
if err != nil {
switch errors.Cause(err) {
case sql.ErrNoRows:
return nil, nil
default:
return nil, errors.Wrap(err, "Error getting addressAssociation from DB")
}
}
return row, nil
}
func (d *PostgresDatabase) AddProcessedTransaction(chain Chain, transactionID, receivingAddress string) (bool, error) {
processedTransactionTable := d.getTable(processedTransactionTableName, nil)
processedTransaction := processedTransactionRow{chain, transactionID, receivingAddress, time.Now()}
_, err := processedTransactionTable.Insert(processedTransaction).Exec()
if err != nil && isDuplicateError(err) {
return true, nil
}
return false, err
}
func (d *PostgresDatabase) IncrementAddressIndex(chain Chain) (uint32, error) {
var key string
switch chain {
case ChainBitcoin:
key = bitcoinAddressIndexKey
case ChainEthereum:
key = ethereumAddressIndexKey
default:
return 0, errors.New("Invalid chain")
}
row := keyValueStoreRow{}
session := d.session.Clone()
keyValueStore := d.getTable(keyValueStoreTableName, session)
err := session.Begin()
if err != nil {
return 0, errors.Wrap(err, "Error starting a new transaction")
}
defer session.Rollback()
err = keyValueStore.Get(&row, map[string]interface{}{"key": key}).Suffix("FOR UPDATE").Exec()
if err != nil {
return 0, errors.Wrap(err, "Error getting `"+key+"` from DB")
}
// TODO check for overflows - should we create a new account 1'?
index, err := strconv.ParseUint(row.Value, 10, 32)
if err != nil {
return 0, errors.Wrap(err, "Error converting `"+key+"` value to uint32")
}
index++
// TODO: something's wrong with db.Table.Update(). Setting the first argument does not work as expected.
_, err = keyValueStore.Update(nil, map[string]interface{}{"key": key}).Set("value", index).Exec()
if err != nil {
return 0, errors.Wrap(err, "Error updating `"+key+"`")
}
err = session.Commit()
if err != nil {
return 0, errors.Wrap(err, "Error commiting a transaction")
}
return uint32(index), nil
}
func (d *PostgresDatabase) ResetBlockCounters() error {
keyValueStore := d.getTable(keyValueStoreTableName, nil)
_, err := keyValueStore.Update(nil, map[string]interface{}{"key": bitcoinLastBlockKey}).Set("value", 0).Exec()
if err != nil {
return errors.Wrap(err, "Error reseting `bitcoinLastBlockKey`")
}
_, err = keyValueStore.Update(nil, map[string]interface{}{"key": ethereumLastBlockKey}).Set("value", 0).Exec()
if err != nil {
return errors.Wrap(err, "Error reseting `ethereumLastBlockKey`")
}
return nil
}
func (d *PostgresDatabase) GetEthereumBlockToProcess() (uint64, error) {
return d.getBlockToProcess(ethereumLastBlockKey)
}
func (d *PostgresDatabase) SaveLastProcessedEthereumBlock(block uint64) error {
return d.saveLastProcessedBlock(ethereumLastBlockKey, block)
}
func (d *PostgresDatabase) GetBitcoinBlockToProcess() (uint64, error) {
return d.getBlockToProcess(bitcoinLastBlockKey)
}
func (d *PostgresDatabase) SaveLastProcessedBitcoinBlock(block uint64) error {
return d.saveLastProcessedBlock(bitcoinLastBlockKey, block)
}
func (d *PostgresDatabase) getBlockToProcess(key string) (uint64, error) {
keyValueStore := d.getTable(keyValueStoreTableName, nil)
row := keyValueStoreRow{}
err := keyValueStore.Get(&row, map[string]interface{}{"key": key}).Exec()
if err != nil {
return 0, errors.Wrap(err, "Error getting `"+key+"` from DB")
}
block, err := strconv.ParseUint(row.Value, 10, 64)
if err != nil {
return 0, errors.Wrap(err, "Error converting `"+key+"` value to uint64")
}
// If set, `block` is the last processed block so we need to start processing from the next one.
if block > 0 {
block++
}
return block, nil
}
func (d *PostgresDatabase) saveLastProcessedBlock(key string, block uint64) error {
row := keyValueStoreRow{}
session := d.session.Clone()
keyValueStore := d.getTable(keyValueStoreTableName, session)
err := session.Begin()
if err != nil {
return errors.Wrap(err, "Error starting a new transaction")
}
defer session.Rollback()
err = keyValueStore.Get(&row, map[string]interface{}{"key": key}).Suffix("FOR UPDATE").Exec()
if err != nil {
return errors.Wrap(err, "Error getting `"+key+"` from DB")
}
lastBlock, err := strconv.ParseUint(row.Value, 10, 64)
if err != nil {
return errors.Wrap(err, "Error converting `"+key+"` value to uint32")
}
if block > lastBlock {
// TODO: something's wrong with db.Table.Update(). Setting the first argument does not work as expected.
_, err = keyValueStore.Update(nil, map[string]interface{}{"key": key}).Set("value", block).Exec()
if err != nil {
return errors.Wrap(err, "Error updating `"+key+"`")
}
}
err = session.Commit()
if err != nil {
return errors.Wrap(err, "Error commiting a transaction")
}
return nil
}
// QueueAdd implements queue.Queue interface. If element already exists in a queue, it should
// return nil.
func (d *PostgresDatabase) QueueAdd(tx queue.Transaction) error {
transactionsQueueTable := d.getTable(transactionsQueueTableName, nil)
transactionQueue := fromQueueTransaction(tx)
_, err := transactionsQueueTable.Insert(transactionQueue).Exec()
if err != nil {
if isDuplicateError(err) {
return nil
}
}
return err
}
// QueuePool receives and removes the head of this queue. Returns nil if no elements found.
// QueuePool implements queue.Queue interface.
func (d *PostgresDatabase) QueuePool() (*queue.Transaction, error) {
row := transactionsQueueRow{}
session := d.session.Clone()
transactionsQueueTable := d.getTable(transactionsQueueTableName, session)
err := session.Begin()
if err != nil {
return nil, errors.Wrap(err, "Error starting a new transaction")
}
defer session.Rollback()
err = transactionsQueueTable.Get(&row, map[string]interface{}{"pooled": false}).OrderBy("id ASC").Suffix("FOR UPDATE").Exec()
if err != nil {
switch errors.Cause(err) {
case sql.ErrNoRows:
return nil, nil
default:
return nil, errors.Wrap(err, "Error getting transaction from a queue")
}
}
// TODO: something's wrong with db.Table.Update(). Setting the first argument does not work as expected.
where := map[string]interface{}{"transaction_id": row.TransactionID, "asset_code": row.AssetCode}
_, err = transactionsQueueTable.Update(nil, where).Set("pooled", true).Exec()
if err != nil {
return nil, errors.Wrap(err, "Error setting transaction as pooled in a queue")
}
err = session.Commit()
if err != nil {
return nil, errors.Wrap(err, "Error commiting a transaction")
}
return row.toQueueTransaction(), nil
}
// AddEvent adds a new server-sent event to the storage.
func (d *PostgresDatabase) AddEvent(event sse.Event) error {
broadcastedEventTable := d.getTable(broadcastedEventTableName, nil)
_, err := broadcastedEventTable.Insert(event).Exec()
if err != nil {
if isDuplicateError(err) {
return nil
}
}
return err
}
// GetEventsSinceID returns all events since `id`. Used to load and publish
// all broadcasted events.
// It returns the last event ID, list of events or error.
// If `id` is equal `-1`:
// * it should return the last event ID and empty list if at least one
// event has been broadcasted.
// * it should return `0` if no events have been broadcasted.
func (d *PostgresDatabase) GetEventsSinceID(id int64) (int64, []sse.Event, error) {
if id == -1 {
lastID, err := d.getEventsLastID()
return lastID, nil, err
}
broadcastedEventTable := d.getTable(broadcastedEventTableName, nil)
rows := []broadcastedEventRow{}
err := broadcastedEventTable.Select(&rows, "id > ?", id).Exec()
if err != nil {
switch errors.Cause(err) {
case sql.ErrNoRows:
return 0, nil, nil
default:
return 0, nil, errors.Wrap(err, "Error getting broadcastedEvent's from DB")
}
}
var lastID int64
returnRows := make([]sse.Event, len(rows))
if len(rows) > 0 {
lastID = rows[len(rows)-1].ID
for i, event := range rows {
returnRows[i] = event.toSSE()
}
}
return lastID, returnRows, nil
}
// Returns the last event ID from broadcasted_event table.
func (d *PostgresDatabase) getEventsLastID() (int64, error) {
row := struct {
ID int64 `db:"id"`
}{}
broadcastedEventTable := d.getTable(broadcastedEventTableName, nil)
// TODO: `1=1`. We should be able to get row without WHERE clause.
// When it's set to nil: `pq: syntax error at or near "ORDER""`
err := broadcastedEventTable.Get(&row, "1=1").OrderBy("id DESC").Exec()
if err != nil {
switch errors.Cause(err) {
case sql.ErrNoRows:
return 0, nil
default:
return 0, errors.Wrap(err, "Error getting events last ID")
}
}
return row.ID, nil
}
func (d *PostgresDatabase) AddRecoveryTransaction(sourceAccount string, txEnvelope string) error {
recoveryTransactionTable := d.getTable(recoveryTransactionTableName, nil)
recoveryTransaction := recoveryTransactionRow{Source: sourceAccount, EnvelopeXDR: txEnvelope}
_, err := recoveryTransactionTable.Insert(recoveryTransaction).Exec()
return err
}