-
Notifications
You must be signed in to change notification settings - Fork 358
/
Copy pathtest-xcm-to-evm-pov.ts
257 lines (237 loc) · 8.51 KB
/
test-xcm-to-evm-pov.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import "@moonbeam-network/api-augment";
import { describeSuite, beforeAll, expect, deployCreateCompiledContract } from "@moonwall/cli";
import { Abi, encodeFunctionData } from "viem";
import { HeavyContract, deployHeavyContracts, expectOk } from "../../../../helpers";
import {
RawXcmMessage,
XcmFragment,
descendOriginFromAddress20,
injectHrmpMessage,
} from "../../../../helpers/xcm.js";
import { GAS_LIMIT_POV_RATIO } from "@moonwall/util";
describeSuite({
id: "D012806",
title: "XCM to EVM - PoV tests",
foundationMethods: "dev",
testCases: ({ context, log, it }) => {
let transferredBalance;
let sendingAddress: `0x${string}`;
let proxyAbi: Abi;
let proxyAddress: `0x${string}`;
const MAX_CONTRACTS = 15;
let contracts: HeavyContract[];
const EXPECTED_POV_ROUGH = 350_000; // bytes
let balancesPalletIndex: number;
beforeAll(async function () {
// Get Pallet balances index
const metadata = await context.polkadotJs().rpc.state.getMetadata();
const foundPallet = metadata.asLatest.pallets.find(
(pallet) => pallet.name.toString() === "Balances"
);
if (!foundPallet || !foundPallet.index) {
throw new Error("Balances pallet or its index not found");
}
balancesPalletIndex = foundPallet.index.toNumber();
// Get derived account
const { originAddress, descendOriginAddress } = descendOriginFromAddress20(context);
sendingAddress = originAddress;
transferredBalance = 10_000_000_000_000_000_000_000n;
// We first fund parachain 2000 sovreign account
await expectOk(
context.createBlock(
context
.polkadotJs()
.tx.balances.transferAllowDeath(descendOriginAddress, transferredBalance)
)
);
const balance = (
(await context.polkadotJs().query.system.account(descendOriginAddress)) as any
).data.free.toBigInt();
expect(balance).to.eq(transferredBalance);
const { abi, contractAddress } = await deployCreateCompiledContract(context, "CallForwarder");
proxyAbi = abi;
proxyAddress = contractAddress;
contracts = await deployHeavyContracts(context, 6000, 6000 + MAX_CONTRACTS);
});
it({
id: "T01",
title: "should fail to execute evm tx with insufficient gas to cover PoV",
test: async function () {
const GAS_LIMIT = 500_000;
const xcmTransactions = [
{
V1: {
gas_limit: GAS_LIMIT,
fee_payment: {
Auto: {
Low: null,
},
},
action: {
Call: proxyAddress,
},
value: 0n,
input: encodeFunctionData({
abi: proxyAbi,
functionName: "callRange",
args: [contracts[0].account, contracts[MAX_CONTRACTS].account],
}),
access_list: null,
},
},
];
const targetXcmWeight = 500_000n * 25000n + 25_000_000n + 800000000n;
const targetXcmFee = targetXcmWeight * 50_000n;
const transferCall = context
.polkadotJs()
.tx.ethereumXcm.transact(xcmTransactions[0] as any);
const transferCallEncoded = transferCall?.method.toHex();
// Build the XCM message
const xcmMessage = new XcmFragment({
assets: [
{
multilocation: {
parents: 0,
interior: {
X1: { PalletInstance: balancesPalletIndex },
},
},
fungible: targetXcmFee,
},
],
weight_limit: {
refTime: targetXcmWeight,
proofSize: (GAS_LIMIT / GAS_LIMIT_POV_RATIO) * 2,
} as any,
descend_origin: sendingAddress,
})
.descend_origin()
.withdraw_asset()
.buy_execution()
.push_any({
Transact: {
originKind: "SovereignAccount",
requireWeightAtMost: {
refTime: 12_525_000_000,
proofSize: GAS_LIMIT / GAS_LIMIT_POV_RATIO,
},
call: {
encoded: transferCallEncoded,
},
},
})
.as_v3();
// Send an XCM and create block to execute it
await injectHrmpMessage(context, 1, {
type: "XcmVersionedXcm",
payload: xcmMessage,
} as RawXcmMessage);
const { result, block } = await context.createBlock();
// With 500k gas we are allowed to use ~150k of POV, so verify the range.
// The tx is still included in the block because it contains the failed tx,
// so POV is included in the block as well.
expect(block.proofSize).to.be.at.least(130_000);
expect(block.proofSize).to.be.at.most(190_000);
// Check the evm tx was not executed because of OutOfGas error
const ethEvents = (await context.polkadotJs().query.system.events()).filter(({ event }) =>
context.polkadotJs().events.ethereum.Executed.is(event)
);
expect(ethEvents).to.have.lengthOf(1);
expect((ethEvents[0].toHuman() as any).event["data"]["exitReason"]["Error"]).equals(
"OutOfGas"
);
},
});
it({
id: "T02",
title: "should execute evm tx with enough gas to cover PoV",
test: async function () {
// Note: we can't use more than 1.6M gas through an XCM message, because it makes the entire
// message weight to go over the allowed weight to execute an XCM message. This is called
// "overweight".
//
// If we use more than 1.6M gas, we receive the "WeightLimitReached" error and
// "OverweightEnqueued" event from the xcmpQueue pallet.
const GAS_LIMIT = 1_600_000;
const xcmTransactions = [
{
V1: {
gas_limit: GAS_LIMIT,
fee_payment: {
Auto: {
Low: null,
},
},
action: {
Call: proxyAddress,
},
value: 0n,
input: encodeFunctionData({
abi: proxyAbi,
functionName: "callRange",
args: [contracts[0].account, contracts[MAX_CONTRACTS].account],
}),
access_list: null,
},
},
];
const targetXcmWeight = 1_600_000n * 25000n + 25_000_000n + 800000000n;
const targetXcmFee = targetXcmWeight * 50_000n;
const transferCall = context
.polkadotJs()
.tx.ethereumXcm.transact(xcmTransactions[0] as any);
const transferCallEncoded = transferCall?.method.toHex();
const xcmMessage = new XcmFragment({
assets: [
{
multilocation: {
parents: 0,
interior: {
X1: { PalletInstance: balancesPalletIndex },
},
},
fungible: targetXcmFee,
},
],
weight_limit: {
refTime: targetXcmWeight,
proofSize: (GAS_LIMIT / GAS_LIMIT_POV_RATIO) * 2,
} as any,
descend_origin: sendingAddress,
})
.descend_origin()
.withdraw_asset()
.buy_execution()
.push_any({
Transact: {
originKind: "SovereignAccount",
requireWeightAtMost: {
refTime: 40_025_000_000,
proofSize: GAS_LIMIT / GAS_LIMIT_POV_RATIO,
},
call: {
encoded: transferCallEncoded,
},
},
})
.as_v3();
// Send an XCM and create block to execute it
await injectHrmpMessage(context, 1, {
type: "XcmVersionedXcm",
payload: xcmMessage,
} as RawXcmMessage);
const { result, block } = await context.createBlock();
expect(block.proofSize).to.be.at.least(EXPECTED_POV_ROUGH / 1.1);
expect(block.proofSize).to.be.at.most(EXPECTED_POV_ROUGH * 1.1);
// Check the evm tx was executed successfully
const ethEvents = (await context.polkadotJs().query.system.events()).filter(({ event }) =>
context.polkadotJs().events.ethereum.Executed.is(event)
);
expect(ethEvents).to.have.lengthOf(1);
expect((ethEvents[0].toHuman() as any).event["data"]["exitReason"]["Succeed"]).equals(
"Stopped"
);
},
});
},
});