Skip to content

Commit

Permalink
feat: track supported remotes for oApps
Browse files Browse the repository at this point in the history
  • Loading branch information
sdlyy committed Mar 19, 2024
1 parent e6364e0 commit 8e76799
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 4 deletions.
66 changes: 66 additions & 0 deletions packages/backend/src/peripherals/database/OAppRemoteRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Logger } from '@l2beat/backend-tools'
import { ChainId } from '@lz/libs'
import type { OAppRemoteRow } from 'knex/types/tables'

import { BaseRepository, CheckConvention } from './shared/BaseRepository'
import { Database } from './shared/Database'

export interface OAppRemoteRecord {
oAppId: number
targetChainId: ChainId
}

export class OAppRemoteRepository extends BaseRepository {
constructor(database: Database, logger: Logger) {
super(database, logger)
this.autoWrap<CheckConvention<OAppRemoteRepository>>(this)
}

public async addMany(records: OAppRemoteRecord[]): Promise<number> {
const rows = records.map(toRow)
const knex = await this.knex()

await knex('oapp_remote')
.insert(rows)
.onConflict(['oapp_id', 'target_chain_id'])
.merge()

return rows.length
}

public async findAll(): Promise<OAppRemoteRecord[]> {
const knex = await this.knex()

const rows = await knex('oapp_remote').select('*')

return rows.map(toRecord)
}
public async findByOAppIds(oAppIds: number[]): Promise<OAppRemoteRecord[]> {
const knex = await this.knex()

const rows = await knex('oapp_remote')
.select('*')
.whereIn('oapp_id', oAppIds)

return rows.map(toRecord)
}

async deleteAll(): Promise<number> {
const knex = await this.knex()
return knex('oapp_remote').delete()
}
}

function toRow(record: OAppRemoteRecord): OAppRemoteRow {
return {
oapp_id: record.oAppId,
target_chain_id: Number(record.targetChainId),
}
}

