-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add decoded xcm data in blocks endpoint
- Loading branch information
Showing
6 changed files
with
204 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// Copyright 2017-2023 Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate API Sidecar. | ||
// | ||
// Substrate API Sidecar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import '@polkadot/api-augment'; | ||
|
||
import { ApiPromise } from '@polkadot/api'; | ||
import { Bytes } from '@polkadot/types'; | ||
|
||
import { | ||
IDownwardMessage, | ||
IExtrinsic, | ||
IFrameMethod, | ||
IHorizontalMessage, | ||
IMessages, | ||
IUpwardMessage, | ||
} from '../../types/responses'; | ||
|
||
enum ChainType { | ||
Relay = 'Relay', | ||
Parachain = 'Parachain', | ||
} | ||
|
||
export class XcmDecoder { | ||
readonly messages: IMessages[]; | ||
readonly api: ApiPromise; | ||
static curChainType: ChainType; | ||
|
||
constructor(api: ApiPromise, specName: string, extrinsics: IExtrinsic[], paraId?: string) { | ||
this.api = api; | ||
XcmDecoder.curChainType = XcmDecoder.getCurChainType(specName); | ||
this.messages = XcmDecoder.getMessages(api, extrinsics, paraId); | ||
} | ||
|
||
static getCurChainType(specName: string): ChainType { | ||
const relay = ['polkadot', 'kusama', 'westend', 'rococo']; | ||
if (relay.includes(specName)) { | ||
return ChainType.Relay; | ||
} else { | ||
return ChainType.Parachain; | ||
} | ||
} | ||
|
||
static getMessages(api: ApiPromise, extrinsics: IExtrinsic[], paraId?: string): IMessages[] { | ||
const xcmMessages: IMessages[] = []; | ||
if (XcmDecoder.curChainType === ChainType.Relay) { | ||
extrinsics.forEach((extrinsic) => { | ||
const frame = extrinsic.method as IFrameMethod; | ||
if (frame.pallet === 'paraInherent' && frame.method === 'enter') { | ||
const data: any = extrinsic.args.data; | ||
const upwardMessage: IUpwardMessage[] = []; | ||
if (paraId !== undefined) { | ||
data.backedCandidates.forEach((candidate: any) => { | ||
Check failure on line 65 in src/services/blocks/XCMDecoder.ts GitHub Actions / lint
Check failure on line 65 in src/services/blocks/XCMDecoder.ts GitHub Actions / lint
|
||
if (candidate.candidate.descriptor.paraId.toString() === paraId) { | ||
Check failure on line 66 in src/services/blocks/XCMDecoder.ts GitHub Actions / lint
|
||
const msg_decoded = XcmDecoder.checkUpwardMsg(api, candidate); | ||
if (msg_decoded != undefined && Object.keys(msg_decoded).length > 0) { | ||
upwardMessage.push(msg_decoded); | ||
} | ||
} | ||
}); | ||
} else { | ||
data.backedCandidates.forEach((candidate: any) => { | ||
Check failure on line 74 in src/services/blocks/XCMDecoder.ts GitHub Actions / lint
Check failure on line 74 in src/services/blocks/XCMDecoder.ts GitHub Actions / lint
|
||
const msg_decoded = XcmDecoder.checkUpwardMsg(api, candidate); | ||
if (msg_decoded != undefined && Object.keys(msg_decoded).length > 0) { | ||
upwardMessage.push(msg_decoded); | ||
} | ||
}); | ||
} | ||
xcmMessages.push({ | ||
upwardMessages: upwardMessage, | ||
}); | ||
} | ||
}); | ||
} else if (XcmDecoder.curChainType === ChainType.Parachain) { | ||
extrinsics.forEach((extrinsic) => { | ||
const frame: IFrameMethod = extrinsic.method as IFrameMethod; | ||
if (frame.pallet === 'parachainSystem' && frame.method === 'setValidationData') { | ||
const data: any = extrinsic.args.data; | ||
data.downwardMessages.forEach((msg: any) => { | ||
if (msg.msg.length > 0) { | ||
const downwardMessage: IDownwardMessage[] = []; | ||
const xcmMessageDecoded = this.decodeMsg(api, msg.msg); | ||
downwardMessage.push({ | ||
sentAt: msg.sentAt, | ||
data: xcmMessageDecoded, | ||
}); | ||
xcmMessages.push({ | ||
downwardMessages: downwardMessage, | ||
}); | ||
} | ||
}); | ||
data.horizontalMessages.forEach((msg: [], index: string) => { | ||
msg.forEach((msg: any) => { | ||
const horizontalMessage: IHorizontalMessage[] = []; | ||
const xcmMessageDecoded = this.decodeMsg(api, msg.data.slice(1)); | ||
horizontalMessage.push({ | ||
sentAt: msg.sentAt, | ||
paraId: index, | ||
data: xcmMessageDecoded, | ||
}); | ||
xcmMessages.push({ | ||
horizontalMessages: horizontalMessage, | ||
}); | ||
}); | ||
}); | ||
} | ||
}); | ||
} | ||
return xcmMessages; | ||
} | ||
|
||
static checkUpwardMsg(api: ApiPromise, candidate: any): IUpwardMessage | undefined { | ||
if (candidate.candidate.commitments.upwardMessages.length > 0) { | ||
const xcmMessage: string = candidate.candidate.commitments.upwardMessages; | ||
const paraId: string = candidate.candidate.descriptor.paraId.toString(); | ||
const xcmMessageDecoded: string = this.decodeMsg(api, xcmMessage[0]); | ||
const upwardMessage = { | ||
paraId: paraId, | ||
data: xcmMessageDecoded[0], | ||
}; | ||
return upwardMessage; | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
|
||
static decodeMsg(api: ApiPromise, message: string): string { | ||
const instructions = []; | ||
let xcmMessage: string = message; | ||
let instructionLength = 0; | ||
while (xcmMessage.length != 0) { | ||
const xcmInstructions: Bytes = api.createType('XcmVersionedXcm', xcmMessage); | ||
instructions.push(xcmInstructions); | ||
instructionLength = xcmInstructions.toU8a().length; | ||
xcmMessage = xcmMessage.slice(instructionLength); | ||
} | ||
return instructions as unknown as string; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2017-2023 Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate API Sidecar. | ||
// | ||
// Substrate API Sidecar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
export interface IUpwardMessage { | ||
paraId: string; | ||
data: string; | ||
} | ||
|
||
export interface IDownwardMessage { | ||
sentAt: string; | ||
data: string; | ||
} | ||
|
||
export interface IHorizontalMessage { | ||
sentAt: string; | ||
paraId: string; | ||
data: string; | ||
} | ||
|
||
export interface IMessages { | ||
horizontalMessages?: IHorizontalMessage[]; | ||
downwardMessages?: IDownwardMessage[]; | ||
upwardMessages?: IUpwardMessage[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters