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 16 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 11 additions & 10 deletions packages/backend/src/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ import { FeederGatewayClient } from './peripherals/starkware/FeederGatewayClient
import { FetchClient } from './peripherals/starkware/FetchClient'
import { handleServerError, reportError } from './tools/ErrorReporter'
import { Logger } from './tools/Logger'
import { shouldShowL2Transactions } from './utils/shouldShowL2Transactions'

export class Application {
start: () => Promise<void>
Expand Down Expand Up @@ -443,14 +442,15 @@ export class Application {
preprocessedStateDetailsRepository,
preprocessedAssetHistoryRepository,
userTransactionRepository,
l2TransactionRepository,
logger
)

const userStatisticsPreprocessor = new UserStatisticsPreprocessor(
preprocessedUserStatisticsRepository,
preprocessedAssetHistoryRepository,
preprocessedStateUpdateRepository,
stateUpdateRepository,
l2TransactionRepository,
kvStore,
logger
)
Expand All @@ -462,6 +462,7 @@ export class Application {
perpetualHistoryPreprocessor,
stateDetailsPreprocessor,
userStatisticsPreprocessor,
l2TransactionRepository,
logger,
isPreprocessorEnabled
)
Expand All @@ -479,14 +480,15 @@ export class Application {
preprocessedStateDetailsRepository,
preprocessedAssetHistoryRepository,
userTransactionRepository,
l2TransactionRepository,
logger
)

const userStatisticsPreprocessor = new UserStatisticsPreprocessor(
preprocessedUserStatisticsRepository,
preprocessedAssetHistoryRepository,
preprocessedStateUpdateRepository,
stateUpdateRepository,
l2TransactionRepository,
kvStore,
logger
)
Expand All @@ -498,6 +500,7 @@ export class Application {
spotHistoryPreprocessor,
stateDetailsPreprocessor,
userStatisticsPreprocessor,
l2TransactionRepository,
logger,
isPreprocessorEnabled
)
Expand All @@ -517,16 +520,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 +542,7 @@ export class Application {
forcedTradeOfferViewService,
withdrawableAssetRepository,
preprocessedUserStatisticsRepository,
config.starkex.contracts.perpetual,
showL2Transactions
config.starkex.contracts.perpetual
)
const stateUpdateController = new StateUpdateController(
pageContextService,
Expand All @@ -551,7 +551,7 @@ export class Application {
userTransactionRepository,
l2TransactionRepository,
preprocessedAssetHistoryRepository,
showL2Transactions
preprocessedStateDetailsRepository
)
const transactionController = new TransactionController(
pageContextService,
Expand Down Expand Up @@ -629,7 +629,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 +640,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
45 changes: 27 additions & 18 deletions packages/backend/src/api/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
PreprocessedAssetHistoryRecord,
PreprocessedAssetHistoryRepository,
} from '../../peripherals/database/PreprocessedAssetHistoryRepository'
import { sumUpTransactionCount } from '../../peripherals/database/PreprocessedL2TransactionsStatistics'
import { PreprocessedUserStatisticsRepository } from '../../peripherals/database/PreprocessedUserStatisticsRepository'
import {
SentTransactionRecord,
Expand Down Expand Up @@ -60,8 +61,7 @@ export class UserController {
private readonly forcedTradeOfferViewService: ForcedTradeOfferViewService,
private readonly withdrawableAssetRepository: WithdrawableAssetRepository,
private readonly preprocessedUserStatisticsRepository: PreprocessedUserStatisticsRepository,
private readonly exchangeAddress: EthereumAddress,
private readonly showL2Transactions: boolean
private readonly exchangeAddress: EthereumAddress
) {}

async getUserRegisterPage(
Expand Down Expand Up @@ -126,7 +126,6 @@ export class UserController {
userAssets,
history,
l2Transactions,
l2TransactionsCount,
sentTransactions,
userTransactions,
userTransactionsCount,
Expand All @@ -150,7 +149,6 @@ export class UserController {
starkKey,
paginationOpts
),
this.l2TransactionRepository.countAllUserSpecific(starkKey),
this.sentTransactionRepository.getByStarkKey(starkKey),
this.userTransactionRepository.getByStarkKey(
starkKey,
Expand All @@ -175,6 +173,16 @@ export class UserController {
}
}

let l2TransactionsStatistics = userStatistics.l2TransactionsStatistics
if (!l2TransactionsStatistics) {
const lastUserStatisticsWithL2TransactionsStatistics =
await this.preprocessedUserStatisticsRepository.findLastWithL2TransactionsStatisticsByStarkKey(
starkKey
)
l2TransactionsStatistics =
lastUserStatisticsWithL2TransactionsStatistics?.l2TransactionsStatistics
}

const assetDetailsMap = await this.assetDetailsService.getAssetDetailsMap({
userAssets: userAssets,
assetHistory: history,
Expand Down Expand Up @@ -214,12 +222,8 @@ export class UserController {
context,
starkKey,
ethereumAddress: registeredUser?.ethAddress,
l2Transactions: this.showL2Transactions
? {
data: l2Transactions.map(l2TransactionToEntry),
total: l2TransactionsCount,
}
: undefined,
l2Transactions: l2Transactions.map(l2TransactionToEntry),
totalL2Transactions: sumUpTransactionCount(l2TransactionsStatistics),
withdrawableAssets: withdrawableAssets.map((asset) => ({
asset: {
hashOrId:
Expand Down Expand Up @@ -316,19 +320,24 @@ export class UserController {
pagination: PaginationOptions
): Promise<ControllerResult> {
const context = await this.pageContextService.getPageContext(givenUser)
const [l2Transactions, l2TransactionsCount] = await Promise.all([
this.l2TransactionRepository.getUserSpecificPaginated(
starkKey,
pagination
),
this.l2TransactionRepository.countAllUserSpecific(starkKey),
])
const [l2Transactions, lastUserStatisticsWithL2TransactionsStatistics] =
await Promise.all([
this.l2TransactionRepository.getUserSpecificPaginated(
starkKey,
pagination
),
this.preprocessedUserStatisticsRepository.findLastWithL2TransactionsStatisticsByStarkKey(
starkKey
),
])

const content = renderUserL2TransactionsPage({
context,
starkKey,
l2Transactions: l2Transactions.map(l2TransactionToEntry),
total: l2TransactionsCount,
total: sumUpTransactionCount(
lastUserStatisticsWithL2TransactionsStatistics?.l2TransactionsStatistics
),
...pagination,
})
return { type: 'success', content }
Expand Down