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

Implement l2 transactions preprocessing #422

Merged
merged 31 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d421b8c
implement l2 transactions preprocessing
torztomasz Jul 11, 2023
d8c190e
fix linter errors
torztomasz Jul 11, 2023
3c0b582
use preprocess l2 statistics for displaying UI
torztomasz Jul 12, 2023
b362a7b
refactor mostRecent -> last
torztomasz Jul 12, 2023
e6cd4b5
indicate l2 transaction being processed better in tables
torztomasz Jul 12, 2023
19bc486
remove redundant db query
torztomasz Jul 12, 2023
51a2fe8
remove unused l2 transaction repo methods
torztomasz Jul 12, 2023
ccb6991
refactor findByTransactionId -> findAggregatedByTransactionId
torztomasz Jul 12, 2023
180767f
improve condition for showing pages for table with pagination
torztomasz Jul 12, 2023
a11cbb0
fix linter errors
torztomasz Jul 12, 2023
919ff3b
fix formatting
torztomasz Jul 12, 2023
2a71ebe
catch up l2 transaction statistics on start
torztomasz Jul 14, 2023
9483ece
launch api server after everything catches up and migrates
torztomasz Jul 14, 2023
4cf9fe9
fix wrongly displayed l2 transaction count for state update
torztomasz Jul 17, 2023
6ec372e
Merge remote-tracking branch 'origin/master' into add-l2-transactions…
torztomasz Jul 17, 2023
fee119e
fix build issues after merge
torztomasz Jul 17, 2023
7c5680d
improve l2 transactions preprocessing for state details
torztomasz Jul 19, 2023
5f5745f
use separate table for preprocessed l2 transaction statistics
torztomasz Jul 19, 2023
e7ccddd
rename preprocessed user l2 tx table
torztomasz Jul 19, 2023
234d1c0
Merge branch 'master' into add-l2-transactions-preprocessing
torztomasz Jul 19, 2023
7fcd38f
after merge fixes
torztomasz Jul 19, 2023
6298824
linter fix
torztomasz Jul 19, 2023
ce739ea
fix check in state details preprocessor
torztomasz Jul 19, 2023
cbdd293
refactor current to latest
torztomasz Jul 19, 2023
1659b4e
log UserL2TransactionsStatisticsPreprocessor differently
torztomasz Jul 19, 2023
c93a2f4
change index order
torztomasz Jul 19, 2023
301543c
do not use random numbers in tests
torztomasz Jul 19, 2023
bd7f1dc
wrap missing functions
torztomasz Jul 20, 2023
3b873f6
run linter fix
torztomasz Jul 20, 2023
4f30c9d
run formatter
torztomasz Jul 20, 2023
d7baae6
fix flaky test
torztomasz Jul 20, 2023
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
34 changes: 24 additions & 10 deletions packages/backend/src/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { PerpetualHistoryPreprocessor } from './core/preprocessing/PerpetualHist
import { Preprocessor } from './core/preprocessing/Preprocessor'
import { SpotHistoryPreprocessor } from './core/preprocessing/SpotHistoryPreprocessor'
import { StateDetailsPreprocessor } from './core/preprocessing/StateDetailsPreprocessor'
import { UserL2TransactionsStatisticsPreprocessor } from './core/preprocessing/UserL2TransactionsPreprocessor'
import { UserStatisticsPreprocessor } from './core/preprocessing/UserStatisticsPreprocessor'
import { SpotValidiumSyncService } from './core/SpotValidiumSyncService'
import { SpotValidiumUpdater } from './core/SpotValidiumUpdater'
Expand All @@ -71,6 +72,7 @@ import { PositionRepository } from './peripherals/database/PositionRepository'
import { PreprocessedAssetHistoryRepository } from './peripherals/database/PreprocessedAssetHistoryRepository'
import { PreprocessedStateDetailsRepository } from './peripherals/database/PreprocessedStateDetailsRepository'
import { PreprocessedStateUpdateRepository } from './peripherals/database/PreprocessedStateUpdateRepository'
import { PreprocessedUserL2TransactionsStatisticsRepository } from './peripherals/database/PreprocessedUserL2TransactionsStatisticsRepository'
import { PreprocessedUserStatisticsRepository } from './peripherals/database/PreprocessedUserStatisticsRepository'
import { Database } from './peripherals/database/shared/Database'
import { StateTransitionRepository } from './peripherals/database/StateTransitionRepository'
Expand All @@ -87,7 +89,6 @@ import { AvailabilityGatewayClient } from './peripherals/starkware/AvailabilityG
import { FeederGatewayClient } from './peripherals/starkware/FeederGatewayClient'
import { FetchClient } from './peripherals/starkware/FetchClient'
import { handleServerError, reportError } from './tools/ErrorReporter'
import { shouldShowL2Transactions } from './utils/shouldShowL2Transactions'

