|
| 1 | +/** |
| 2 | + * Script to assign extra cores to the bulletin parachain on the relay chain. |
| 3 | + * |
| 4 | + * This script constructs and submits a sudo(batch(assign_core)) extrinsic |
| 5 | + * to assign multiple cores to a specified parachain. |
| 6 | + * |
| 7 | + * Usage: |
| 8 | + * node assign_cores.js <relay_endpoint> <para_id> <cores...> |
| 9 | + * |
| 10 | + * Example: |
| 11 | + * node assign_cores.js ws://localhost:9942 1006 0 1 2 |
| 12 | + * (assigns cores 0, 1, and 2 to parachain 1006) |
| 13 | + */ |
| 14 | + |
| 15 | +const { ApiPromise, WsProvider, Keyring } = require("@polkadot/api"); |
| 16 | + |
| 17 | +async function connect(endpoint) { |
| 18 | + const provider = new WsProvider(endpoint); |
| 19 | + const api = await ApiPromise.create({ |
| 20 | + provider, |
| 21 | + throwOnConnect: false, |
| 22 | + }); |
| 23 | + return api; |
| 24 | +} |
| 25 | + |
| 26 | +async function assignCores(endpoint, paraId, cores) { |
| 27 | + console.log(`Connecting to relay chain at: ${endpoint}`); |
| 28 | + console.log(`Assigning cores [${cores.join(", ")}] to parachain ${paraId}`); |
| 29 | + |
| 30 | + const api = await connect(endpoint); |
| 31 | + |
| 32 | + // Wait for the API to be ready |
| 33 | + await api.isReady; |
| 34 | + |
| 35 | + // Create keyring and add Alice (used for sudo) |
| 36 | + const keyring = new Keyring({ type: "sr25519" }); |
| 37 | + const alice = keyring.addFromUri("//Alice"); |
| 38 | + |
| 39 | + // Create assign_core calls for each core |
| 40 | + // Each assignment is: (CoreAssignment::Task(para_id), PartsOf57600) |
| 41 | + // 57600 represents a full timeslice allocation |
| 42 | + const assignCoreCalls = cores.map((core) => { |
| 43 | + return api.tx.coretime.assignCore( |
| 44 | + core, // core number |
| 45 | + 0, // begin (immediate) |
| 46 | + [[{ Task: paraId }, 57600]], // assignment: [(Task(para_id), 57600)] |
| 47 | + null // end_hint: None |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + console.log(`Created ${assignCoreCalls.length} assign_core calls`); |
| 52 | + |
| 53 | + // Wrap in utility.batch |
| 54 | + const batchCall = api.tx.utility.batch(assignCoreCalls); |
| 55 | + console.log("Created batch call"); |
| 56 | + |
| 57 | + // Wrap in sudo |
| 58 | + const sudoCall = api.tx.sudo.sudo(batchCall); |
| 59 | + console.log("Created sudo call"); |
| 60 | + console.log(`Call data (hex): ${sudoCall.method.toHex()}`); |
| 61 | + |
| 62 | + // Sign and submit the transaction |
| 63 | + console.log("Submitting transaction..."); |
| 64 | + |
| 65 | + return new Promise((resolve, reject) => { |
| 66 | + sudoCall.signAndSend(alice, { nonce: -1 }, ({ status, events, dispatchError }) => { |
| 67 | + console.log(`Transaction status: ${status.type}`); |
| 68 | + |
| 69 | + if (status.isInBlock) { |
| 70 | + console.log(`Transaction included in block: ${status.asInBlock.toHex()}`); |
| 71 | + } |
| 72 | + |
| 73 | + if (status.isFinalized) { |
| 74 | + console.log(`Transaction finalized in block: ${status.asFinalized.toHex()}`); |
| 75 | + |
| 76 | + // Check for errors |
| 77 | + if (dispatchError) { |
| 78 | + if (dispatchError.isModule) { |
| 79 | + const decoded = api.registry.findMetaError(dispatchError.asModule); |
| 80 | + const { docs, name, section } = decoded; |
| 81 | + console.error(`Error: ${section}.${name}: ${docs.join(" ")}`); |
| 82 | + reject(new Error(`${section}.${name}`)); |
| 83 | + } else { |
| 84 | + console.error(`Error: ${dispatchError.toString()}`); |
| 85 | + reject(new Error(dispatchError.toString())); |
| 86 | + } |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // Log events |
| 91 | + events.forEach(({ event }) => { |
| 92 | + const { section, method, data } = event; |
| 93 | + console.log(` Event: ${section}.${method}`, data.toString()); |
| 94 | + }); |
| 95 | + |
| 96 | + // Check for sudo success |
| 97 | + const sudoSuccess = events.find(({ event }) => |
| 98 | + event.section === "sudo" && event.method === "Sudid" |
| 99 | + ); |
| 100 | + |
| 101 | + if (sudoSuccess) { |
| 102 | + const result = sudoSuccess.event.data[0]; |
| 103 | + if (result.isOk) { |
| 104 | + console.log("✅ Cores assigned successfully!"); |
| 105 | + resolve(); |
| 106 | + } else { |
| 107 | + console.error("❌ Sudo call failed:", result.asErr.toString()); |
| 108 | + reject(new Error("Sudo call failed")); |
| 109 | + } |
| 110 | + } else { |
| 111 | + console.log("✅ Transaction finalized (no sudo event found, checking events above)"); |
| 112 | + resolve(); |
| 113 | + } |
| 114 | + |
| 115 | + api.disconnect(); |
| 116 | + } |
| 117 | + }).catch((err) => { |
| 118 | + console.error("Error submitting transaction:", err); |
| 119 | + reject(err); |
| 120 | + }); |
| 121 | + }); |
| 122 | +} |
| 123 | + |
| 124 | +// Parse command line arguments |
| 125 | +const args = process.argv.slice(2); |
| 126 | + |
| 127 | +if (args.length < 3) { |
| 128 | + console.log("Usage: node assign_cores.js <relay_endpoint> <para_id> <cores...>"); |
| 129 | + console.log(""); |
| 130 | + console.log("Arguments:"); |
| 131 | + console.log(" relay_endpoint WebSocket endpoint of the relay chain (e.g., ws://localhost:9942)"); |
| 132 | + console.log(" para_id Parachain ID to assign cores to (e.g., 1006)"); |
| 133 | + console.log(" cores Space-separated list of core numbers to assign"); |
| 134 | + console.log(""); |
| 135 | + console.log("Example:"); |
| 136 | + console.log(" node assign_cores.js ws://localhost:9942 1006 0 1 2"); |
| 137 | + console.log(" (assigns cores 0, 1, and 2 to parachain 1006)"); |
| 138 | + process.exit(1); |
| 139 | +} |
| 140 | + |
| 141 | +const endpoint = args[0]; |
| 142 | +const paraId = parseInt(args[1], 10); |
| 143 | +const cores = args.slice(2).map((c) => parseInt(c, 10)); |
| 144 | + |
| 145 | +if (isNaN(paraId)) { |
| 146 | + console.error("Error: para_id must be a number"); |
| 147 | + process.exit(1); |
| 148 | +} |
| 149 | + |
| 150 | +for (const core of cores) { |
| 151 | + if (isNaN(core)) { |
| 152 | + console.error("Error: all core numbers must be integers"); |
| 153 | + process.exit(1); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +assignCores(endpoint, paraId, cores) |
| 158 | + .then(() => { |
| 159 | + console.log("Done!"); |
| 160 | + process.exit(0); |
| 161 | + }) |
| 162 | + .catch((err) => { |
| 163 | + console.error("Failed to assign cores:", err.message); |
| 164 | + process.exit(1); |
| 165 | + }); |
| 166 | + |
0 commit comments