From 6c8bdb1f96051f452cb0391cf727b433ba55d0bd Mon Sep 17 00:00:00 2001 From: Theezr Date: Thu, 15 Aug 2024 14:07:11 +0200 Subject: [PATCH] removed cache --- .github/workflows/build.yml | 2 +- src/account/account.service.ts | 22 +++-------------- src/block/block-repo.service.ts | 26 ++++---------------- src/block/block.module.ts | 11 +-------- src/chain-event/chain-event.service.ts | 14 ++++++++--- src/prisma/prisma.module.ts | 2 +- src/prisma/prisma.service.ts | 2 +- src/transaction/transaction-repo.service.ts | 27 +++++---------------- src/transaction/transaction.module.ts | 4 +-- src/transaction/transaction.service.ts | 5 ++-- src/validator/validator.module.ts | 3 +-- src/validator/validator.repo-service.ts | 27 +++++---------------- 12 files changed, 40 insertions(+), 105 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3b71498..9a9ecb5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -5,7 +5,7 @@ on: push: branches: - main - - build-fix + - improve-db-calls env: REGISTRY: ghcr.io diff --git a/src/account/account.service.ts b/src/account/account.service.ts index 0b4a189..9858dfa 100644 --- a/src/account/account.service.ts +++ b/src/account/account.service.ts @@ -14,21 +14,10 @@ export class AccountService { return this.accountRepoService.createAccountsBulk(accounts); } - public async upsertAccount(accountData: Prisma.AccountUpsertArgs) { - return this.accountRepoService.upsertAccount(accountData); - } - public async updateAccount(accountData: Prisma.AccountUpdateArgs) { return this.accountRepoService.updateAccount(accountData); } - public async createAccount(accountData: Prisma.AccountCreateInput) { - const account = await this.getAccount({ address: accountData.address }); - if (account) return; - return this.accountRepoService.createAccount(accountData); - } - - // ! Upserting gives prisma problems public async updateOrCreateAccount(params: { address: string; name?: string; @@ -36,15 +25,10 @@ export class AccountService { }) { const { address, name, publicKey } = params; - const updateResult = await this.accountRepoService.updateAccounts({ + await this.accountRepoService.upsertAccount({ where: { address }, - data: { publicKey, name }, + update: { publicKey, name }, + create: { address, publicKey, name }, }); - - if (updateResult.count === 0) { - const account = await this.getAccount({ address }); - if (account) return; - await this.createAccountsBulk([{ address, name, publicKey }]); - } } } diff --git a/src/block/block-repo.service.ts b/src/block/block-repo.service.ts index 9e68aa2..71fb679 100644 --- a/src/block/block-repo.service.ts +++ b/src/block/block-repo.service.ts @@ -1,18 +1,10 @@ import { Injectable } from '@nestjs/common'; import { Block, Prisma, PrismaPromise } from '@prisma/client'; -import { DbCacheService } from 'src/db-cache/db-cache.service'; -import { Models } from 'src/db-cache/types'; import { PrismaService } from 'src/prisma/prisma.service'; -import { StateService } from 'src/state/state.service'; -import { IndexerState, Modules } from 'src/state/types'; @Injectable() export class BlockRepoService { - constructor( - private state: StateService, - private prisma: PrismaService, - private dbCache: DbCacheService, - ) {} + constructor(private prisma: PrismaService) {} public async getBlock( blockWhereUniqueInput: Prisma.BlockWhereUniqueInput, @@ -111,18 +103,10 @@ export class BlockRepoService { data: Prisma.BlockUpdateInput; }): Promise { const { where, data } = params; - if (this.state.get(Modules.INDEXER) === IndexerState.INDEXING) { - await this.prisma.block.update({ - where, - data, - }); - } else { - await this.dbCache.add({ - where, - data, - model: Models.BLOCK, - }); - } + await this.prisma.block.update({ + where, + data, + }); } public async searchBlocks(search: string): Promise<{ id: string; height: number }[]> { diff --git a/src/block/block.module.ts b/src/block/block.module.ts index ae381f2..eeaf234 100644 --- a/src/block/block.module.ts +++ b/src/block/block.module.ts @@ -5,19 +5,10 @@ import { BlockController } from './block.controller'; import { EventModule } from 'src/event/event.module'; import { NodeApiModule } from 'src/node-api/node-api.module'; import { ChainEventModule } from 'src/chain-event/chain-event.module'; -import { StateModule } from 'src/state/state.module'; -import { DbCacheModule } from 'src/db-cache/db-cache.module'; import { TransactionModule } from 'src/transaction/transaction.module'; @Module({ - imports: [ - StateModule, - EventModule, - TransactionModule, - NodeApiModule, - ChainEventModule, - DbCacheModule, - ], + imports: [EventModule, TransactionModule, NodeApiModule, ChainEventModule], providers: [BlockService, BlockRepoService], controllers: [BlockController], exports: [BlockService, BlockRepoService], diff --git a/src/chain-event/chain-event.service.ts b/src/chain-event/chain-event.service.ts index 6fac313..bd83920 100644 --- a/src/chain-event/chain-event.service.ts +++ b/src/chain-event/chain-event.service.ts @@ -17,6 +17,8 @@ export class ChainEventService { ) {} public async checkUserAccountsAndSaveEvents(blocks: Block[]): Promise { + const newAccounts = []; + const promises = blocks.map(async (block) => { const chainEvents = await this.nodeApiService.invokeApi( NodeApi.CHAIN_GET_EVENTS, @@ -31,11 +33,17 @@ export class ChainEventService { e.transactionID = this.getTransactionId(e.topics); return e; }); - await this.handleAccounts(decodedChainEvents); + + const accounts = await this.handleAccounts(decodedChainEvents); + if (accounts.length > 0) newAccounts.push(...accounts); return decodedChainEvents; }); + const chainEvents = await Promise.all(promises); + + if (newAccounts.length > 0) await this.accountService.createAccountsBulk(newAccounts); + return chainEvents.flat(); } @@ -51,14 +59,14 @@ export class ChainEventService { }); } - // TODO: handle accounts for block batch instead of every block itself private async handleAccounts(chainEvents: ChainEvent[]) { const accounts = chainEvents .filter((e) => e.name === 'initializeUserAccount') .map((e) => { return { address: JSON.parse(e.data as string)['address'] }; }); - if (accounts.length > 0) await this.accountService.createAccountsBulk(accounts); + + return accounts; } private getTransactionId(topics: string): string | null { diff --git a/src/prisma/prisma.module.ts b/src/prisma/prisma.module.ts index b268f1d..7207426 100644 --- a/src/prisma/prisma.module.ts +++ b/src/prisma/prisma.module.ts @@ -4,6 +4,6 @@ import { PrismaService } from './prisma.service'; @Global() @Module({ providers: [PrismaService], - exports: [PrismaService], //export this service to use in other modules + exports: [PrismaService], }) export class PrismaModule {} diff --git a/src/prisma/prisma.service.ts b/src/prisma/prisma.service.ts index 6d56835..67edfd9 100644 --- a/src/prisma/prisma.service.ts +++ b/src/prisma/prisma.service.ts @@ -17,7 +17,7 @@ export class PrismaService extends PrismaClient implements OnModuleInit { if (process.env.NODE_ENV === 'dev') { this.logger.warn('DEV mode: clearing DB'); - // await this.DEVonlyClearDB(); + await this.DEVonlyClearDB(); } } diff --git a/src/transaction/transaction-repo.service.ts b/src/transaction/transaction-repo.service.ts index 2949715..7f067b9 100644 --- a/src/transaction/transaction-repo.service.ts +++ b/src/transaction/transaction-repo.service.ts @@ -1,18 +1,10 @@ import { Injectable } from '@nestjs/common'; import { Prisma, Transaction } from '@prisma/client'; -import { DbCacheService } from 'src/db-cache/db-cache.service'; -import { Models } from 'src/db-cache/types'; import { PrismaService } from 'src/prisma/prisma.service'; -import { StateService } from 'src/state/state.service'; -import { IndexerState, Modules } from 'src/state/types'; @Injectable() export class TransactionRepoService { - constructor( - private state: StateService, - private prisma: PrismaService, - private dbCache: DbCacheService, - ) {} + constructor(private prisma: PrismaService) {} public async getTransaction( transactionWhereUniqueInput: Prisma.TransactionWhereUniqueInput, @@ -81,18 +73,11 @@ export class TransactionRepoService { data: Prisma.TransactionUpdateInput; }): Promise { const { where, data } = params; - if (this.state.get(Modules.INDEXER) === IndexerState.INDEXING) { - await this.prisma.transaction.update({ - where, - data, - }); - } else { - await this.dbCache.add({ - where, - data, - model: Models.TRANSACTION, - }); - } + + await this.prisma.transaction.update({ + where, + data, + }); } public async searchTransactions(search: string): Promise< diff --git a/src/transaction/transaction.module.ts b/src/transaction/transaction.module.ts index 3b3434d..a71b1dc 100644 --- a/src/transaction/transaction.module.ts +++ b/src/transaction/transaction.module.ts @@ -4,12 +4,10 @@ import { TransactionController } from './transaction.controller'; import { NodeApiModule } from 'src/node-api/node-api.module'; import { TransactionService } from './transaction.service'; import { EventModule } from 'src/event/event.module'; -import { StateModule } from 'src/state/state.module'; -import { DbCacheModule } from 'src/db-cache/db-cache.module'; import { AccountModule } from 'src/account/account.module'; @Module({ - imports: [StateModule, NodeApiModule, EventModule, DbCacheModule, AccountModule], + imports: [NodeApiModule, EventModule, AccountModule], providers: [TransactionRepoService, TransactionService], exports: [TransactionService, TransactionRepoService], controllers: [TransactionController], diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index 40420bb..da0207c 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -1,7 +1,7 @@ import { Injectable, Logger } from '@nestjs/common'; import { TransactionRepoService } from './transaction-repo.service'; import { OnEvent } from '@nestjs/event-emitter'; -import { ChainEvents, Events, GatewayEvents, Payload } from 'src/event/types'; +import { ChainEvents, GatewayEvents, Payload } from 'src/event/types'; import { ChainEvent, Transaction } from 'src/node-api/types'; import { NodeApiService } from 'src/node-api/node-api.service'; import { Prisma } from '@prisma/client'; @@ -113,8 +113,9 @@ export class TransactionService { } // When a tx fails the recipient account is not created / registered. This is to cover that + // TODO: inefficient because it will call the db for all recipients if (recipientAddress) { - await this.accountService.createAccount({ + await this.accountService.updateOrCreateAccount({ address: recipientAddress, }); } diff --git a/src/validator/validator.module.ts b/src/validator/validator.module.ts index 2ffedd4..fb974b6 100644 --- a/src/validator/validator.module.ts +++ b/src/validator/validator.module.ts @@ -5,11 +5,10 @@ import { ValidatorController } from './validator.controller'; import { AccountModule } from 'src/account/account.module'; import { NodeApiModule } from 'src/node-api/node-api.module'; import { StateModule } from 'src/state/state.module'; -import { DbCacheModule } from 'src/db-cache/db-cache.module'; import { BlockModule } from 'src/block/block.module'; @Module({ - imports: [StateModule, AccountModule, NodeApiModule, DbCacheModule, BlockModule], + imports: [StateModule, AccountModule, NodeApiModule, BlockModule], providers: [ValidatorRepoService, ValidatorService], exports: [ValidatorService, ValidatorRepoService], controllers: [ValidatorController], diff --git a/src/validator/validator.repo-service.ts b/src/validator/validator.repo-service.ts index 388837d..bf392bd 100644 --- a/src/validator/validator.repo-service.ts +++ b/src/validator/validator.repo-service.ts @@ -1,18 +1,10 @@ import { Injectable } from '@nestjs/common'; import { Prisma, Validator } from '@prisma/client'; -import { DbCacheService } from 'src/db-cache/db-cache.service'; -import { Models } from 'src/db-cache/types'; import { PrismaService } from 'src/prisma/prisma.service'; -import { StateService } from 'src/state/state.service'; -import { IndexerState, Modules } from 'src/state/types'; @Injectable() export class ValidatorRepoService { - constructor( - private prisma: PrismaService, - private state: StateService, - private dbCache: DbCacheService, - ) {} + constructor(private prisma: PrismaService) {} public async getValidator( validatorWhereUniqueInput: Prisma.ValidatorWhereUniqueInput, @@ -84,18 +76,11 @@ export class ValidatorRepoService { data: Prisma.ValidatorUpdateInput; }): Promise { const { where, data } = params; - if (this.state.get(Modules.INDEXER) === IndexerState.INDEXING) { - await this.prisma.validator.update({ - where, - data, - }); - } else { - await this.dbCache.add({ - where, - data, - model: Models.VALIDATOR, - }); - } + + await this.prisma.validator.update({ + where, + data, + }); } public async getAllValidators(): Promise {