export class Application {
start: () => Promise<void>
Expand Down Expand Up @@ -414,6 +415,16 @@ export class Application {
const preprocessedUserStatisticsRepository =
new PreprocessedUserStatisticsRepository(database, logger)

const preprocessedUserL2TransactionsStatisticsRepository =
new PreprocessedUserL2TransactionsStatisticsRepository(database, logger)

const userL2TransactionsPreprocessor =
new UserL2TransactionsStatisticsPreprocessor(
preprocessedUserL2TransactionsStatisticsRepository,
l2TransactionRepository,
logger
)

let preprocessor: Preprocessor<AssetHash> | Preprocessor<AssetId>
const isPreprocessorEnabled = config.enablePreprocessing

Expand Down Expand Up @@ -443,13 +454,13 @@ export class Application {
preprocessedStateDetailsRepository,
preprocessedAssetHistoryRepository,
userTransactionRepository,
l2TransactionRepository,
logger
)

const userStatisticsPreprocessor = new UserStatisticsPreprocessor(
preprocessedUserStatisticsRepository,
preprocessedAssetHistoryRepository,
preprocessedStateUpdateRepository,
stateUpdateRepository,
kvStore,
logger
Expand All @@ -462,6 +473,8 @@ export class Application {
perpetualHistoryPreprocessor,
stateDetailsPreprocessor,
userStatisticsPreprocessor,
userL2TransactionsPreprocessor,
l2TransactionRepository,
logger,
isPreprocessorEnabled
)
Expand All @@ -479,13 +492,13 @@ export class Application {
preprocessedStateDetailsRepository,
preprocessedAssetHistoryRepository,
userTransactionRepository,
l2TransactionRepository,
logger
)

const userStatisticsPreprocessor = new UserStatisticsPreprocessor(
preprocessedUserStatisticsRepository,
preprocessedAssetHistoryRepository,
preprocessedStateUpdateRepository,
stateUpdateRepository,
kvStore,
logger
Expand All @@ -498,6 +511,8 @@ export class Application {
spotHistoryPreprocessor,
stateDetailsPreprocessor,
userStatisticsPreprocessor,
userL2TransactionsPreprocessor,
l2TransactionRepository,
logger,
isPreprocessorEnabled
)
Expand All @@ -517,16 +532,14 @@ export class Application {

// #endregion core
// #region api
const showL2Transactions = shouldShowL2Transactions(config)
const homeController = new HomeController(
pageContextService,
assetDetailsService,
forcedTradeOfferViewService,
userTransactionRepository,
forcedTradeOfferRepository,
l2TransactionRepository,
preprocessedStateDetailsRepository,
showL2Transactions
preprocessedStateDetailsRepository
)

const userController = new UserController(
Expand All @@ -541,8 +554,8 @@ export class Application {
forcedTradeOfferViewService,
withdrawableAssetRepository,
preprocessedUserStatisticsRepository,
config.starkex.contracts.perpetual,
showL2Transactions
preprocessedUserL2TransactionsStatisticsRepository,
config.starkex.contracts.perpetual
)
const stateUpdateController = new StateUpdateController(
pageContextService,
Expand All @@ -551,7 +564,7 @@ export class Application {
userTransactionRepository,
l2TransactionRepository,
preprocessedAssetHistoryRepository,
showL2Transactions
preprocessedStateDetailsRepository
)
const transactionController = new TransactionController(
pageContextService,
Expand Down Expand Up @@ -629,7 +642,6 @@ export class Application {
this.start = async () => {
logger.for(this).info('Starting')

await apiServer.listen()
if (config.freshStart) await database.rollbackAll()
await database.migrateToLatest()
await preprocessor.catchUp()
Expand All @@ -641,6 +653,8 @@ export class Application {
await stateUpdateWithBatchIdMigrator.migrate()
await stateUpdater.initTree()

await apiServer.listen()

if (config.enableSync) {
transactionStatusService.start()
await syncScheduler.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe(ForcedTradeOfferController.name, () => {
const pageContext: PageContext = {
user: undefined,
tradingMode: 'perpetual',
showL2Transactions: true,
chainId: 1,
instanceName: 'dYdX',
collateralAsset: fakeCollateralAsset,
Expand Down
31 changes: 16 additions & 15 deletions packages/backend/src/api/controllers/HomeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { PageContextService } from '../../core/PageContextService'
import { PaginationOptions } from '../../model/PaginationOptions'
import { ForcedTradeOfferRepository } from '../../peripherals/database/ForcedTradeOfferRepository'
import { L2TransactionRepository } from '../../peripherals/database/L2TransactionRepository'
import { sumUpTransactionCount } from '../../peripherals/database/PreprocessedL2TransactionsStatistics'
import { PreprocessedStateDetailsRepository } from '../../peripherals/database/PreprocessedStateDetailsRepository'
import { UserTransactionData } from '../../peripherals/database/transactions/UserTransaction'
import { UserTransactionRepository } from '../../peripherals/database/transactions/UserTransactionRepository'
Expand All @@ -33,8 +34,7 @@ export class HomeController {
private readonly userTransactionRepository: UserTransactionRepository,
private readonly forcedTradeOfferRepository: ForcedTradeOfferRepository,
private readonly l2TransactionRepository: L2TransactionRepository,
private readonly preprocessedStateDetailsRepository: PreprocessedStateDetailsRepository,
private readonly showL2Transactions: boolean
private readonly preprocessedStateDetailsRepository: PreprocessedStateDetailsRepository
) {}

async getHomePage(
Expand All @@ -44,7 +44,7 @@ export class HomeController {
const paginationOpts = { offset: 0, limit: 6 }
const [
l2Transactions,
l2TransactionsCount,
lastStateDetailsWithL2TransactionsStatistics,
stateUpdates,
stateUpdatesCount,
forcedUserTransactions,
Expand All @@ -53,7 +53,7 @@ export class HomeController {
availableOffersCount,
] = await Promise.all([
this.l2TransactionRepository.getPaginatedWithoutMulti(paginationOpts),
this.l2TransactionRepository.countAllDistinctTransactionIds(),
this.preprocessedStateDetailsRepository.findLastWithL2TransactionsStatistics(),
this.preprocessedStateDetailsRepository.getPaginated(paginationOpts),
this.preprocessedStateDetailsRepository.countAll(),
this.userTransactionRepository.getPaginated({
Expand Down Expand Up @@ -85,12 +85,10 @@ export class HomeController {
const content = renderHomePage({
context,
tutorials: [], // explicitly no tutorials
l2Transactions: this.showL2Transactions
? {
data: l2Transactions.map(l2TransactionToEntry),
total: l2TransactionsCount,
}
: undefined,
l2Transactions: l2Transactions.map(l2TransactionToEntry),
totalL2Transactions: sumUpTransactionCount(
lastStateDetailsWithL2TransactionsStatistics?.cumulativeL2TransactionsStatistics
),
stateUpdates: stateUpdateEntries,
totalStateUpdates: stateUpdatesCount,
forcedTransactions: forcedTransactionEntries,
Expand All @@ -111,16 +109,19 @@ export class HomeController {
): Promise<ControllerResult> {
const context = await this.pageContextService.getPageContext(givenUser)

const [total, l2Transactions] = await Promise.all([
this.l2TransactionRepository.countAllDistinctTransactionIds(),
this.l2TransactionRepository.getPaginatedWithoutMulti(pagination),
])
const [l2Transactions, lastStateDetailsWithL2TransactionsStatistics] =
await Promise.all([
this.l2TransactionRepository.getPaginatedWithoutMulti(pagination),
this.preprocessedStateDetailsRepository.findLastWithL2TransactionsStatistics(),
])

const content = renderHomeL2TransactionsPage({
context,
l2Transactions: l2Transactions.map(l2TransactionToEntry),
total: sumUpTransactionCount(
lastStateDetailsWithL2TransactionsStatistics?.cumulativeL2TransactionsStatistics
),
...pagination,
total,
})
return { type: 'success', content }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export class L2TransactionController {
return { type: 'not found' }
}
const aggregatedL2Transaction =
await this.l2TransactionRepository.findByTransactionId(transactionId)
await this.l2TransactionRepository.findAggregatedByTransactionId(
transactionId
)

if (!aggregatedL2Transaction) {
return {
Expand Down
30 changes: 18 additions & 12 deletions packages/backend/src/api/controllers/StateUpdateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
PreprocessedAssetHistoryRecord,
PreprocessedAssetHistoryRepository,
} from '../../peripherals/database/PreprocessedAssetHistoryRepository'
import { sumUpTransactionCount } from '../../peripherals/database/PreprocessedL2TransactionsStatistics'
import { PreprocessedStateDetailsRepository } from '../../peripherals/database/PreprocessedStateDetailsRepository'
import { StateUpdateRepository } from '../../peripherals/database/StateUpdateRepository'
import { UserTransactionData } from '../../peripherals/database/transactions/UserTransaction'
import { UserTransactionRepository } from '../../peripherals/database/transactions/UserTransactionRepository'
Expand All @@ -38,7 +40,7 @@ export class StateUpdateController {
private readonly userTransactionRepository: UserTransactionRepository,
private readonly l2TransactionRepository: L2TransactionRepository,
private readonly preprocessedAssetHistoryRepository: PreprocessedAssetHistoryRepository,
private readonly showL2Transactions: boolean
private readonly preprocessedStateDetailsRepository: PreprocessedStateDetailsRepository
) {}

async getStateUpdatePage(
Expand All @@ -56,7 +58,7 @@ export class StateUpdateController {
forcedUserTransactions,
totalForcedUserTransactions,
l2Transactions,
totalL2Transactions,
preprocessedStateDetails,
] = await Promise.all([
this.stateUpdateRepository.findById(stateUpdateId),
this.preprocessedAssetHistoryRepository.getByStateUpdateIdPaginated(
Expand All @@ -82,7 +84,7 @@ export class StateUpdateController {
limit: 6,
}
),
this.l2TransactionRepository.countAllDistinctTransactionIdsByStateUpdateId(
this.preprocessedStateDetailsRepository.findByStateUpdateId(
stateUpdateId
),
])
Expand Down Expand Up @@ -127,12 +129,12 @@ export class StateUpdateController {
balanceChanges: balanceChangeEntries,
totalBalanceChanges,
priceChanges: priceEntries,
l2Transactions: this.showL2Transactions
? {
data: l2Transactions.map(l2TransactionToEntry),
total: totalL2Transactions,
}
: undefined,
l2Transactions: l2Transactions.map(l2TransactionToEntry),
totalL2Transactions: preprocessedStateDetails?.l2TransactionsStatistics
? sumUpTransactionCount(
preprocessedStateDetails.l2TransactionsStatistics
)
: 'processing',
transactions,
totalTransactions: totalForcedUserTransactions,
})
Expand All @@ -147,12 +149,12 @@ export class StateUpdateController {
): Promise<ControllerResult> {
const context = await this.pageContextService.getPageContext(givenUser)

const [l2Transactions, total] = await Promise.all([
const [l2Transactions, preprocessedStateDetails] = await Promise.all([
this.l2TransactionRepository.getPaginatedWithoutMultiByStateUpdateId(
stateUpdateId,
pagination
),
this.l2TransactionRepository.countAllDistinctTransactionIdsByStateUpdateId(
this.preprocessedStateDetailsRepository.findByStateUpdateId(
stateUpdateId
),
])
Expand All @@ -162,7 +164,11 @@ export class StateUpdateController {
id: stateUpdateId.toString(),
l2Transactions: l2Transactions.map(l2TransactionToEntry),
...pagination,
total,
total: preprocessedStateDetails?.l2TransactionsStatistics
? sumUpTransactionCount(
preprocessedStateDetails.l2TransactionsStatistics
)
: 'processing',
})

return { type: 'success', content }
Expand Down
Loading
Loading