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

feat(idea/meta-storage): support sails idl #1533

Merged
merged 9 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions idea/common/src/interfaces/rpc-request/meta-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ export interface GetMetaParams {
hash: string;
hex: HexString;
}

export interface AddSailsIdlParams {
codeId: string;
data: string;
}
5 changes: 5 additions & 0 deletions idea/common/src/jsonrpc-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ export const JSONRPC_ERRORS = {
code: -32400,
message: 'Address is not supported',
},
SailsIdlNotFound: {
name: 'SailsIdlNotFound',
code: -32404,
message: 'Sails IDL not found',
},
};

export function isExistError(name: string) {
Expand Down
4 changes: 2 additions & 2 deletions idea/meta-storage/src/database/data-source.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DataSource } from 'typeorm';

import { Meta } from './entities';
import { Code, Meta, SailsIdl } from './entities';
import config from '../config';

export const AppDataSource = new DataSource({
Expand All @@ -10,7 +10,7 @@ export const AppDataSource = new DataSource({
username: config.db.user,
password: config.db.password,
database: config.db.name,
entities: [Meta],
entities: [Meta, SailsIdl, Code],
synchronize: true,
logging: ['error', 'schema'],
});
15 changes: 15 additions & 0 deletions idea/meta-storage/src/database/entities/code.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Entity, ManyToOne, PrimaryColumn } from 'typeorm';
import { SailsIdl } from './sails.entity';

@Entity()
export class Code {
constructor(props: Partial<Code>) {
Object.assign(this, props);
}

@PrimaryColumn()
id: string;

@ManyToOne(() => SailsIdl, (sails) => sails.codes)
sails: SailsIdl;
}
2 changes: 2 additions & 0 deletions idea/meta-storage/src/database/entities/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { Meta } from './meta.entity';
export { SailsIdl } from './sails.entity';
export { Code } from './code.entity';
18 changes: 18 additions & 0 deletions idea/meta-storage/src/database/entities/sails.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Column, Entity, OneToMany, PrimaryColumn } from 'typeorm';
import { Code } from './code.entity';

@Entity()
export class SailsIdl {
constructor(props: Partial<SailsIdl>) {
Object.assign(this, props);
}

@PrimaryColumn()
id: string;

@Column()
data: string;

@OneToMany(() => Code, (code) => code.id)
codes: Code[];
}
45 changes: 41 additions & 4 deletions idea/meta-storage/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { Repository } from 'typeorm';
import { AddMetaDetailsParams, AddMetahashParams, GetMetaParams, logger } from '@gear-js/common';
import { ProgramMetadata, MetadataVersion, HumanTypesRepr } from '@gear-js/api';
import { Repository } from 'typeorm';
import * as crypto from 'crypto';

import { Meta, AppDataSource } from './database';
import { InvalidParamsError, MetaNotFoundError } from './util/errors';
import { Meta, AppDataSource, SailsIdl } from './database';
import { InvalidParamsError, MetaNotFoundError, SailsIdlNotFoundError } from './util/errors';
import { validateMetaHex } from './util/validate';
import { ProgramMetadata, MetadataVersion, HumanTypesRepr } from '@gear-js/api';
import { Code } from './database/entities/code.entity';

const getHash = (data: string) => crypto.createHash('sha256').update(data).digest('hex');

export class MetaService {
private metaRepo: Repository<Meta>;
private sailsRepo: Repository<SailsIdl>;
private codeRepo: Repository<Code>;

constructor() {
this.metaRepo = AppDataSource.getRepository(Meta);
this.sailsRepo = AppDataSource.getRepository(SailsIdl);
this.codeRepo = AppDataSource.getRepository(Code);
}

async addMeta({ metahash }: AddMetahashParams): Promise<string[]> {
Expand Down Expand Up @@ -82,4 +90,33 @@ export class MetaService {
const meta = await this.metaRepo.find({ where: { hasState: true }, select: { hash: true } });
return meta.map((m) => m.hash);
}

async addIdl({ codeId, data }) {
const hash = getHash(data);

let sails = await this.sailsRepo.findOne({ where: { id: hash } });

if (!sails) {
const code = await this.codeRepo.findOne({ where: { id: codeId } });
if (code) {
throw new InvalidParamsError('Code already has IDL');
}
sails = new SailsIdl({ id: hash, data });
}

const code = new Code({ id: codeId, sails });

await this.sailsRepo.save(sails);
await this.codeRepo.save(code);
}

async getIdl({ codeId }) {
const code = await this.codeRepo.findOne({ where: { id: codeId }, relations: { sails: true } });

if (!code) {
throw new SailsIdlNotFoundError();
}

return code.sails.data;
}
}
4 changes: 4 additions & 0 deletions idea/meta-storage/src/util/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ export class InvalidMetadataError extends Error {
export class InvalidParamsError extends Error {
name = JSONRPC_ERRORS.InvalidParams.name;
}

export class SailsIdlNotFoundError extends Error {
name = JSONRPC_ERRORS.SailsIdlNotFound.name;
}
Loading