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 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
55 changes: 19 additions & 36 deletions .github/workflows/e2e-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ env:
REACT_APP_DEFAULT_NODES_URL: ${{ secrets.REACT_APP_DEFAULT_NODES_URL }}
REACT_APP_RRT: ''
REACT_APP_HCAPTCHA_SITE_KEY: ${{ secrets.REACT_APP_HCAPTCHA_SITE_KEY }}
NIGHTLY_TOOLCHAIN_VERSION: ${{ vars.NIGHTLY_TOOLCHAIN_VERSION }}
BINARYEN_VERSION: version_111

jobs:
check-labels:
Expand Down Expand Up @@ -303,51 +303,34 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Install NodeJS 18.x
- name: Install NodeJS 20.x
uses: actions/[email protected]
with:
node-version: 18.x

- name: "Install dependencies"
run: YARN_ENABLE_IMMUTABLE_INSTALLS=false yarn install

- name: "Build: @gear-js/common"
run: yarn build:common

- name: "Install: Nightly toolchain"
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
target: wasm32-unknown-unknown
components: llvm-tools-preview

- name: "Install: Show specific nightly version"
if: ${{ env.NIGHTLY_TOOLCHAIN_VERSION != '' }}
run: echo $NIGHTLY_TOOLCHAIN_VERSION | sed 's/-/ - /g'

- name: "Install: Specific nightly toolchain"
if: ${{ env.NIGHTLY_TOOLCHAIN_VERSION != '' }}
uses: actions-rs/toolchain@v1
node-version: 20.x

- name: Set up Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: nightly-${{ env.NIGHTLY_TOOLCHAIN_VERSION }}
target: wasm32-unknown-unknown
components: llvm-tools-preview
targets: wasm32-unknown-unknown

- name: "Install: Pin to specific nightly toolchain"
if: ${{ env.NIGHTLY_TOOLCHAIN_VERSION != '' }}
run: |
rm -rf $RUSTUP_HOME/toolchains/nightly-x86_64-unknown-linux-gnu
ln -s $RUSTUP_HOME/toolchains/nightly-$NIGHTLY_TOOLCHAIN_VERSION-x86_64-unknown-linux-gnu $RUSTUP_HOME/toolchains/nightly-x86_64-unknown-linux-gnu

- name: "Prepare: Build test programs"
working-directory: api/programs
run: cargo build --release

