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

Commit

Permalink
♻️ Update getChildQueue to replace default topic
Browse files Browse the repository at this point in the history
- getChildQueue is updated to replace default topic
- Transaction execution to have a transactionID with prefix as default topic
- CCM execution to have a ccmID with prefix as default topic
  • Loading branch information
ishantiw committed Oct 19, 2023
1 parent 6fc44ea commit 07b3ee0
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
CCMProcessedResult,
} from '../../events/ccm_processed';
import { InvalidRMTVerification } from '../../events/invalid_rmt_verification';
import { EVENT_TOPIC_CCM_EXECUTION } from '../../../../state_machine/constants';

// https://github.com/LiskHQ/lips/blob/main/proposals/lip-0054.md#message-recovery-command
export class RecoverMessageCommand extends BaseInteroperabilityCommand<MainchainInteroperabilityInternalMethod> {
Expand Down Expand Up @@ -203,7 +204,9 @@ export class RecoverMessageCommand extends BaseInteroperabilityCommand<Mainchain
const ctx: CrossChainMessageContext = {
...context,
ccm,
eventQueue: context.eventQueue.getChildQueue(ccmID),
eventQueue: context.eventQueue.getChildQueue(
Buffer.concat([EVENT_TOPIC_CCM_EXECUTION, ccmID]),
),
};
let recoveredCCM: CCMsg;
// If the sending chain is the mainchain, recover the CCM.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
getIDFromCCMBytes,
} from '../../utils';
import { MainchainInteroperabilityInternalMethod } from '../internal_method';
import { EVENT_TOPIC_CCM_EXECUTION } from '../../../../state_machine/constants';

// https://github.com/LiskHQ/lips/blob/main/proposals/lip-0053.md#commands
export class SubmitMainchainCrossChainUpdateCommand extends BaseCrossChainUpdateCommand<MainchainInteroperabilityInternalMethod> {
Expand Down Expand Up @@ -98,7 +99,9 @@ export class SubmitMainchainCrossChainUpdateCommand extends BaseCrossChainUpdate
const ccmContext = {
...context,
ccm,
eventQueue: context.eventQueue.getChildQueue(ccmID),
eventQueue: context.eventQueue.getChildQueue(
Buffer.concat([EVENT_TOPIC_CCM_EXECUTION, ccmID]),
),
};

// If the receiving chain is the mainchain, apply the CCM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
VerificationResult,
VerifyStatus,
} from '../../../../state_machine';
import { EVENT_TOPIC_CCM_EXECUTION } from '../../../../state_machine/constants';
import { BaseCrossChainUpdateCommand } from '../../base_cross_chain_update_command';
import { CONTEXT_STORE_KEY_CCM_PROCESSING } from '../../constants';
import { CrossChainUpdateTransactionParams } from '../../types';
Expand Down Expand Up @@ -57,7 +58,9 @@ export class SubmitSidechainCrossChainUpdateCommand extends BaseCrossChainUpdate
const ccmContext = {
...context,
ccm,
eventQueue: context.eventQueue.getChildQueue(ccmID),
eventQueue: context.eventQueue.getChildQueue(
Buffer.concat([EVENT_TOPIC_CCM_EXECUTION, ccmID]),
),
};

await this.apply(ccmContext);
Expand Down
2 changes: 2 additions & 0 deletions framework/src/state_machine/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ export const EVENT_INDEX_BEFORE_TRANSACTIONS = Buffer.from([2]);
export const EVENT_INDEX_AFTER_TRANSACTIONS = Buffer.from([3]);

export const EVENT_TRANSACTION_NAME = 'commandExecutionResult';
export const EVENT_TOPIC_TRANSACTION_EXECUTION = Buffer.from([4]);
export const EVENT_TOPIC_CCM_EXECUTION = Buffer.from([5]);
3 changes: 1 addition & 2 deletions framework/src/state_machine/event_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ export class EventQueue {
}

public getChildQueue(topicID: Buffer): EventQueue {
const allTopics = [...this._defaultTopics, topicID];
return new EventQueue(this._height, this._events, allTopics);
return new EventQueue(this._height, this._events, [topicID]);
}

