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

Update interop example #9144

Merged
merged 10 commits into from
Dec 5, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { MAX_RESERVED_ERROR_STATUS, CROSS_CHAIN_COMMAND_REACT } from '../constan
import { ReactionStore, ReactionStoreData } from '../stores/reaction';
import { MessageStore } from '../stores/message';

export class ReactCommand extends BaseCCCommand {
export class ReactCCCommand extends BaseCCCommand {
public schema = CCReactMessageParamsSchema;

public get name(): string {
Expand All @@ -22,36 +22,49 @@ export class ReactCommand 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 @@ -77,8 +90,9 @@ export class ReactCommand 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);
Tschakki marked this conversation as resolved.
Show resolved Hide resolved

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,9 +14,10 @@ import {
TransactionVerifyContext,
utils,
VerificationResult,
VerifyStatus,
} from 'lisk-sdk';
import { CreateHelloCommand } from './commands/create_hello_command';
import { ReactCommand } 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 All @@ -42,7 +43,7 @@ export class HelloModule extends BaseInteroperableModule {
public endpoint = new HelloEndpoint(this.stores, this.offchainStores);
public method = new HelloMethod(this.stores, this.events);
public commands = [new CreateHelloCommand(this.stores, this.events)];
public reactCCCommand = new ReactCommand(this.stores, this.events);
public reactCCCommand = new ReactCCCommand(this.stores, this.events);
public crossChainMethod = new HelloInteroperableMethod(this.stores, this.events);
public crossChainCommand = [this.reactCCCommand];

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
Expand Up @@ -44,6 +44,7 @@ export class CrossChainReactCommand extends BaseCommand {
error: err as Error,
};
}

return {
status: VerifyStatus.OK,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
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.
Expand All @@ -7,20 +24,9 @@ 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,
},
};

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