Skip to content

Commit

Permalink
New Weights, RefTime, ProofSize, RocksDB changes (#1001)
Browse files Browse the repository at this point in the history
* rev

* rev

* ref time / 4

* rev

* rev

* check in commit

* rev

* curr status

* rev

* rev

* format tables

* rev

* add

* rev

* rev

* rev

* rev

* Update learn/core-concepts/tx-fees.md

Co-authored-by: albertov19 <[email protected]>

* rev

* rev

* rev

* rev

---------

Co-authored-by: albertov19 <[email protected]>
  • Loading branch information
themacexpert and albertov19 authored Oct 18, 2024
1 parent 361a832 commit 1630fdb
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ApiPromise, WsProvider } from '@polkadot/api';

const main = async () => {
// Construct API provider
const wsProvider = new WsProvider('INSERT_WSS_ENDPOINT');
const api = await ApiPromise.create({ provider: wsProvider });

const allowedAssets =
await api.call.xcmPaymentApi.queryAcceptablePaymentAssets(4);
console.log(allowedAssets);

// Disconnect the API
await api.disconnect();
};

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { ApiPromise, WsProvider } from '@polkadot/api';

const main = async () => {
// Construct API provider
const wsProvider = new WsProvider('INSERT_WSS_ENDPOINT');
const api = await ApiPromise.create({ provider: wsProvider });

const amountToSend = BigInt(1 * 10 ** 12); // Sending 1 token (assuming 12 decimal places)
const assetMultiLocation = {
parents: 0,
interior: { X1: { PalletInstance: 3 } },
}; // The asset's location (adjust PalletInstance as needed)
const recipientAccount = '0x1234567890abcdef1234567890abcdef12345678'; // The recipient's account on the destination chain

// 2. XCM Destination (e.g., Parachain ID 2000)
const dest = { V3: { parents: 1, interior: { X1: { Parachain: 2000 } } } };

// 3. XCM Instruction 1: Withdraw the asset from the sender
const instr1 = {
WithdrawAsset: [
{
id: { Concrete: assetMultiLocation },
fun: { Fungible: amountToSend },
},
],
};

// 4. XCM Instruction 2: Deposit the asset into the recipient's account on the destination chain
const instr2 = {
DepositAsset: {
assets: { Wild: 'All' }, // Sending all withdrawn assets (in this case, 1 token)
beneficiary: {
parents: 0,
interior: { X1: { AccountKey20: { key: recipientAccount } } },
},
},
};

// 5. Build the XCM Message
const message = { V3: [instr1, instr2] };

const theWeight = await api.call.xcmPaymentApi.queryXcmWeight(message);
console.log(theWeight);

// Disconnect the API
await api.disconnect();
};

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ApiPromise, WsProvider } from '@polkadot/api';

const main = async () => {
// Construct API provider
const wsProvider = new WsProvider('INSERT_WSS_ENDPOINT');
const api = await ApiPromise.create({ provider: wsProvider });

const fee = await api.call.xcmPaymentApi.queryWeightToAssetFee(
{
refTime: 10_000_000_000n,
proofSize: 0n,
},
{
V3: {
Concrete: { parents: 1, interior: 'Here' },
},
}
);

console.log(fee);

// Disconnect the API
await api.disconnect();
};

