forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexer.go
427 lines (382 loc) · 11.4 KB
/
indexer.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package indexer
import (
"fmt"
"io"
"sync"
"github.com/gorilla/rpc/v2"
"go.uber.org/zap"
"github.com/ava-labs/avalanchego/api/server"
"github.com/ava-labs/avalanchego/chains"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/engine/avalanche/vertex"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/json"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/utils/wrappers"
)
const (
indexNamePrefix = "index-"
txPrefix = 0x01
vtxPrefix = 0x02
blockPrefix = 0x03
isIncompletePrefix = 0x04
previouslyIndexedPrefix = 0x05
)
var (
_ Indexer = (*indexer)(nil)
hasRunKey = []byte{0x07}
)
// Config for an indexer
type Config struct {
DB database.Database
Log logging.Logger
IndexingEnabled bool
AllowIncompleteIndex bool
BlockAcceptorGroup snow.AcceptorGroup
TxAcceptorGroup snow.AcceptorGroup
VertexAcceptorGroup snow.AcceptorGroup
APIServer server.PathAdder
ShutdownF func()
}
// Indexer causes accepted containers for a given chain
// to be indexed by their ID and by the order in which
// they were accepted by this node.
// Indexer is threadsafe.
type Indexer interface {
chains.Registrant
// Close will do nothing and return nil after the first call
io.Closer
}
// NewIndexer returns a new Indexer and registers a new endpoint on the given API server.
func NewIndexer(config Config) (Indexer, error) {
indexer := &indexer{
log: config.Log,
db: config.DB,
allowIncompleteIndex: config.AllowIncompleteIndex,
indexingEnabled: config.IndexingEnabled,
blockAcceptorGroup: config.BlockAcceptorGroup,
txAcceptorGroup: config.TxAcceptorGroup,
vertexAcceptorGroup: config.VertexAcceptorGroup,
txIndices: map[ids.ID]*index{},
vtxIndices: map[ids.ID]*index{},
blockIndices: map[ids.ID]*index{},
pathAdder: config.APIServer,
shutdownF: config.ShutdownF,
}
hasRun, err := indexer.hasRun()
if err != nil {
return nil, err
}
indexer.hasRunBefore = hasRun
return indexer, indexer.markHasRun()
}
type indexer struct {
clock mockable.Clock
lock sync.RWMutex
log logging.Logger
db database.Database
closed bool
// Called in a goroutine on shutdown
shutdownF func()
// true if this is not the first run using this database
hasRunBefore bool
// Used to add API endpoint for new indices
pathAdder server.PathAdder
// If true, allow running in such a way that could allow the creation
// of an index which could be missing accepted containers.
allowIncompleteIndex bool
// If false, don't create index for a chain when RegisterChain is called
indexingEnabled bool
// Chain ID --> index of blocks of that chain (if applicable)
blockIndices map[ids.ID]*index
// Chain ID --> index of vertices of that chain (if applicable)
vtxIndices map[ids.ID]*index
// Chain ID --> index of txs of that chain (if applicable)
txIndices map[ids.ID]*index
// Notifies of newly accepted blocks
blockAcceptorGroup snow.AcceptorGroup
// Notifies of newly accepted transactions
txAcceptorGroup snow.AcceptorGroup
// Notifies of newly accepted vertices
vertexAcceptorGroup snow.AcceptorGroup
}
// Assumes [ctx.Lock] is not held
func (i *indexer) RegisterChain(chainName string, ctx *snow.ConsensusContext, vm common.VM) {
i.lock.Lock()
defer i.lock.Unlock()
if i.closed {
i.log.Debug("not registering chain to indexer",
zap.String("reason", "indexer is closed"),
zap.String("chainName", chainName),
)
return
} else if ctx.SubnetID != constants.PrimaryNetworkID {
i.log.Debug("not registering chain to indexer",
zap.String("reason", "not in the primary network"),
zap.String("chainName", chainName),
)
return
}
chainID := ctx.ChainID
if i.blockIndices[chainID] != nil || i.txIndices[chainID] != nil || i.vtxIndices[chainID] != nil {
i.log.Warn("chain is already being indexed",
zap.Stringer("chainID", chainID),
)
return
}
// If the index is incomplete, make sure that's OK. Otherwise, cause node to die.
isIncomplete, err := i.isIncomplete(chainID)
if err != nil {
i.log.Error("couldn't get whether chain is incomplete",
zap.String("chainName", chainName),
zap.Error(err),
)
if err := i.close(); err != nil {
i.log.Error("failed to close indexer",
zap.Error(err),
)
}
return
}
// See if this chain was indexed in a previous run
previouslyIndexed, err := i.previouslyIndexed(chainID)
if err != nil {
i.log.Error("couldn't get whether chain was previously indexed",
zap.String("chainName", chainName),
zap.Error(err),
)
if err := i.close(); err != nil {
i.log.Error("failed to close indexer",
zap.Error(err),
)
}
return
}
if !i.indexingEnabled { // Indexing is disabled
if previouslyIndexed && !i.allowIncompleteIndex {
// We indexed this chain in a previous run but not in this run.
// This would create an incomplete index, which is not allowed, so exit.
i.log.Fatal("running would cause index to become incomplete but incomplete indices are disabled",
zap.String("chainName", chainName),
)
if err := i.close(); err != nil {
i.log.Error("failed to close indexer",
zap.Error(err),
)
}
return
}
// Creating an incomplete index is allowed. Mark index as incomplete.
err := i.markIncomplete(chainID)
if err == nil {
return
}
i.log.Fatal("couldn't mark chain as incomplete",
zap.String("chainName", chainName),
zap.Error(err),
)
if err := i.close(); err != nil {
i.log.Error("failed to close indexer",
zap.Error(err),
)
}
return
}
if !i.allowIncompleteIndex && isIncomplete && (previouslyIndexed || i.hasRunBefore) {
i.log.Fatal("index is incomplete but incomplete indices are disabled. Shutting down",
zap.String("chainName", chainName),
)
if err := i.close(); err != nil {
i.log.Error("failed to close indexer",
zap.Error(err),
)
}
return
}
// Mark that in this run, this chain was indexed
if err := i.markPreviouslyIndexed(chainID); err != nil {
i.log.Error("couldn't mark chain as indexed",
zap.String("chainName", chainName),
zap.Error(err),
)
if err := i.close(); err != nil {
i.log.Error("failed to close indexer",
zap.Error(err),
)
}
return
}
index, err := i.registerChainHelper(chainID, blockPrefix, chainName, "block", i.blockAcceptorGroup)
if err != nil {
i.log.Fatal("failed to create index",
zap.String("chainName", chainName),
zap.String("endpoint", "block"),
zap.Error(err),
)
if err := i.close(); err != nil {
i.log.Error("failed to close indexer",
zap.Error(err),
)
}
return
}
i.blockIndices[chainID] = index
switch vm.(type) {
case vertex.DAGVM:
vtxIndex, err := i.registerChainHelper(chainID, vtxPrefix, chainName, "vtx", i.vertexAcceptorGroup)
if err != nil {
i.log.Fatal("couldn't create index",
zap.String("chainName", chainName),
zap.String("endpoint", "vtx"),
zap.Error(err),
)
if err := i.close(); err != nil {
i.log.Error("failed to close indexer",
zap.Error(err),
)
}
return
}
i.vtxIndices[chainID] = vtxIndex
txIndex, err := i.registerChainHelper(chainID, txPrefix, chainName, "tx", i.txAcceptorGroup)
if err != nil {
i.log.Fatal("couldn't create index",
zap.String("chainName", chainName),
zap.String("endpoint", "tx"),
zap.Error(err),
)
if err := i.close(); err != nil {
i.log.Error("failed to close indexer",
zap.Error(err),
)
}
return
}
i.txIndices[chainID] = txIndex
case block.ChainVM:
default:
vmType := fmt.Sprintf("%T", vm)
i.log.Error("got unexpected vm type",
zap.String("vmType", vmType),
)
if err := i.close(); err != nil {
i.log.Error("failed to close indexer",
zap.Error(err),
)
}
}
}
func (i *indexer) registerChainHelper(
chainID ids.ID,
prefixEnd byte,
name, endpoint string,
acceptorGroup snow.AcceptorGroup,
) (*index, error) {
prefix := make([]byte, ids.IDLen+wrappers.ByteLen)
copy(prefix, chainID[:])
prefix[ids.IDLen] = prefixEnd
indexDB := prefixdb.New(prefix, i.db)
index, err := newIndex(indexDB, i.log, i.clock)
if err != nil {
_ = indexDB.Close()
return nil, err
}
// Register index to learn about new accepted vertices
if err := acceptorGroup.RegisterAcceptor(chainID, fmt.Sprintf("%s%s", indexNamePrefix, chainID), index, true); err != nil {
_ = index.Close()
return nil, err
}
// Create an API endpoint for this index
apiServer := rpc.NewServer()
codec := json.NewCodec()
apiServer.RegisterCodec(codec, "application/json")
apiServer.RegisterCodec(codec, "application/json;charset=UTF-8")
if err := apiServer.RegisterService(&service{index: index}, "index"); err != nil {
_ = index.Close()
return nil, err
}
if err := i.pathAdder.AddRoute(apiServer, "index/"+name, "/"+endpoint); err != nil {
_ = index.Close()
return nil, err
}
return index, nil
}
// Close this indexer. Stops indexing all chains.
// Closes [i.db]. Assumes Close is only called after
// the node is done making decisions.
// Calling Close after it has been called does nothing.
func (i *indexer) Close() error {
i.lock.Lock()
defer i.lock.Unlock()
return i.close()
}
func (i *indexer) close() error {
if i.closed {
return nil
}
i.closed = true
errs := &wrappers.Errs{}
for chainID, txIndex := range i.txIndices {
errs.Add(
txIndex.Close(),
i.txAcceptorGroup.DeregisterAcceptor(chainID, fmt.Sprintf("%s%s", indexNamePrefix, chainID)),
)
}
for chainID, vtxIndex := range i.vtxIndices {
errs.Add(
vtxIndex.Close(),
i.vertexAcceptorGroup.DeregisterAcceptor(chainID, fmt.Sprintf("%s%s", indexNamePrefix, chainID)),
)
}
for chainID, blockIndex := range i.blockIndices {
errs.Add(
blockIndex.Close(),
i.blockAcceptorGroup.DeregisterAcceptor(chainID, fmt.Sprintf("%s%s", indexNamePrefix, chainID)),
)
}
errs.Add(i.db.Close())
go i.shutdownF()
return errs.Err
}
func (i *indexer) markIncomplete(chainID ids.ID) error {
key := make([]byte, ids.IDLen+wrappers.ByteLen)
copy(key, chainID[:])
key[ids.IDLen] = isIncompletePrefix
return i.db.Put(key, nil)
}
// Returns true if this chain is incomplete
func (i *indexer) isIncomplete(chainID ids.ID) (bool, error) {
key := make([]byte, ids.IDLen+wrappers.ByteLen)
copy(key, chainID[:])
key[ids.IDLen] = isIncompletePrefix
return i.db.Has(key)
}
func (i *indexer) markPreviouslyIndexed(chainID ids.ID) error {
key := make([]byte, ids.IDLen+wrappers.ByteLen)
copy(key, chainID[:])
key[ids.IDLen] = previouslyIndexedPrefix
return i.db.Put(key, nil)
}
// Returns true if this chain is incomplete
func (i *indexer) previouslyIndexed(chainID ids.ID) (bool, error) {
key := make([]byte, ids.IDLen+wrappers.ByteLen)
copy(key, chainID[:])
key[ids.IDLen] = previouslyIndexedPrefix
return i.db.Has(key)
}
// Mark that the node has run at least once
func (i *indexer) markHasRun() error {
return i.db.Put(hasRunKey, nil)
}
// Returns true if the node has run before
func (i *indexer) hasRun() (bool, error) {
return i.db.Has(hasRunKey)
}