-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewards_handler.go
220 lines (183 loc) · 4.73 KB
/
rewards_handler.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
// Copyright (C) 2022, Chain4Travel AG. All rights reserved.
//
// This file is a derived work, based on ava-labs code.
//
// It is distributed under the same license conditions as the
// original code from which it is derived.
//
// Much love to the original authors for their work.
// **********************************************************
package rewards
import (
"context"
"fmt"
"time"
"github.com/lasthyphen/dijetsnodego/vms/platformvm/txs"
"go.uber.org/zap"
"github.com/lasthyphen/dijetsnodego/api"
"github.com/lasthyphen/dijetsnodego/ids"
"github.com/lasthyphen/dijetsnodego/utils/formatting"
caminoGoDjtx "github.com/lasthyphen/dijetsnodego/vms/components/djtx"
"github.com/lasthyphen/dijetsnodego/vms/platformvm"
"github.com/lasthyphen/mages/db"
"github.com/lasthyphen/mages/models"
"github.com/lasthyphen/mages/services"
"github.com/lasthyphen/mages/services/indexes/djtx"
"github.com/lasthyphen/mages/servicesctrl"
"github.com/lasthyphen/mages/utils"
)
type Handler struct {
client platformvm.Client
conns *utils.Connections
perist db.Persist
djtxAssetID ids.ID
writer *djtx.Writer
cid ids.ID
doneCh chan struct{}
}
func (r *Handler) Start(sc *servicesctrl.Control) error {
conns, err := sc.Database()
if err != nil {
return err
}
go r.runTicker(sc, conns)
return nil
}
func (r *Handler) Close() {
close(r.doneCh)
}
func (r *Handler) runTicker(sc *servicesctrl.Control, conns *utils.Connections) {
sc.Log.Info("start")
defer func() {
sc.Log.Info("stop")
}()
ticker := time.NewTicker(24 * time.Hour)
r.doneCh = make(chan struct{}, 1)
r.conns = conns
r.client = platformvm.NewClient(sc.ServicesCfg.CaminoNode)
r.perist = db.NewPersist()
r.djtxAssetID = sc.GenesisContainer.DjtxAssetID
r.cid = ids.Empty
r.writer = djtx.NewWriter(r.cid.String(), r.djtxAssetID)
defer func() {
ticker.Stop()
_ = conns.Close()
}()
for {
select {
case <-ticker.C:
err := r.processRewards()
if err != nil {
sc.Log.Error("failed processing rewards",
zap.Error(err),
)
}
case <-r.doneCh:
return
}
}
}
func (r *Handler) processRewards() error {
job := r.conns.Stream().NewJob("rewards-handler")
sess := r.conns.DB().NewSessionForEventReceiver(job)
ctx := context.Background()
var err error
type RewardTx struct {
ID string
Txid string
Type models.BlockType
CreatedAt time.Time
}
var reardsTxs []RewardTx
_, err = sess.Select(
db.TableRewards+".id",
db.TableRewards+".txid",
db.TablePvmBlocks+".type",
db.TableRewards+".created_at",
).
From(db.TableRewards).
Join(db.TablePvmBlocks, db.TableRewards+".block_id = "+db.TablePvmBlocks+".parent_id").
Where(db.TableRewards+".processed = ? and "+db.TableRewards+".created_at < ?", 0, time.Now().Add(-3*time.Second)).
LoadContext(ctx, &reardsTxs)
if err != nil {
return err
}
if len(reardsTxs) == 0 {
return nil
}
for _, rewardTx := range reardsTxs {
if rewardTx.Type == models.BlockTypeAbort {
err = r.markRewardProcessed(rewardTx.ID)
if err != nil {
return err
}
continue
}
id, err := ids.FromString(rewardTx.Txid)
if err != nil {
return err
}
var rewardsUtxos [][]byte
arg := &api.GetTxArgs{TxID: id, Encoding: formatting.Hex}
rewardsUtxos, err = r.client.GetRewardUTXOs(ctx, arg)
if err != nil {
return err
}
if len(rewardsUtxos) == 0 {
return fmt.Errorf("no rewards %s", rewardTx.Txid)
}
err = r.processRewardUtxos(rewardsUtxos, rewardTx.CreatedAt)
if err != nil {
return err
}
err = r.markRewardProcessed(rewardTx.ID)
if err != nil {
return err
}
}
return nil
}
func (r *Handler) processRewardUtxos(rewardsUtxos [][]byte, createdAt time.Time) error {
job := r.conns.Stream().NewJob("rewards-handler-persist")
sess := r.conns.DB().NewSessionForEventReceiver(job)
dbTx, err := sess.Begin()
if err != nil {
return err
}
defer dbTx.RollbackUnlessCommitted()
ctx := context.Background()
for _, reawrdUtxo := range rewardsUtxos {
var utxo *caminoGoDjtx.UTXO
_, err = txs.Codec.Unmarshal(reawrdUtxo, &utxo)
if err != nil {
return err
}
cCtx := services.NewConsumerContext(ctx, sess, createdAt.Unix(), int64(createdAt.Nanosecond()), r.perist, r.cid.String())
_, _, err = r.writer.ProcessStateOut(
cCtx,
utxo.Out,
utxo.TxID,
utxo.OutputIndex,
utxo.AssetID(),
0,
0,
r.cid.String(),
false,
false,
)
if err != nil {
return err
}
}
return dbTx.Commit()
}
func (r *Handler) markRewardProcessed(id string) error {
job := r.conns.Stream().NewJob("rewards-handler")
sess := r.conns.DB().NewSessionForEventReceiver(job)
ctx := context.Background()
reward := &db.Rewards{
ID: id,
Processed: 1,
}
return r.perist.UpdateRewardsProcessed(ctx, sess, reward)
}