Skip to content

Commit 32d6838

Browse files
committed
fix: make core components use config json file with constant default values
1 parent 94ab09b commit 32d6838

File tree

9 files changed

+53
-30
lines changed

9 files changed

+53
-30
lines changed

.env.template

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ CHAIN=songbird
1010
# rpc config for the chain ${CHAIN}
1111
RPC_URL=https://songbird-api.flare.network/ext/C/rpc
1212
RPC_API_KEY=
13-
LOG_QUERY_BATCH_SIZE=29
1413

1514
# exposed port for the running api
1615
API_PORT=3000

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ If new events want to be added to tracking without stopping the indexer, you sho
3333
Additional optional dev settings:
3434
- **CONFIG_PATH**: should be set to a json file containing a list of indexed event names, see `./configs/everything.json`;
3535
- **ADDRESSES_JSON**: path to file with the FAsset deployment contracts (when indexing non-official test deployments);
36-
- **LOG_QUERY_BATCH_SIZE**: the size of the log batch requested by indexer to the rpc node (should be strictly less than 30);
3736
- **MIN_BLOCK_NUMBER**: the block number that the indexer will start with. If empty, it is set to the block at which the FAsset contracts were deployed. Other values can lead to errors due to event gaps.
3837

3938
To run the indexer, run `yarn run-indexer`.

compose.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ services:
2626
RPC_URL: ${RPC_URL}
2727
RPC_API_KEY: ${RPC_API_KEY:-}
2828
CONFIG_PATH: /usr/src/app/config.json
29-
LOG_QUERY_BATCH_SIZE: ${LOG_QUERY_BATCH_SIZE:-}
3029
REINDEX_TYPE: ${REINDEX_TYPE:-}
3130
REINDEX_DIFF: ${REINDEX_DIFF:-}
3231
REINDEX_NAME: ${REINDEX_NAME:-}

packages/fasset-indexer-core/.env.template

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ RPC_URL=
55
RPC_API_KEY=
66
ADDRESSES_JSON=
77
MIN_BLOCK_NUMBER=
8-
LOG_QUERY_BATCH_SIZE=
98

109
# database
1110
DB_TYPE=sqlite|postgres

packages/fasset-indexer-core/src/config/config.ts

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import { SqliteDriver } from "@mikro-orm/sqlite"
44
import { PostgreSqlDriver } from "@mikro-orm/postgresql"
55
import { getContractInfo } from "./contracts"
66
import { SchemaUpdate } from "../orm/interface"
7+
import {
8+
EVENT_NAMES,
9+
DEFAULT_EVM_BLOCK_HEIGHT_OFFSET,
10+
DEFAULT_EVM_LOG_FETCH_BLOCK_BATCH_SIZE,
11+
DEFAULT_EVM_LOG_FETCH_SLEEP_MS,
12+
DEFAULT_EVM_STATE_UPDATE_SLEEP_MS
13+
} from "./constants"
714
import type { ContractInfo, ConfigJson, ReindexConfig } from "./interface"
815

916

@@ -84,20 +91,32 @@ export class ConfigLoader {
8491
return this.isNull(minBlock) ? undefined : parseInt(minBlock!)
8592
}
8693

