Skip to content

Commit c367984

Browse files
committed
Merge branch 'andrius/fsp-indexer' into 'main'
Add FSP indexing mode with smart catchup See merge request flarenetwork/FSP/flare-system-c-chain-indexer!80
2 parents 1c1be26 + 16ef8c0 commit c367984

65 files changed

Lines changed: 2586 additions & 847 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.dockerignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.git
2+
.gitignore
3+
.idea
4+
.vscode
5+
.DS_Store
6+
**/.DS_Store
7+
8+
*.log
9+
*.out
10+
coverage*

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* @ryanc-flare @tilenflare
1+
* @adg-flare @tilenflare

.github/workflows/build_and_test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ jobs:
3535
args: --timeout 5m0s
3636

3737
- name: Build
38-
run: go build
38+
run: go build ./...
3939

4040
- name: Test
4141
run: |
42-
mysql -u root -p'root' -h 127.0.0.1 -P 3306 < database/docker/db_init/flare_ftso_indexer.sql
43-
go test -v ./indexer
42+
mysql -u root -p'root' -h 127.0.0.1 -P 3306 < internal/database/docker/db_init/flare_ftso_indexer.sql
43+
go test -v ./test

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
logger/logs
1+
logs/
22
tmp/
33
*.log
44
config.toml
55
.vscode/
66
coverage.out
7-
7+
.gocache
8+
.gitlab
9+
.github

.gitlab-ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ test:
5555
- name: mysql:latest
5656
alias: mysql
5757
script:
58-
- sed -i 's/test_host = "localhost"/test_host = "mysql"/' testing/config_test.toml
59-
- go test ./indexer
58+
- sed -i 's/test_host = "localhost"/test_host = "mysql"/' test/config_test.toml
59+
- go test ./test
6060

6161
build-container:
6262
stage: build

.gitlab/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* @ryancollingham @tilen.marc
1+
* @adg-flare @tilen.marc

CHANGELOG.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,47 @@ and this project adheres to
77
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
88

99

10+
## Unreleased
11+
12+
### Added
13+
14+
- New `indexer.mode = "fsp"` mode for the FSP provider stack, introduced to
15+
massively speed up indexing from scratch by collecting only the data required
16+
for FSP operation. The required FSP transaction and log filters (for example
17+
Submission, Relay, FlareSystemsManager, and VoterRegistry) are built in and
18+
merged with any user-supplied `collect_transactions` / `collect_logs`, so a
19+
minimal config is enough to run an FSP indexer. `indexer.mode = "full"`
20+
keeps the previous generic-indexer behaviour.
21+
- `indexer.history_epochs` now controls retention in FSP mode. The indexer
22+
keeps `history_epochs` reward epochs of fully indexed blocks, plus the
23+
signing-policy event metadata needed for those epochs.
24+
In this mode `indexer.start_index` and `db.history_drop` are ignored.
25+
**`history_epochs = 0` is the recommended setting for FSP provider
26+
operation**: the indexer fully indexes only the last ~2 hours of blocks and
27+
backfills only the required metadata events for the three most recent reward
28+
epochs. Higher values are mainly useful for reward calculation.
29+
- Resolution of contract addresses by name via the on-chain ContractRegistry,
30+
removing the need to hardcode addresses in config.
31+
- `GET /health` endpoint on port 8080: returns 503 while startup catchup is in
32+
progress and 200 once the indexer reaches continuous-indexing mode. Suitable
33+
as a Docker / Kubernetes readiness probe.
34+
- New `first_database_fsp_event_block` state row exposed alongside
35+
`first_database_block`, so clients can distinguish "earliest fully-indexed
36+
block" from "earliest block with FSP event coverage" and reason about
37+
available history.
38+
39+
### Changed
40+
41+
- Repository structure refactored under `cmd/` and `internal/` to follow
42+
conventional Go layout. The runnable binary moved to `./cmd/indexer`.
43+
- Block-by-timestamp lookup uses heuristics to narrow the search window before
44+
binary search, avoiding requests for very old blocks when running against
45+
RPC nodes with limited history.
46+
47+
1048
## \[[v1.1.2](https://github.com/flare-foundation/flare-system-c-chain-indexer/tree/v1.1.2)\] - 2025-11-03
1149

1250
### Added
1351

