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

Introduce interoperability_getMainchainID endpoint #8758

Merged
merged 2 commits into from
Jul 31, 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
16 changes: 16 additions & 0 deletions framework/src/modules/interoperability/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,22 @@ export const isChainNameAvailableResponseSchema = {
$id: '/modules/interoperability/endpoint/isChainNameAvailableResponseSchema',
};

export const getMainchainIDRequestSchema = {
...isChainIDAvailableRequestSchema,
$id: '/modules/interoperability/endpoint/getMainchainIDRequestSchema',
};

export const getMainchainIDResponseSchema = {
$id: '/modules/interoperability/endpoint/getMainchainIDResponseSchema',
type: 'object',
required: ['mainchainID'],
properties: {
result: {
type: 'string',
},
},
};

export const getChannelRequestSchema = getChainAccountRequestSchema;

export const getTerminatedStateAccountRequestSchema = getChainAccountRequestSchema;
Expand Down
14 changes: 12 additions & 2 deletions framework/src/modules/interoperability/sidechain/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@
*
* Removal or modification of this copyright notice is prohibited.
*/

import { validator } from '@liskhq/lisk-validator';
import { BaseInteroperabilityEndpoint } from '../base_interoperability_endpoint';
import { ModuleEndpointContext } from '../../../types';
import { isChainIDAvailableRequestSchema } from '../schemas';
import { getMainchainID } from '../utils';

export class SidechainInteroperabilityEndpoint extends BaseInteroperabilityEndpoint {
public getMainchainID(context: ModuleEndpointContext): { mainchainID: string } {
validator.validate(isChainIDAvailableRequestSchema, context.params);

export class SidechainInteroperabilityEndpoint extends BaseInteroperabilityEndpoint {}
const chainID = Buffer.from(context.params.chainID as string, 'hex');
return { mainchainID: getMainchainID(chainID).toString('hex') };
}
}
7 changes: 7 additions & 0 deletions framework/src/modules/interoperability/sidechain/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
getChannelRequestSchema,
getTerminatedOutboxAccountRequestSchema,
getTerminatedStateAccountRequestSchema,
getMainchainIDRequestSchema,
getMainchainIDResponseSchema,
} from '../schemas';
import { allChainAccountsSchema, chainDataSchema, ChainStatus } from '../stores/chain_account';
import { channelSchema } from '../stores/channel_data';
Expand Down Expand Up @@ -194,6 +196,11 @@ export class SidechainInteroperabilityModule extends BaseInteroperabilityModule
request: getChainValidatorsRequestSchema,
response: getChainValidatorsResponseSchema,
},
{
name: this.endpoint.getMainchainID.name,
request: getMainchainIDRequestSchema,
response: getMainchainIDResponseSchema,
},
],
assets: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright © 2023 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/

import { createTransientModuleEndpointContext } from '../../../../../src/testing';
import { SidechainInteroperabilityEndpoint } from '../../../../../src/modules/interoperability/sidechain/endpoint';
import { ModuleEndpointContext } from '../../../../../src';

describe('SidechainInteroperabilityEndpoint', () => {
let endpoint: SidechainInteroperabilityEndpoint;

const storesMock = {};
const offchainStoresMock = {};

beforeEach(() => {
endpoint = new SidechainInteroperabilityEndpoint(storesMock as any, offchainStoresMock as any);
});

describe('getMainchainID', () => {
let context: ModuleEndpointContext;

it('should throw error for chainID having less than 8 chars', () => {
context = createTransientModuleEndpointContext({
params: {
chainID: '1234',
},
});

// https://stackoverflow.com/questions/47397208/error-is-thrown-but-jests-tothrow-does-not-capture-the-error
expect(() => endpoint.getMainchainID(context)).toThrow(
"Property '.chainID' must NOT have fewer than 8 characters",
);
});

it('should throw error for chainID having more than 8 chars', () => {
context = createTransientModuleEndpointContext({
params: {
chainID: '123456789',
},
});

expect(() => endpoint.getMainchainID(context)).toThrow(
"Property '.chainID' must NOT have more than 8 characters",
);
});

it('should return 00000000 for chainID 00123456', () => {
context = createTransientModuleEndpointContext({
params: {
chainID: '00123456',
},
});

const { mainchainID } = endpoint.getMainchainID(context);
expect(mainchainID).toBe('00000000');
});

it('should return 02000000 for chainID 02345678', () => {
context = createTransientModuleEndpointContext({
params: {
chainID: '02345678',
},
});

const { mainchainID } = endpoint.getMainchainID(context);
expect(mainchainID).toBe('02000000');
});
});
});