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

Commit

Permalink
Merge branch 'release/6.1.0' into 8193-unit-test-review-interop-ccu-c…
Browse files Browse the repository at this point in the history
…ommands
  • Loading branch information
ishantiw authored Nov 23, 2023
2 parents f1ed310 + 799585e commit 28ec134
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import { BaseCCCommand, CrossChainMessageContext, codec, cryptography, db } from 'lisk-sdk';
import { CCReactMessageParamsSchema, CCReactMessageParams } from '../schemas';
import { MAX_RESERVED_ERROR_STATUS, CROSS_CHAIN_COMMAND_NAME_REACT } from '../constants';
import { MAX_RESERVED_ERROR_STATUS, CROSS_CHAIN_COMMAND_REACT } from '../constants';
import { ReactionStore, ReactionStoreData } from '../stores/reaction';
import { MessageStore } from '../stores/message';

export class ReactCCCommand extends BaseCCCommand {
public schema = CCReactMessageParamsSchema;

public get name(): string {
return CROSS_CHAIN_COMMAND_NAME_REACT;
return CROSS_CHAIN_COMMAND_REACT;
}

// eslint-disable-next-line @typescript-eslint/require-await
Expand Down Expand Up @@ -39,6 +39,7 @@ export class ReactCCCommand extends BaseCCCommand {
logger.info(params, 'parameters');
// Get helloMessageID and reactionType from the parameters
const { helloMessageID, reactionType } = params;
const { senderAddress } = ctx.transaction;
const reactionSubstore = this.stores.get(ReactionStore);
const messageCreatorAddress = cryptography.address.getAddressFromLisk32Address(helloMessageID);
let msgReactions: ReactionStoreData;
Expand All @@ -56,24 +57,26 @@ export class ReactCCCommand extends BaseCCCommand {
{ helloMessageID, crossChainCommand: this.name },
`No entry exists for given helloMessageID ${helloMessageID}. Creating a default entry.`,
);
msgReactions = { reactions: { like: [] } };
msgReactions = { reactions: { likes: [] } };
}

let { likes } = msgReactions.reactions;
// Check if the reactions is a like
if (reactionType === 0) {
const hasLiked = msgReactions.reactions.like.indexOf(ctx.transaction.senderAddress);
const likedPos = likes.indexOf(senderAddress);
// If the sender has already liked the message
if (hasLiked > -1) {
if (likedPos > -1) {
// Remove the sender address from the likes for the message
msgReactions.reactions.like = msgReactions.reactions.like.splice(hasLiked, 1);
likes = likes.splice(likedPos, 1);
// If the sender has not liked the message yet
} else {
// Add the sender address to the likes of the message
msgReactions.reactions.like.push(ctx.transaction.senderAddress);
likes.push(senderAddress);
}
} 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const CROSS_CHAIN_COMMAND_NAME_REACT = 'reactCrossChain';
export const CROSS_CHAIN_COMMAND_REACT = 'crossChainReact';
export const MAX_RESERVED_ERROR_STATUS = 63;
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
VerificationResult,
} from 'lisk-sdk';
import { CreateHelloCommand } from './commands/create_hello_command';
import { ReactCCCommand } from './cc_commands/react_command';
import { ReactCCCommand } from './cc_commands/react_cc_command';
import { HelloEndpoint } from './endpoint';
import { NewHelloEvent } from './events/new_hello';
import { HelloMethod } from './method';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BaseStore } from 'lisk-sdk';

export interface ReactionStoreData {
reactions: {
like: Buffer[];
likes: Buffer[];
};
}

