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

[SERVICES-1824] improve escrow queries #1174

Merged
merged 5 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
59 changes: 56 additions & 3 deletions src/modules/escrow/services/escrow.abi.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
import { GetOrSetCache } from 'src/helpers/decorators/caching.decorator';
import { oneDay } from 'src/helpers/helpers';
import { CacheTtlInfo } from 'src/services/caching/cache.ttl.info';
import { CachingService } from 'src/services/caching/cache.service';
import { EscrowSetterService } from './escrow.setter.service';

@Injectable()
export class EscrowAbiService extends GenericAbiService {
constructor(
protected readonly mxProxy: MXProxyService,
private readonly mxGateway: MXGatewayService,
private readonly cachingService: CachingService,
private readonly escrowSetter: EscrowSetterService,
) {
super(mxProxy);
}
Expand Down Expand Up @@ -110,10 +114,17 @@
}

async getAllReceiversRaw(senderAddress: string): Promise<string[]> {
const hexValues = await this.mxGateway.getSCStorageKeys(
scAddress.escrow,
[],
let hexValues = await this.cachingService.getCache<object>(
`escrow.scKeys`,
);
if (!hexValues || hexValues === undefined) {
hexValues = await this.mxGateway.getSCStorageKeys(
scAddress.escrow,
[],
);
await this.escrowSetter.setSCStorageKeys(hexValues);
}

const receivers = [];
const allSendersHex = Buffer.from('allSenders').toString('hex');
const itemHex = Buffer.from('.item').toString('hex');
Expand Down Expand Up @@ -266,6 +277,12 @@
remoteTtl: oneDay(),
})
async addressPermission(address: string): Promise<SCPermissions[]> {
const addressesWithPermissions =
await this.allAddressesWithPermissions();

Check warning on line 281 in src/modules/escrow/services/escrow.abi.service.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/escrow/services/escrow.abi.service.ts#L281

Added line #L281 was not covered by tests
if (!addressesWithPermissions.includes(address)) {
return [SCPermissions.NONE];

Check warning on line 283 in src/modules/escrow/services/escrow.abi.service.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/escrow/services/escrow.abi.service.ts#L283

Added line #L283 was not covered by tests
}

return await this.getAddressPermissionRaw(address);
}

Expand Down Expand Up @@ -293,4 +310,40 @@
return [];
}
}

@ErrorLoggerAsync({
className: EscrowAbiService.name,
logArgs: true,
})
@GetOrSetCache({
baseKey: 'escrow',
remoteTtl: oneDay(),
})
async allAddressesWithPermissions(): Promise<string[]> {
return await this.getAllAddressesWithPermissionsRaw();

Check warning on line 323 in src/modules/escrow/services/escrow.abi.service.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/escrow/services/escrow.abi.service.ts#L323

Added line #L323 was not covered by tests
}

async getAllAddressesWithPermissionsRaw(): Promise<string[]> {
let hexValues = await this.cachingService.getCache<object>(

Check warning on line 327 in src/modules/escrow/services/escrow.abi.service.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/escrow/services/escrow.abi.service.ts#L327

Added line #L327 was not covered by tests
`escrow.scKeys`,
);
if (!hexValues || hexValues === undefined) {
hexValues = await this.mxGateway.getSCStorageKeys(

Check warning on line 331 in src/modules/escrow/services/escrow.abi.service.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/escrow/services/escrow.abi.service.ts#L331

Added line #L331 was not covered by tests
scAddress.escrow,
[],
);
await this.escrowSetter.setSCStorageKeys(hexValues);

Check warning on line 335 in src/modules/escrow/services/escrow.abi.service.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/escrow/services/escrow.abi.service.ts#L335

Added line #L335 was not covered by tests
}

const addresses = [];
const permissionsHex = Buffer.from('permissions').toString('hex');
Object.keys(hexValues).forEach((key) => {

Check warning on line 340 in src/modules/escrow/services/escrow.abi.service.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/escrow/services/escrow.abi.service.ts#L338-L340

Added lines #L338 - L340 were not covered by tests
if (key.includes(permissionsHex)) {
const addressHex = key.split(permissionsHex)[1];
addresses.push(Address.fromHex(addressHex).bech32());

Check warning on line 343 in src/modules/escrow/services/escrow.abi.service.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/escrow/services/escrow.abi.service.ts#L342-L343

Added lines #L342 - L343 were not covered by tests
}
});

return addresses;

Check warning on line 347 in src/modules/escrow/services/escrow.abi.service.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/escrow/services/escrow.abi.service.ts#L347

Added line #L347 was not covered by tests
}
}
10 changes: 10 additions & 0 deletions src/modules/escrow/services/escrow.setter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CachingService } from 'src/services/caching/cache.service';
import { GenericSetterService } from 'src/services/generics/generic.setter.service';
import { Logger } from 'winston';
import { ScheduledTransferModel } from '../models/escrow.model';
import { CacheTtlInfo } from 'src/services/caching/cache.ttl.info';

