Skip to content

Commit

Permalink
Merge pull request #34 from klayrHQ/improve-db-calls
Browse files Browse the repository at this point in the history
removed cache
  • Loading branch information
Theezr authored Aug 15, 2024
2 parents ad3b80d + 6c8bdb1 commit 705ee97
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
push:
branches:
- main
- build-fix
- improve-db-calls

env:
REGISTRY: ghcr.io
Expand Down
22 changes: 3 additions & 19 deletions src/account/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,21 @@ 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;
publicKey?: string;
}) {
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 }]);
}
}
}
26 changes: 5 additions & 21 deletions src/block/block-repo.service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -111,18 +103,10 @@ export class BlockRepoService {
data: Prisma.BlockUpdateInput;
}): Promise<void> {
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 }[]> {
Expand Down
11 changes: 1 addition & 10 deletions src/block/block.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
14 changes: 11 additions & 3 deletions src/chain-event/chain-event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export class ChainEventService {
) {}

public async checkUserAccountsAndSaveEvents(blocks: Block[]): Promise<ChainEvent[]> {
const newAccounts = [];

const promises = blocks.map(async (block) => {
const chainEvents = await this.nodeApiService.invokeApi<ChainEvent[]>(
NodeApi.CHAIN_GET_EVENTS,
Expand All @@ -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();
}

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/prisma/prisma.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
2 changes: 1 addition & 1 deletion src/prisma/prisma.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
27 changes: 6 additions & 21 deletions src/transaction/transaction-repo.service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -81,18 +73,11 @@ export class TransactionRepoService {
data: Prisma.TransactionUpdateInput;
}): Promise<void> {
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<
Expand Down
4 changes: 1 addition & 3 deletions src/transaction/transaction.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
5 changes: 3 additions & 2 deletions src/transaction/transaction.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
});
}
Expand Down
3 changes: 1 addition & 2 deletions src/validator/validator.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
27 changes: 6 additions & 21 deletions src/validator/validator.repo-service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -84,18 +76,11 @@ export class ValidatorRepoService {
data: Prisma.ValidatorUpdateInput;
}): Promise<void> {
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<Validator[]> {
Expand Down

0 comments on commit 705ee97

Please sign in to comment.