public createSnapshot(): number {
Expand Down
9 changes: 7 additions & 2 deletions framework/src/state_machine/transaction_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
BlockHeader,
BlockAssets,
} from './types';
import { EVENT_TOPIC_TRANSACTION_EXECUTION } from './constants';

interface ContextParams {
chainID: Buffer;
Expand Down Expand Up @@ -77,7 +78,9 @@ export class TransactionContext {
if (!this._assets) {
throw new Error('Transaction Execution requires block assets in the context.');
}
const childQueue = this._eventQueue.getChildQueue(this._transaction.id);
const childQueue = this._eventQueue.getChildQueue(
Buffer.concat([EVENT_TOPIC_TRANSACTION_EXECUTION, this._transaction.id]),
);
return {
logger: this._logger,
chainID: this._chainID,
Expand Down Expand Up @@ -132,7 +135,9 @@ export class TransactionContext {
if (!this._assets) {
throw new Error('Transaction Execution requires block assets in the context.');
}
const childQueue = this._eventQueue.getChildQueue(this._transaction.id);
const childQueue = this._eventQueue.getChildQueue(
Buffer.concat([EVENT_TOPIC_TRANSACTION_EXECUTION, this._transaction.id]),
);
return {
logger: this._logger,
chainID: this._chainID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
} from '../../../../../../src/modules/interoperability/events/ccm_processed';
import { CcmSendSuccessEvent } from '../../../../../../src/modules/interoperability/events/ccm_send_success';
import { InvalidRMTVerification } from '../../../../../../src/modules/interoperability/events/invalid_rmt_verification';
import { EVENT_TOPIC_CCM_EXECUTION } from '../../../../../../src/state_machine/constants';

describe('MessageRecoveryCommand', () => {
const interopModule = new MainchainInteroperabilityModule();
Expand Down Expand Up @@ -540,7 +541,9 @@ describe('MessageRecoveryCommand', () => {
const ctx: CrossChainMessageContext = {
...commandExecuteContext,
ccm,
eventQueue: commandExecuteContext.eventQueue.getChildQueue(utils.hash(crossChainMessage)),
eventQueue: commandExecuteContext.eventQueue.getChildQueue(
Buffer.concat([EVENT_TOPIC_CCM_EXECUTION, utils.hash(crossChainMessage)]),
),
};

expect(command['_applyRecovery']).toHaveBeenCalledWith(ctx);
Expand All @@ -567,7 +570,9 @@ describe('MessageRecoveryCommand', () => {
const ctx: CrossChainMessageContext = {
...commandExecuteContext,
ccm,
eventQueue: commandExecuteContext.eventQueue.getChildQueue(utils.hash(crossChainMessage)),
eventQueue: commandExecuteContext.eventQueue.getChildQueue(
Buffer.concat([EVENT_TOPIC_CCM_EXECUTION, utils.hash(crossChainMessage)]),
),
};

expect(command['_forwardRecovery']).toHaveBeenCalledWith(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import {
OwnChainAccountStore,
OwnChainAccount,
} from '../../../../../../src/modules/interoperability/stores/own_chain_account';
import { EVENT_TOPIC_CCM_EXECUTION } from '../../../../../../src/state_machine/constants';

describe('SubmitMainchainCrossChainUpdateCommand', () => {
const interopMod = new MainchainInteroperabilityModule();
Expand Down Expand Up @@ -538,7 +539,9 @@ describe('SubmitMainchainCrossChainUpdateCommand', () => {
expect(mainchainCCUUpdateCommand['apply']).toHaveBeenCalledWith({
...executeContext,
ccm: decodedCCM,
eventQueue: executeContext.eventQueue.getChildQueue(ccmID),
eventQueue: executeContext.eventQueue.getChildQueue(
Buffer.concat([EVENT_TOPIC_CCM_EXECUTION, ccmID]),
),
});
expect(mainchainCCUUpdateCommand['internalMethod'].appendToInboxTree).toHaveBeenCalledTimes(
3,
Expand Down Expand Up @@ -567,15 +570,19 @@ describe('SubmitMainchainCrossChainUpdateCommand', () => {
expect(mainchainCCUUpdateCommand['_forward']).toHaveBeenCalledWith({
...executeContext,
ccm: firstDecodedCCM,
eventQueue: executeContext.eventQueue.getChildQueue(firstCCMID),
eventQueue: executeContext.eventQueue.getChildQueue(
Buffer.concat([EVENT_TOPIC_CCM_EXECUTION, firstCCMID]),
),
});
const { ccmID: thirdCCMID, decodedCCM: thirdDecodedCCM } = getDecodedCCMAndID(
params.inboxUpdate.crossChainMessages[2],
);
expect(mainchainCCUUpdateCommand['_forward']).toHaveBeenCalledWith({
...executeContext,
ccm: thirdDecodedCCM,
eventQueue: executeContext.eventQueue.getChildQueue(thirdCCMID),
eventQueue: executeContext.eventQueue.getChildQueue(
Buffer.concat([EVENT_TOPIC_CCM_EXECUTION, thirdCCMID]),
),
});
expect(mainchainCCUUpdateCommand['internalMethod'].appendToInboxTree).toHaveBeenCalledTimes(
3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import {
OwnChainAccountStore,
OwnChainAccount,
} from '../../../../../../src/modules/interoperability/stores/own_chain_account';
import { EVENT_TOPIC_CCM_EXECUTION } from '../../../../../../src/state_machine/constants';

describe('SubmitSidechainCrossChainUpdateCommand', () => {
const interopMod = new SidechainInteroperabilityModule();
Expand Down Expand Up @@ -406,7 +407,9 @@ describe('SubmitSidechainCrossChainUpdateCommand', () => {
expect(sidechainCCUUpdateCommand['apply']).toHaveBeenCalledWith({
...executeContext,
ccm: decodedCCM,
eventQueue: executeContext.eventQueue.getChildQueue(ccmID),
eventQueue: executeContext.eventQueue.getChildQueue(
Buffer.concat([EVENT_TOPIC_CCM_EXECUTION, ccmID]),
),
});
}
expect(sidechainCCUUpdateCommand['internalMethod'].appendToInboxTree).toHaveBeenCalledTimes(
Expand Down
6 changes: 5 additions & 1 deletion framework/test/unit/state_machine/state_machine.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
EVENT_INDEX_BEFORE_TRANSACTIONS,
EVENT_INDEX_FINALIZE_GENESIS_STATE,
EVENT_INDEX_INIT_GENESIS_STATE,
// EVENT_TOPIC_TRANSACTION_EXECUTION,
} from '../../../src/state_machine/constants';
import { PrefixedStateReadWriter } from '../../../src/state_machine/prefixed_state_read_writer';
import { InMemoryPrefixedStateDB } from '../../../src/testing/in_memory_prefixed_state';
Expand All @@ -58,6 +59,7 @@ describe('state_machine', () => {
params: codec.encode(new CustomCommand0(new NamedRegistry(), new NamedRegistry()).schema, {
data: 'some info',
}),
id: utils.hash(utils.getRandomBytes(2)),
} as Transaction;

let stateMachine: StateMachine;
Expand Down Expand Up @@ -215,6 +217,7 @@ describe('state_machine', () => {
params: codec.encode(new CustomCommand3(new NamedRegistry(), new NamedRegistry()).schema, {
data: 'some info',
}),
id: utils.hash(utils.getRandomBytes(2)),
} as Transaction;
stateMachine.registerModule(new CustomModule3());
const ctx = new TransactionContext({
Expand All @@ -232,7 +235,8 @@ describe('state_machine', () => {
const events = ctx.eventQueue.getEvents();
const dataDecoded = codec.decode(standardEventDataSchema, events[0].toObject().data);
expect(events).toHaveLength(1);
expect(events[0].toObject().topics[0]).toEqual(transaction.id);

expect(events[0].toObject().topics[0]).toEqual(transactionWithInvalidCommand.id);
expect(dataDecoded).toStrictEqual({ success: false });
});

Expand Down

0 comments on commit 07b3ee0

Please sign in to comment.