- name: "Prepare: Copy built programs"
run: |
cp api/programs/target/wasm32-unknown-unknown/release/* idea/tests/wasm-test
run: cp api/programs/target/wasm32-unknown-unknown/release/* idea/tests/wasm-test

- name: "Prepare: Build test programs"
working-directory: idea/tests/programs
run: cargo build --release

- name: "Install dependencies"
run: YARN_ENABLE_IMMUTABLE_INSTALLS=false yarn install

- name: "Build: @gear-js/common"
run: yarn build:common

- name: "Run tests"
timeout-minutes: 7
Expand Down
2 changes: 1 addition & 1 deletion api/programs/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[toolchain]
channel = "nightly-2023-10-14"
channel = "nightly-2024-01-25"
components = [ "llvm-tools" ]
targets = [ "wasm32-unknown-unknown" ]
profile = "default"
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,20 @@ export async function validateJsonRpcRequestMiddleware({ body }: Request, res: R
next();
}

function isValidRequestParams({ id, method, jsonrpc, params }: IRpcRequest): boolean {
return !!id && !!method && !!jsonrpc && !!params;
}
const isValidRequestParams = ({ id, method, jsonrpc, params }: IRpcRequest): boolean =>
!!id && !!method && !!jsonrpc && !!params;

function getInvalidParamsResponse(req: IRpcRequest) {
logger.info('Invalid params error', { req });

const error = JSONRPC_ERRORS.InvalidParams.name;
const error = JSONRPC_ERRORS.InvalidParams;

return {
jsonrpc: '2.0',
id: req.id || null,
error: {
message: JSONRPC_ERRORS[error].message,
code: JSONRPC_ERRORS[error].code,
message: error.message,
code: error.code,
},
};
}
2 changes: 2 additions & 0 deletions idea/common/src/enums/api-methods.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export enum INDEXER_INTERNAL_METHODS {
export enum META_STORAGE_METHODS {
META_GET = 'meta.get',
META_ADD = 'meta.add',
SAILS_ADD = 'sails.add',
SAILS_GET = 'sails.get',
}

export enum META_STORAGE_INTERNAL_METHODS {
Expand Down
9 changes: 8 additions & 1 deletion idea/common/src/interfaces/api-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ interface GetAllStateResult {
interface ProgramDataResult extends Omit<IProgram, 'meta'> {
meta?: { meta?: string };
}

interface AddSailsResult {
status: 'Sails idl added';
}

interface IRpcResponse {
jsonrpc: '2.0';
id: number;
Expand All @@ -50,7 +55,8 @@ interface IRpcResponse {
| IMessage[]
| AddMetaResult
| GetAllCodeResult
| GetStatesResult;
| GetStatesResult
| AddSailsResult;
error?: IRpcError;
}

Expand All @@ -70,4 +76,5 @@ export {
IRpcResponse,
IRpcError,
GetStatesResult,
AddSailsResult,
};
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
2 changes: 1 addition & 1 deletion idea/meta-storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"scripts": {
"build": "rm -rf dist && tsc",
"start": "node dist/main.js",
"watch": "ts-node-dev src/main.ts"
"watch": "clear && ts-node-dev src/main.ts"
},
"lint-staged": {
"*.ts": [
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()
public id: string;

@ManyToOne(() => SailsIdl, (sails) => sails.id)
public 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()
public id: string;

@Column()
public data: string;

@OneToMany(() => Code, (code) => code.id)
public codes: Code[];
}
10 changes: 8 additions & 2 deletions idea/meta-storage/src/rmq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export class RMQService {
this.methods = {
[META_STORAGE_METHODS.META_ADD]: this.metaService.addMetaDetails.bind(this.metaService),
[META_STORAGE_METHODS.META_GET]: this.metaService.get.bind(this.metaService),
[META_STORAGE_METHODS.SAILS_ADD]: this.metaService.addIdl.bind(this.metaService),
[META_STORAGE_METHODS.SAILS_GET]: this.metaService.getIdl.bind(this.metaService),
[META_STORAGE_INTERNAL_METHODS.META_HASH_ADD]: this.metaService.addMeta.bind(this.metaService),
};
}
Expand Down Expand Up @@ -81,9 +83,13 @@ export class RMQService {
const params = JSON.parse(msg.content.toString());
const correlationId = msg.properties.correlationId;

const result = await this.handleIncomingMsg(method, params);
try {
const result = await this.handleIncomingMsg(method, params);

this.sendMsg(RMQExchange.DIRECT_EX, RMQQueue.REPLIES, result, correlationId);
this.sendMsg(RMQExchange.DIRECT_EX, RMQQueue.REPLIES, result, correlationId);
} catch (error) {
logger.error('Failed to handle incoming message', { error: error.message, stack: error.stack });
}
},
{ noAck: true },
);
Expand Down
53 changes: 49 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,41 @@ 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 }) {
if (!codeId || !data) {
throw new InvalidParamsError();
}

const hash = getHash(data);

logger.info('Adding IDL', { codeId, hash });

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);

return { status: 'Sails idl added' };
}

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;
}
4 changes: 3 additions & 1 deletion idea/tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
wasm/
./wasm/
.env
dist/
.yarn/
wasm-test/*
!wasm-test/app.opt.wasm
genesis

target/
22 changes: 22 additions & 0 deletions idea/tests/e2e/meta-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import * as path from 'path';
import { PATH_TO_PROGRAMS } from './config';
import { generateCodeHash, HexString } from '@gear-js/api';

const PING_WASM_PATH = './programs/target/wasm32-unknown-unknown/release/ping.opt.wasm';
const PING_IDL_PATH = './programs/ping-sails/wasm/ping.idl';

const pingCode = fs.readFileSync(PING_WASM_PATH);
const pingIdl = fs.readFileSync(PING_IDL_PATH, 'utf-8');

const meta: HexString = `0x${fs.readFileSync(path.join(PATH_TO_PROGRAMS, 'test_meta.meta.txt'), 'utf-8')}`;
const hash = generateCodeHash(meta);

Expand All @@ -24,4 +30,20 @@ describe('meta-storage methods', () => {
expect(response.result).toHaveProperty('hash', hash);
expect(response.result).toHaveProperty('hex', meta);
});

test(META_STORAGE_METHODS.SAILS_ADD, async () => {
const codehash = generateCodeHash(pingCode);
const response = await request(META_STORAGE_METHODS.SAILS_ADD, { codeId: codehash, data: pingIdl });

expect(response).toHaveProperty('result');
expect(response.result).toHaveProperty('status', 'Sails idl added');
});

test(META_STORAGE_METHODS.SAILS_GET, async () => {
const codehash = generateCodeHash(pingCode);
const response = await request(META_STORAGE_METHODS.SAILS_GET, { codeId: codehash });

expect(response).toHaveProperty('result');
expect(response.result).toBe(pingIdl);
});
});
Loading
Loading