Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modularize discovery #17

Merged
merged 9 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions packages/backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
LOCAL_DB_URL=
TEST_DB_URL=

ETHEREUM_DISCOVERY_ENABLED=true
ETHEREUM_RPC_URL=
ETHERSCAN_API_KEY=

ARBITRUM_DISCOVERY_ENABLED=true
ARBITRUM_RPC_URL=
ARBISCAN_API_KEY=



OPTIMISM_DISCOVERY_ENABLED=true
OPTIMISM_RPC_URL=
OPTIMISTIC_ETHERSCAN_API_KEY=

#-----OPTIONAL-----

ETHEREUM_CLOCK_INTERVAL_MS=
CLOCK_TICK_INTERVAL_MS=

ETHEREUM_START_BLOCK=
ETHERSCAN_MIN_TIMESTAMP=

ARBITRUM_CLOCK_INTERVAL_MS=
ARBITRUM_START_BLOCK=
ARBISCAN_MIN_TIMESTAMP=
ARBISCAN_MIN_TIMESTAMP=

OPTIMISM_START_BLOCK=
OPTIMISTIC_ETHERSCAN_MIN_TIMESTAMP=
6 changes: 2 additions & 4 deletions packages/backend/src/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { Logger } from '@l2beat/backend-tools'
import { ApiServer } from './api/ApiServer'
import { Config } from './config'
import { ApplicationModule } from './modules/ApplicationModule'
import { createArbitrumDiscoveryModule } from './modules/ArbitrumDiscoveryModule'
import { createEthereumDiscoveryModule } from './modules/EthereumDiscoveryModule'
import { createDiscoveryModule } from './modules/DiscoveryModule'
import { createHealthModule } from './modules/HealthModule'
import { Database } from './peripherals/database/shared/Database'

