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

Add OApp remotes resolution #113

Merged
merged 4 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Logger } from '@l2beat/backend-tools'
import { ChainId } from '@lz/libs'
import { expect } from 'earl'

import { setupDatabaseTestSuite } from '../../test/database'
import { OAppRemoteRecord, OAppRemoteRepository } from './OAppRemoteRepository'

describe(OAppRemoteRepository.name, () => {
const { database } = setupDatabaseTestSuite()
const repository = new OAppRemoteRepository(database, Logger.SILENT)

before(async () => await repository.deleteAll())
afterEach(async () => await repository.deleteAll())

describe(OAppRemoteRepository.prototype.addMany.name, () => {
it('merges rows on insert', async () => {
const record1 = mockRecord({ oAppId: 1, targetChainId: ChainId.ETHEREUM })
const record2 = mockRecord({ oAppId: 2, targetChainId: ChainId.OPTIMISM })

await repository.addMany([record1, record2])

const recordsBeforeMerge = await repository.findAll()

await repository.addMany([record1, record2])

const recordsAfterMerge = await repository.findAll()

expect(recordsBeforeMerge.length).toEqual(2)
expect(recordsAfterMerge.length).toEqual(2)
})
})
})

function mockRecord(overrides?: Partial<OAppRemoteRecord>): OAppRemoteRecord {
return {
oAppId: 1,
targetChainId: ChainId.ETHEREUM,
...overrides,
}
}
57 changes: 57 additions & 0 deletions packages/backend/src/peripherals/database/OAppRemoteRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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)
}

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
}
}
41 changes: 40 additions & 1 deletion packages/backend/src/tracking/TrackingModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@ 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 { OFTInterfaceResolver } from './domain/providers/interface-resolvers/OFTInterfaceResolver'
import { StargateInterfaceResolver } from './domain/providers/interface-resolvers/StargateResolver'
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 +50,7 @@ interface SubmoduleDependencies {
oApp: OAppRepository
oAppConfiguration: OAppConfigurationRepository
oAppDefaultConfiguration: OAppDefaultConfigurationRepository
oAppRemote: OAppRemoteRepository
}
}

Expand Down Expand Up @@ -75,6 +81,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 +111,7 @@ function createTrackingModule(dependencies: Dependencies): ApplicationModule {
oApp: oAppRepo,
oAppConfiguration: oAppConfigurationRepo,
oAppDefaultConfiguration: oAppDefaultConfigurationRepo,
oAppRemote: oAppRemoteRepo,
},
},
chainName,
Expand Down Expand Up @@ -134,6 +146,9 @@ function createTrackingSubmodule(

const httpClient = new HttpClient()

const OFTResolver = new OFTInterfaceResolver(multicall)
const stargateResolver = new StargateInterfaceResolver(multicall)

const oAppListProvider = new HttpOAppListProvider(
logger,
httpClient,
Expand All @@ -155,6 +170,18 @@ function createTrackingSubmodule(
logger,
)

const supportedChains = ChainId.getAll()
const resolvers = [OFTResolver, stargateResolver]

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

const clockIndexer = new ClockIndexer(logger, config.tickIntervalMs, chainId)
const oAppListIndexer = new OAppListIndexer(
logger,
Expand All @@ -164,13 +191,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 +226,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
@@ -1,5 +1,5 @@
import { Logger } from '@l2beat/backend-tools'
import { ChildIndexer, Indexer } from '@l2beat/uif'
import { Indexer } from '@l2beat/uif'
import { ChainId } from '@lz/libs'

import {
Expand All @@ -9,9 +9,9 @@ import {
import { OAppConfigurations } from '../configuration'
import { ProtocolVersion } from '../const'
import { DefaultConfigurationsProvider } from '../providers/DefaultConfigurationsProvider'
import { InMemoryIndexer } from './InMemoryIndexer'

export class DefaultConfigurationIndexer extends ChildIndexer {
protected height = 0
export class DefaultConfigurationIndexer extends InMemoryIndexer {
constructor(
logger: Logger,
private readonly chainId: ChainId,
Expand Down Expand Up @@ -40,19 +40,6 @@ export class DefaultConfigurationIndexer extends ChildIndexer {

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)
}
}

function configToRecords(
Expand Down
19 changes: 19 additions & 0 deletions packages/backend/src/tracking/domain/indexers/InMemoryIndexer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ChildIndexer } from '@l2beat/uif'

export { InMemoryIndexer }

abstract class InMemoryIndexer extends ChildIndexer {
protected height = 0
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
@@ -1,58 +1,56 @@
import { Logger } from '@l2beat/backend-tools'
import { ChildIndexer, Indexer } from '@l2beat/uif'
import { Indexer } from '@l2beat/uif'
import { ChainId } from '@lz/libs'

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'
import { InMemoryIndexer } from './InMemoryIndexer'

export class OAppConfigurationIndexer extends ChildIndexer {
protected height = 0
export class OAppConfigurationIndexer extends InMemoryIndexer {
constructor(
logger: Logger,
private readonly chainId: ChainId,
private readonly oAppConfigProvider: OAppConfigurationProvider,
private readonly oAppRepo: OAppRepository,
private readonly oAppRemoteRepo: OAppRemoteRepository,
private readonly oAppConfigurationRepo: OAppConfigurationRepository,
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 [oApps, oAppsRemotes] = await Promise.all([
this.oAppRepo.getBySourceChain(this.chainId),
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
}

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)
}
}

function configToRecord(
Expand Down
Loading
Loading