diff --git a/examples/interop/README.md b/examples/interop/README.md index 61750cf20e..4f776fab4e 100644 --- a/examples/interop/README.md +++ b/examples/interop/README.md @@ -116,7 +116,7 @@ Run below command inside each application folder. ##### Transfer sidechain two to sidechain one -- Run `ts-node pos-sidechain-example-one/config/scripts/transfer_sidechain_one.ts` from `interop` folder. +- Run `ts-node pos-sidechain-example-two/config/scripts/transfer_sidechain_one.ts` from `interop` folder. - Check balance for `lskxvesvwgxpdnhp4rdukmsx42teehpxkeod7xv7f` using `token_getBalances` RPC on sidechain one. ##### Transfer sidechain one to sidechain two @@ -137,6 +137,13 @@ Run below command inside each application folder. #### Other options -There are `genesis_assets_103_validators.json` file under config folder of each app. You can also use this genesis-assets if you want to run an application for 103 validators. In order to do so follow these steps: +There are `genesis_assets_103_validators.json` file under config folder of each app. You can also use this +genesis-assets if you want to run an application for 103 validators. In order to do so follow these steps: -- Update genesis block using command: `./bin/run genesis-block:create --output config/default/ --assets-file config/default/genesis_assets_103_validators.json` +- Update genesis block using + command: `./bin/run genesis-block:create --output config/default/ --assets-file config/default/genesis_assets_103_validators.json` + +## Learn More + +[Here](https://github.com/LiskHQ/lisk-docs/blob/7a7c1606c688f8cd91b50d0ddc199907c6b4f759/docs/modules/ROOT/images/build-blockchain/interop-example.png) +is reference to diagram explaining interop setup nicely. diff --git a/examples/interop/pos-sidechain-example-one/src/app/modules/hello/cc_commands/react_cc_command.ts b/examples/interop/pos-sidechain-example-one/src/app/modules/hello/cc_commands/react_cc_command.ts index 58b4545d87..d7f0531e39 100644 --- a/examples/interop/pos-sidechain-example-one/src/app/modules/hello/cc_commands/react_cc_command.ts +++ b/examples/interop/pos-sidechain-example-one/src/app/modules/hello/cc_commands/react_cc_command.ts @@ -1,7 +1,8 @@ /* eslint-disable class-methods-use-this */ import { BaseCCCommand, CrossChainMessageContext, codec, cryptography, db } from 'lisk-sdk'; -import { CCReactMessageParamsSchema, CCReactMessageParams } from '../schemas'; +import { CCReactMessageParamsSchema } from '../schemas'; +import { CCReactMessageParams } from '../types'; import { MAX_RESERVED_ERROR_STATUS, CROSS_CHAIN_COMMAND_REACT } from '../constants'; import { ReactionStore, ReactionStoreData } from '../stores/reaction'; import { MessageStore } from '../stores/message'; @@ -21,9 +22,12 @@ export class ReactCCCommand extends BaseCCCommand { throw new Error('Invalid CCM status code.'); } - const params = codec.decode(CCReactMessageParamsSchema, ccm.params); + const ccReactMessageParams = codec.decode( + CCReactMessageParamsSchema, + ccm.params, + ); const messageCreatorAddress = cryptography.address.getAddressFromLisk32Address( - params.helloMessageID, + ccReactMessageParams.helloMessageID, ); if (!(await this.stores.get(MessageStore).has(ctx, messageCreatorAddress))) { throw new Error('Message ID does not exists.'); @@ -31,26 +35,36 @@ export class ReactCCCommand extends BaseCCCommand { } public async execute(ctx: CrossChainMessageContext): Promise { - const { ccm, logger } = ctx; + const { ccm, logger, transaction } = ctx; logger.info('Executing React CCM'); - // const { sendingChainID, status, receivingChainID } = ccm; + // Decode the provided CCM parameters - const params = codec.decode(CCReactMessageParamsSchema, ccm.params); - logger.info(params, 'parameters'); + const ccReactMessageParams = codec.decode( + CCReactMessageParamsSchema, + ccm.params, + ); + logger.info(ccReactMessageParams, 'parameters'); + // Get helloMessageID and reactionType from the parameters - const { helloMessageID, reactionType } = params; - const { senderAddress } = ctx.transaction; + const { helloMessageID, reactionType } = ccReactMessageParams; + const { senderAddress } = transaction; const reactionSubstore = this.stores.get(ReactionStore); - const messageCreatorAddress = cryptography.address.getAddressFromLisk32Address(helloMessageID); - let msgReactions: ReactionStoreData; + const msgCreatorAddress = cryptography.address.getAddressFromLisk32Address(helloMessageID); + let msgReactions: ReactionStoreData; // Get existing reactions for a Hello message, or initialize an empty reaction object, if none exists,yet. try { - msgReactions = await reactionSubstore.get(ctx, messageCreatorAddress); + msgReactions = await reactionSubstore.get(ctx, msgCreatorAddress); } catch (error) { if (!(error instanceof db.NotFoundError)) { - logger.info({ helloMessageID, crossChainCommand: this.name }, (error as Error).message); - logger.error({ error }, 'Error when getting the reaction substore'); + logger.error( + { + helloMessageID, + crossChainCommand: this.name, + error, + }, + 'Error when getting the reaction substore', + ); throw error; } logger.info( @@ -76,8 +90,9 @@ export class ReactCCCommand extends BaseCCCommand { } else { logger.error({ reactionType }, 'invalid reaction type'); } + msgReactions.reactions.likes = likes; // Update the reaction store with the reactions for the specified Hello message - await reactionSubstore.set(ctx, messageCreatorAddress, msgReactions); + await reactionSubstore.set(ctx, msgCreatorAddress, msgReactions); } } diff --git a/examples/interop/pos-sidechain-example-one/src/app/modules/hello/endpoint.ts b/examples/interop/pos-sidechain-example-one/src/app/modules/hello/endpoint.ts index 4db39083be..af5cb5aa6b 100644 --- a/examples/interop/pos-sidechain-example-one/src/app/modules/hello/endpoint.ts +++ b/examples/interop/pos-sidechain-example-one/src/app/modules/hello/endpoint.ts @@ -19,7 +19,6 @@ export class HelloEndpoint extends BaseEndpoint { if (typeof address !== 'string') { throw new Error('Parameter address must be a string.'); } - cryptography.address.validateLisk32Address(address); const reactions = await reactionSubStore.get( ctx, @@ -36,7 +35,7 @@ export class HelloEndpoint extends BaseEndpoint { if (typeof address !== 'string') { throw new Error('Parameter address must be a string.'); } - cryptography.address.validateLisk32Address(address); + const helloMessage = await messageSubStore.get( ctx, cryptography.address.getAddressFromLisk32Address(address), diff --git a/examples/interop/pos-sidechain-example-one/src/app/modules/hello/module.ts b/examples/interop/pos-sidechain-example-one/src/app/modules/hello/module.ts index 7473a096b6..9c0219774d 100644 --- a/examples/interop/pos-sidechain-example-one/src/app/modules/hello/module.ts +++ b/examples/interop/pos-sidechain-example-one/src/app/modules/hello/module.ts @@ -14,6 +14,7 @@ import { TransactionVerifyContext, utils, VerificationResult, + VerifyStatus, } from 'lisk-sdk'; import { CreateHelloCommand } from './commands/create_hello_command'; import { ReactCCCommand } from './cc_commands/react_cc_command'; @@ -115,7 +116,7 @@ export class HelloModule extends BaseInteroperableModule { public async verifyTransaction(_context: TransactionVerifyContext): Promise { // verify transaction will be called multiple times in the transaction pool const result = { - status: 1, + status: VerifyStatus.OK, }; return result; } diff --git a/examples/interop/pos-sidechain-example-one/src/app/modules/hello/schemas.ts b/examples/interop/pos-sidechain-example-one/src/app/modules/hello/schemas.ts index d52b87de21..87e57ec0e7 100644 --- a/examples/interop/pos-sidechain-example-one/src/app/modules/hello/schemas.ts +++ b/examples/interop/pos-sidechain-example-one/src/app/modules/hello/schemas.ts @@ -1,9 +1,5 @@ -export interface CreateHelloParams { - message: string; -} - export const createHelloSchema = { - $id: 'hello/createHello-params', + $id: 'hello/createHello', title: 'CreateHelloCommand transaction parameter for the Hello module', type: 'object', required: ['message'], @@ -42,7 +38,7 @@ export const configSchema = { }; export const getHelloCounterResponseSchema = { - $id: 'modules/hello/endpoint/getHelloCounter', + $id: 'modules/hello/endpoint/getHelloCounterResponse', type: 'object', required: ['counter'], properties: { @@ -54,7 +50,7 @@ export const getHelloCounterResponseSchema = { }; export const getHelloResponseSchema = { - $id: 'modules/hello/endpoint/getHello', + $id: 'modules/hello/endpoint/getHelloResponse', type: 'object', required: ['message'], properties: { @@ -77,32 +73,14 @@ export const getHelloRequestSchema = { }, }; -/** - * Parameters of the reactCrossChain CCM - */ -export interface CCReactMessageParams { - /** - * A number indicating the type of the reaction. - */ - reactionType: number; - /** - * ID of the message. - */ - helloMessageID: string; - /** Optional field for data / messages. */ - data: string; -} - -/** - * Schema for the parameters of the reactCrossChain CCM - */ +// Schema for the parameters of the crossChainReact CCM export const CCReactMessageParamsSchema = { - /** The unique identifier of the schema. */ - $id: '/lisk/hello/ccReactParams', + // The unique identifier of the schema. + $id: '/lisk/hello/ccReactMessageParams', type: 'object', - /** The required parameters for the command. */ + // The required parameters for the CCM. required: ['reactionType', 'helloMessageID', 'data'], - /** A list describing the available parameters for the command. */ + // A list describing the required parameters for the CCM. properties: { reactionType: { dataType: 'uint32', diff --git a/examples/interop/pos-sidechain-example-one/src/app/modules/hello/types.ts b/examples/interop/pos-sidechain-example-one/src/app/modules/hello/types.ts index d1c1ddc9f3..20b25cc1ba 100644 --- a/examples/interop/pos-sidechain-example-one/src/app/modules/hello/types.ts +++ b/examples/interop/pos-sidechain-example-one/src/app/modules/hello/types.ts @@ -7,3 +7,17 @@ export interface ModuleConfig { } export type ModuleConfigJSON = JSONObject; + +export interface CreateHelloParams { + message: string; +} + +// Parameters of the reactCrossChain CCM +export interface CCReactMessageParams { + // A number indicating the type of the reaction. + reactionType: number; + // ID of the Hello message being reacted to. + helloMessageID: string; + // Optional field for data / messages. + data: string; +} diff --git a/examples/interop/pos-sidechain-example-two/src/app/modules/react/commands/react_cc_command.ts b/examples/interop/pos-sidechain-example-two/src/app/modules/react/commands/react_cc_command.ts index 8cbf9bf99e..08c178ed89 100644 --- a/examples/interop/pos-sidechain-example-two/src/app/modules/react/commands/react_cc_command.ts +++ b/examples/interop/pos-sidechain-example-two/src/app/modules/react/commands/react_cc_command.ts @@ -12,7 +12,7 @@ import { CROSS_CHAIN_COMMAND_REACT } from '../constants'; import { CCReactCommandParamsSchema, CCReactMessageParamsSchema } from '../schemas'; import { CCReactMessageParams, CCReactCommandParams, InteroperabilityMethod } from '../types'; -export class ReactCrossChainCommand extends BaseCommand { +export class CrossChainReactCommand extends BaseCommand { private _interoperabilityMethod!: InteroperabilityMethod; public schema = CCReactCommandParamsSchema; @@ -44,6 +44,7 @@ export class ReactCrossChainCommand extends BaseCommand { error: err as Error, }; } + return { status: VerifyStatus.OK, }; diff --git a/examples/interop/pos-sidechain-example-two/src/app/modules/react/module.ts b/examples/interop/pos-sidechain-example-two/src/app/modules/react/module.ts index f49d2451c8..4e30f4d521 100644 --- a/examples/interop/pos-sidechain-example-two/src/app/modules/react/module.ts +++ b/examples/interop/pos-sidechain-example-two/src/app/modules/react/module.ts @@ -2,7 +2,7 @@ /* eslint-disable @typescript-eslint/member-ordering */ import { BaseInteroperableModule, ModuleMetadata, ModuleInitArgs } from 'lisk-sdk'; -import { ReactCrossChainCommand } from './commands/react_cc_command'; +import { CrossChainReactCommand } from './commands/react_cc_command'; import { ReactEndpoint } from './endpoint'; import { ReactMethod } from './method'; import { ReactInteroperableMethod } from './cc_method'; @@ -11,7 +11,7 @@ import { InteroperabilityMethod } from './types'; export class ReactModule extends BaseInteroperableModule { public endpoint = new ReactEndpoint(this.stores, this.offchainStores); public method = new ReactMethod(this.stores, this.events); - public commands = [new ReactCrossChainCommand(this.stores, this.events)]; + public commands = [new CrossChainReactCommand(this.stores, this.events)]; private _interoperabilityMethod!: InteroperabilityMethod; public crossChainMethod = new ReactInteroperableMethod(this.stores, this.events); diff --git a/examples/interop/pos-sidechain-example-two/src/app/modules/react/schemas.ts b/examples/interop/pos-sidechain-example-two/src/app/modules/react/schemas.ts index 1bfa4e87df..ced05eb7aa 100644 --- a/examples/interop/pos-sidechain-example-two/src/app/modules/react/schemas.ts +++ b/examples/interop/pos-sidechain-example-two/src/app/modules/react/schemas.ts @@ -1,4 +1,21 @@ -// Schema for the parameters of the reactCrossChain CCM +const reactionType = { + dataType: 'uint32', + fieldNumber: 1, +}; + +const helloMessageID = { + dataType: 'string', + fieldNumber: 2, +}; + +const data = { + dataType: 'string', + fieldNumber: 3, + minLength: 0, + maxLength: 64, +}; + +// Schema for the parameters of the crossChainReact CCM export const CCReactMessageParamsSchema = { // The unique identifier of the schema. $id: '/lisk/react/ccReactMessageParams', @@ -7,24 +24,13 @@ export const CCReactMessageParamsSchema = { required: ['reactionType', 'helloMessageID', 'data'], // A list describing the required parameters for the CCM. properties: { - reactionType: { - dataType: 'uint32', - fieldNumber: 1, - }, - helloMessageID: { - dataType: 'string', - fieldNumber: 2, - }, - data: { - dataType: 'string', - fieldNumber: 3, - minLength: 0, - maxLength: 64, - }, + reactionType, + helloMessageID, + data, }, }; -// Schema for the parameters of the react reactCrossChain command +// Schema for the parameters of the react crossChainReact command export const CCReactCommandParamsSchema = { // The unique identifier of the schema. $id: '/lisk/react/ccReactCommandParams', @@ -33,20 +39,9 @@ export const CCReactCommandParamsSchema = { required: ['reactionType', 'helloMessageID', 'receivingChainID', 'data', 'messageFee'], // A list describing the available parameters for the command. properties: { - reactionType: { - dataType: 'uint32', - fieldNumber: 1, - }, - helloMessageID: { - dataType: 'string', - fieldNumber: 2, - }, - data: { - dataType: 'string', - fieldNumber: 3, - minLength: 0, - maxLength: 64, - }, + reactionType, + helloMessageID, + data, receivingChainID: { dataType: 'bytes', fieldNumber: 4,