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

Commit

Permalink
Update interoperability module based on LIP0045 update (#8055)
Browse files Browse the repository at this point in the history
* πŸ’…πŸ» RenamchainIDSchema->e registeredNamesSchema

* ♻️  Update sendInternal method

* πŸ’…πŸ» Rename chainAccountSchema -> chainDataSchema

* ♻️ Update bounce based on LIP0045 update

* πŸ”™ Revert ownchainDataSchema -> ownchainAccountSchema
  • Loading branch information
ishantiw authored Jan 20, 2023
1 parent 8d54bc7 commit 723817d
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
} from './utils';
import { ChainValidatorsStore } from './stores/chain_validators';
import { ChannelDataStore } from './stores/channel_data';
import { OwnChainAccountStore } from './stores/own_chain_account';

export abstract class BaseCrossChainUpdateCommand<
T extends BaseInteroperabilityInternalMethod,
Expand Down Expand Up @@ -294,7 +295,9 @@ export abstract class BaseCrossChainUpdateCommand<
});
}
}

/**
* @see https://github.com/LiskHQ/lips/blob/main/proposals/lip-0045.md#bounce
*/
protected async bounce(
context: CrossChainMessageContext,
ccmSize: number,
Expand Down Expand Up @@ -323,14 +326,21 @@ export abstract class BaseCrossChainUpdateCommand<
receivingChainID: ccm.sendingChainID,
fee: ccmStatusCode === CCMStatusCode.FAILED_CCM ? BigInt(0) : ccm.fee - minFee,
};

let partnerChainID: Buffer;
const doesReceivingChainExist = await this.stores
.get(ChainAccountStore)
.has(context, bouncedCCM.receivingChainID);
if (!doesReceivingChainExist) {
partnerChainID = getMainchainID(bouncedCCM.receivingChainID);
} else {

const mainchainID = getMainchainID(bouncedCCM.receivingChainID);
const ownChainAccount = await this.stores.get(OwnChainAccountStore).get(context, EMPTY_BYTES);
// Processing on the mainchain
if (ownChainAccount.chainID.equals(mainchainID)) {
partnerChainID = bouncedCCM.receivingChainID;
// Processing on a sidechain
} else {
// Check for direct channel
partnerChainID = doesReceivingChainExist ? bouncedCCM.receivingChainID : mainchainID;
}

await this.internalMethod.addToOutbox(context, partnerChainID, bouncedCCM);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,18 +441,6 @@ export abstract class BaseInteroperabilityInternalMethod extends BaseInternalMet
sendingChainID: ownChainAccount.chainID,
status,
};
// Not possible to send messages to the own chain.
if (receivingChainID.equals(ownChainAccount.chainID)) {
this.events.get(CcmSentFailedEvent).log(
context,
{
ccm: { ...ccm, params: EMPTY_BYTES },
code: CCMSentFailedCode.INVALID_RECEIVING_CHAIN,
},
true,
);
throw new Error('Sending chain cannot be the receiving chain.');
}

// Validate ccm size.
try {
Expand All @@ -471,6 +459,18 @@ export abstract class BaseInteroperabilityInternalMethod extends BaseInternalMet
}
// From now on, we can assume that the ccm is valid.

// Not possible to send messages to the own chain.
if (receivingChainID.equals(ownChainAccount.chainID)) {
this.events.get(CcmSentFailedEvent).log(
context,
{
ccm: { ...ccm, params: EMPTY_BYTES },
code: CCMSentFailedCode.INVALID_RECEIVING_CHAIN,
},
true,
);
throw new Error('Sending chain cannot be the receiving chain.');
}
// receivingChainID must correspond to a live chain.
const isReceivingChainLive = await this.isLive(
context,
Expand Down Expand Up @@ -500,6 +500,26 @@ export abstract class BaseInteroperabilityInternalMethod extends BaseInternalMet
throw error;
}
}

