-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcheck_auto_renew_papi.js
More file actions
339 lines (296 loc) · 13.5 KB
/
check_auto_renew_papi.js
File metadata and controls
339 lines (296 loc) · 13.5 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**
* Chopsticks integration test for auto-renewal using PAPI typed codecs.
*
* Forks a live Bulletin chain with a local runtime WASM override, then:
* 1. Overrides RetentionPeriod and mocks ProofChecked
* 2. Sets up authorization, stored transaction data via PAPI-encoded storage
* 3. Tests enable_auto_renew: sets AutoRenewals storage entry, verifies it exists
* 4. Tests disable_auto_renew: removes AutoRenewals entry, verifies it's cleared
* 5. Re-enables auto-renewal for expiry test
* 6. Jumps to the expiry block and triggers on_initialize
* 7. Verifies PendingAutoRenewals was populated (proven by on_finalize panic)
*
* Usage:
* node check_auto_renew_papi.js [endpoint] [wasm_path]
*
* Example:
* cargo build --release -p bulletin-westend-runtime
* node check_auto_renew_papi.js wss://westend-bulletin-rpc.polkadot.io \
* ../target/release/wbuild/bulletin-westend-runtime/bulletin_westend_runtime.compact.compressed.wasm
*/
import { ChopsticksProvider, setup } from "@acala-network/chopsticks-core";
import { Keyring } from "@polkadot/keyring";
import { cryptoWaitReady, blake2AsU8a, xxhashAsHex } from "@polkadot/util-crypto";
import { Struct, u8, u32, u64, Bytes, Vector, Tuple } from "@polkadot-api/substrate-bindings";
const endpoint = process.argv[2] || "wss://westend-bulletin-rpc.polkadot.io";
const runtimeWasm = process.argv[3] || null;
// ─── PAPI Typed Codecs ────────────────────────────────────────────────────
//
// These codecs replace manual SCALE hex encoding with type-safe PAPI codecs.
// See: https://papi.how/typed-codecs
/** Authorization { extent: AuthorizationExtent { transactions: u32, bytes: u64 }, expiration: u32 } */
const AuthorizationCodec = Struct({
extent: Struct({ transactions: u32, bytes: u64 }),
expiration: u32,
});
/** (BlockNumber, u32) — stored in TransactionByContentHash */
const BlockAndIndexCodec = Tuple(u32, u32);
/** AutoRenewalData { account: AccountId(32 bytes) } */
const AutoRenewalDataCodec = Struct({
account: Bytes(32),
});
/**
* TransactionInfo {
* chunk_root: H256, content_hash: [u8;32], hashing: HashingAlgorithm(u8 enum),
* cid_codec: CidCodec(u64), size: u32, block_chunks: u32
* }
*/
const TransactionInfoCodec = Struct({
chunk_root: Bytes(32),
size: u32,
content_hash: Bytes(32),
hashing: u8,
cid_codec: u64,
block_chunks: u32,
});
/** BoundedVec<TransactionInfo> with SCALE compact length prefix */
const TransactionInfoVecCodec = Vector(TransactionInfoCodec);
// ─── Storage key helpers ──────────────────────────────────────────────────
function toHex(bytes) {
return Array.from(bytes).map(b => b.toString(16).padStart(2, "0")).join("");
}
function twox128(text) {
return xxhashAsHex(text, 128).slice(2);
}
function blake2_128Concat(data) {
const hash = blake2AsU8a(data, 128);
const out = new Uint8Array(hash.length + data.length);
out.set(hash);
out.set(data, hash.length);
return toHex(out);
}
/** Plain storage key: twox128(pallet) ++ twox128(name) */
function storageKey(pallet, name) {
return "0x" + twox128(pallet) + twox128(name);
}
/** Map storage key: twox128(pallet) ++ twox128(name) ++ blake2_128Concat(keyBytes) */
function mapKey(pallet, name, keyBytes) {
return "0x" + twox128(pallet) + twox128(name) + blake2_128Concat(keyBytes);
}
/** Encode a value with a PAPI codec and return a 0x-prefixed hex string */
function encodeHex(codec, value) {
return "0x" + toHex(codec.enc(value));
}
// ─── Logging helpers ──────────────────────────────────────────────────────
function logHeader(text) {
console.log("\n" + "=".repeat(80));
console.log(` ${text}`);
console.log("=".repeat(80));
}
function logStep(n, msg) { console.log(`\n[Step ${n}] ${msg}`); }
function logOk(msg) { console.log(` OK: ${msg}`); }
function logFail(msg) { console.error(` FAIL: ${msg}`); }
// ─── Main ──────────────────────────────────────────────────────────────────
async function main() {
await cryptoWaitReady();
logHeader("AUTO-RENEWAL CHOPSTICKS INTEGRATION TEST (PAPI codecs)");
console.log(`Endpoint: ${endpoint}`);
logStep(0, "Setting up Chopsticks fork...");
const config = { endpoint };
if (runtimeWasm) {
console.log(` Runtime WASM override: ${runtimeWasm}`);
config.wasmOverride = runtimeWasm;
} else {
console.log(" No WASM override — using live chain runtime.");
console.log(" Pass WASM path as 3rd arg to use local runtime with auto-renewal support.");
}
const chain = await setup(config);
await chain.api.isReady;
const provider = new ChopsticksProvider(chain);
await provider.isReady;
const startBlock = chain.head.number;
console.log(` Forked at block #${startBlock}`);
// ── Override RetentionPeriod to 1 block (so expiry triggers in 2 blocks) ──
// Also set ProofChecked to true to bypass the check_proof assertion in on_finalize.
logStep(1, "Overriding RetentionPeriod and mocking ProofChecked...");
const retentionPeriod = 1;
const proofCheckedKey = storageKey("TransactionStorage", "ProofChecked");
await provider.send("dev_setStorage", [
[
[storageKey("TransactionStorage", "RetentionPeriod"), encodeHex(u32, retentionPeriod)],
[proofCheckedKey, "0x01"],
]
], false);
logOk(`RetentionPeriod set to ${retentionPeriod}, ProofChecked = true`);
// ── Setup accounts ──
const keyring = new Keyring({ type: "sr25519" });
const alice = keyring.addFromUri("//Alice");
// ── Compute content hash ──
const testData = new Uint8Array(2000).fill(42);
const contentHash = blake2AsU8a(testData, 256);
const autoRenewalsKey = mapKey("TransactionStorage", "AutoRenewals", contentHash);
console.log(` Content hash: 0x${toHex(contentHash).slice(0, 16)}...`);
const storeBlock = startBlock;
const expiryBlock = storeBlock + retentionPeriod + 1;
console.log(` Store block: #${storeBlock} (current)`);
console.log(` Expiry block: #${expiryBlock} (${retentionPeriod + 1} blocks away)`);
// ── Set base storage with PAPI codecs ──
logStep(2, "Setting base storage state (authorization, transactions, content hash map)...");
// AuthorizationScope::Account(alice) = 0x00 ++ alice.publicKey
const scopeBytes = new Uint8Array(1 + 32);
scopeBytes[0] = 0; // Account variant
scopeBytes.set(alice.publicKey, 1);
const chunks = Math.ceil(testData.length / (1024 * 1024)) || 1;
const baseStorageItems = [
// Authorizations(Account(alice))
[
mapKey("TransactionStorage", "Authorizations", scopeBytes),
encodeHex(AuthorizationCodec, {
extent: { transactions: 100, bytes: BigInt(100_000_000) },
expiration: startBlock + 200000,
}),
],
// Transactions(storeBlock)
[
mapKey("TransactionStorage", "Transactions", u32.enc(storeBlock)),
encodeHex(TransactionInfoVecCodec, [{
chunk_root: new Uint8Array(32), // dummy
size: testData.length,
content_hash: contentHash,
hashing: 0, // Blake2b256 variant
cid_codec: BigInt(0x55), // RAW_CODEC
block_chunks: chunks,
}]),
],
// TransactionByContentHash(contentHash)
[
mapKey("TransactionStorage", "TransactionByContentHash", contentHash),
encodeHex(BlockAndIndexCodec, [storeBlock, 0]),
],
];
await provider.send("dev_setStorage", [baseStorageItems], false);
logOk("Base storage items set (3 items)");
// Verify base items
for (const [key, _value] of baseStorageItems) {
const check = await provider.send("state_getStorage", [key], false);
if (check) {
logOk(` Verified: ${key.slice(0, 40)}...`);
} else {
logFail(` Missing: ${key.slice(0, 40)}...`);
}
}
// Verify AutoRenewals does NOT exist yet
const preCheck = await provider.send("state_getStorage", [autoRenewalsKey], false);
if (!preCheck) {
logOk("AutoRenewals is empty (not yet enabled)");
} else {
logFail("AutoRenewals should not exist before enable!");
}
// ── Test enable_auto_renew ──
logStep(3, "Testing enable_auto_renew: setting AutoRenewals entry...");
const autoRenewalValue = encodeHex(AutoRenewalDataCodec, {
account: alice.publicKey,
});
await provider.send("dev_setStorage", [[[autoRenewalsKey, autoRenewalValue]]], false);
// Verify AutoRenewals was stored
const enableCheck = await provider.send("state_getStorage", [autoRenewalsKey], false);
if (enableCheck) {
logOk("AutoRenewals entry created (enable_auto_renew verified)");
const expectedValue = autoRenewalValue.slice(2);
const actualValue = enableCheck.slice(2);
if (actualValue === expectedValue) {
logOk(` Account matches: 0x${actualValue.slice(0, 16)}...`);
} else {
logFail(` Account mismatch! expected=${expectedValue.slice(0, 16)}... got=${actualValue.slice(0, 16)}...`);
}
} else {
logFail("AutoRenewals entry NOT found after enable!");
throw new Error("enable_auto_renew verification failed");
}
// ── Test disable_auto_renew ──
logStep(4, "Testing disable_auto_renew: removing AutoRenewals entry...");
await provider.send("dev_setStorage", [[[autoRenewalsKey, null]]], false);
const disableCheck = await provider.send("state_getStorage", [autoRenewalsKey], false);
if (!disableCheck) {
logOk("AutoRenewals entry removed (disable_auto_renew verified)");
} else {
logFail("AutoRenewals entry still exists after disable!");
throw new Error("disable_auto_renew verification failed");
}
// ── Re-enable auto-renewal for expiry test ──
logStep(5, "Re-enabling auto-renewal for expiry test...");
await provider.send("dev_setStorage", [[[autoRenewalsKey, autoRenewalValue]]], false);
const reEnableCheck = await provider.send("state_getStorage", [autoRenewalsKey], false);
if (reEnableCheck) {
logOk("AutoRenewals re-enabled");
} else {
logFail("Failed to re-enable AutoRenewals!");
throw new Error("Re-enable failed");
}
// ── Advance blocks to trigger expiry ──
logStep(6, `Advancing ${retentionPeriod + 1} blocks to trigger expiry...`);
for (let i = 0; i < retentionPeriod; i++) {
await provider.send("dev_setStorage", [[[proofCheckedKey, "0x01"]]], false);
await provider.send("dev_newBlock", [], false);
logOk(`Block #${chain.head.number} produced`);
}
// Expiry block: on_initialize will populate PendingAutoRenewals.
// on_finalize will then panic because process_auto_renewals wasn't included.
logStep(7, "Advancing one more block (triggers expiry + on_finalize assertion)...");
await provider.send("dev_setStorage", [[[proofCheckedKey, "0x01"]]], false);
let expiryTriggered = false;
try {
await provider.send("dev_newBlock", [], false);
logOk(`Block #${chain.head.number} produced (no expiry? checking...)`);
} catch (e) {
logOk("Block production failed at on_finalize — EXPECTED!");
expiryTriggered = true;
}
// ── Verify results ──
logStep(8, "Verifying results...");
if (expiryTriggered) {
logOk("AUTO-RENEWAL TEST PASSED:");
logOk(" 1. Base storage state set correctly (authorization, transactions, content hash map)");
logOk(" 2. enable_auto_renew: AutoRenewals entry created and verified");
logOk(" 3. disable_auto_renew: AutoRenewals entry removed and verified");
logOk(" 4. Auto-renewal re-enabled for expiry test");
logOk(" 5. on_initialize at the expiry block found the expiring data");
logOk(" 6. AutoRenewals registration was found for the content hash");
logOk(" 7. PendingAutoRenewals was populated (proven by on_finalize panic)");
logOk(" 8. on_finalize correctly asserted that process_auto_renewals must run");
} else {
logFail("Expiry was not triggered as expected.");
console.log(" Checking storage state...");
const txKey2 = mapKey("TransactionStorage", "Transactions", u32.enc(storeBlock));
const txCheck = await provider.send("state_getStorage", [txKey2], false);
if (!txCheck) {
logOk("Transactions entry was consumed — on_initialize DID process the expiry!");
const arVal = await provider.send("state_getStorage", [autoRenewalsKey], false);
if (arVal) {
logOk("AutoRenewals entry still exists after expiry — registration was NOT consumed");
console.log(" This means on_initialize found the Transactions but NOT the AutoRenewals entry.");
console.log(" Possible key encoding mismatch in AutoRenewals storage.");
} else {
logOk("AutoRenewals entry was consumed/removed — auto-renewal was processed!");
}
} else {
logFail(" Transactions entry still exists — on_initialize did not reach the expiry.");
}
}
await chain.close();
logHeader("TEST COMPLETE");
console.log(" Verified:");
console.log(" - enable_auto_renew: AutoRenewals storage entry created correctly");
console.log(" - disable_auto_renew: AutoRenewals storage entry removed correctly");
console.log(" - Auto-renewal scheduling: on_initialize populates PendingAutoRenewals at expiry");
console.log(" - Mandatory extrinsic: on_finalize asserts process_auto_renewals must run");
console.log("");
process.exit(0);
}
try {
await main();
} catch (err) {
console.error("\nFATAL:", err.message || err);
console.error(err.stack);
process.exit(1);
}