Expand All @@ -15,7 +15,7 @@ export const reactionStoreSchema = {
type: 'object',
fieldNumber: 1,
properties: {
like: {
likes: {
type: 'array',
fieldNumber: 1,
items: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,16 @@ import {
VerifyStatus,
codec,
} from 'lisk-sdk';
import { CROSS_CHAIN_COMMAND_NAME_REACT } from '../constants';
import {
CCReactCommandParamsSchema,
CCReactMessageParams,
CCReactMessageParamsSchema,
CCReactCommandParams,
} from '../schemas';
import { InteroperabilityMethod } from '../types';
import { CROSS_CHAIN_COMMAND_REACT } from '../constants';
import { CCReactCommandParamsSchema, CCReactMessageParamsSchema } from '../schemas';
import { CCReactMessageParams, CCReactCommandParams, InteroperabilityMethod } from '../types';

export class ReactCrossChainCommand extends BaseCommand {
private _interoperabilityMethod!: InteroperabilityMethod;
public schema = CCReactCommandParamsSchema;

public get name(): string {
return CROSS_CHAIN_COMMAND_NAME_REACT;
return CROSS_CHAIN_COMMAND_REACT;
}

public init(args: { interoperabilityMethod: InteroperabilityMethod }) {
Expand Down Expand Up @@ -60,7 +55,7 @@ export class ReactCrossChainCommand extends BaseCommand {
transaction: { senderAddress },
} = context;

const reactCCM: CCReactMessageParams = {
const ccReactMessageParams: CCReactMessageParams = {
reactionType: params.reactionType,
data: params.data,
helloMessageID: params.helloMessageID,
Expand All @@ -70,10 +65,10 @@ export class ReactCrossChainCommand extends BaseCommand {
context.getMethodContext(),
senderAddress,
'hello',
CROSS_CHAIN_COMMAND_NAME_REACT,
CROSS_CHAIN_COMMAND_REACT,
params.receivingChainID,
params.messageFee,
codec.encode(CCReactMessageParamsSchema, reactCCM),
codec.encode(CCReactMessageParamsSchema, ccReactMessageParams),
context.header.timestamp,
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const CROSS_CHAIN_COMMAND_NAME_REACT = 'reactCrossChain';
export const CROSS_CHAIN_COMMAND_REACT = 'crossChainReact';

export const MAX_RESERVED_ERROR_STATUS = 63;
export const CCM_STATUS_OK = 0;
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_command';
import { ReactCrossChainCommand } from './commands/react_cc_command';
import { ReactEndpoint } from './endpoint';
import { ReactMethod } from './method';
import { ReactInteroperableMethod } from './cc_method';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,11 @@
/**
* 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;
}

/**
* Parameters of the react reactCrossChain command
*/
export interface CCReactCommandParams extends CCReactMessageParams {
/**
* The chain ID of the receiving chain.
*
* `maxLength` and `minLength` are equal to 4.
*/
receivingChainID: Buffer;
/**
* The fee for sending the CCM across chains.
*/
messageFee: bigint;
}

/**
* Schema for the parameters of the reactCrossChain CCM
*/
// Schema for the parameters of the reactCrossChain CCM
export const CCReactMessageParamsSchema = {
/** The unique identifier of the schema. */
$id: '/lisk/react/ccmParams',
// The unique identifier of the schema.
$id: '/lisk/react/ccReactMessageParams',
type: 'object',
/** The required parameters for the CCM. */
// The required parameters for the CCM.
required: ['reactionType', 'helloMessageID', 'data'],
/** A list describing the required parameters for the CCM. */
// A list describing the required parameters for the CCM.
properties: {
reactionType: {
dataType: 'uint32',
Expand All @@ -58,16 +24,14 @@ export const CCReactMessageParamsSchema = {
},
};

/**
* Schema for the parameters of the react reactCrossChain command
*/
// Schema for the parameters of the react reactCrossChain command
export const CCReactCommandParamsSchema = {
/** The unique identifier of the schema. */
$id: '/lisk/react/ccCommandParams',
// The unique identifier of the schema.
$id: '/lisk/react/ccReactCommandParams',
type: 'object',
/** The required parameters for the command. */
// The required parameters for the command.
required: ['reactionType', 'helloMessageID', 'receivingChainID', 'data', 'messageFee'],
/** A list describing the available parameters for the command. */
// A list describing the available parameters for the command.
properties: {
reactionType: {
dataType: 'uint32',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ import {
} from 'lisk-sdk';

export type TokenID = Buffer;
// 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;
}

// Parameters of the react reactCrossChain command
export interface CCReactCommandParams extends CCReactMessageParams {
// The chain ID of the receiving chain.
receivingChainID: Buffer;
// The fee for sending the CCM across chains.
messageFee: bigint;
}

export interface InteroperabilityMethod {
getOwnChainAccount(methodContext: ImmutableMethodContext): Promise<OwnChainAccount>;
Expand Down

0 comments on commit 28ec134

Please sign in to comment.