Expand All @@ -21,8 +20,7 @@ export class Application {

const modules: (ApplicationModule | undefined)[] = [
createHealthModule(config),
createEthereumDiscoveryModule(database, logger, config),
createArbitrumDiscoveryModule(database, logger, config),
createDiscoveryModule({ database, logger, config }),
]

const apiServer = new ApiServer(
Expand Down
11 changes: 8 additions & 3 deletions packages/backend/src/config/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ export interface Config {
Partial<LoggerOptions>
readonly health: HealthConfig
readonly discovery: {
readonly ethereum: EthereumLikeDiscoveryConfig
readonly arbitrum: EthereumLikeDiscoveryConfig
readonly clock: {
readonly tickIntervalMs: number
}
readonly modules: {
readonly ethereum: false | EthereumLikeDiscoveryConfig
readonly arbitrum: false | EthereumLikeDiscoveryConfig
readonly optimism: false | EthereumLikeDiscoveryConfig
}
}
}

Expand All @@ -34,7 +40,6 @@ export interface HealthConfig {

export interface EthereumLikeDiscoveryConfig {
startBlock: number
clockIntervalMs: number
rpcUrl: string
blockExplorerApiUrl: string
blockExplorerApiKey: string
Expand Down
65 changes: 43 additions & 22 deletions packages/backend/src/config/config.local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { UnixTime } from '@l2beat/discovery/dist/utils/UnixTime'
import { Config } from './Config'
import { arbitrumDiscoveryConfig } from './discovery/arbitrum'
import { ethereumDiscoveryConfig } from './discovery/ethereum'
import { optimismDiscoveryConfig } from './discovery/optimism'
import { getGitCommitSha } from './getGitCommitSha'

export function getLocalConfig(env: Env): Config {
Expand All @@ -27,29 +28,49 @@ export function getLocalConfig(env: Env): Config {
commitSha: getGitCommitSha(),
},
discovery: {
ethereum: {
// This is an arbitrary number so we do not start too far in the past.
startBlock: env.integer('ETHEREUM_START_BLOCK', 18127698),
clockIntervalMs: env.integer('ETHEREUM_CLOCK_INTERVAL_MS', 10 * 1000),
rpcUrl: env.string('ETHEREUM_RPC_URL'),
blockExplorerApiUrl: 'https://api.etherscan.io/api',
blockExplorerApiKey: env.string('ETHERSCAN_API_KEY'),
blockExplorerMinTimestamp: new UnixTime(
env.integer('ETHERSCAN_MIN_TIMESTAMP', 0),
),
discovery: ethereumDiscoveryConfig,
clock: {
tickIntervalMs: env.integer('CLOCK_TICK_INTERVAL_MS', 10 * 1000),
},
arbitrum: {
startBlock: env.integer('ARBITRUM_START_BLOCK', 133212747),
clockIntervalMs: env.integer('ARBITRUM_CLOCK_INTERVAL_MS', 10 * 1000),
rpcUrl: env.string('ARBITRUM_RPC_URL'),
blockExplorerApiUrl: 'https://api.arbiscan.io/api',
blockExplorerApiKey: env.string('ARBISCAN_API_KEY'),
// ~ Timestamp of block number 0 on Arbitrum
blockExplorerMinTimestamp: UnixTime.fromDate(
new Date('2021-05-28T22:15:00Z'),
),
discovery: arbitrumDiscoveryConfig,
modules: {
ethereum: env.boolean('ETHEREUM_DISCOVERY_ENABLED', false) && {
// This is an arbitrary number so we do not start too far in the past.
startBlock: env.integer('ETHEREUM_START_BLOCK', 18127698),
rpcUrl: env.string('ETHEREUM_RPC_URL'),
blockExplorerApiUrl: 'https://api.etherscan.io/api',
blockExplorerApiKey: env.string('ETHERSCAN_API_KEY'),
blockExplorerMinTimestamp: new UnixTime(
env.integer('ETHERSCAN_MIN_TIMESTAMP', 0),
),
discovery: ethereumDiscoveryConfig,
},
arbitrum: env.boolean('ARBITRUM_DISCOVERY_ENABLED', false) && {
startBlock: env.integer('ARBITRUM_START_BLOCK', 133212747),
rpcUrl: env.string('ARBITRUM_RPC_URL'),
blockExplorerApiUrl: 'https://api.arbiscan.io/api',
blockExplorerApiKey: env.string('ARBISCAN_API_KEY'),
blockExplorerMinTimestamp: new UnixTime(
env.integer(
'ARBISCAN_MIN_TIMESTAMP',
// ~ Timestamp of block number 0 on Arbitrum
new Date('2021-05-28T22:15:00Z').getTime() / 1000,
),
),
discovery: arbitrumDiscoveryConfig,
},
optimism: env.boolean('OPTIMISM_DISCOVERY_ENABLED', false) && {
startBlock: env.integer('OPTIMISM_START_BLOCK', 109983712),
rpcUrl: env.string('OPTIMISM_RPC_URL'),
blockExplorerApiUrl: 'https://api-optimistic.etherscan.io/api',
blockExplorerApiKey: env.string('OPTIMISTIC_ETHERSCAN_API_KEY'),
blockExplorerMinTimestamp: new UnixTime(
env.integer(
'OPTIMISTIC_ETHERSCAN_MIN_TIMESTAMP',
// ~ Timestamp of block number 0 on Optimism
new Date('2021-01-14T15:52:00Z').getTime() / 1000,
),
),
discovery: optimismDiscoveryConfig,
},
},
},
}
Expand Down
66 changes: 42 additions & 24 deletions packages/backend/src/config/config.production.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { UnixTime } from '@l2beat/discovery/dist/utils/UnixTime'
import { Config } from './Config'
import { arbitrumDiscoveryConfig } from './discovery/arbitrum'
import { ethereumDiscoveryConfig } from './discovery/ethereum'
import { optimismDiscoveryConfig } from './discovery/optimism'
import { getGitCommitSha } from './getGitCommitSha'

export function getProductionConfig(env: Env): Config {
Expand All @@ -30,32 +31,49 @@ export function getProductionConfig(env: Env): Config {
commitSha: getGitCommitSha(),
},
discovery: {
ethereum: {
// This is an arbitrary number so we do not start too far in the past.
startBlock: env.integer('ETHEREUM_START_BLOCK', 18127698),
clockIntervalMs: env.integer('ETHEREUM_CLOCK_INTERVAL_MS', 10 * 1000),
rpcUrl: env.string('ETHEREUM_RPC_URL'),
blockExplorerApiUrl: 'https://api.etherscan.io/api',
blockExplorerApiKey: env.string('ETHERSCAN_API_KEY'),
blockExplorerMinTimestamp: new UnixTime(
env.integer('ETHERSCAN_MIN_TIMESTAMP', 0),
),
discovery: ethereumDiscoveryConfig,
clock: {
tickIntervalMs: env.integer('CLOCK_TICK_INTERVAL_MS', 10 * 1000),
},
arbitrum: {
startBlock: env.integer('ARBITRUM_START_BLOCK', 134410848),
clockIntervalMs: env.integer('ARBITRUM_CLOCK_INTERVAL_MS', 10 * 1000),
rpcUrl: env.string('ARBITRUM_RPC_URL'),
blockExplorerApiUrl: 'https://api.arbiscan.io/api',
blockExplorerApiKey: env.string('ARBISCAN_API_KEY'),
// ~ Timestamp of block number 0 on Arbitrum
blockExplorerMinTimestamp: new UnixTime(
env.integer(
'ARBISCAN_MIN_TIMESTAMP',
new Date('2021-05-28T22:15:00Z').getTime() / 1000,
modules: {
ethereum: env.boolean('ETHEREUM_DISCOVERY_ENABLED', false) && {
// This is an arbitrary number so we do not start too far in the past.
startBlock: env.integer('ETHEREUM_START_BLOCK', 18127698),
rpcUrl: env.string('ETHEREUM_RPC_URL'),
blockExplorerApiUrl: 'https://api.etherscan.io/api',
blockExplorerApiKey: env.string('ETHERSCAN_API_KEY'),
blockExplorerMinTimestamp: new UnixTime(
env.integer('ETHERSCAN_MIN_TIMESTAMP', 0),
),
),
discovery: arbitrumDiscoveryConfig,
discovery: ethereumDiscoveryConfig,
},
arbitrum: env.boolean('ARBITRUM_DISCOVERY_ENABLED', false) && {
startBlock: env.integer('ARBITRUM_START_BLOCK', 133212747),
rpcUrl: env.string('ARBITRUM_RPC_URL'),
blockExplorerApiUrl: 'https://api.arbiscan.io/api',
blockExplorerApiKey: env.string('ARBISCAN_API_KEY'),
blockExplorerMinTimestamp: new UnixTime(
env.integer(
'ARBISCAN_MIN_TIMESTAMP',
// ~ Timestamp of block number 0 on Arbitrum
new Date('2021-05-28T22:15:00Z').getTime() / 1000,
),
),
discovery: arbitrumDiscoveryConfig,
},
optimism: env.boolean('OPTIMISM_DISCOVERY_ENABLED', false) && {
startBlock: env.integer('OPTIMISM_START_BLOCK', 109983712),
rpcUrl: env.string('OPTIMISM_RPC_URL'),
blockExplorerApiUrl: 'https://api-optimistic.etherscan.io/api',
blockExplorerApiKey: env.string('OPTIMISTIC_ETHERSCAN_API_KEY'),
blockExplorerMinTimestamp: new UnixTime(
env.integer(
'OPTIMISTIC_ETHERSCAN_MIN_TIMESTAMP',
// ~ Timestamp of block number 0 on Optimism
new Date('2021-01-14T15:52:00Z').getTime() / 1000,
),
),
discovery: optimismDiscoveryConfig,
},
},
},
}
Expand Down
20 changes: 20 additions & 0 deletions packages/backend/src/config/discovery/optimism.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { DiscoveryConfig } from '@l2beat/discovery'
import { ChainId } from '@lz/libs'

import { createConfigFromTemplate } from '../discoveryConfig'

export { optimismDiscoveryConfig }

const optimismRawConfig = createConfigFromTemplate({
chain: ChainId.OPTIMISM,
initialAddresses: [
'0x4D73AdB72bC3DD368966edD0f0b2148401A178E2', // UltraLightNodeV2
],
addresses: {
ultraLightNodeV2: '0x4D73AdB72bC3DD368966edD0f0b2148401A178E2',
endpoint: '0x3c2269811836af69497E5F486A85D7316753cf62',
layerZeroMultisig: '0x2458BAAbfb21aE1da11D9dD6AD4E48aB2fBF9959',
},
})

const optimismDiscoveryConfig = new DiscoveryConfig(optimismRawConfig)
7 changes: 1 addition & 6 deletions packages/backend/src/indexers/ClockIndexer.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Logger } from '@l2beat/backend-tools'
import { ChildIndexer } from '@l2beat/uif'
import { ChainId } from '@lz/libs'
import { install, InstalledClock } from '@sinonjs/fake-timers'
import { expect } from 'earl'
import { spy } from 'sinon'
Expand All @@ -22,11 +21,7 @@ describe(ClockIndexer.name, () => {
const tickInterval = 10 * 1000 // 10s
const expectedUpdateCount = 5

const clockIndexer = new ClockIndexer(
Logger.SILENT,
ChainId.ETHEREUM,
tickInterval,
)
const clockIndexer = new ClockIndexer(Logger.SILENT, tickInterval)

const testChildIndexer = new TestChildIndexer(Logger.SILENT, [clockIndexer])

Expand Down
6 changes: 2 additions & 4 deletions packages/backend/src/indexers/ClockIndexer.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { Logger } from '@l2beat/backend-tools'
import { RootIndexer } from '@l2beat/uif'
import { ChainId } from '@lz/libs'

export class ClockIndexer extends RootIndexer {
constructor(
logger: Logger,
// Just for pretty logging purposes
chainId: ChainId,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

private readonly tickInterval: number,
) {
super(logger.tag(ChainId.getName(chainId)))
super(logger)
}
override async start(): Promise<void> {
await super.start()
Expand Down
66 changes: 0 additions & 66 deletions packages/backend/src/modules/ArbitrumDiscoveryModule.ts

This file was deleted.

Loading