Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add in-transit XCM msgs in blocks endpoint #1412

Merged
merged 7 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/services/blocks/BlocksService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ import {
blockHash3356195,
blockHash6202603,
blockHash18468942,
blockHash19772575,
defaultMockApi,
mockApiBlock18468942,
mockApiBlock19772575,
mockAssetHubKusamaApiBlock3356195,
mockAssetHubKusamaApiBlock6202603,
mockForkedBlock789629,
Expand All @@ -52,16 +54,19 @@ import { events789629 } from '../test-helpers/mock/data/events789629Hex';
import { events3356195 } from '../test-helpers/mock/data/events3356195Hex';
import { events6202603 } from '../test-helpers/mock/data/events6202603Hex';
import { events18468942 } from '../test-helpers/mock/data/events18468942Hex';
import { events19772575 } from '../test-helpers/mock/data/events19772575Hex';
import { validators789629Hex } from '../test-helpers/mock/data/validators789629Hex';
import { validators3356195Hex } from '../test-helpers/mock/data/validators3356195Hex';
import { validators6202603Hex } from '../test-helpers/mock/data/validators6202603Hex';
import { validators18468942Hex } from '../test-helpers/mock/data/validators18468942Hex';
import { validators19772575Hex } from '../test-helpers/mock/data/validators19772575Hex';
import { parseNumberOrThrow } from '../test-helpers/mock/parseNumberOrThrow';
import block789629Extrinsic from '../test-helpers/responses/blocks/block789629Extrinsic.json';
import block3356195Response from '../test-helpers/responses/blocks/block3356195.json';
import block6202603pId2087Response from '../test-helpers/responses/blocks/block6202603paraId2087.json';
import block18468942Response from '../test-helpers/responses/blocks/block18468942.json';
import block18468942pId2000Response from '../test-helpers/responses/blocks/block18468942paraId2000.json';
import block19772575Response from '../test-helpers/responses/blocks/block19772575.json';
import blocks789629Response from '../test-helpers/responses/blocks/blocks789629.json';
import blocks789629Raw from '../test-helpers/responses/blocks/blocks789629Raw.json';
import { BlocksService } from './BlocksService';
Expand Down Expand Up @@ -767,5 +772,81 @@ describe('BlocksService', () => {

expect(sanitizeNumbers(block)).toMatchObject(block6202603pId2087Response);
});

it('Should give back two decoded horizontal XCM messages (with different origin & destination paraId) that are `in transit` in Polkadot Relay block 19772575', async () => {
// Reset LRU cache
cache.clear();

// fetchBlock options
const options = {
eventDocs: true,
extrinsicDocs: true,
checkFinalized: false,
queryFinalizedHead: false,
omitFinalizedTag: false,
noFees: false,
checkDecodedXcm: true,
paraId: undefined,
};

const validatorsAt = (_hash: Hash) =>
Promise.resolve().then(() => polkadotRegistryV1000001.createType('Vec<ValidatorId>', validators19772575Hex));

const eventsAt = (_hash: Hash) =>
Promise.resolve().then(() => polkadotRegistryV1000001.createType('Vec<EventRecord>', events19772575));

const nextFeeMultiplierAt = (_hash: Hash) =>
Promise.resolve().then(() => polkadotRegistryV1000001.createType('Fixed128', 1000000000));

const mockHistoricApiXCM = {
registry: polkadotRegistryV1000001,
call: {
transactionPaymentApi: {},
},
consts: {
transactionPayment: {
transactionByteFee: polkadotRegistryV1000001.createType('Balance', 1000000),
weightToFee: [
{
coeffFrac: polkadotRegistryV1000001.createType('Perbill', 80000000),
coeffInteger: polkadotRegistryV1000001.createType('Balance', 0),
degree: polkadotRegistryV1000001.createType('u8', 1),
negative: false,
},
],
},
system: {
extrinsicBaseWeight: polkadotRegistryV1000001.createType('u64', 125000000),
},
},
query: {
session: {
validators: validatorsAt,
},
system: {
events: eventsAt,
},
transactionPayment: {
nextFeeMultiplier: nextFeeMultiplierAt,
},
},
} as unknown as ApiDecoration<'promise'>;

const mockApiXCM = {
...mockApiBlock19772575,
query: {
transactionPayment: {
nextFeeMultiplier: { at: nextFeeMultiplierAt },
},
},
at: (_hash: Hash) => mockHistoricApiXCM,
} as unknown as ApiPromise;

// Block Service
const blocksServiceXCM = new BlocksService(mockApiXCM, 0, cache, new QueryFeeDetailsCache(null, null));
const block = await blocksServiceXCM.fetchBlock(blockHash19772575, mockHistoricApiXCM, options);

expect(sanitizeNumbers(block)).toMatchObject(block19772575Response);
});
});
});
115 changes: 73 additions & 42 deletions src/services/blocks/XCMDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ import type {
IDownwardMessage,
IExtrinsic,
IFrameMethod,
IHorizontalMessage,
IHorizontalMessageInParachain,
IHorizontalMessageInRelayChain,
IMessages,
IUpwardMessage,
} from '../../types/responses';
import type { ISanitizedBackedCandidate } from '../../types/responses/SanitizedBackedCandidate';
import type { ISanitizedBackedCandidateHorizontalMessage } from '../../types/responses/SanitizedBackedCandidatesHorizontalMessage';
import type { ISanitizedParachainInherentData } from '../../types/responses/SanitizedParachainInherentData';
import type { ISanitizedParentInherentData } from '../../types/responses/SanitizedParentInherentData';
import { IOption } from '../../types/util';

