This repository has been archived by the owner on Jan 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
237 lines (192 loc) · 7.1 KB
/
app.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
import { ApiPromise, WsProvider } from "@polkadot/api"
import { AnyJson } from "@polkadot/types-codec/types"
import { execSync} from 'child_process';
import * as readlineSync from "readline-sync"
import * as fs from 'fs';
import * as path from 'path';
import { parachains, system } from "@polkadot/types/interfaces/definitions";
import { spawn } from 'child_process';
// this order determines the value for `relaychain` so handle with care.
const networkWsUrls: Record<string, string | undefined> = {
polkadot: "wss://rpc.polkadot.io",
kusama: "wss://kusama-rpc.polkadot.io",
}
// Declare parachainValues at the top-level scope
const parachainValues: any[] = [];
// Declare parachainValues at the top-level scope
const parachainCalls: any[] = [];
// final parachain values that are passed on to chopsticks cli
const parachainNames: any[] = [];
// the call fed to the relaychain
let preimage: any = [];
let chopChild: ChildProcess;
// save relay chain decision
var relaychain: string = "polkadot";
async function main() {
const networkOptions = Object.keys(networkWsUrls)
// Prompt the user for the network selection
const selectedIndex = readlineSync.keyInSelect(networkOptions, "Select a network:")
if (selectedIndex === 2) {
relaychain = "kusama";
}
if (selectedIndex === -1) {
console.log("Cancelled. Exiting...")
process.exitCode = 1
return
}
const selectedNetwork = networkOptions[selectedIndex]
await getRuntimeUpgradeByProposalHash(selectedNetwork)
}
async function getRuntimeUpgradeByProposalHash(network: string) {
const wsUrl = networkWsUrls[network]
if (typeof wsUrl !== "string") throw new Error("Invalid network selection")
// Ask the user for a proposal hash
const proposalHash = readlineSync.question("Enter the proposal hash: ", {
limit: (input) => input.length > 0,
limitMessage: "Input a valid proposal hash, please.",
})
// Ask the user for the len
const proposalLen = readlineSync.questionInt("Enter the len: ").toString()
const api = await ApiPromise.create({ provider: new WsProvider(wsUrl) })
try {
preimage = await (await api.query.preimage.preimageFor([proposalHash, proposalLen])).toHuman();
console.log("fetched preimage: ", preimage);
// Check for null fetch
if (preimage == null) {
console.log('Could not fetch preimage. Data has been cleared or inputs were wrong.')
return
}
// Display the results
const metadata = await api.rpc.state.getMetadata()
console.log("Metadata Version:", metadata.version.toString())
// Decode the extrinsic using the extrinsic type
const decodedExtr = api.createType("Call", preimage).toHuman()
console.log("Decoded Extrinsic:", formatCall(decodedExtr))
// display involved parachainID's
console.log("ParachainIDs:", parachainValues.toString())
// get their names
getParachainNames();
} catch (error) {
throw new Error(`Error fetching data for proposal hash ${proposalHash}: ${String(error)}`)
}
let chopChild;
try {
chopChild = await startChopsticks();
// at this point the process is launched. Time to feed it the call.
await chopsticksHandler(chopChild, preimage);
} catch (error) {
throw new Error(`Error dry running proposal hash ${proposalHash}: ${String(error)}`)
} finally {
api.disconnect();
if (chopChild) {
chopChild.kill();
}
}
}
// convert all paraID's to their name based on the relaychain decision.
// even needed?
function getParachainNames() {
// Example usage
const filePath = 'parachains.json';
const chainData = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
parachainValues.forEach((id) => {
console.log("id", id, "was resolved to name :", chainData.relaychain[relaychain][id]);
parachainNames.push(chainData.relaychain[relaychain][id]);
});
return
}
function formatCall(call: Record<string, AnyJson> | AnyJson | null, depth = 0): string {
if (call === null || call === undefined) return ""
return Object.entries(call)
.map(([key, value]) => {
// This assumes that each mention of parachain and a following call is coupled.
// track all parachainIDs
if (key === "Parachain") {
parachainValues.push(value);
}
// track all enocded calls towards a parachain
// atm not used.
if (key === "encoded") {
parachainCalls.push(value);
console.log('detected encoded call:', value, "for parachain:", parachainValues[parachainValues.length - 1])
}
// Handle arrays (e.g., nested calls)
if (Array.isArray(value))
return value
.map((item) => `${" ".repeat(depth * 2)}${key}:\n${formatCall(item, depth + 1)}`)
.join("")
// Handle nested objects
if (typeof value === "object")
return `${" ".repeat(depth * 2)}${key}:\n${formatCall(value, depth + 1)}`
// Display key-value pair
return `${" ".repeat(depth * 2)}${key}: ${value}\n`
})
.join("")
}
main()
.then(() => {if (chopChild) {
chopChild.kill()
};
process.exit(0)
})
.catch((error) => {
console.error(error)
process.exit(1)
})
async function chopsticksHandler(childProcess: ChildProcess, preimage): Promise<void> {
// Sleep for a few seconds to allow the child process to initialize
await new Promise(resolve => setTimeout(resolve, 40000)); // Sleep to wait for network setup.
// create the api of the local relay chain
const localAPI = await ApiPromise.create({ provider: new WsProvider('ws://127.0.0.1:8001') });
// feed the encoded call
const number = (await localAPI.rpc.chain.getHeader()).number.toNumber()
console.log("trying to feed the preimage", preimage);
console.log("chain height is ", number);
await localAPI.rpc('dev_setStorage', {
scheduler: {
agenda: [
[
[number + 1], [
{
call: {
Inline: preimage
},
origin: {
system: 'Root'
}
}
]
]
]
}
})
await localAPI.rpc('dev_newBlock');
return Promise.resolve();
}
// Modify startChopsticks to accept parachainNames as an argument
// no idea how to properly restrict the type :/
async function startChopsticks(): Promise<ChildProcess> {
return new Promise((resolve, reject) => {
// Construct the command and its arguments
const command = 'npx';
// TODO: create parachain arguments for each entry in parachainNames
const chopsticksArgs = [`@acala-network/chopsticks@latest`, `xcm`, `--relaychain=${relaychain}`, `--parachain=${parachainNames[0]}`];
console.log('Starting chopsticks with command:', `${command} ${chopsticksArgs.join(' ')}`);
// Spawn the child process
const child = spawn(command, chopsticksArgs, {
stdio: 'inherit',
shell: true,
detached: false
});
// Attach an error listener
child.on('error', (error) => {
reject(error);
});
child.on('exit', (code, signal) => {
console.log(`Child process exited with code ${code} and signal ${signal}`);
// Additional logic after child process exit can be placed here
});
// Resolve with the child process
resolve(child);
});
}