// Pay message fee.
if (fee > 0) {
try {
// eslint-disable-next-line no-lonely-if
await this._tokenMethod.payMessageFee(context, sendingAddress, fee, ccm.receivingChainID);
} catch (error) {
this.events.get(CcmSentFailedEvent).log(
context,
{
ccm: { ...ccm, params: EMPTY_BYTES },
code: CCMSentFailedCode.MESSAGE_FEE_EXCEPTION,
},
true,
);

throw new Error('Failed to pay message fee.');
}
}

const mainchainID = getMainchainID(ownChainAccount.chainID);
let partnerChainID: Buffer;
// Processing on the mainchain.
Expand Down Expand Up @@ -529,25 +549,6 @@ export abstract class BaseInteroperabilityInternalMethod extends BaseInternalMet
throw new Error('Receiving chain is not active.');
}

// Pay message fee.
if (fee > 0) {
try {
// eslint-disable-next-line no-lonely-if
await this._tokenMethod.payMessageFee(context, sendingAddress, fee, partnerChainID);
} catch (error) {
this.events.get(CcmSentFailedEvent).log(
context,
{
ccm: { ...ccm, params: EMPTY_BYTES },
code: CCMSentFailedCode.MESSAGE_FEE_EXCEPTION,
},
true,
);

throw new Error('Failed to pay message fee.');
}
}

