Skip to content

Commit b960661

Browse files
author
Abdul Rabbani
committed
Refactor: Decouple knownGaps and Indexer
This commit decouples knownGaps and Indexer. All knownGaps logic is in its own file. This makes testing and maintainability easier. We have also removed all efforts to check the `lastProcessedblock` - This is because we won't ever run into this issue (hyptothetically), because geth won't let it happen.
1 parent fc341a9 commit b960661

File tree

23 files changed

+541
-643
lines changed

23 files changed

+541
-643
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ related-repositories/hive/**
5656
related-repositories/ipld-eth-db/**
5757
statediff/indexer/database/sql/statediffing_test_file.sql
5858
statediff/statediffing_test_file.sql
59+
statediff/known_gaps.sql

cmd/geth/config.go

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
177177

178178
if ctx.GlobalBool(utils.StateDiffFlag.Name) {
179179
var indexerConfig interfaces.Config
180-
var fileConfig interfaces.Config
181180
var clientName, nodeID string
182181
if ctx.GlobalIsSet(utils.StateDiffWritingFlag.Name) {
183182
clientName = ctx.GlobalString(utils.StateDiffDBClientNameFlag.Name)
@@ -192,15 +191,6 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
192191
if err != nil {
193192
utils.Fatalf("%v", err)
194193
}
195-
196-
if dbType != shared.FILE {
197-
fileConfig = file.Config{
198-
FilePath: ctx.GlobalString(utils.StateDiffFilePath.Name),
199-
}
200-
} else {
201-
fileConfig = nil
202-
}
203-
204194
switch dbType {
205195
case shared.FILE:
206196
indexerConfig = file.Config{
@@ -262,14 +252,14 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
262252
}
263253
}
264254
p := statediff.Config{
265-
IndexerConfig: indexerConfig,
266-
FileConfig: fileConfig,
267-
ID: nodeID,
268-
ClientName: clientName,
269-
Context: context.Background(),
270-
EnableWriteLoop: ctx.GlobalBool(utils.StateDiffWritingFlag.Name),
271-
NumWorkers: ctx.GlobalUint(utils.StateDiffWorkersFlag.Name),
272-
WaitForSync: ctx.GlobalBool(utils.StateDiffWaitForSync.Name),
255+
IndexerConfig: indexerConfig,
256+
KnownGapsFilePath: ctx.GlobalString(utils.StateDiffKnownGapsFilePath.Name),
257+
ID: nodeID,
258+
ClientName: clientName,
259+
Context: context.Background(),
260+
EnableWriteLoop: ctx.GlobalBool(utils.StateDiffWritingFlag.Name),
261+
NumWorkers: ctx.GlobalUint(utils.StateDiffWorkersFlag.Name),
262+
WaitForSync: ctx.GlobalBool(utils.StateDiffWaitForSync.Name),
273263
}
274264
utils.RegisterStateDiffService(stack, eth, &cfg.Eth, p, backend)
275265
}

cmd/geth/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ var (
176176
utils.StateDiffWritingFlag,
177177
utils.StateDiffWorkersFlag,
178178
utils.StateDiffFilePath,
179+
utils.StateDiffKnownGapsFilePath,
179180
utils.StateDiffWaitForSync,
180181
configFileFlag,
181182
}

cmd/geth/usage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ var AppHelpFlagGroups = []flags.FlagGroup{
246246
utils.StateDiffWritingFlag,
247247
utils.StateDiffWorkersFlag,
248248
utils.StateDiffFilePath,
249+
utils.StateDiffKnownGapsFilePath,
249250
utils.StateDiffWaitForSync,
250251
},
251252
},

cmd/utils/flags.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,11 @@ var (
867867
Name: "statediff.file.path",
868868
Usage: "Full path (including filename) to write statediff data out to when operating in file mode",
869869
}
870+
StateDiffKnownGapsFilePath = cli.StringFlag{
871+
Name: "statediff.knowngapsfile.path",
872+
Usage: "Full path (including filename) to write knownGaps statements when the DB is unavailable.",
873+
Value: "./known_gaps.sql",
874+
}
870875
StateDiffDBClientNameFlag = cli.StringFlag{
871876
Name: "statediff.db.clientname",
872877
Usage: "Client name to use when writing state diffs to database",

statediff/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import (
2626

2727
// Config contains instantiation parameters for the state diffing service
2828
type Config struct {
29-
// The configuration used for the primary stateDiff Indexer
29+
// The configuration used for the stateDiff Indexer
3030
IndexerConfig interfaces.Config
31-
// The configuration used for the file stateDiff Indexer. This indexer is used if the primary indexer fails and is not a FILE indexer.
32-
FileConfig interfaces.Config
31+
// The filepath to write knownGaps insert statements if we can't connect to the DB.
32+
KnownGapsFilePath string
3333
// A unique ID used for this service
3434
ID string
3535
// Name for the client this service is running

statediff/docs/KnownGaps.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ The known gaps table is updated when the following events occur:
1010

1111
1. At start up we check the latest block from the `eth.headers_cid` table. We compare the first block that we are processing with the latest block from the DB. If they are not one unit of expectedDifference away from each other, add the gap between the two blocks.
1212
2. If there is any error in processing a block (db connection, deadlock, etc), add that block to the knownErrorBlocks slice, when the next block is successfully written, write this slice into the DB.
13-
3. If the last processed block is not one unit of expectedDifference away from the current block being processed. This can be due to any unknown or unhandled errors in geth.
1413

1514
# Glossary
1615

statediff/docs/database.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Overview
2+
3+
This document will go through some notes on the database component of the statediff service.
4+
5+
# Components
6+
7+
- Indexer: The indexer creates IPLD and DB models to insert to the Postgres DB. It performs the insert utilizing and atomic function.
8+
- Builder: The builder constructs the statediff object that needs to be inserted.
9+
- Known Gaps: Captures any gaps that might have occured and either writes them to the DB, local sql file, to prometeus, or a local error.
10+
11+
# Making Code Changes
12+
13+
## Adding a New Function to the Indexer
14+
15+
If you want to implement a new feature for adding data to the database. Keep the following in mind:
16+
17+
1. You need to handle `sql`, `file`, and `dump`.
18+
1. `sql` - Contains the code needed to write directly to the `sql` db.
19+
2. `file` - Contains all the code required to write the SQL statements to a file.
20+
3. `dump` - Contains all the code for outputting events to the console.
21+
2. You will have to add it to the `interfaces.StateDiffIndexer` interface.
-19.8 KB
Loading

statediff/indexer/constructor.go

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,55 +32,50 @@ import (
3232
)
3333

3434
// NewStateDiffIndexer creates and returns an implementation of the StateDiffIndexer interface.
35-
// You can specify the specific
36-
func NewStateDiffIndexer(ctx context.Context, chainConfig *params.ChainConfig, nodeInfo node.Info, config interfaces.Config, specificIndexer shared.DBType) (interfaces.StateDiffIndexer, error) {
37-
var indexerToCreate shared.DBType
38-
if specificIndexer == "" {
39-
indexerToCreate = config.Type()
40-
} else {
41-
indexerToCreate = specificIndexer
42-
}
43-
log.Info("Indexer to create is", "indexer", indexerToCreate)
44-
switch indexerToCreate {
35+
func NewStateDiffIndexer(ctx context.Context, chainConfig *params.ChainConfig, nodeInfo node.Info, config interfaces.Config) (sql.Database, interfaces.StateDiffIndexer, error) {
36+
switch config.Type() {
4537
case shared.FILE:
46-
log.Info("Creating a statediff indexer in SQL file writing mode")
38+
log.Info("Starting statediff service in SQL file writing mode")
4739
fc, ok := config.(file.Config)
4840
if !ok {
49-
return nil, fmt.Errorf("file config is not the correct type: got %T, expected %T", config, file.Config{})
41+
return nil, nil, fmt.Errorf("file config is not the correct type: got %T, expected %T", config, file.Config{})
5042
}
5143
fc.NodeInfo = nodeInfo
52-
return file.NewStateDiffIndexer(ctx, chainConfig, fc)
44+
ind, err := file.NewStateDiffIndexer(ctx, chainConfig, fc)
45+
return nil, ind, err
5346
case shared.POSTGRES:
54-
log.Info("Creating a statediff service in Postgres writing mode")
47+
log.Info("Starting statediff service in Postgres writing mode")
5548
pgc, ok := config.(postgres.Config)
5649
if !ok {
57-
return nil, fmt.Errorf("postgres config is not the correct type: got %T, expected %T", config, postgres.Config{})
50+
return nil, nil, fmt.Errorf("postgres config is not the correct type: got %T, expected %T", config, postgres.Config{})
5851
}
5952
var err error
6053
var driver sql.Driver
6154
switch pgc.Driver {
6255
case postgres.PGX:
6356
driver, err = postgres.NewPGXDriver(ctx, pgc, nodeInfo)
6457
if err != nil {
65-
return nil, err
58+
return nil, nil, err
6659
}
6760
case postgres.SQLX:
6861
driver, err = postgres.NewSQLXDriver(ctx, pgc, nodeInfo)
6962
if err != nil {
70-
return nil, err
63+
return nil, nil, err
7164
}
7265
default:
73-
return nil, fmt.Errorf("unrecognized Postgres driver type: %s", pgc.Driver)
66+
return nil, nil, fmt.Errorf("unrecognized Postgres driver type: %s", pgc.Driver)
7467
}
75-
return sql.NewStateDiffIndexer(ctx, chainConfig, postgres.NewPostgresDB(driver))
68+
db := postgres.NewPostgresDB(driver)
69+
ind, err := sql.NewStateDiffIndexer(ctx, chainConfig, db)
70+
return db, ind, err
7671
case shared.DUMP:
77-
log.Info("Creating statediff indexer in data dump mode")
72+
log.Info("Starting statediff service in data dump mode")
7873
dumpc, ok := config.(dump.Config)
7974
if !ok {
80-
return nil, fmt.Errorf("dump config is not the correct type: got %T, expected %T", config, dump.Config{})
75+
return nil, nil, fmt.Errorf("dump config is not the correct type: got %T, expected %T", config, dump.Config{})
8176
}
82-
return dump.NewStateDiffIndexer(chainConfig, dumpc), nil
77+
return nil, dump.NewStateDiffIndexer(chainConfig, dumpc), nil
8378
default:
84-
return nil, fmt.Errorf("unrecognized database type: %s", indexerToCreate)
79+
return nil, nil, fmt.Errorf("unrecognized database type: %s", config.Type())
8580
}
8681
}

0 commit comments

Comments
 (0)