-
Notifications
You must be signed in to change notification settings - Fork 402
/
chore.go
121 lines (102 loc) · 3.09 KB
/
chore.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
// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package storjscan
import (
"context"
"time"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/sync2"
"storj.io/storj/satellite/payments"
)
// ChoreErr is storjscan chore err class.
var ChoreErr = errs.Class("storjscan chore")
// Chore periodically queries for new payments from storjscan.
//
// architecture: Chore
type Chore struct {
log *zap.Logger
client *Client
paymentsDB PaymentsDB
TransactionCycle *sync2.Cycle
confirmations int
disableLoop bool
}
// NewChore creates new chore.
func NewChore(log *zap.Logger, client *Client, paymentsDB PaymentsDB, confirmations int, interval time.Duration, disableLoop bool) *Chore {
return &Chore{
log: log,
client: client,
paymentsDB: paymentsDB,
TransactionCycle: sync2.NewCycle(interval),
confirmations: confirmations,
disableLoop: disableLoop,
}
}
// Run runs storjscan payment loop.
func (chore *Chore) Run(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
return chore.TransactionCycle.Run(ctx, func(ctx context.Context) error {
var from int64
if chore.disableLoop {
chore.log.Debug("Skipping chore iteration as loop is disabled", zap.Bool("disableLoop", chore.disableLoop))
return nil
}
blockNumber, err := chore.paymentsDB.LastBlock(ctx, payments.PaymentStatusConfirmed)
switch {
case err == nil:
from = blockNumber + 1
case errs.Is(err, ErrNoPayments):
from = 0
default:
chore.log.Error("error retrieving last payment", zap.Error(ChoreErr.Wrap(err)))
return nil
}
latestPayments, err := chore.client.AllPayments(ctx, from)
if err != nil {
chore.log.Error("error retrieving payments", zap.Error(ChoreErr.Wrap(err)))
return nil
}
if len(latestPayments.Payments) == 0 {
return nil
}
var cachedPayments []CachedPayment
for _, payment := range latestPayments.Payments {
var status payments.PaymentStatus
if latestPayments.LatestBlock.Number-payment.BlockNumber >= int64(chore.confirmations) {
status = payments.PaymentStatusConfirmed
} else {
status = payments.PaymentStatusPending
}
cachedPayments = append(cachedPayments, CachedPayment{
From: payment.From,
To: payment.To,
TokenValue: payment.TokenValue,
USDValue: payment.USDValue,
Status: status,
BlockHash: payment.BlockHash,
BlockNumber: payment.BlockNumber,
Transaction: payment.Transaction,
LogIndex: payment.LogIndex,
Timestamp: payment.Timestamp,
})
}
err = chore.paymentsDB.DeletePending(ctx)
if err != nil {
chore.log.Error("error removing pending payments from the DB", zap.Error(ChoreErr.Wrap(err)))
return nil
}
err = chore.paymentsDB.InsertBatch(ctx, cachedPayments)
if err != nil {
chore.log.Error("error storing payments to db", zap.Error(ChoreErr.Wrap(err)))
return nil
}
return nil
})
}
// Close closes all underlying resources.
func (chore *Chore) Close() (err error) {
defer mon.Task()(nil)(&err)
chore.TransactionCycle.Close()
return nil
}