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

Commit

Permalink
Update interop example (#9144)
Browse files Browse the repository at this point in the history
* Update interop example

* Update schema id

* Refactor some sections

* Adjust naming

* Add interop setup diagram reference

* Fix command example

---------

Co-authored-by: Khalid Hameed <sitetester2010@gmail.com>
Co-authored-by: !shan <ishantiw.quasar@gmail.com>
  • Loading branch information
3 people authored Dec 5, 2023
1 parent 733e028 commit 59007d4
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 84 deletions.
13 changes: 10 additions & 3 deletions examples/interop/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -21,36 +22,49 @@ export class ReactCCCommand extends BaseCCCommand {
throw new Error('Invalid CCM status code.');
}

const params = codec.decode<CCReactMessageParams>(CCReactMessageParamsSchema, ccm.params);
const ccReactMessageParams = codec.decode<CCReactMessageParams>(
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.');
}
}

public async execute(ctx: CrossChainMessageContext): Promise<void> {
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<CCReactMessageParams>(CCReactMessageParamsSchema, ccm.params);
logger.info(params, 'parameters');
const ccReactMessageParams = codec.decode<CCReactMessageParams>(
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(
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -115,7 +116,7 @@ export class HelloModule extends BaseInteroperableModule {
public async verifyTransaction(_context: TransactionVerifyContext): Promise<VerificationResult> {
// verify transaction will be called multiple times in the transaction pool
const result = {
status: 1,
status: VerifyStatus.OK,
};
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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'],
Expand Down Expand Up @@ -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: {
Expand All @@ -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: {
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,17 @@ export interface ModuleConfig {
}

export type ModuleConfigJSON = JSONObject<ModuleConfig>;

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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -44,6 +44,7 @@ export class ReactCrossChainCommand extends BaseCommand {
error: err as Error,
};
}

return {
status: VerifyStatus.OK,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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',
Expand All @@ -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,
Expand Down

0 comments on commit 59007d4

Please sign in to comment.