Skip to content

Commit

Permalink
improve LiveL2TransactionDownloader
Browse files Browse the repository at this point in the history
  • Loading branch information
torztomasz committed Jul 17, 2023
1 parent 0e16506 commit 3c0b66b
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 130 deletions.
26 changes: 26 additions & 0 deletions packages/backend/src/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ import { SpotValidiumSyncService } from './core/SpotValidiumSyncService'
import { SpotValidiumUpdater } from './core/SpotValidiumUpdater'
import { StatusService } from './core/StatusService'
import { BlockDownloader } from './core/sync/BlockDownloader'
import { Clock } from './core/sync/Clock'
import { LiveL2TransactionDownloader } from './core/sync/LiveL2TransactionDownloader'
import { SyncScheduler } from './core/sync/SyncScheduler'
import { TransactionStatusService } from './core/TransactionStatusService'
import { UserService } from './core/UserService'
Expand Down Expand Up @@ -85,6 +87,7 @@ import { TokenInspector } from './peripherals/ethereum/TokenInspector'
import { AvailabilityGatewayClient } from './peripherals/starkware/AvailabilityGatewayClient'
import { FeederGatewayClient } from './peripherals/starkware/FeederGatewayClient'
import { FetchClient } from './peripherals/starkware/FetchClient'
import { L2TransactionClient } from './peripherals/starkware/L2TransactionClient'
import { handleServerError, reportError } from './tools/ErrorReporter'
import { Logger } from './tools/Logger'
import { shouldShowL2Transactions } from './utils/shouldShowL2Transactions'
Expand All @@ -100,6 +103,8 @@ export class Application {
reportError,
})

const clock = new Clock()

// #endregion tools
// #region peripherals

Expand Down Expand Up @@ -219,6 +224,7 @@ export class Application {
let stateTransitionCollector: IStateTransitionCollector

let feederGatewayCollector: FeederGatewayCollector | undefined
let l2TransactionDownloader: LiveL2TransactionDownloader | undefined

if (config.starkex.dataAvailabilityMode === 'validium') {
const availabilityGatewayClient = new AvailabilityGatewayClient(
Expand All @@ -235,6 +241,24 @@ export class Application {
)
stateTransitionCollector = perpetualValidiumStateTransitionCollector

const l2TransactionClient = config.starkex.l2TransactionApi
? new L2TransactionClient(
config.starkex.l2TransactionApi,
fetchClient
)
: undefined

l2TransactionDownloader = l2TransactionClient
? new LiveL2TransactionDownloader(
l2TransactionClient,
l2TransactionRepository,
stateUpdateRepository,
kvStore,
clock,
logger
)
: undefined

const feederGatewayClient = config.starkex.feederGateway
? new FeederGatewayClient(
config.starkex.feederGateway,
Expand Down Expand Up @@ -278,6 +302,7 @@ export class Application {
perpetualValidiumUpdater,
withdrawalAllowedCollector,
feederGatewayCollector,
l2TransactionDownloader,
logger
)
} else {
Expand Down Expand Up @@ -644,6 +669,7 @@ export class Application {
if (config.enableSync) {
transactionStatusService.start()
await syncScheduler.start()
await l2TransactionDownloader?.start()
await blockDownloader.start()
}

Expand Down
5 changes: 4 additions & 1 deletion packages/backend/src/config/starkex/apex-mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ export function getApexMainnetConfig(): StarkexConfig {
auth: clientAuth,
},
l2TransactionApi: {
getUrl: (startId, expectCount) => {
getTransactionsUrl: (startId, expectCount) => {
return `${getEnv(
'APEX_TRANSACTION_API_URL'
)}?startApexId=${startId}&expectCount=${expectCount}`
},
getThirdPartyIdByTransactionIdUrl: (transactionId) => {
return `${getEnv('APEX_THIRD_PARTY_ID_API_URL')}?txId=${transactionId}`
},
auth: clientAuth,
},
collateralAsset: {
Expand Down
5 changes: 3 additions & 2 deletions packages/backend/src/core/IDataSyncService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { PerpetualRollupStateTransition } from './PerpetualRollupUpdater'
import { ValidiumStateTransition } from './PerpetualValidiumUpdater'

export interface IDataSyncService {
sync(blockRange: BlockRange): Promise<void>
sync(blockRange: BlockRange, isTip?: boolean): Promise<void>
// I made isTip optional but as soon as we will support other types like PerpetualRollup etc. we will need to make it required.
processStateTransitions(
stateTransitions:
| ValidiumStateTransition[]
| PerpetualRollupStateTransition[]
): Promise<void>
): Promise<number | void>
discardAfter(blockNumber: BlockNumber): Promise<void>
}
10 changes: 9 additions & 1 deletion packages/backend/src/core/PerpetualValidiumSyncService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
PerpetualValidiumUpdater,
ValidiumStateTransition,
} from './PerpetualValidiumUpdater'
import { LiveL2TransactionDownloader } from './sync/LiveL2TransactionDownloader'

export class PerpetualValidiumSyncService implements IDataSyncService {
constructor(
Expand All @@ -24,12 +25,15 @@ export class PerpetualValidiumSyncService implements IDataSyncService {
private readonly perpetualValidiumUpdater: PerpetualValidiumUpdater,
private readonly withdrawalAllowedCollector: WithdrawalAllowedCollector,
private readonly feederGatewayCollector: FeederGatewayCollector | undefined,
private readonly L2TransactionDownloader:
| LiveL2TransactionDownloader
| undefined,
private readonly logger: Logger
) {
this.logger = logger.for(this)
}

async sync(blockRange: BlockRange) {
async sync(blockRange: BlockRange, isTip: boolean) {
const userRegistrations = await this.userRegistrationCollector.collect(
blockRange
)
Expand All @@ -49,6 +53,10 @@ export class PerpetualValidiumSyncService implements IDataSyncService {

await this.processStateTransitions(stateTransitions)
await this.feederGatewayCollector?.collect()

if (isTip) {
await this.L2TransactionDownloader?.enableSync()
}
}

async processStateTransitions(stateTransitions: ValidiumStateTransition[]) {
Expand Down
104 changes: 0 additions & 104 deletions packages/backend/src/core/sync/L2TransactionDownloader.ts

This file was deleted.

Loading

0 comments on commit 3c0b66b

Please sign in to comment.