function toRecord(row: OAppRemoteRow): OAppRemoteRecord {
return {
oAppId: row.oapp_id,
targetChainId: ChainId(row.target_chain_id),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
====== IMPORTANT NOTICE ======
DO NOT EDIT OR RENAME THIS FILE
This is a migration file. Once created the file should not be renamed or edited,
because migrations are only run once on the production server.
If you find that something was incorrectly set up in the `up` function you
should create a new migration file that fixes the issue.
*/

import { Knex } from 'knex'

export async function up(knex: Knex): Promise<void> {
await knex.schema.createTable('oapp_remote', (table) => {
table.integer('oapp_id').notNullable()
table.integer('target_chain_id').notNullable()
table.unique(['oapp_id', 'target_chain_id'])
})
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.dropTable('oapp_remote')
}
6 changes: 6 additions & 0 deletions packages/backend/src/peripherals/database/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ declare module 'knex/types/tables' {
configuration: string
}

interface OAppRemoteRow {
oapp_id: number
target_chain_id: number
}

interface Tables {
block_numbers: BlockNumberRow
indexer_states: IndexerStateRow
Expand All @@ -94,5 +99,6 @@ declare module 'knex/types/tables' {
oapp: OAppRow
oapp_configuration: OAppConfigurationRow
oapp_default_configuration: OAppDefaultConfigurationRow
oapp_remote: OAppRemoteRow
}
}
31 changes: 30 additions & 1 deletion packages/backend/src/tracking/TrackingModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ import { ApplicationModule } from '../modules/ApplicationModule'
import { CurrentDiscoveryRepository } from '../peripherals/database/CurrentDiscoveryRepository'
import { OAppConfigurationRepository } from '../peripherals/database/OAppConfigurationRepository'
import { OAppDefaultConfigurationRepository } from '../peripherals/database/OAppDefaultConfigurationRepository'
import { OAppRemoteRepository } from '../peripherals/database/OAppRemoteRepository'
import { OAppRepository } from '../peripherals/database/OAppRepository'
import { Database } from '../peripherals/database/shared/Database'
import { ProtocolVersion } from './domain/const'
import { ClockIndexer } from './domain/indexers/ClockIndexer'
import { DefaultConfigurationIndexer } from './domain/indexers/DefaultConfigurationIndexer'
import { OAppConfigurationIndexer } from './domain/indexers/OAppConfigurationIndexer'
import { OAppListIndexer } from './domain/indexers/OAppListIndexer'
import { OAppRemoteIndexer } from './domain/indexers/OAppRemotesIndexer'
import { DiscoveryDefaultConfigurationsProvider } from './domain/providers/DefaultConfigurationsProvider'
import { BlockchainOAppConfigurationProvider } from './domain/providers/OAppConfigurationProvider'
import { BlockchainOAppRemotesProvider } from './domain/providers/OAppRemotesProvider'
import { HttpOAppListProvider } from './domain/providers/OAppsListProvider'
import { TrackingController } from './http/TrackingController'
import { createTrackingRouter } from './http/TrackingRouter'
Expand All @@ -45,6 +48,7 @@ interface SubmoduleDependencies {
oApp: OAppRepository
oAppConfiguration: OAppConfigurationRepository
oAppDefaultConfiguration: OAppDefaultConfigurationRepository
oAppRemote: OAppRemoteRepository
}
}

Expand Down Expand Up @@ -75,6 +79,11 @@ function createTrackingModule(dependencies: Dependencies): ApplicationModule {
dependencies.logger,
)

const oAppRemoteRepo = new OAppRemoteRepository(
dependencies.database,
dependencies.logger,
)

const controller = new TrackingController(
oAppRepo,
oAppConfigurationRepo,
Expand All @@ -100,6 +109,7 @@ function createTrackingModule(dependencies: Dependencies): ApplicationModule {
oApp: oAppRepo,
oAppConfiguration: oAppConfigurationRepo,
oAppDefaultConfiguration: oAppDefaultConfigurationRepo,
oAppRemote: oAppRemoteRepo,
},
},
chainName,
Expand Down Expand Up @@ -155,6 +165,13 @@ function createTrackingSubmodule(
logger,
)

const oAppRemotesProvider = new BlockchainOAppRemotesProvider(
provider,
multicall,
chainId,
logger,
)

const clockIndexer = new ClockIndexer(logger, config.tickIntervalMs, chainId)
const oAppListIndexer = new OAppListIndexer(
logger,
Expand All @@ -164,13 +181,23 @@ function createTrackingSubmodule(
[clockIndexer],
)

const remotesIndexer = new OAppRemoteIndexer(
logger,
chainId,
repositories.oApp,
repositories.oAppRemote,
oAppRemotesProvider,
[oAppListIndexer],
)

const oAppConfigurationIndexer = new OAppConfigurationIndexer(
logger,
chainId,
oAppConfigProvider,
repositories.oApp,
repositories.oAppRemote,
repositories.oAppConfiguration,
[oAppListIndexer],
[remotesIndexer],
)

const defaultConfigurationIndexer = new DefaultConfigurationIndexer(
Expand All @@ -189,6 +216,8 @@ function createTrackingSubmodule(
await oAppListIndexer.start()
await oAppConfigurationIndexer.start()
await defaultConfigurationIndexer.start()

await remotesIndexer.start()
statusLogger.info('Tracking submodule started')
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
OAppConfigurationRecord,
OAppConfigurationRepository,
} from '../../../peripherals/database/OAppConfigurationRepository'
import { OAppRemoteRepository } from '../../../peripherals/database/OAppRemoteRepository'
import { OAppRepository } from '../../../peripherals/database/OAppRepository'
import { OAppConfigurations } from '../configuration'
import { OAppConfigurationProvider } from '../providers/OAppConfigurationProvider'
Expand All @@ -17,6 +18,7 @@ export class OAppConfigurationIndexer extends ChildIndexer {
private readonly chainId: ChainId,
private readonly oAppConfigProvider: OAppConfigurationProvider,
private readonly oAppRepo: OAppRepository,
private readonly oAppRemoteRepo: OAppRemoteRepository,
private readonly oAppConfigurationRepo: OAppConfigurationRepository,
parents: Indexer[],
) {
Expand All @@ -25,17 +27,24 @@ export class OAppConfigurationIndexer extends ChildIndexer {

protected override async update(_from: number, to: number): Promise<number> {
const oApps = await this.oAppRepo.getBySourceChain(this.chainId)
const oAppsRemotes = await this.oAppRemoteRepo.findAll()

const configurationRecords = await Promise.all(
oApps.map(async (oApp) => {
const supportedChains = oAppsRemotes
.filter((remote) => remote.oAppId === oApp.id)
.map((remote) => remote.targetChainId)

const oAppConfigs = await this.oAppConfigProvider.getConfiguration(
oApp.address,
supportedChains,
)

return configToRecord(oAppConfigs, oApp.id)
}),
)

await this.oAppConfigurationRepo.deleteAll()
await this.oAppConfigurationRepo.addMany(configurationRecords.flat())

return to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export class OAppListIndexer extends ChildIndexer {
amount: oApps.length,
})

await this.oAppRepo.deleteAll()

await this.oAppRepo.addMany(
oApps.map((oApp) => ({
...oApp,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Logger } from '@l2beat/backend-tools'
import { ChildIndexer, Indexer } from '@l2beat/uif'
import { ChainId } from '@lz/libs'

import {
OAppRemoteRecord,
OAppRemoteRepository,
} from '../../../peripherals/database/OAppRemoteRepository'
import { OAppRepository } from '../../../peripherals/database/OAppRepository'
import { OAppRemotesProvider } from '../providers/OAppRemotesProvider'

export class OAppRemoteIndexer extends ChildIndexer {
protected height = 0
constructor(
logger: Logger,
private readonly chainId: ChainId,
private readonly oAppRepo: OAppRepository,
private readonly oAppRemotesRepo: OAppRemoteRepository,
private readonly oAppRemoteProvider: OAppRemotesProvider,
parents: Indexer[],
) {
super(logger.tag(ChainId.getName(chainId)), parents)
}

protected override async update(_from: number, to: number): Promise<number> {
const oApps = await this.oAppRepo.getBySourceChain(this.chainId)

const records: OAppRemoteRecord[][] = await Promise.all(
oApps.map(async (oApp) => {
const supportedRemoteChains =
await this.oAppRemoteProvider.getSupportedRemotes(oApp.address)

return supportedRemoteChains.map((chainId) => ({
oAppId: oApp.id,
targetChainId: chainId,
}))
}),
)

await this.oAppRemotesRepo.deleteAll()
await this.oAppRemotesRepo.addMany(records.flat())

return to
}

public override getSafeHeight(): Promise<number> {
return Promise.resolve(this.height)
}

protected override setSafeHeight(height: number): Promise<void> {
this.height = height
return Promise.resolve()
}

protected override invalidate(targetHeight: number): Promise<number> {
return Promise.resolve(targetHeight)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export { BlockchainOAppConfigurationProvider }
export type { OAppConfigurationProvider }

interface OAppConfigurationProvider {
getConfiguration(address: EthereumAddress): Promise<OAppConfigurations>
getConfiguration(
address: EthereumAddress,
supportedChains: ChainId[],
): Promise<OAppConfigurations>
}

const iface = new utils.Interface([
Expand All @@ -35,11 +38,10 @@ class BlockchainOAppConfigurationProvider implements OAppConfigurationProvider {
}
public async getConfiguration(
address: EthereumAddress,
supportedChains: ChainId[],
): Promise<OAppConfigurations> {
const blockNumber = await this.provider.getBlockNumber()

const supportedChains = ChainId.getAll()

const supportedEids = supportedChains.flatMap(
(chainId) => EndpointID.encodeV1(chainId) ?? [],
)
Expand Down
Loading

0 comments on commit 8e76799

Please sign in to comment.