owned this note
owned this note
Published
Linked with GitHub
# Statemine <> Robonomics HRMP Channel Fix proposal
Referendum 207 ([Polkassembly]( https://kusama.polkassembly.io/referendum/207)) was supposed to create bi-directional HRMP channel by accepting channel request from Robonomics and initiating a channel from Statemine to Robonomics. The last stage of the referendum failed due insufficient weight provided for the XCM Transact operation to open channels. Hence the HRMP channels were not opened.
Here is the overview of what happened and fix proposal.
The same issue occurred in the Polkadot ecosystem, there is a postmortem and fix for it created by [@joepetrowski](https://hackmd.io/@joepetrowski) ([hackmd postmortem](https://hackmd.io/qAT35jE2Sn6IjBXlMXfuBg?view)). We used that report as a reference to fix the current issue.
## Issue overview
Stage one worked as expected and sent a message to Statemint:
![](https://i.imgur.com/Z6FB28Q.png)
([Pic1. Stage One Dispatch block 13,320,000](https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fkusama-rpc.polkadot.io#/explorer/query/13320000))
Stage two also worked as expected. Statemine received the message and sent one back with:
![](https://i.imgur.com/GLfM8M9.png)
([Pic2. Stage Two Dispatch block 2,368,721](https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fstatemine.api.onfinality.io%2Fpublic-ws#/explorer/query/2368721))
The required weight at most was set to `1_000_000_000`:
![](https://i.imgur.com/cwCprv3.png)
([Pic3. Stage Two Dispatch block 2,368,721](https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fstatemine.api.onfinality.io%2Fpublic-ws#/explorer/query/2368721))
Stage 3 is where the problems came up.
First, the instructions to open the HRMP channels failed because the required weight was underestimated (Pic3).
Second, because the call wasn't executed, some of the fee assets (about 0.999955 KSM) got _trapped_.
![](https://i.imgur.com/Zw0End1.png)
([Pic4. Stage Three Dispatch block 13,320,002](https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fkusama-rpc.polkadot.io#/explorer/query/13320002))
## Required weight calculation
### Manual weights calculation
#### Data
Kusama v9230
hrmp_init_open_channel 31_261_000, 9 reads, 5 writes
hrmp_accept_open_channel 28_688_000, 6 reads, 4 writes
rockdb read 25_000 per nano sec
rockdb write 100_000 per nano sec
batch_all 17_804_000 + (4_771_000 * number_of_items)
Sources:
- [hrmp weight](https://github.com/paritytech/polkadot/blob/v0.9.25-rc2/runtime/kusama/src/weights/runtime_parachains_hrmp.rs#L55)
- [rockdb reads/writes](https://github.com/paritytech/polkadot/blob/v0.9.23/runtime/kusama/constants/src/weights/rocksdb_weights.rs#L27)
- [utility batch all weight](https://github.com/paritytech/polkadot/blob/067c8a2847c841644d41d2df7c1cb137fdc8b014/runtime/kusama/src/weights/pallet_utility.rs#L57)
#### Calculation
``` rust=
fn main() {
const WEIGHT_PER_SECOND: i64 = 1_000_000_000_000;
const WEIGHT_PER_NANOS: i64 = WEIGHT_PER_SECOND / 1000 / 1000 / 1000;
let reads = 25_000 * WEIGHT_PER_NANOS;
let writes = 100_000 * WEIGHT_PER_NANOS;
let init_channel = 31_261_000 + 9 * reads + 5 * writes;
let accept_channel = 28_688_000 + 6 * reads + 4 * writes;
let batch_all = 17_804_000 + (4_771_000 * 2);
let total = init_channel + accept_channel + batch_all;
println!("{}", total); // 1_362_295_000
}
// playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=63d9a8406c75ff0dc7b0ed54d24be469
```
Weight: **1_362_295_000**
### Weights and fee calculation via substrate
There is an rpc call to calculate the weights and the fee for a given extrinsic. But it does not support currently `Call`s.
We created a small test within the current kusama runtime version to calculate the weights and the fees for our unsigned extrinsic.
Weight: **1_356_990_000**
Fee: **71_741_379 ≈ 0,000_071 KSM**
[Link to the test [debug branch]](https://github.com/paritytech/polkadot/blob/b2c248ecfa5cf54307f045317e781d1999c7e073/runtime/kusama/src/lib.rs#L2252L2281)
Considering the `1` multiplier we used to calculate the fee, and the fact we have enough of _trapped_ balance for the fix call we will use `2_000_000_000` as `requreWeightAtMost` weight.
### Call to open channels and claim trapped assets
Same as in the original referendum:
> 0x1802083c01000800003c0000080000e803000000900100
[Link to decode](https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fkusama-rpc.polkadot.io#/extrinsics/decode/0x1802083c01000800003c0000080000e803000000900100)
#### XCM message to execute on Statemint
We claim trapped assets and set `requireWeightAtMost` for `Transact` instruction to `2_000_000_000`. The rest is the same as in the original referendum.
![](https://i.imgur.com/gNm9KfB.png)
> 0x1f0001010002141804000000000760f0f8d1e8000013000000000760f0f8d1e800060003009435775c1802083c01000800003c0000080000e803000000900100140d01010000000004000101006d6f646c70792f74727372790000000000000000000000000000000000000000
[Link to decode](https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fstatemine-rpc.polkadot.io#/extrinsics/decode/0x1f0001010002141804000000000760f0f8d1e8000013000000000760f0f8d1e800060003009435775c1802083c01000800003c0000080000e803000000900100140d01010000000004000101006d6f646c70792f74727372790000000000000000000000000000000000000000)
#### To execute from Kusama on referendum
This time we do not need to transfer any amount from the Treasury and just send the previous XCM Transact instruction to the Statemint.
We also can use less weight than in the original call for `Transact` since its a weight just to send the message. The same test we used before calculates `100_000_000` units of weight, we can use double of it for safety.
![](https://i.imgur.com/jKamqBC.png)
> 0x630001000100a10f020406020208af2fb5011f0001010002141804000000000760f0f8d1e8000013000000000760f0f8d1e800060003009435775c1802083c01000800003c0000080000e803000000900100140d01010000000004000101006d6f646c70792f74727372790000000000000000000000000000000000000000
[Link to decode](https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fkusama-rpc.polkadot.io#/extrinsics/decode/0x630001000100a10f020406020208af2fb5011f0001010002141804000000000760f0f8d1e8000013000000000760f0f8d1e800060003009435775c1802083c01000800003c0000080000e803000000900100140d01010000000004000101006d6f646c70792f74727372790000000000000000000000000000000000000000)
## References
- [Substrate: Transactions, weights, and fee](https://docs.substrate.io/main-docs/build/tx-weights-fees/)
- [W3F Research: Slow adjusting mechanism](https://research.web3.foundation/en/latest/polkadot/overview/2-token-economics.html#-2.-slow-adjusting-mechanism)
- [Sub0 Online: Weights, Why and How - Gavin Wood, Founder of Polkadot](https://www.youtube.com/watch?v=bb0JotmiGEM)