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

Commit

Permalink
Use chainID from genesisConfig and fix ccm selection (#8358)
Browse files Browse the repository at this point in the history
🐛 Use chainID from genesisConfig and fix ccm selection
  • Loading branch information
ishantiw authored Apr 13, 2023
1 parent 9f38ca6 commit fe66827
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
OutboxRootWitness,
JSONObject,
Schema,
OwnChainAccountJSON,
Transaction,
LastCertificate,
CcmSendSuccessEventData,
Expand Down Expand Up @@ -140,15 +139,7 @@ export class ChainConnectorPlugin extends BasePlugin<ChainConnectorPluginConfig>
this.endpoint.load(this._chainConnectorStore);

this._sendingChainClient = this.apiClient;

this._ownChainID = Buffer.from(
(
await this._sendingChainClient.invoke<OwnChainAccountJSON>(
'interoperability_getOwnChainAccount',
)
).chainID,
'hex',
);
this._ownChainID = Buffer.from(this.appConfig.genesis.chainID, 'hex');
if (this._receivingChainID[0] !== this._ownChainID[0]) {
throw new Error('Receiving Chain ID network does not match the sending chain network.');
}
Expand Down Expand Up @@ -313,15 +304,32 @@ export class ChainConnectorPlugin extends BasePlugin<ChainConnectorPluginConfig>
record.height <= (newCertificate ? newCertificate.height : this._lastCertificate.height),
);
// Calculate messageWitnessHashes for pending CCMs if any
const sendingChainChannelDataJSON = await this._receivingChainClient.invoke<ChannelDataJSON>(
const channelDataOnReceivingChain = await this._receivingChainClient.invoke<ChannelDataJSON>(
'interoperability_getChannel',
{ chainID: this._ownChainID.toString('hex') },
);
const sendingChainChannelData = channelDataJSONToObj(sendingChainChannelDataJSON);
if (!channelDataOnReceivingChain?.inbox) {
this.logger.info('Receiving chain is not registered yet on the sending chain.');
return;
}
const inboxSizeOnReceivingChain = channelDataJSONToObj(channelDataOnReceivingChain).inbox.size;

const receivingChainChannelDataJSON = await this._sendingChainClient.invoke<ChannelDataJSON>(
'interoperability_getChannel',
{ chainID: this._receivingChainID.toString('hex') },
);

if (!receivingChainChannelDataJSON?.outbox) {
this.logger.info('Sending chain is not registered yet on the receiving chain.');
return;
}
const outboxSizeOnSendingChain = channelDataJSONToObj(receivingChainChannelDataJSON).outbox
.size;
const messageWitnessHashesForCCMs = calculateMessageWitnesses(
sendingChainChannelData,
ccmsToBeIncluded,
inboxSizeOnReceivingChain,
outboxSizeOnSendingChain,
lastSentCCM,
ccmsToBeIncluded,
this._maxCCUSize,
);
const { crossChainMessages, lastCCMToBeSent, messageWitnessHashes } =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Removal or modification of this copyright notice is prohibited.
*/

import { ccmSchema, codec, tree, ChannelData } from 'lisk-sdk';
import { ccmSchema, codec, tree } from 'lisk-sdk';
import { CCMsFromEvents, LastSentCCMWithHeight } from './types';

/**
Expand All @@ -33,12 +33,13 @@ import { CCMsFromEvents, LastSentCCMWithHeight } from './types';
}
*/
export const calculateMessageWitnesses = (
sendingChainChannelInfo: ChannelData,
ccmsToBeIncluded: CCMsFromEvents[],
lastSentCCMInfo: {
inboxSizeOnReceivingChain: number,
outboxSizeOnSendingChain: number,
lastSentCCM: {
height: number;
nonce: bigint;
},
ccmsToBeIncluded: CCMsFromEvents[],
maxCCUSize: number,
): {
crossChainMessages: Buffer[];
Expand All @@ -51,9 +52,14 @@ export const calculateMessageWitnesses = (
let totalSize = 0;
// Make an array of ccms with nonce greater than last sent ccm nonce
for (const ccmsFromEvents of ccmsToBeIncluded) {
const { ccms } = ccmsFromEvents;
const { ccms, height } = ccmsFromEvents;
for (const ccm of ccms) {
if (ccm.nonce > lastSentCCMInfo.nonce) {
if (height !== 0 && lastSentCCM.height === height) {
if (ccm.nonce === lastSentCCM.nonce) {
continue;
}
}
if (inboxSizeOnReceivingChain < outboxSizeOnSendingChain) {
const ccmBytes = codec.encode(ccmSchema, ccm);
totalSize += ccmBytes.length;
if (totalSize < maxCCUSize) {
Expand Down Expand Up @@ -86,7 +92,7 @@ export const calculateMessageWitnesses = (
const remainingSerializedCCMs = allSerializedCCMs.slice(includedSerializedCCMs.length);
// Generate messageWitness
const messageWitnessHashes = tree.regularMerkleTree.calculateRightWitness(
sendingChainChannelInfo.inbox.size + includedSerializedCCMs.length,
inboxSizeOnReceivingChain + includedSerializedCCMs.length,
remainingSerializedCCMs,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import * as chainConnectorDB from '../../src/db';
import { CCMsFromEvents, CCMsFromEventsJSON, LastSentCCMWithHeightJSON } from '../../src/types';
import { ccmsFromEventsToJSON, getMainchainID } from '../../src/utils';

describe('getSentCCUs', () => {
describe('endpoints', () => {
const ownChainID = Buffer.from('10000000', 'hex');
const appConfigForPlugin: ApplicationConfigForPlugin = {
system: {
keepEventsForHeights: -1,
Expand All @@ -52,7 +53,9 @@ describe('getSentCCUs', () => {
minEntranceFeePriority: '0',
minReplacementFeeDifference: '10',
},
genesis: {} as GenesisConfig,
genesis: {
chainID: ownChainID.toString('hex'),
} as GenesisConfig,
generator: {
keys: {
fromFile: '',
Expand Down Expand Up @@ -114,7 +117,6 @@ describe('getSentCCUs', () => {
const defaultPassword = '123';
const defaultCCUFee = '100000000';

const ownChainID = Buffer.from('10000000', 'hex');
let chainConnectorPlugin: ChainConnectorPlugin;

beforeEach(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,17 @@
* Removal or modification of this copyright notice is prohibited.
*/

import {
CCMsg,
Certificate,
testing,
cryptography,
BlockHeader,
tree,
ChannelData,
} from 'lisk-sdk';
import { CCMsg, tree } from 'lisk-sdk';
import { CCU_TOTAL_CCM_SIZE } from '../../src/constants';
import { CCMsFromEvents } from '../../src/types';
import { calculateMessageWitnesses } from '../../src/inbox_update';
import { getSampleCCM } from '../utils/sampleCCM';

describe('inboxUpdate', () => {
let sampleBlock: BlockHeader;
let sampleCertificate: Certificate;

let sampleCCMs: CCMsg[];
let sampleCCMsFromEvents: CCMsFromEvents[];

beforeEach(() => {
sampleBlock = testing.createFakeBlockHeader({
stateRoot: cryptography.utils.getRandomBytes(32),
validatorsHash: cryptography.utils.getRandomBytes(32),
height: 100,
}) as BlockHeader & { validatorsHash: Buffer; stateRoot: Buffer };

sampleCertificate = {
blockID: sampleBlock.id,
height: sampleBlock.height,
timestamp: sampleBlock.timestamp,
validatorsHash: sampleBlock.validatorsHash as Buffer,
stateRoot: sampleBlock.stateRoot as Buffer,
aggregationBits: Buffer.alloc(0),
signature: cryptography.utils.getRandomBytes(32),
};

sampleCCMs = new Array(4).fill(0).map((_, index) => getSampleCCM(index + 1));

sampleCCMsFromEvents = [
Expand All @@ -73,27 +46,16 @@ describe('inboxUpdate', () => {
});

describe('calculateMessageWitnesses', () => {
const channelData: ChannelData = {
inbox: {
size: 2,
appendPath: [],
root: Buffer.alloc(1),
},
outbox: {
size: 2,
appendPath: [],
root: Buffer.alloc(1),
},
messageFeeTokenID: Buffer.from('04000001', 'hex'),
partnerChainOutboxRoot: Buffer.alloc(2),
minReturnFeePerByte: BigInt(0),
};
it('should return one inboxUpdate when all the ccms can be included', () => {
jest.spyOn(tree.regularMerkleTree, 'calculateRightWitness').mockReturnValue([]);
const messageWitnessHashesForCCMs = calculateMessageWitnesses(
channelData,
0,
1,
{
height: 1,
nonce: BigInt(0),
},
sampleCCMsFromEvents,
{ height: 1, nonce: BigInt(0) },
CCU_TOTAL_CCM_SIZE,
);

Expand All @@ -119,9 +81,13 @@ describe('inboxUpdate', () => {
];

const messageWitnessHashesForCCMs = calculateMessageWitnesses(
channelData,
0,
1,
{
height: 1,
nonce: BigInt(0),
},
ccmListWithBigSize,
{ height: 1, nonce: BigInt(0) },
CCU_TOTAL_CCM_SIZE,
);

Expand All @@ -136,9 +102,13 @@ describe('inboxUpdate', () => {
.mockReturnValue([Buffer.alloc(1)]);
const { crossChainMessages, lastCCMToBeSent, messageWitnessHashes } =
calculateMessageWitnesses(
channelData,
0,
1,
{
height: 1,
nonce: BigInt(0),
},
[],
{ height: sampleCertificate.height + 1, nonce: BigInt(2) },
CCU_TOTAL_CCM_SIZE,
);

Expand Down
Loading

0 comments on commit fe66827

Please sign in to comment.