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

Commit 59007d4

Browse files
Tschakkisitetesterishantiw
authored
Update interop example (#9144)
* Update interop example * Update schema id * Refactor some sections * Adjust naming * Add interop setup diagram reference * Fix command example --------- Co-authored-by: Khalid Hameed <[email protected]> Co-authored-by: !shan <[email protected]>
1 parent 733e028 commit 59007d4

File tree

9 files changed

+94
-84
lines changed

9 files changed

+94
-84
lines changed

Diff for: examples/interop/README.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Run below command inside each application folder.
116116

117117
##### Transfer sidechain two to sidechain one
118118

119-
- Run `ts-node pos-sidechain-example-one/config/scripts/transfer_sidechain_one.ts` from `interop` folder.
119+
- Run `ts-node pos-sidechain-example-two/config/scripts/transfer_sidechain_one.ts` from `interop` folder.
120120
- Check balance for `lskxvesvwgxpdnhp4rdukmsx42teehpxkeod7xv7f` using `token_getBalances` RPC on sidechain one.
121121

122122
##### Transfer sidechain one to sidechain two
@@ -137,6 +137,13 @@ Run below command inside each application folder.
137137

138138
#### Other options
139139

140-
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:
140+
There are `genesis_assets_103_validators.json` file under config folder of each app. You can also use this
141+
genesis-assets if you want to run an application for 103 validators. In order to do so follow these steps:
141142

142-
- Update genesis block using command: `./bin/run genesis-block:create --output config/default/ --assets-file config/default/genesis_assets_103_validators.json`
143+
- Update genesis block using
144+
command: `./bin/run genesis-block:create --output config/default/ --assets-file config/default/genesis_assets_103_validators.json`
145+
146+
## Learn More
147+
148+
[Here](https://github.com/LiskHQ/lisk-docs/blob/7a7c1606c688f8cd91b50d0ddc199907c6b4f759/docs/modules/ROOT/images/build-blockchain/interop-example.png)
149+
is reference to diagram explaining interop setup nicely.

Diff for: examples/interop/pos-sidechain-example-one/src/app/modules/hello/cc_commands/react_cc_command.ts

+30-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* eslint-disable class-methods-use-this */
22

33
import { BaseCCCommand, CrossChainMessageContext, codec, cryptography, db } from 'lisk-sdk';
4-
import { CCReactMessageParamsSchema, CCReactMessageParams } from '../schemas';
4+
import { CCReactMessageParamsSchema } from '../schemas';
5+
import { CCReactMessageParams } from '../types';
56
import { MAX_RESERVED_ERROR_STATUS, CROSS_CHAIN_COMMAND_REACT } from '../constants';
67
import { ReactionStore, ReactionStoreData } from '../stores/reaction';
78
import { MessageStore } from '../stores/message';
@@ -21,36 +22,49 @@ export class ReactCCCommand extends BaseCCCommand {
2122
throw new Error('Invalid CCM status code.');
2223
}
2324

24-
const params = codec.decode<CCReactMessageParams>(CCReactMessageParamsSchema, ccm.params);
25+
const ccReactMessageParams = codec.decode<CCReactMessageParams>(
26+
CCReactMessageParamsSchema,
27+
ccm.params,
28+
);
2529
const messageCreatorAddress = cryptography.address.getAddressFromLisk32Address(
26-
params.helloMessageID,
30+
ccReactMessageParams.helloMessageID,
2731
);
2832
if (!(await this.stores.get(MessageStore).has(ctx, messageCreatorAddress))) {
2933
throw new Error('Message ID does not exists.');
3034
}
3135
}
3236

3337
public async execute(ctx: CrossChainMessageContext): Promise<void> {
34-
const { ccm, logger } = ctx;
38+
const { ccm, logger, transaction } = ctx;
3539
logger.info('Executing React CCM');
36-
// const { sendingChainID, status, receivingChainID } = ccm;
40+
3741
// Decode the provided CCM parameters
38-
const params = codec.decode<CCReactMessageParams>(CCReactMessageParamsSchema, ccm.params);
39-
logger.info(params, 'parameters');
42+
const ccReactMessageParams = codec.decode<CCReactMessageParams>(
43+
CCReactMessageParamsSchema,
44+
ccm.params,
45+
);
46+
logger.info(ccReactMessageParams, 'parameters');
47+
4048
// Get helloMessageID and reactionType from the parameters
41-
const { helloMessageID, reactionType } = params;
42-
const { senderAddress } = ctx.transaction;
49+
const { helloMessageID, reactionType } = ccReactMessageParams;
50+
const { senderAddress } = transaction;
4351
const reactionSubstore = this.stores.get(ReactionStore);
44-
const messageCreatorAddress = cryptography.address.getAddressFromLisk32Address(helloMessageID);
45-
let msgReactions: ReactionStoreData;
52+
const msgCreatorAddress = cryptography.address.getAddressFromLisk32Address(helloMessageID);
4653

54+
let msgReactions: ReactionStoreData;
4755
// Get existing reactions for a Hello message, or initialize an empty reaction object, if none exists,yet.
4856
try {
49-
msgReactions = await reactionSubstore.get(ctx, messageCreatorAddress);
57+
msgReactions = await reactionSubstore.get(ctx, msgCreatorAddress);
5058
} catch (error) {
5159
if (!(error instanceof db.NotFoundError)) {
52-
logger.info({ helloMessageID, crossChainCommand: this.name }, (error as Error).message);
53-
logger.error({ error }, 'Error when getting the reaction substore');
60+
logger.error(
61+
{
62+
helloMessageID,
63+
crossChainCommand: this.name,
64+
error,
65+
},
66+
'Error when getting the reaction substore',
67+
);
5468
throw error;
5569
}
5670
logger.info(
@@ -76,8 +90,9 @@ export class ReactCCCommand extends BaseCCCommand {
7690
} else {
7791
logger.error({ reactionType }, 'invalid reaction type');
7892
}
93+
7994
msgReactions.reactions.likes = likes;
8095
// Update the reaction store with the reactions for the specified Hello message
81-
await reactionSubstore.set(ctx, messageCreatorAddress, msgReactions);
96+
await reactionSubstore.set(ctx, msgCreatorAddress, msgReactions);
8297
}
8398
}

Diff for: examples/interop/pos-sidechain-example-one/src/app/modules/hello/endpoint.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export class HelloEndpoint extends BaseEndpoint {
1919
if (typeof address !== 'string') {
2020
throw new Error('Parameter address must be a string.');
2121
}
22-
cryptography.address.validateLisk32Address(address);
2322

2423
const reactions = await reactionSubStore.get(
2524
ctx,
@@ -36,7 +35,7 @@ export class HelloEndpoint extends BaseEndpoint {
3635
if (typeof address !== 'string') {
3736
throw new Error('Parameter address must be a string.');
3837
}
39-
cryptography.address.validateLisk32Address(address);
38+
4039
const helloMessage = await messageSubStore.get(
4140
ctx,
4241
cryptography.address.getAddressFromLisk32Address(address),

Diff for: examples/interop/pos-sidechain-example-one/src/app/modules/hello/module.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
TransactionVerifyContext,
1515
utils,
1616
VerificationResult,
17+
VerifyStatus,
1718
} from 'lisk-sdk';
1819
import { CreateHelloCommand } from './commands/create_hello_command';
1920
import { ReactCCCommand } from './cc_commands/react_cc_command';
@@ -115,7 +116,7 @@ export class HelloModule extends BaseInteroperableModule {
115116
public async verifyTransaction(_context: TransactionVerifyContext): Promise<VerificationResult> {
116117
// verify transaction will be called multiple times in the transaction pool
117118
const result = {
118-
status: 1,
119+
status: VerifyStatus.OK,
119120
};
120121
return result;
121122
}

Diff for: examples/interop/pos-sidechain-example-one/src/app/modules/hello/schemas.ts

+8-30
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
export interface CreateHelloParams {
2-
message: string;
3-
}
4-
51
export const createHelloSchema = {
6-
$id: 'hello/createHello-params',
2+
$id: 'hello/createHello',
73
title: 'CreateHelloCommand transaction parameter for the Hello module',
84
type: 'object',
95
required: ['message'],
@@ -42,7 +38,7 @@ export const configSchema = {
4238
};
4339

4440
export const getHelloCounterResponseSchema = {
45-
$id: 'modules/hello/endpoint/getHelloCounter',
41+
$id: 'modules/hello/endpoint/getHelloCounterResponse',
4642
type: 'object',
4743
required: ['counter'],
4844
properties: {
@@ -54,7 +50,7 @@ export const getHelloCounterResponseSchema = {
5450
};
5551

5652
export const getHelloResponseSchema = {
57-
$id: 'modules/hello/endpoint/getHello',
53+
$id: 'modules/hello/endpoint/getHelloResponse',
5854
type: 'object',
5955
required: ['message'],
6056
properties: {
@@ -77,32 +73,14 @@ export const getHelloRequestSchema = {
7773
},
7874
};
7975

80-
/**
81-
* Parameters of the reactCrossChain CCM
82-
*/
83-
export interface CCReactMessageParams {
84-
/**
85-
* A number indicating the type of the reaction.
86-
*/
87-
reactionType: number;
88-
/**
89-
* ID of the message.
90-
*/
91-
helloMessageID: string;
92-
/** Optional field for data / messages. */
93-
data: string;
94-
}
95-
96-
/**
97-
* Schema for the parameters of the reactCrossChain CCM
98-
*/
76+
// Schema for the parameters of the crossChainReact CCM
9977
export const CCReactMessageParamsSchema = {
100-
/** The unique identifier of the schema. */
101-
$id: '/lisk/hello/ccReactParams',
78+
// The unique identifier of the schema.
79+
$id: '/lisk/hello/ccReactMessageParams',
10280
type: 'object',
103-
/** The required parameters for the command. */
81+
// The required parameters for the CCM.
10482
required: ['reactionType', 'helloMessageID', 'data'],
105-
/** A list describing the available parameters for the command. */
83+
// A list describing the required parameters for the CCM.
10684
properties: {
10785
reactionType: {
10886
dataType: 'uint32',

Diff for: examples/interop/pos-sidechain-example-one/src/app/modules/hello/types.ts

+14
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,17 @@ export interface ModuleConfig {
77
}
88

99
export type ModuleConfigJSON = JSONObject<ModuleConfig>;
10+
11+
export interface CreateHelloParams {
12+
message: string;
13+
}
14+
15+
// Parameters of the reactCrossChain CCM
16+
export interface CCReactMessageParams {
17+
// A number indicating the type of the reaction.
18+
reactionType: number;
19+
// ID of the Hello message being reacted to.
20+
helloMessageID: string;
21+
// Optional field for data / messages.
22+
data: string;
23+
}

Diff for: examples/interop/pos-sidechain-example-two/src/app/modules/react/commands/react_cc_command.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { CROSS_CHAIN_COMMAND_REACT } from '../constants';
1212
import { CCReactCommandParamsSchema, CCReactMessageParamsSchema } from '../schemas';
1313
import { CCReactMessageParams, CCReactCommandParams, InteroperabilityMethod } from '../types';
1414

15-
export class ReactCrossChainCommand extends BaseCommand {
15+
export class CrossChainReactCommand extends BaseCommand {
1616
private _interoperabilityMethod!: InteroperabilityMethod;
1717
public schema = CCReactCommandParamsSchema;
1818

@@ -44,6 +44,7 @@ export class ReactCrossChainCommand extends BaseCommand {
4444
error: err as Error,
4545
};
4646
}
47+
4748
return {
4849
status: VerifyStatus.OK,
4950
};

Diff for: examples/interop/pos-sidechain-example-two/src/app/modules/react/module.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* eslint-disable @typescript-eslint/member-ordering */
33

44
import { BaseInteroperableModule, ModuleMetadata, ModuleInitArgs } from 'lisk-sdk';
5-
import { ReactCrossChainCommand } from './commands/react_cc_command';
5+
import { CrossChainReactCommand } from './commands/react_cc_command';
66
import { ReactEndpoint } from './endpoint';
77
import { ReactMethod } from './method';
88
import { ReactInteroperableMethod } from './cc_method';
@@ -11,7 +11,7 @@ import { InteroperabilityMethod } from './types';
1111
export class ReactModule extends BaseInteroperableModule {
1212
public endpoint = new ReactEndpoint(this.stores, this.offchainStores);
1313
public method = new ReactMethod(this.stores, this.events);
14-
public commands = [new ReactCrossChainCommand(this.stores, this.events)];
14+
public commands = [new CrossChainReactCommand(this.stores, this.events)];
1515
private _interoperabilityMethod!: InteroperabilityMethod;
1616

1717
public crossChainMethod = new ReactInteroperableMethod(this.stores, this.events);

Diff for: examples/interop/pos-sidechain-example-two/src/app/modules/react/schemas.ts

+25-30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1-
// Schema for the parameters of the reactCrossChain CCM
1+
const reactionType = {
2+
dataType: 'uint32',
3+
fieldNumber: 1,
4+
};
5+
6+
const helloMessageID = {
7+
dataType: 'string',
8+
fieldNumber: 2,
9+
};
10+
11+
const data = {
12+
dataType: 'string',
13+
fieldNumber: 3,
14+
minLength: 0,
15+
maxLength: 64,
16+
};
17+
18+
// Schema for the parameters of the crossChainReact CCM
219
export const CCReactMessageParamsSchema = {
320
// The unique identifier of the schema.
421
$id: '/lisk/react/ccReactMessageParams',
@@ -7,24 +24,13 @@ export const CCReactMessageParamsSchema = {
724
required: ['reactionType', 'helloMessageID', 'data'],
825
// A list describing the required parameters for the CCM.
926
properties: {
10-
reactionType: {
11-
dataType: 'uint32',
12-
fieldNumber: 1,
13-
},
14-
helloMessageID: {
15-
dataType: 'string',
16-
fieldNumber: 2,
17-
},
18-
data: {
19-
dataType: 'string',
20-
fieldNumber: 3,
21-
minLength: 0,
22-
maxLength: 64,
23-
},
27+
reactionType,
28+
helloMessageID,
29+
data,
2430
},
2531
};
2632

27-
// Schema for the parameters of the react reactCrossChain command
33+
// Schema for the parameters of the react crossChainReact command
2834
export const CCReactCommandParamsSchema = {
2935
// The unique identifier of the schema.
3036
$id: '/lisk/react/ccReactCommandParams',
@@ -33,20 +39,9 @@ export const CCReactCommandParamsSchema = {
3339
required: ['reactionType', 'helloMessageID', 'receivingChainID', 'data', 'messageFee'],
3440
// A list describing the available parameters for the command.
3541
properties: {
36-
reactionType: {
37-
dataType: 'uint32',
38-
fieldNumber: 1,
39-
},
40-
helloMessageID: {
41-
dataType: 'string',
42-
fieldNumber: 2,
43-
},
44-
data: {
45-
dataType: 'string',
46-
fieldNumber: 3,
47-
minLength: 0,
48-
maxLength: 64,
49-
},
42+
reactionType,
43+
helloMessageID,
44+
data,
5045
receivingChainID: {
5146
dataType: 'bytes',
5247
fieldNumber: 4,

0 commit comments

Comments
 (0)