const { ccmID } = getEncodedCCMAndID(ccm);
await this.addToOutbox(context, partnerChainID, ccm);
ownChainAccount.nonce += BigInt(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
*/

import { BaseEvent, EventQueuer } from '../../base_event';
import { chainAccountSchema, ChainAccount } from '../stores/chain_account';
import { chainDataSchema, ChainAccount } from '../stores/chain_account';

export class ChainAccountUpdatedEvent extends BaseEvent<ChainAccount> {
public schema = chainAccountSchema;
public schema = chainDataSchema;

public log(ctx: EventQueuer, chainID: Buffer, data: ChainAccount): void {
this.add(ctx, data, [chainID]);
Expand Down
4 changes: 2 additions & 2 deletions framework/src/modules/interoperability/mainchain/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
isChainIDAvailableRequestSchema,
} from '../schemas';
import {
chainAccountSchema,
chainDataSchema,
allChainAccountsSchema,
ChainAccountStore,
} from '../stores/chain_account';
Expand Down Expand Up @@ -172,7 +172,7 @@ export class MainchainInteroperabilityModule extends BaseInteroperabilityModule
{
name: this.endpoint.getChainAccount.name,
request: getChainAccountRequestSchema,
response: chainAccountSchema,
response: chainDataSchema,
},
{
name: this.endpoint.getAllChainAccounts.name,
Expand Down
8 changes: 4 additions & 4 deletions framework/src/modules/interoperability/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import {
HASH_LENGTH,
NUMBER_ACTIVE_VALIDATORS_MAINCHAIN,
} from './constants';
import { chainAccountSchema } from './stores/chain_account';
import { chainDataSchema } from './stores/chain_account';
import { chainValidatorsSchema } from './stores/chain_validators';
import { channelSchema } from './stores/channel_data';
import { outboxRootSchema } from './stores/outbox_root';
import { ownChainAccountSchema } from './stores/own_chain_account';
import { chainIDSchema } from './stores/registered_names';
import { registeredNamesSchema } from './stores/registered_names';
import { terminatedOutboxSchema } from './stores/terminated_outbox';
import { terminatedStateSchema } from './stores/terminated_state';

Expand Down Expand Up @@ -663,7 +663,7 @@ export const genesisInteroperabilitySchema = {
fieldNumber: 1,
},
storeValue: {
...chainAccountSchema,
...chainDataSchema,
fieldNumber: 2,
},
},
Expand Down Expand Up @@ -771,7 +771,7 @@ export const genesisInteroperabilitySchema = {
fieldNumber: 1,
},
storeValue: {
...chainIDSchema,
...registeredNamesSchema,
fieldNumber: 2,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
import { BaseInteroperabilityCommand } from '../../base_interoperability_command';
import { EMPTY_BYTES, LIVENESS_LIMIT } from '../../constants';
import { stateRecoveryInitParamsSchema } from '../../schemas';
import { chainAccountSchema, ChainAccountStore, ChainStatus } from '../../stores/chain_account';
import { chainDataSchema, ChainAccountStore, ChainStatus } from '../../stores/chain_account';
import { OwnChainAccountStore } from '../../stores/own_chain_account';
import { TerminatedStateAccount, TerminatedStateStore } from '../../stores/terminated_state';
import { ChainAccount, StateRecoveryInitParams } from '../../types';
Expand Down Expand Up @@ -63,7 +63,7 @@ export class InitializeStateRecoveryCommand extends BaseInteroperabilityCommand<
}

const deserializedSidechainAccount = codec.decode<ChainAccount>(
chainAccountSchema,
chainDataSchema,
sidechainAccount,
);
const mainchainAccount = await this.stores.get(ChainAccountStore).get(context, mainchainID);
Expand Down Expand Up @@ -117,10 +117,7 @@ export class InitializeStateRecoveryCommand extends BaseInteroperabilityCommand<
// LIP: https://github.com/LiskHQ/lips/blob/main/proposals/lip-0054.md#execution-3
public async execute(context: CommandExecuteContext<StateRecoveryInitParams>): Promise<void> {
const { params } = context;
const sidechainAccount = codec.decode<ChainAccount>(
chainAccountSchema,
params.sidechainAccount,
);
const sidechainAccount = codec.decode<ChainAccount>(chainDataSchema, params.sidechainAccount);

const doesTerminatedStateAccountExist = await this.stores
.get(TerminatedStateStore)
Expand Down
4 changes: 2 additions & 2 deletions framework/src/modules/interoperability/sidechain/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
isChainIDAvailableRequestSchema,
} from '../schemas';
import {
chainAccountSchema,
chainDataSchema,
allChainAccountsSchema,
ChainAccountStore,
} from '../stores/chain_account';
Expand Down Expand Up @@ -150,7 +150,7 @@ export class SidechainInteroperabilityModule extends BaseInteroperabilityModule
{
name: this.endpoint.getChainAccount.name,
request: getChainAccountRequestSchema,
response: chainAccountSchema,
response: chainDataSchema,
},
{
name: this.endpoint.getAllChainAccounts.name,
Expand Down
12 changes: 6 additions & 6 deletions framework/src/modules/interoperability/stores/chain_account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface ChainAccount {
status: ChainStatus;
}

const chainAccountJSONSchema = {
const chainDataJSONSchema = {
type: 'object',
required: ['name', 'lastCertificate', 'status'],
properties: {
Expand Down Expand Up @@ -85,9 +85,9 @@ const chainAccountJSONSchema = {
};

// https://github.com/LiskHQ/lips/blob/main/proposals/lip-0045.md#chain-data-substore
export const chainAccountSchema = {
$id: '/modules/interoperability/chainAccount',
...chainAccountJSONSchema,
export const chainDataSchema = {
$id: '/modules/interoperability/chainData',
...chainDataJSONSchema,
};

export const allChainAccountsSchema = {
Expand All @@ -97,13 +97,13 @@ export const allChainAccountsSchema = {
properties: {
chains: {
type: 'array',
items: chainAccountJSONSchema,
items: chainDataJSONSchema,
},
},
};

export class ChainAccountStore extends BaseStore<ChainAccount> {
public schema = chainAccountSchema;
public schema = chainDataSchema;

public async getAllAccounts(
context: ImmutableStoreGetter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { BaseStore } from '../../base_store';
import { CHAIN_ID_LENGTH } from '../constants';

// https://github.com/LiskHQ/lips/blob/main/proposals/lip-0045.md#registered-names-substore
export const chainIDSchema = {
export const registeredNamesSchema = {
$id: '/modules/interoperability/chainId',
type: 'object',
required: ['chainID'],
Expand All @@ -34,5 +34,5 @@ export interface ChainID {
}

export class RegisteredNamesStore extends BaseStore<ChainID> {
public schema = chainIDSchema;
public schema = registeredNamesSchema;
}
Loading

0 comments on commit 723817d

Please sign in to comment.