87-
get logQueryBatchSize(): number {
88-
const size = process.env.LOG_QUERY_BATCH_SIZE
89-
return this.isNull(size) ? 28 : parseInt(size!)
94+
get evmLogFetchBlockBatchSize(): number {
95+
const def = DEFAULT_EVM_LOG_FETCH_BLOCK_BATCH_SIZE
96+
const val = this.json?.events.logFetchBlockBatchSize
97+
return val == null ? def : val
9098
}
9199

92-
get json(): ConfigJson | undefined {
93-
if (this._configJson == null) {
94-
const configPath = process.env.CONFIG_PATH
95-
if (configPath != null) {
96-
const file = readFileSync(configPath)
97-
this._configJson = JSON.parse(file.toString())
98-
}
99-
}
100-
return this._configJson
100+
get evmLogFetchCycleSleepMs(): number {
101+
const def = DEFAULT_EVM_LOG_FETCH_SLEEP_MS
102+
const val = this.json?.events.logFetchCycleSleepMs
103+
return val == null ? def : val
104+
}
105+
106+
get evmLogFetchBlockHeightOffset(): number {
107+
const def = DEFAULT_EVM_BLOCK_HEIGHT_OFFSET
108+
const val = this.json?.events.logFetchBlockHeightOffset
109+
return val == null ? def : val
110+
}
111+
112+
get stateFetchCycleSleepMs(): number {
113+
const def = DEFAULT_EVM_STATE_UPDATE_SLEEP_MS
114+
const val = this.json?.watchdog.cycleSleepMs
115+
return val == null ? def : val
116+
}
117+
118+
get indexEvents(): string[] {
119+
return this.json?.indexEvents ?? EVENT_NAMES
101120
}
102121

103122
get reindexing(): ReindexConfig | null {
@@ -111,6 +130,17 @@ export class ConfigLoader {
111130
return { type, diff: diff.split(','), name }
112131
}
113132

133+
protected get json(): ConfigJson | undefined {
134+
if (this._configJson == null) {
135+
const configPath = process.env.CONFIG_PATH
136+
if (configPath != null) {
137+
const file = readFileSync(configPath)
138+
this._configJson = JSON.parse(file.toString())
139+
}
140+
}
141+
return this._configJson
142+
}
143+
114144
protected get dbType(): string {
115145
return this.required('DB_TYPE')
116146
}

packages/fasset-indexer-core/src/config/constants.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
export const SLEEP_AFTER_ERROR_MS = 3000
33

44
// evm event scrape config
5-
export const EVM_LOG_FETCH_SLEEP_MS = 10 * 1000 // collect logs every 10 seconds
6-
export const EVM_STATE_UPDATE_SLEEP_MS = 60 * 1000 // collect state every one minute
7-
export const EVM_BLOCK_HEIGHT_OFFSET = 10 // log collection offset from the current block height
5+
export const DEFAULT_EVM_LOG_FETCH_SLEEP_MS = 10 * 1000 // collect logs every 10 seconds
6+
export const DEFAULT_EVM_STATE_UPDATE_SLEEP_MS = 60 * 1000 // collect state every one minute
7+
export const DEFAULT_EVM_BLOCK_HEIGHT_OFFSET = 10 // log collection offset from the current block height
8+
export const DEFAULT_EVM_LOG_FETCH_BLOCK_BATCH_SIZE = 29
89

910
// db settings
1011
export const MIN_DATABASE_POOL_CONNECTIONS = 2

packages/fasset-indexer-core/src/indexer/indexer.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { EventScraper } from './eventlib/event-scraper'
55
import { logger } from '../logger'
66
import {
77
FIRST_UNHANDLED_EVENT_BLOCK_DB_KEY,
8-
EVM_BLOCK_HEIGHT_OFFSET,
98
MIN_EVM_BLOCK_NUMBER_DB_KEY
109
} from '../config/constants'
1110
import type { Log } from 'ethers'
@@ -38,8 +37,9 @@ export class EventIndexer {
3837
if (endBlock === undefined || endBlock > lastBlockToHandle) {
3938
endBlock = lastBlockToHandle
4039
}
41-
for (let i = startBlock; i <= endBlock; i += this.context.config.logQueryBatchSize + 1) {
42-
const endLoopBlock = Math.min(endBlock, i + this.context.config.logQueryBatchSize)
40+
const batchSize = this.context.config.evmLogFetchBlockBatchSize
41+
for (let i = startBlock; i <= endBlock; i += batchSize + 1) {
42+
const endLoopBlock = Math.min(endBlock, i + batchSize)
4343
const logs = await this.eventScraper.getLogs(i, endLoopBlock)
4444
await this.storeLogs(logs)
4545
await this.setFirstUnhandledBlock(endLoopBlock + 1)
@@ -49,8 +49,7 @@ export class EventIndexer {
4949

5050
async lastBlockToHandle(): Promise<number> {
5151
const blockHeight = await this.context.provider.getBlockNumber()
52-
const offset = this.context.config.json?.events.logFetchBlockHeightOffset ?? EVM_BLOCK_HEIGHT_OFFSET
53-
return blockHeight - offset
52+
return blockHeight - this.context.config.evmLogFetchBlockHeightOffset
5453
}
5554

5655
async firstUnhandledBlock(startBlock?: number): Promise<number> {

packages/fasset-indexer-core/src/indexer/watchdog.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { AgentVault } from "../orm/entities/agent"
44
import { FtsoPrice } from "../orm/entities/state/price"
55
import { isUntrackedAgentVault, updateAgentVaultInfo } from "./utils"
66
import { Context } from "../context/context"
7-
import { EVM_STATE_UPDATE_SLEEP_MS } from "../config/constants"
87
import { logger } from "../logger"
98
import type { EntityManager } from "@mikro-orm/knex"
109
import type { FAssetType } from "../shared"
@@ -24,7 +23,7 @@ export class EvmStateWatchdog {
2423
} catch (e: any) {
2524
logger.error(`error in top-level Flare watchdog: ${e}`)
2625
}
27-
await sleep(this.context.config.json?.watchdog.cycleSleepMs ?? EVM_STATE_UPDATE_SLEEP_MS)
26+
await sleep(this.context.config.stateFetchCycleSleepMs)
2827
}
2928
}
3029

packages/fasset-indexer-core/src/run/run-indexer.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { IndexerRunner } from "../indexer/runner"
77
import { EventIndexerParallelBackPopulation } from "../indexer/reindexing/indexer-parallel-back-population"
88
import { EventIndexerParallelRacePopulation } from "../indexer/reindexing/indexer-parallel-race-population"
99
import { migrateCollateralPoolEvents } from "../scripts/migrate-collateral-pool-events"
10-
import { EVENT_NAMES, EVM_LOG_FETCH_SLEEP_MS } from "../config/constants"
1110
import { logger } from "../logger"
1211

1312

@@ -17,7 +16,7 @@ async function runIndexer() {
1716

1817
// define indexer by configured type
1918
let indexer: EventIndexer | EventIndexerParallelBackPopulation | EventIndexerParallelRacePopulation
20-
const allEvents = config.json?.indexEvents ?? EVENT_NAMES
19+
const allEvents = config.indexEvents
2120
if (config.reindexing == null) {
2221
indexer = new EventIndexer(context, allEvents)
2322
} else if (config.reindexing.type == 'back') {
@@ -44,10 +43,9 @@ async function runIndexer() {
4443

4544
logger.info(`starting ${context.chain} event indexer...`)
4645
const runner = new IndexerRunner(indexer, 'native')
47-
const sleepms = config.json?.events.logFetchCycleSleepMs ?? EVM_LOG_FETCH_SLEEP_MS
4846
await Promise.all([
4947
migrateCollateralPoolEvents(context.orm.em.fork()),
50-
runner.run(sleepms, undefined, config.reindexing != null)
48+
runner.run(config.evmLogFetchCycleSleepMs, undefined, config.reindexing != null)
5149
])
5250
}
5351

0 commit comments

Comments
 (0)