main();
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ const instr1 = {
};
const instr2 = {
DepositAsset: {
assets: { Wild: 'All' },
assets: {
Wild: {
AllCounted: 1,
},
},
beneficiary: {
parents: 0,
interior: {
Expand All @@ -30,8 +34,7 @@ const instr2 = {
},
};
const message = { V4: [instr1, instr2] };
const maxWeight = { refTime: 400000000n, proofSize: 14484n };

const maxWeight = { refTime: 7250000000n, proofSize: 19374n };

const executeXcmMessage = async () => {
// 2. Create Keyring instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const instr2 = {
},
};
const message = { V4: [instr1, instr2] };
const maxWeight = { refTime: 400000000n, proofSize: 14484n };
const maxWeight = { refTime: 7250000000n, proofSize: 19374n };

const getEncodedXcmMessage = async () => {
// 2. Create Substrate API provider
Expand Down
114 changes: 83 additions & 31 deletions builders/interoperability/xcm/core-concepts/weights-fees.md

Large diffs are not rendered by default.

12 changes: 5 additions & 7 deletions builders/interoperability/xcm/send-execute-xcm.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,19 @@ The `execute` function of the Polkadot XCM Pallet accepts two parameters: `messa
- The multilocation of the beneficiary account on Moonbase Alpha

```js
--8<-- 'code/builders/interoperability/xcm/send-execute-xcm/execute/execute-with-polkadot.js:15:31'
--8<-- 'code/builders/interoperability/xcm/send-execute-xcm/execute/execute-with-polkadot.js:15:35'
```

3. Combine the XCM instructions into a versioned XCM message:

```js
--8<-- 'code/builders/interoperability/xcm/send-execute-xcm/execute/execute-with-polkadot.js:32:32'
--8<-- 'code/builders/interoperability/xcm/send-execute-xcm/execute/execute-with-polkadot.js:36:36'
```

4. Specify the `maxWeight`, which includes a value for `refTime` and `proofSize` that you will need to define:
- The `refTime` is the amount of computational time that can be used for execution. For this example, you can set it to `400000000` since the `refTime` for [`WithdrawAsset`](https://github.com/moonbeam-foundation/moonbeam/blob/{{networks.moonbase.spec_version}}/pallets/moonbeam-xcm-benchmarks/src/weights/fungible.rs#L36){target=\_blank} and [`DepositAsset`](https://github.com/moonbeam-foundation/moonbeam/blob/{{networks.moonbase.spec_version}}/pallets/moonbeam-xcm-benchmarks/src/weights/fungible.rs#L58){target=\_blank} is set to `200000000` each
- The `proofSize` is the amount of storage in bytes that can be used. You can set this to `14484` since the `proofSize` for [`WithdrawAsset`](https://github.com/moonbeam-foundation/moonbeam/blob/{{networks.moonbase.spec_version}}/pallets/moonbeam-xcm-benchmarks/src/weights/fungible.rs#L36){target=\_blank} and [`DepositAsset`](https://github.com/moonbeam-foundation/moonbeam/blob/{{networks.moonbase.spec_version}}/pallets/moonbeam-xcm-benchmarks/src/weights/fungible.rs#L58){target=\_blank} is set to `7242` each
4. Specify the `maxWeight`, which includes a value for `refTime` and `proofSize` that you will need to define. You can get both of these values by providing the XCM message as a parameter to the `queryXcmWeight` method of the [`xcmPaymentApi` runtime call](https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fmoonbeam-alpha.api.onfinality.io%2Fpublic-ws#/runtime){target=\_blank}.

```js
--8<-- 'code/builders/interoperability/xcm/send-execute-xcm/execute/execute-with-polkadot.js:33:33'
--8<-- 'code/builders/interoperability/xcm/send-execute-xcm/execute/execute-with-polkadot.js:37:37'
```

Now that you have the values for each of the parameters, you can write the script for the execution. You'll take the following steps:
Expand All @@ -178,7 +176,7 @@ Now that you have the values for each of the parameters, you can write the scrip
```
!!! note
You can view an example of the above script, which sends 1 DEV to Bobs's account on Moonbeam, on [Polkadot.js Apps](https://polkadot.js.org/apps/?rpc=wss://wss.api.moonbase.moonbeam.network#/extrinsics/decode/0x1c030408000400010403001300008a5d784563010d0100000103003cd0a705a2dc65e5b1e1205896baa2be8a07c6e00700e876481700){target=\_blank} using the following encoded calldata: `0x1c030408000400010403001300008a5d784563010d0100000103003cd0a705a2dc65e5b1e1205896baa2be8a07c6e00700e876481700`.
You can view an example of the above script, which sends 1 DEV to Bob's account on Moonbase Alpha, on [Polkadot.js Apps](https://polkadot.js.org/apps/?rpc=wss://wss.api.moonbase.moonbeam.network#/extrinsics/decode/0x1c030408000400010403001300008a5d784563010d010204000103003cd0a705a2dc65e5b1e1205896baa2be8a07c6e007803822b001ba2e0100){target=\_blank} using the following encoded calldata: `0x1c030408000400010403001300008a5d784563010d010204000103003cd0a705a2dc65e5b1e1205896baa2be8a07c6e007803822b001ba2e0100`.

Once the transaction is processed, the 0.1 DEV tokens should be withdrawn from Alice's account along with the associated XCM fees, and the destination account should have received 0.1 DEV tokens in their account. A `polkadotXcm.Attempted` event will be emitted with the outcome.
Expand Down
14 changes: 7 additions & 7 deletions builders/interoperability/xcm/xc20/send-xc20s/xtokens-pallet.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,13 @@ To determine the weight needed for XCM execution on the destination chain, you'l

In this example, where you're transferring xcUNIT from Moonbase Alpha to the Alphanet relay chain, the instructions that are executed on Alphanet are:

| Instruction | Weight |
|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|:-------------------------------------------------------------------:|
| [`WithdrawAsset`](https://github.com/paritytech/polkadot-sdk/blob/polkadot-{{ networks.alphanet.spec_version }}/polkadot/runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_fungible.rs#L54-L62){target=\_blank} | {{ networks.alphanet.xcm_instructions.withdraw.total_weight }} |
| [`ClearOrigin`](https://github.com/paritytech/polkadot-sdk/blob/polkadot-{{ networks.alphanet.spec_version }}/polkadot/runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_generic.rs#L134-L140){target=\_blank} | {{ networks.alphanet.xcm_instructions.clear_origin.total_weight }} |
| [`BuyExecution`](https://github.com/paritytech/polkadot-sdk/blob/polkadot-{{ networks.alphanet.spec_version }}/polkadot/runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_generic.rs#L75-L81){target=\_blank} | {{ networks.alphanet.xcm_instructions.buy_exec.total_weight }} |
| [`DepositAsset`](https://github.com/paritytech/polkadot-sdk/blob/polkadot-{{ networks.alphanet.spec_version }}/polkadot/runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_fungible.rs#L132-L140){target=\_blank} | {{ networks.alphanet.xcm_instructions.deposit_asset.total_weight }} |
| **TOTAL** | **{{ networks.alphanet.xcm_message.transfer.weight.display }}** |
| Instruction | Ref Time | Proof Size |
|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|:----------------------------------------------------------:|:------------------------------------------------------------:|
| [`WithdrawAsset`](https://github.com/moonbeam-foundation/moonbeam/blob/{{ networks.moonbeam.spec_version }}/pallets/moonbeam-xcm-benchmarks/src/weights/fungible.rs#L35){target=\_blank} | {{ xcm.generic_weights.ref_time.deposit_asset.display }} | {{ xcm.generic_weights.proof_size.deposit_asset.display }} |
| [`ClearOrigin`](https://github.com/moonbeam-foundation/moonbeam/blob/{{ networks.moonbeam.spec_version }}/pallets/moonbeam-xcm-benchmarks/src/weights/generic.rs#L191){target=\_blank} | {{ xcm.generic_weights.ref_time.clear_origin.display }} | {{ xcm.generic_weights.proof_size.zero }} |
| [`BuyExecution`](https://github.com/moonbeam-foundation/moonbeam/blob/{{ networks.moonbeam.spec_version }}/pallets/moonbeam-xcm-benchmarks/src/weights/generic.rs#L128-L129){target=\_blank} | {{ networks.alphanet.xcm_instructions.buy_exec.ref_time }} | {{ networks.alphanet.xcm_instructions.buy_exec.proof_size }} |
| [`DepositAsset`](https://github.com/moonbeam-foundation/moonbeam/blob/{{ networks.moonbeam.spec_version }}/pallets/moonbeam-xcm-benchmarks/src/weights/fungible.rs#L60){target=\_blank} | {{ xcm.generic_weights.ref_time.deposit_asset.display }} | {{ xcm.generic_weights.proof_size.deposit_asset.display }} |
| **TOTAL** | **{{ networks.alphanet.xcm_message.transfer.ref_time }}** | **{{ networks.alphanet.xcm_message.transfer.proof_size }}** |

!!! note
Some weights include database reads and writes; for example, the `WithdrawAsset` and `DepositAsset` instructions include both one database read and one write. To get the total weight, you'll need to add the weight of any required database reads or writes to the base weight of the given instruction.
Expand Down
16 changes: 12 additions & 4 deletions learn/core-concepts/tx-fees.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ There are some key differences between the transaction fee model on Moonbeam and

- The [dynamic fee mechanism](https://forum.moonbeam.network/t/proposal-status-idea-dynamic-fee-mechanism-for-moonbeam-and-moonriver/241){target=\_blank} resembles that of [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559){target=\_blank} but the implementation is different

- The amount of gas used in Moonbeam's transaction fee model is mapped from the transaction's Substrate extrinsic weight value via a fixed factor of {{ networks.moonbase.tx_weight_to_gas_ratio }}. This value is then multiplied with the unit gas price to calculate the transaction fee. This fee model means it can potentially be significantly cheaper to send transactions such as basic balance transfers via the Ethereum API than the Substrate API
- The amount of gas used in Moonbeam's transaction fee model is mapped from the transaction's Substrate extrinsic `refTime` component of the transaction weight via a fixed factor of `{{ networks.moonbase.tx_weight_to_gas_ratio }}` and `proofSize` component of the transaction weight via a fixed factor of `{{ xcm.generic_weights.proof_size.weight_per_gas }}`. The transaction weight vector is then multiplied with the unit gas price to calculate the transaction fee. This fee model means it can potentially be significantly cheaper to send transactions such as basic balance transfers via the Ethereum API than the Substrate API.

- The EVM is designed to solely have capacity for gas and Moonbeam requires additional metrics outside of gas. In particular, Moonbeam needs the ability to record proof size, which is the amount of storage required on Moonbeam for a relay chain validator to verify a state transition. When the capacity limit for proof size has been reached for the current block, which is 25% of the block limit, an "Out of Gas" error will be thrown. This can happen even if there is remaining *legacy* gas in the gasometer. This additional metric also impacts refunds. Refunds are based on the more consumed resource after the execution. In other words, if more proof size has been consumed proportionally than legacy gas, the refund will be calculated using proof size

Expand Down Expand Up @@ -385,16 +385,24 @@ The paths to the relevant values have also been truncated and reproduced below:

### Transaction Weight {: #transaction-weight}

`TransactionWeight` is a Substrate mechanism used to measure the execution time a given transaction takes to be executed within a block. For all transactions types, `TransactionWeight` can be retrieved under the event of the relevant extrinsic where the `method` field is set to:
`TransactionWeight` is a Substrate mechanism used to measure the execution time a given transaction takes to be executed within a block. A transaction's weight is a vector of two components: `refTime` and `proofSize`. `refTime` refers to the amount of computational time that can be used for execution. `proofSize` refers to the size of the PoV (Proof of Validity) of the Moonbeam block that gets submitted to the Polkadot Relay Chain for validation. Since both `refTime` and `proofSize` are integral components of determining a weight, it is impossible to obtain an accurate weight value with just one of these values.

For all transactions types, `TransactionWeight` can be retrieved under the event of the relevant extrinsic where the `method` field is set to:

```text
pallet: "system", method: "ExtrinsicSuccess"
```

And then `TransactionWeight` is mapped to the following field of the block JSON object:
And then `TransactionWeight` is mapped to the following two fields of the block JSON object. `proofSize` is mapped as follows:

```text
extrinsics[extrinsic_number].events[event_number].data[0].weight.proof_size
```

And `refTime` is mapped as follows:

```text
extrinsics[extrinsic_number].events[event_number].data[0].weight
extrinsics[extrinsic_number].events[event_number].data[0].weight.ref_time
```

### Fee History Endpoint {: #eth-feehistory-endpoint }
Expand Down
82 changes: 57 additions & 25 deletions variables.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ networks:
account_id_hex: '0xc4db7bcb733e117c0b34ac96354b10d47e84a006b9e7e66a229d174e8ff2a063'
instructions:
wei_per_weight:
display: 50,000
display: 12,500
units_per_second:
xcbetadev:
transact_numbers_only: 50000000000000000
Expand Down Expand Up @@ -456,7 +456,7 @@ networks:
account_id_hex: '0x5a071f642798f89d68b050384132eea7b65db483b00dbb05548d3ce472cfef48'
instructions:
wei_per_weight:
display: 50,000
display: 12,500
erc20_xcm:
transfer_gas_limit: 800,000 # corresponds to Erc20XcmBridgeTransferGasLimit
conviction:
Expand Down Expand Up @@ -841,21 +841,21 @@ networks:
account_id_hex: '0x5a071f642798f89d68b050384132eea7b65db483b00dbb05548d3ce472cfef48'
instructions:
wei_per_weight:
display: 5,000,000
numbers_only: 5000000
display: 1,250,000
numbers_only: 1250000
transfer_dot:
total_weight:
display: 686,274,000
numbers_only: 686274000
exec_time: 0.000686274
xcdot_cost: 0.0022694246
display: 4,428,242,000
numbers_only: 4428242000
exec_time: 0.004428242
xcdot_cost: 0.008892
transfer_glmr:
wei_cost: 1000000000000000
glmr_cost: 0.001
wei_cost: 250000000000000
glmr_cost: 0.00025
units_per_second:
xcdot:
display: 33,068,783,068
numbers_only: 33068783068
display: 20,080,321,285
numbers_only: 20080321285
erc20_xcm:
transfer_gas_limit: 400,000 # Erc20XcmBridgeTransferGasLimit
conviction:
Expand Down Expand Up @@ -1283,14 +1283,17 @@ networks:
clear_origin:
total_weight: 2,620,000
buy_exec:
total_weight: 2,823,000
ref_time: 348,048,000
proof_size: 19,056
deposit_asset:
total_weight: 150,445,000
xcm_message:
transfer:
weight:
display: 305,986,000
numbers_only: 305986000
ref_time: 8,103,242,000
proof_size: 4,979,056
moonbase_beta:
xcm_message:
transact:
Expand All @@ -1303,20 +1306,49 @@ polkadot_sdk: polkadot-v1.11.0
xcm:
db_weights:
rocksdb_read:
display: 25,000,000
numbers_only: 25000000
display: 41,742,000
numbers_only: 41742000
rocksdb_write:
display: 100,000,000
display: 81,283,000
fungible_weights:
display: 200,000,000
numbers_only: 200000000
generic_weights:
clear_origin:
display: 5,194,000
buy_exec:
base_weight:
display: 181,080,000
numbers_only: 181080000
total_weight:
display: 281,080,000
numbers_only: 281080000
weight_per_gas:
display: 25,000
numbers_only: 25000
ref_time:
mint_into_gas:
display: 155,000
numbers_only: 155000
deposit_asset:
display: 3,875,000,000
numbers_only: 3875000000
withdraw_asset:
display: 3,875,000,000
numbers_only: 3875000000
clear_origin:
display: 5,194,000
buy_exec:
base_weight:
display: 181,080,000
numbers_only: 181080000
total_weight:
display: 281,080,000
numbers_only: 281,080,000
proof_size:
zero: 0
weight_per_gas: 16
withdraw_asset:
display: 2,480,000
numbers_only: 2480000
buy_execution:
display: 19,056
numbers_only: 19056
deposit_asset:
display: 2,480,000
numbers_only: 2480000
transfer_dot_total:
display: 2,499,056
numbers_only: 2499056

0 comments on commit 1630fdb

Please sign in to comment.