Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
fix(batch-worker): schema breaking changes (#85)
Browse files Browse the repository at this point in the history
Schema changes due to limiting the size of POV created
breaking change to how schemas are accessed.

Update how schemas are queried to ensure that a schema payload
is returned.

issue-84

Co-authored-by: Enddy Dumbrique <[email protected]>
  • Loading branch information
enddynayn and Enddy Dumbrique authored Mar 29, 2024
1 parent 10b690c commit 4f9e8ff
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
6 changes: 3 additions & 3 deletions apps/worker/src/batch_announcer/batch.announcer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const mockConfigService = {
};

const mockBlockchainService = {
getSchema: jest.fn(),
getSchemaPayload: jest.fn(),
};

const mockIpfsService = {
Expand Down Expand Up @@ -74,7 +74,7 @@ describe('BatchAnnouncer', () => {
it('should announce a batch to IPFS', async () => {
// Mock the necessary dependencies' behavior
mockConfigService.getIpfsCidPlaceholder.mockReturnValue('mockIpfsUrl');
mockBlockchainService.getSchema.mockReturnValue({ model: Buffer.from(stringToHex(JSON.stringify(broadcast))) });
mockBlockchainService.getSchemaPayload.mockReturnValue(Buffer.from(stringToHex(JSON.stringify(broadcast))));
mockIpfsService.getPinned.mockReturnValue(Buffer.from('mockContentBuffer'));
mockIpfsService.ipfsPin.mockReturnValue({ cid: 'mockCid', size: 10, hash: 'mockHash' });

Expand All @@ -87,6 +87,6 @@ describe('BatchAnnouncer', () => {
const result = await ipfsAnnouncer.announce(batchJob);
assert(result);
expect(mockConfigService.getIpfsCidPlaceholder).toHaveBeenCalledWith('mockCid');
expect(mockBlockchainService.getSchema).toHaveBeenCalledWith(123);
expect(mockBlockchainService.getSchemaPayload).toHaveBeenCalledWith(123);
});
});
7 changes: 3 additions & 4 deletions apps/worker/src/batch_announcer/batch.announcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ParquetWriter } from '@dsnp/parquetjs';
import { fromFrequencySchema } from '@dsnp/frequency-schemas/parquet';
import { InjectRedis } from '@liaoliaots/nestjs-redis';
import Redis from 'ioredis';
import { PalletSchemasSchema } from '@polkadot/types/lookup';
import { hexToString } from '@polkadot/util';
import { RedisUtils } from '../../../../libs/common/src/utils/redis';
import { BlockchainService } from '../../../../libs/common/src/blockchain/blockchain.service';
Expand Down Expand Up @@ -33,13 +32,13 @@ export class BatchAnnouncer {
const schemaCacheKey = `schema:${schemaId}`;
let cachedSchema: string | null = await this.cacheManager.get(schemaCacheKey);
if (!cachedSchema) {
const schemaResponse = await this.blockchainService.getSchema(schemaId);
const schemaResponse = await this.blockchainService.getSchemaPayload(schemaId);
cachedSchema = JSON.stringify(schemaResponse);
await this.cacheManager.setex(schemaCacheKey, RedisUtils.STORAGE_EXPIRE_UPPER_LIMIT_SECONDS, cachedSchema);
}

const frequencySchema: PalletSchemasSchema = JSON.parse(cachedSchema);
const hexString: string = Buffer.from(frequencySchema.model).toString('utf8');
const frequencySchemaPayload = JSON.parse(cachedSchema);
const hexString: string = Buffer.from(frequencySchemaPayload).toString('utf8');
const schema = JSON.parse(hexToString(hexString));
if (!schema) {
throw new Error(`Unable to parse schema for schemaId ${schemaId}`);
Expand Down
8 changes: 4 additions & 4 deletions libs/common/src/blockchain/blockchain.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { KeyringPair } from '@polkadot/keyring/types';
import { BlockHash, BlockNumber, DispatchError, DispatchInfo, Hash, SignedBlock } from '@polkadot/types/interfaces';
import { SubmittableExtrinsic } from '@polkadot/api/types';
import { AnyNumber, ISubmittableResult, RegistryError } from '@polkadot/types/types';
import { u32, Option, u128 } from '@polkadot/types';
import { PalletCapacityCapacityDetails, PalletCapacityEpochInfo, PalletSchemasSchema } from '@polkadot/types/lookup';
import { u32, Option, u128, Bytes } from '@polkadot/types';
import { PalletCapacityCapacityDetails, PalletCapacityEpochInfo } from '@polkadot/types/lookup';
import { ConfigService } from '../config/config.service';
import { Extrinsic } from './extrinsic';

Expand Down Expand Up @@ -152,8 +152,8 @@ export class BlockchainService implements OnApplicationBootstrap, OnApplicationS
return this.api.consts.frequencyTxPayment.maximumCapacityBatchLength.toNumber();
}

public async getSchema(schemaId: number): Promise<PalletSchemasSchema> {
const schema: PalletSchemasSchema = await this.query('schemas', 'schemas', schemaId);
public async getSchemaPayload(schemaId: number): Promise<Bytes> {
const schema: Bytes = await this.query('schemas', 'schemaPayloads', schemaId);
return schema;
}

Expand Down

0 comments on commit 4f9e8ff

Please sign in to comment.