-
Notifications
You must be signed in to change notification settings - Fork 77
/
worker.go
368 lines (307 loc) · 10.4 KB
/
worker.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
package parser
import (
"encoding/json"
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/x/authz"
"github.com/forbole/juno/v5/logging"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/forbole/juno/v5/database"
"github.com/forbole/juno/v5/types/config"
"github.com/forbole/juno/v5/modules"
tmctypes "github.com/cometbft/cometbft/rpc/core/types"
tmtypes "github.com/cometbft/cometbft/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/forbole/juno/v5/node"
"github.com/forbole/juno/v5/types"
"github.com/forbole/juno/v5/types/utils"
)
// Worker defines a job consumer that is responsible for getting and
// aggregating block and associated data and exporting it to a database.
type Worker struct {
index int
queue types.HeightQueue
codec codec.Codec
modules []modules.Module
node node.Node
db database.Database
logger logging.Logger
}
// NewWorker allows to create a new Worker implementation.
func NewWorker(ctx *Context, queue types.HeightQueue, index int) Worker {
return Worker{
index: index,
codec: ctx.EncodingConfig.Codec,
node: ctx.Node,
queue: queue,
db: ctx.Database,
modules: ctx.Modules,
logger: ctx.Logger,
}
}
// Start starts a worker by listening for new jobs (block heights) from the
// given worker queue. Any failed job is logged and re-enqueued.
func (w Worker) Start() {
logging.WorkerCount.Inc()
chainID, err := w.node.ChainID()
if err != nil {
w.logger.Error("error while getting chain ID from the node ", "err", err)
}
for i := range w.queue {
if err := w.ProcessIfNotExists(i); err != nil {
// re-enqueue any failed job after average block time
time.Sleep(config.GetAvgBlockTime())
// TODO: Implement exponential backoff or max retries for a block height.
go func() {
w.logger.Error("re-enqueueing failed block", "height", i, "err", err)
w.queue <- i
}()
}
logging.WorkerHeight.WithLabelValues(fmt.Sprintf("%d", w.index), chainID).Set(float64(i))
}
}
// ProcessIfNotExists defines the job consumer workflow. It will fetch a block for a given
// height and associated metadata and export it to a database if it does not exist yet. It returns an
// error if any export process fails.
func (w Worker) ProcessIfNotExists(height int64) error {
exists, err := w.db.HasBlock(height)
if err != nil {
return fmt.Errorf("error while searching for block: %s", err)
}
if exists {
w.logger.Debug("skipping already exported block", "height", height)
return nil
}
return w.Process(height)
}
// Process fetches a block for a given height and associated metadata and export it to a database.
// It returns an error if any export process fails.
func (w Worker) Process(height int64) error {
if height == 0 {
cfg := config.Cfg.Parser
genesisDoc, genesisState, err := utils.GetGenesisDocAndState(cfg.GenesisFilePath, w.node)
if err != nil {
return fmt.Errorf("failed to get genesis: %s", err)
}
return w.HandleGenesis(genesisDoc, genesisState)
}
w.logger.Debug("processing block", "height", height)
block, err := w.node.Block(height)
if err != nil {
return fmt.Errorf("failed to get block from node: %s", err)
}
events, err := w.node.BlockResults(height)
if err != nil {
return fmt.Errorf("failed to get block results from node: %s", err)
}
txs, err := w.node.Txs(block)
if err != nil {
return fmt.Errorf("failed to get transactions for block: %s", err)
}
vals, err := w.node.Validators(height)
if err != nil {
return fmt.Errorf("failed to get validators for block: %s", err)
}
return w.ExportBlock(block, events, txs, vals)
}
// ProcessTransactions fetches transactions for a given height and stores them into the database.
// It returns an error if the export process fails.
func (w Worker) ProcessTransactions(height int64) error {
block, err := w.node.Block(height)
if err != nil {
return fmt.Errorf("failed to get block from node: %s", err)
}
txs, err := w.node.Txs(block)
if err != nil {
return fmt.Errorf("failed to get transactions for block: %s", err)
}
return w.ExportTxs(txs)
}
// HandleGenesis accepts a GenesisDoc and calls all the registered genesis handlers
// in the order in which they have been registered.
func (w Worker) HandleGenesis(genesisDoc *tmtypes.GenesisDoc, appState map[string]json.RawMessage) error {
// Call the genesis handlers
for _, module := range w.modules {
if genesisModule, ok := module.(modules.GenesisModule); ok {
if err := genesisModule.HandleGenesis(genesisDoc, appState); err != nil {
w.logger.GenesisError(module, err)
}
}
}
return nil
}
// SaveValidators persists a list of Tendermint validators with an address and a
// consensus public key. An error is returned if the public key cannot be Bech32
// encoded or if the DB write fails.
func (w Worker) SaveValidators(vals []*tmtypes.Validator) error {
var validators = make([]*types.Validator, len(vals))
for index, val := range vals {
consAddr := sdk.ConsAddress(val.Address).String()
consPubKey, err := types.ConvertValidatorPubKeyToBech32String(val.PubKey)
if err != nil {
return fmt.Errorf("failed to convert validator public key for validators %s: %s", consAddr, err)
}
validators[index] = types.NewValidator(consAddr, consPubKey)
}
err := w.db.SaveValidators(validators)
if err != nil {
return fmt.Errorf("error while saving validators: %s", err)
}
return nil
}
// ExportBlock accepts a finalized block and a corresponding set of transactions
// and persists them to the database along with attributable metadata. An error
// is returned if the write fails.
func (w Worker) ExportBlock(
b *tmctypes.ResultBlock, r *tmctypes.ResultBlockResults, txs []*types.Tx, vals *tmctypes.ResultValidators,
) error {
// Save all validators
err := w.SaveValidators(vals.Validators)
if err != nil {
return err
}
// Make sure the proposer exists
proposerAddr := sdk.ConsAddress(b.Block.ProposerAddress)
val := findValidatorByAddr(proposerAddr.String(), vals)
if val == nil {
return fmt.Errorf("failed to find validator by proposer address %s: %s", proposerAddr.String(), err)
}
// Save the block
err = w.db.SaveBlock(types.NewBlockFromTmBlock(b, sumGasTxs(txs)))
if err != nil {
return fmt.Errorf("failed to persist block: %s", err)
}
// Save the commits
err = w.ExportCommit(b.Block.LastCommit, vals)
if err != nil {
return err
}
// Call the block handlers
for _, module := range w.modules {
if blockModule, ok := module.(modules.BlockModule); ok {
err = blockModule.HandleBlock(b, r, txs, vals)
if err != nil {
w.logger.BlockError(module, b, err)
}
}
}
// Export the transactions
return w.ExportTxs(txs)
}
// ExportCommit accepts a block commitment and a corresponding set of
// validators for the commitment and persists them to the database. An error is
// returned if any write fails or if there is any missing aggregated data.
func (w Worker) ExportCommit(commit *tmtypes.Commit, vals *tmctypes.ResultValidators) error {
var signatures []*types.CommitSig
for _, commitSig := range commit.Signatures {
// Avoid empty commits
if commitSig.Signature == nil {
continue
}
valAddr := sdk.ConsAddress(commitSig.ValidatorAddress)
val := findValidatorByAddr(valAddr.String(), vals)
if val == nil {
return fmt.Errorf("failed to find validator by commit validator address %s", valAddr.String())
}
signatures = append(signatures, types.NewCommitSig(
types.ConvertValidatorAddressToBech32String(commitSig.ValidatorAddress),
val.VotingPower,
val.ProposerPriority,
commit.Height,
commitSig.Timestamp,
))
}
err := w.db.SaveCommitSignatures(signatures)
if err != nil {
return fmt.Errorf("error while saving commit signatures: %s", err)
}
return nil
}
// saveTx accepts the transaction and persists it inside the database.
// An error is returned if the write fails.
func (w Worker) saveTx(tx *types.Tx) error {
err := w.db.SaveTx(tx)
if err != nil {
return fmt.Errorf("failed to handle transaction with hash %s: %s", tx.TxHash, err)
}
return nil
}
// handleTx accepts the transaction and calls the tx handlers.
func (w Worker) handleTx(tx *types.Tx) {
// Call the tx handlers
for _, module := range w.modules {
if transactionModule, ok := module.(modules.TransactionModule); ok {
err := transactionModule.HandleTx(tx)
if err != nil {
w.logger.TxError(module, tx, err)
}
}
}
}
// handleMessage accepts the transaction and handles messages contained
// inside the transaction.
func (w Worker) handleMessage(index int, msg sdk.Msg, tx *types.Tx) {
// Allow modules to handle the message
for _, module := range w.modules {
if messageModule, ok := module.(modules.MessageModule); ok {
err := messageModule.HandleMsg(index, msg, tx)
if err != nil {
w.logger.MsgError(module, tx, msg, err)
}
}
}
// If it's a MsgExecute, we need to make sure the included messages are handled as well
if msgExec, ok := msg.(*authz.MsgExec); ok {
for authzIndex, msgAny := range msgExec.Msgs {
var executedMsg sdk.Msg
err := w.codec.UnpackAny(msgAny, &executedMsg)
if err != nil {
w.logger.Error("unable to unpack MsgExec inner message", "index", authzIndex, "error", err)
}
for _, module := range w.modules {
if messageModule, ok := module.(modules.AuthzMessageModule); ok {
err = messageModule.HandleMsgExec(index, msgExec, authzIndex, executedMsg, tx)
if err != nil {
w.logger.MsgError(module, tx, executedMsg, err)
}
}
}
}
}
}
// ExportTxs accepts a slice of transactions and persists then inside the database.
// An error is returned if the write fails.
func (w Worker) ExportTxs(txs []*types.Tx) error {
// handle all transactions inside the block
for _, tx := range txs {
// save the transaction
err := w.saveTx(tx)
if err != nil {
return fmt.Errorf("error while storing txs: %s", err)
}
// call the tx handlers
w.handleTx(tx)
// handle all messages contained inside the transaction
sdkMsgs := make([]sdk.Msg, len(tx.Body.Messages))
for i, msg := range tx.Body.Messages {
var stdMsg sdk.Msg
err := w.codec.UnpackAny(msg, &stdMsg)
if err != nil {
return err
}
sdkMsgs[i] = stdMsg
}
// call the msg handlers
for i, sdkMsg := range sdkMsgs {
w.handleMessage(i, sdkMsg, tx)
}
}
totalBlocks := w.db.GetTotalBlocks()
logging.DbBlockCount.WithLabelValues("total_blocks_in_db").Set(float64(totalBlocks))
dbLatestHeight, err := w.db.GetLastBlockHeight()
if err != nil {
return err
}
logging.DbLatestHeight.WithLabelValues("db_latest_height").Set(float64(dbLatestHeight))
return nil
}