1452
- simplify calculation of starting index within indexer
1553
- add extra env var overrides for DB configuration
16-

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ Run the tests with:
6464
$ go test ./...
6565
```
6666

67-
Note that the `main_test.go` is an integration test which requires an RPC connection and MySQL database -
68-
you can edit the test config file at `testing/config_test.toml`. Environment variable overrides are also
67+
Note that `cmd/indexer/main_test.go` is an integration test which requires an RPC connection and MySQL database -
68+
you can edit the test config file at `test/config_test.toml`. Environment variable overrides are also
6969
supported for convenience.
7070

7171
## Release process (if applicable)

Dockerfile

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ RUN go mod download
1111
COPY . ./
1212

1313
# Build the applications
14-
RUN go build -o /app/flare_cchain_indexer ./main.go
14+
RUN go build -o /app/flare_cchain_indexer ./cmd/indexer
1515

16-
FROM debian:trixie@sha256:72547dd722cd005a8c2aa2079af9ca0ee93aad8e589689135feaed60b0a8c08d AS execution
16+
FROM debian:trixie-slim@sha256:cedb1ef40439206b673ee8b33a46a03a0c9fa90bf3732f54704f99cb061d2c5a AS execution
17+
18+
# ca-certificates: outbound HTTPS (RPC endpoints, etc.).
19+
# wget: docker healthcheck against /health on port 8080.
20+
RUN apt-get update \
21+
&& apt-get install -y --no-install-recommends ca-certificates wget \
22+
&& rm -rf /var/lib/apt/lists/*
1723

1824
WORKDIR /app
1925

2026
COPY --from=builder /app/flare_cchain_indexer .
21-
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
2227

2328
CMD ["./flare_cchain_indexer"]

README.md

Lines changed: 30 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<a href="CHANGELOG.md">Changelog</a>
1717
</div>
1818

19-
# Flare FTSO indexer
19+
# Flare C-Chain Indexer
2020

2121
This code implements a fast and parallelized indexer of C-chain that fetches data needed for
2222
various Flare protocols. It saves the data in a MySQL database.
@@ -30,59 +30,12 @@ docker-compose.yaml file for automatic deployment of a database).
3030

3131
The configuration is read from a `toml` file. Config file can be specified using the command line parameter `--config`, e.g., `./flare-ftso-indexer --config config.toml`.
3232
The default config file name is `config.toml`.
33-
Below is the list of configuration parameters, most are self-explanatory. Note that the chain node, to which the indexer connects
34-
(parameter `node_url`), needs to allow many simultaneous request if the indexer is about to index big amount of data.
35-
36-
```toml
37-
[indexer]
38-
start_index = 0 # the number of the block that the indexer will start with; will default to the appropriate block number according to the history drop configuration. Does not usually need to be set unless disabling history drop.
39-
stop_index = 0 # the number of the block that the indexer will stop with; set 0 or skip to index indefinitely
40-
num_parallel_req = 100 # the number of threads doing requests to the chain in parallel
41-
batch_size = 1000 # the number of blocks that will be pushed to a database in a batch (should be divisible by num_parallel_req)
42-
log_range = 10 # the size of the interval of blocks used to request logs in each request; suggested value is log_range = batch_size / num_parallel_req; note that a blockchain node might have an upper bound on this
43-
new_block_check_millis = 1000 # interval for checking for new blocks
44-
45-
[[indexer.collect_transactions]]
46-
contract_address = "22474d350ec2da53d717e30b96e9a2b7628ede5b" # address of the contract (can be "undefined")
47-
func_sig = "f14fcbc8" # signature of the function on the contract (can be "undefined")
48-
status=true # boolean indicating if it should be checked if the transaction succeeded
49-
collect_events=true # boolean indicating if the logs of the emitted events should be saved to the database
50-
signature = true # boolean indicating if the transaction signature should be saved to the database
51-
52-
[[indexer.collect_transactions]]
53-
contract_address = "22474d350ec2da53d717e30b96e9a2b7628ede5b"
54-
func_sig = "4369af80"
55-
status = true
56-
collect_events = true
57-
58-
[[indexer.collect_logs]]
59-
contract_address = "b682deef4f8e298d86bfc3e21f50c675151fb974" # address of the contract calling the log (can be "undefined")
60-
topic = "undefined" # topic0 of the log (can be "undefined")
61-
62-
[db]
63-
host = "localhost"
64-
port = 3306
65-
database = "flare_ftso_indexer"
66-
username = "root"
67-
password = "root"
68-
log_queries = false
69-
drop_table_at_start = false # set to true to drop existing tables at the start of the indexing - useful to force re-indexing
70-
history_drop = 604800 # Enable deleting the transactions and logs in DB that are older (timestamp of the block) than history_drop (in seconds); set 0 to turn off; defaults to 7 days for Flare/Songbird and 2 days for Coston*
71-
72-
[logger]
73-
level = "INFO"
74-
file = "./logger/logs/flare-ftso-indexer.log"
75-
console = true
76-
77-
[chain]
78-
node_url = "http://127.0.0.1:8545/" # or NODE_URL environment variable
79-
# api_key = ... or NODE_API_KEY environment variable
80-
# chain_type = 1 # default Avalanche based chain=1, Ethereum based chain=2
81-
82-
[timeout]
83-
backoff_max_elapsed_time_seconds = 300 # optional, defaults to 300s = 5 minutes. Affects how long the indexer will keep retrying in case of a complete outage of the node provider. Set to 0 to retry indefinitely.
84-
timeout_milis = 1000 # optional, defaults to 1000ms = 1s. Try increasing if you see timeout errors often.
85-
```
33+
Use [`config.example.toml`](config.example.toml) as the single config template. It includes full mode/indexer/db/chain/logger/timeout examples and comments.
34+
35+
#### Mode selection
36+
37+
- `indexer.mode = "fsp"`: use this when running as part of the FSP provider stack. Required FSP transactions/logs are hardcoded and auto-applied, so you do not need to specify `[[indexer.collect_transactions]]` or `[[indexer.collect_logs]]` in config (you can still add extra entries; they are merged and deduplicated). FSP startup also does selective indexing for required reward-epoch metadata windows instead of blindly indexing full historical ranges, which makes startup significantly faster. In this mode, `indexer.start_index` and `db.history_drop` are ignored; retention is derived from `indexer.history_epochs`.
38+
- `indexer.mode = "full"`: use this for a generic C-chain indexer. In this mode you should define what to index via `[[indexer.collect_transactions]]` and `[[indexer.collect_logs]]`.
8639

8740
If the C chain indexer has been previously run and there is existing data in the database,
8841
subsequent runs will resume indexing from after the last indexed block. Only when starting with an
@@ -101,41 +54,54 @@ set it back to false afterwards to avoid losing data on subsequent runs.
10154

10255
### Database
10356

104-
In `database/docker` we provide a simple database. Navigate to the folder and run
57+
In `internal/database/docker` we provide a simple database. Navigate to the folder and run
10558

10659
```bash
10760
docker-compose up
10861
```
10962

110-
### Running FTSO indexer
63+
### Running indexer
11164

11265
Simply run
11366

11467
```bash
115-
go run main.go --config config.toml
68+
go run ./cmd/indexer --config config.toml
11669
```
11770

11871
or build and run the binaries with
11972

12073
```bash
121-
go build
74+
go build -o flare-ftso-indexer ./cmd/indexer
12275
./flare-ftso-indexer --config config.toml
12376
```
12477

78+
### Health endpoint
79+
80+
The indexer exposes `GET /health` on port `8080`.
81+
82+
- Returns `503` while startup catchup/backfill is still running.
83+
- Returns `200` after startup is complete and the indexer has entered continuous indexing mode.
84+
85+
Example:
86+
87+
```bash
88+
curl -i http://localhost:8080/health
89+
```
90+
12591
### Tests
12692

12793
There is an integration test which checks the historical indexing against known transactions and
12894
logs on Coston2. To run this test you will need a MySQL server and a Coston2 node, preferably one that is not rate-limited.
129-
The integration test is configured via `testing/config_test.toml`. You can execute it with:
95+
The integration test is configured via `test/config_test.toml`. You can execute it with:
13096

13197
```bash
132-
$ go test ./main_test.go
98+
$ go test ./cmd/indexer
13399
```
134100

135-
Additionally, a unit test with a mocked chain node is available in `indexer/indexer_test.go`. Like the integration test, it uses `testing/config_test.toml` for configuration. You can run it using:
101+
Additionally, a mocked-chain integration test is available in `test/indexer_test.go`. It uses `test/config_test.toml` for configuration. You can run it using:
136102

137103
```bash
138-
go test ./indexer
104+
go test ./test
139105
```
140106

141107
To run tests with coverage analysis across all packages, save the results to `coverage.out`, and convert the report into an interactive HTML file run:
@@ -147,8 +113,8 @@ go tool cover -html=coverage.out
147113

148114
### Benchmarks
149115

150-
File `benchmarks/songbird_test.go` contains a benchmark test for indexing the FTSO protocol on the Songbird network. It processes 10,000 blocks and analyzes them. The test configuration is specified in `benchmarks/config_benchmark.toml`. To run the benchmark (replacing 10x with any desired number of repetitions), use:
116+
File `benchmarks/songbird_test.go` contains a benchmark test for indexing the FTSO protocol on the Songbird network. It processes 10,000 blocks and analyzes them. The test configuration is specified in `benchmarks/config_banchmark.toml`. To run the benchmark (replacing 10x with any desired number of repetitions), use:
151117

152118
```bash
153-
go test -benchmem -run=^$ -benchtime 10x -bench ^BenchmarkBlockRequests$ flare-ftso-indexer/benchmarks
119+
go test -benchmem -run=^$ -benchtime 10x -bench ^BenchmarkBlockRequests$ ./benchmarks
154120
```

0 commit comments

Comments
 (0)