enum ChainType {
Relay = 'Relay',
Expand Down Expand Up @@ -63,23 +66,28 @@ export class XcmDecoder {
const frame = extrinsic.method as IFrameMethod;
if (frame.pallet === 'paraInherent' && frame.method === 'enter') {
const data = extrinsic.args.data as ISanitizedParentInherentData;
if (paraId !== undefined) {
data.backedCandidates.forEach((candidate) => {
if (candidate.candidate.descriptor.paraId.toString() === paraId.toString()) {
const msg_decoded = this.checkUpwardMsg(api, candidate);
if (msg_decoded != undefined && Object.keys(msg_decoded).length > 0) {
xcmMessages.upwardMessages?.push(msg_decoded);
}
data.backedCandidates.forEach((candidate) => {
if (paraId === undefined || candidate.candidate.descriptor.paraId.toString() === paraId.toString()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (paraId === undefined || candidate.candidate.descriptor.paraId.toString() === paraId.toString()) {
if (!paraId || candidate.candidate.descriptor.paraId.toString() === paraId.toString()) {

const horizontalMsgs: IHorizontalMessageInRelayChain[] = this.checkMessagesInRelay(
api,
candidate,
'horizontal',
paraId,
) as IHorizontalMessageInRelayChain[];
if (horizontalMsgs != null && horizontalMsgs.length > 0) {
horizontalMsgs.forEach((msg: IHorizontalMessageInRelayChain) => {
xcmMessages.horizontalMessages?.push(msg);
});
}
});
} else {
data.backedCandidates.forEach((candidate) => {
const msg_decoded = this.checkUpwardMsg(api, candidate);
if (msg_decoded != undefined && Object.keys(msg_decoded).length > 0) {
xcmMessages.upwardMessages?.push(msg_decoded);

const upwardMsgs = this.checkMessagesInRelay(api, candidate, 'upward', paraId) as IUpwardMessage[];
if (upwardMsgs != null && upwardMsgs.length > 0) {
upwardMsgs.forEach((msg: IUpwardMessage) => {
xcmMessages.upwardMessages?.push(msg);
});
}
});
}
}
});
}
});
} else if (this.curChainType === ChainType.Parachain) {
Expand All @@ -100,44 +108,67 @@ export class XcmDecoder {
}
});
data.horizontalMessages.forEach((msgs, index) => {
msgs.forEach((msg) => {
const xcmMessageDecoded = this.decodeMsg(api, msg.data.slice(1));
let horizontalMessage: IHorizontalMessage;
if (paraId !== undefined && index.toString() === paraId.toString()) {
horizontalMessage = {
if (paraId === undefined || index.toString() === paraId.toString()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (paraId === undefined || index.toString() === paraId.toString()) {
if (!paraId || index.toString() === paraId.toString()) {

msgs.forEach((msg) => {
const xcmMessageDecoded = this.decodeMsg(api, msg.data.slice(1));
const horizontalMessage: IHorizontalMessageInParachain = {
sentAt: msg.sentAt,
paraId: index,
originParaId: index,
data: xcmMessageDecoded,
};
xcmMessages.horizontalMessages?.push(horizontalMessage);
} else if (paraId === undefined) {
horizontalMessage = {
sentAt: msg.sentAt,
paraId: index,
data: xcmMessageDecoded,
};
xcmMessages.horizontalMessages?.push(horizontalMessage);
}
});
});
}
});
}
});
}
return xcmMessages;
}

