paritytech / polkadot-sdk

The Parity Polkadot Blockchain SDK
https://polkadot.com/
1.89k stars 697 forks source link

[Help] Mapping XCM Extrinsics Between Polkadot and Asset Hub #5433

Closed HmFlashy closed 4 weeks ago

HmFlashy commented 2 months ago

Issue Description:

I'm trying to efficiently map XCM Extrinsics between Polkadot and Asset Hub. My current approach involves listening to new blocks on both chains and filtering transactions based on involved addresses. However, I'm facing challenges in reliably determining if a transaction has been successfully completed on the destination chain.

Specific Questions:

XCM Message Data: Is there a way to extract relevant information from XCM messages on the destination chain to correlate it with the source chain transaction? Transaction Status: How can I reliably determine if a transaction has been successfully completed on the Asset Hub? Are there specific events or status codes to look for? Performance Optimization: Are there any techniques or best practices for optimizing the process of tracking and correlating XCM transactions across chains?

Additional Context:

I'm using Polkadot.js API for interacting with the Polkadot and Asset Hub networks. I'm listening to new block events on both chains and filtering transactions based on involved addresses. I'm trying to ensure that when I send DOT funds from Polkadot to Asset Hub, I can confidently track the transaction's status and completion.

bkchr commented 2 months ago

CC @paritytech/xcm

acatangiu commented 2 months ago

You can check how e.g. Subscan does it, their code is open-source, and they are tracking crosschain transfers: e.g. for Polkadot Relay: dashboard, messages, transfers.

to your specific questions:

XCM Message Data: Is there a way to extract relevant information from XCM messages on the destination chain to correlate it with the source chain transaction?

Yes, XCM messages have a message_hash associated with them that can be tracked crosschain. E.g. Transfer from AssetHub to Interlay:

Transaction Status: How can I reliably determine if a transaction has been successfully completed on the Asset Hub? Are there specific events or status codes to look for?

yes, all transactions emit (pallet-specific) events saying whether they succeeded or not (outcome is the result): example event for pallet-xcm.

Performance Optimization: Are there any techniques or best practices for optimizing the process of tracking and correlating XCM transactions across chains?

I'm not aware of any better process other than following and correlating blocks and their events...

HmFlashy commented 2 months ago

Thanks a lot for your quick answer. But looking at this example, we can see the two extrinsics have been inserted in a block at the same time, so i am scared that I could miss some events on destination chain due to the fact it was too quick. Is there a way to compute the message id before hand ?

And the messageId could be not unique.

yes, all transactions emit (pallet-specific) events saying whether they succeeded or not (outcome is the result): example event for pallet-xcm.

It looks like this event is only for the local xcm execution, am I wrong ? https://github.com/paritytech/polkadot-sdk/blob/master/polkadot/xcm/pallet-xcm/src/lib.rs#L1790-L1797

HmFlashy commented 2 months ago

And I agree Subscan is doing a great job at it but we don't have the same problem. They are tracking and indexing everything that is happening on chain. So it is a bit easier to map the extrinsics for them. But we don't want to index every xcm message going through each parachain just for this, we just want to get the information we need, We need at some point to get the message id from the source chain and then catch the extrinsic with same messageId and same timestamp as the source chain extrinsic. And that all of this is robust and error prone and reconcilable.

One thing that could be great is if there was some Deposit event in the destination chain. Like this no need to map with message id, we would have just filter the extrinsic thanks to this event because we know which addresses to track. For transportAssets for example.

HmFlashy commented 2 months ago

I got one more little question. I found this XCM message. It failed on destination chain. How can we know what happened there ? I don't see any error log. https://assethub-polkadot.subscan.io/xcm_message/polkadot-90a5fe60af6be9e0fc7a91a60756356d5b07fdb4

acatangiu commented 2 months ago

The on-chain runtime doesn't provide much information now on XCM failures, unfortunately. One more thing to work on to improve..

The de-facto way to debug these things right now is to use a debug runtime and replay the transaction using chopsticks.

E.g. for replaying your exact block (where error occurs) using a custom runtime:

npx @acala-network/chopsticks@latest run-block --endpoint=wss://polkadot-asset-hub-rpc.polkadot.io --block 6825655  --wasm-override=/path/to/runtimes/asset_hub_polkadot_runtime.wasm --runtime-log-level=5
HmFlashy commented 2 months ago

Thanks for your help much clearer now.