@Injectable()
export class EscrowSetterService extends GenericSetterService {
Expand Down Expand Up @@ -70,4 +71,13 @@ export class EscrowSetterService extends GenericSetterService {
oneDay(),
);
}

async setSCStorageKeys(value: object): Promise<string> {
return await this.setData(
this.getCacheKey('scKeys'),
value,
CacheTtlInfo.ContractState.remoteTtl,
CacheTtlInfo.ContractState.localTtl,
);
}
}
7 changes: 7 additions & 0 deletions src/modules/escrow/specs/escrow.abi.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import { CacheModule } from '@nestjs/cache-manager';
import { CachingService } from 'src/services/caching/cache.service';
import { ApiConfigService } from 'src/helpers/api.config.service';
import winston from 'winston';
import { EscrowSetterService } from '../services/escrow.setter.service';

describe('EscrowAbiService', () => {
let service: EscrowAbiService;
let mxGateway: MXGatewayService;
let cachingService: CachingService;

beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
Expand All @@ -27,6 +29,7 @@ describe('EscrowAbiService', () => {
],
providers: [
EscrowAbiService,
EscrowSetterService,
MXProxyServiceProvider,
MXGatewayServiceProvider,
CachingService,
Expand All @@ -36,6 +39,7 @@ describe('EscrowAbiService', () => {

service = module.get<EscrowAbiService>(EscrowAbiService);
mxGateway = module.get<MXGatewayService>(MXGatewayService);
cachingService = module.get<CachingService>(CachingService);
});

it('should be defined', () => {
Expand Down Expand Up @@ -119,6 +123,7 @@ describe('EscrowAbiService', () => {

receivers = await service.getAllReceiversRaw(sender.bech32());
expect(receivers).toEqual([]);
await cachingService.deleteInCache(`escrow.scKeys`);
});

it('should get one receiver for sender', async () => {
Expand All @@ -136,6 +141,7 @@ describe('EscrowAbiService', () => {
expect(receivers).toEqual([
'erd1devnet6uy8xjusvusfy3q83qadfhwrtty5fwa8ceh9cl60q2p6ysra7aaa',
]);
await cachingService.deleteInCache(`escrow.scKeys`);
});

it('should get two receivers for sender', async () => {
Expand All @@ -160,5 +166,6 @@ describe('EscrowAbiService', () => {
'erd1devnet6uy8xjusvusfy3q83qadfhwrtty5fwa8ceh9cl60q2p6ysra7aaa',
'erd1932eft30w753xyvme8d49qejgkjc09n5e49w4mwdjtm0neld797su0dlxp',
]);
await cachingService.deleteInCache(`escrow.scKeys`);
});
});
10 changes: 10 additions & 0 deletions src/modules/rabbitmq/handlers/escrow.handler.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import {
} from '@multiversx/sdk-exchange';
import { Inject, Injectable } from '@nestjs/common';
import { RedisPubSub } from 'graphql-redis-subscriptions';
import { scAddress } from 'src/config';
import { EscrowAbiService } from 'src/modules/escrow/services/escrow.abi.service';
import { EscrowSetterService } from 'src/modules/escrow/services/escrow.setter.service';
import { MXGatewayService } from 'src/services/multiversx-communication/mx.gateway.service';
import { PUB_SUB } from 'src/services/redis.pubSub.module';

@Injectable()
export class EscrowHandlerService {
constructor(
private readonly escrowAbi: EscrowAbiService,
private readonly escrowSetter: EscrowSetterService,
private readonly mxGateway: MXGatewayService,
@Inject(PUB_SUB) private pubSub: RedisPubSub,
) {}

Expand Down Expand Up @@ -82,6 +85,13 @@ export class EscrowHandlerService {
sender: string,
receiver: string,
): Promise<void> {
const scKeys = await this.mxGateway.getSCStorageKeys(
scAddress.escrow,
[],
);
const cachedKey = await this.escrowSetter.setSCStorageKeys(scKeys);
await this.deleteCacheKeys([cachedKey]);

const [allSenders, allReceivers, scheduledTransfers] =
await Promise.all([
this.escrowAbi.getAllSendersRaw(receiver),
Expand Down
Loading