private checkUpwardMsg(api: ApiPromise, candidate: ISanitizedBackedCandidate): IUpwardMessage | undefined {
if (candidate.candidate.commitments.upwardMessages.length > 0) {
const xcmMessage = candidate.candidate.commitments.upwardMessages;
const paraId: string = candidate.candidate.descriptor.paraId;
const xcmMessageDecoded: string = this.decodeMsg(api, xcmMessage[0]);
const upwardMessage = {
paraId: paraId,
data: xcmMessageDecoded[0],
};
return upwardMessage;
private checkMessagesInRelay(
api: ApiPromise,
candidate: ISanitizedBackedCandidate,
messageType: 'upward' | 'horizontal',
paraId?: number,
): IOption<IUpwardMessage[] | IHorizontalMessageInRelayChain[]> {
const messages: IUpwardMessage[] | IHorizontalMessageInRelayChain[] = [];
const xcmMessages =
messageType === 'upward'
? candidate.candidate.commitments.upwardMessages
: candidate.candidate.commitments.horizontalMessages;

if (xcmMessages.length > 0) {
const paraIdCandidate: string = candidate.candidate.descriptor.paraId.toString();

xcmMessages.forEach((msg: string | ISanitizedBackedCandidateHorizontalMessage) => {
const msgData: string =
messageType === 'upward'
? (msg as string)
: (msg as ISanitizedBackedCandidateHorizontalMessage).data.slice(1);
const xcmMessageDecoded: string = this.decodeMsg(api, msgData);

if (paraId === undefined || paraIdCandidate === paraId.toString()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (paraId === undefined || paraIdCandidate === paraId.toString()) {
if (!paraId || paraIdCandidate === paraId.toString()) {

if (messageType === 'upward') {
const upwardMessage: IUpwardMessage = {
originParaId: paraIdCandidate,
data: xcmMessageDecoded,
};
(messages as IUpwardMessage[]).push(upwardMessage);
} else {
const horizontalMessage: IHorizontalMessageInRelayChain = {
originParaId: paraIdCandidate,
destinationParaId: (msg as ISanitizedBackedCandidateHorizontalMessage).recipient.toString(),
data: xcmMessageDecoded,
};
(messages as IHorizontalMessageInRelayChain[]).push(horizontalMessage);
}
}
});

return messages;
} else {
return undefined;
return null;
}
}

Expand Down
37 changes: 37 additions & 0 deletions src/services/test-helpers/mock/data/block19772575.json

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions src/services/test-helpers/mock/data/events19772575Hex.ts

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions src/services/test-helpers/mock/data/validators19772575Hex.ts

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/services/test-helpers/mock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
export * from './addresses';
export * from './mockApi';
export * from './mockApiBlock18468942';
export * from './mockApiBlock19772575';
export * from './mockAssetHubKusamaApi';
export * from './mockAssetHubKusamaApiBlock3356195';
export * from './mockAssetHubKusamaApiBlock6202603';
Expand All @@ -28,5 +29,6 @@ export * from './mockBlock5236177';
export * from './mockBlock6202603';
export * from './mockBlock13641102';
export * from './mockBlock18468942';
export * from './mockBlock19772575';
export * from './mockBlockHashes';
export * from './transactions';