-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathprepare-state-for-zombienet.js
More file actions
269 lines (240 loc) · 12.9 KB
/
Copy pathprepare-state-for-zombienet.js
File metadata and controls
269 lines (240 loc) · 12.9 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
const fs = require('fs');
const {TypeRegistry} = require('@polkadot/types');
const {hexToU8a, u8aToHex} = require('@polkadot/util');
const { xxhashAsHex } = require('@polkadot/util-crypto');
// Define network names
const NEW_NAME = process.env.CHAIN_NAME || "Hydration Local Testnet";
const NEW_ID = process.env.CHAIN_ID || "local_testnet";
const NEW_RELAY_CHAIN = "rococo_local_testnet";
// Define replacement values
const AURA_AUTHORITIES_VALUE = "0x08d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48";
const COUNCIL_AND_TECHNICAL_COMMITTEE_VALUE = "0x04d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d";
const SYSTEM_ACCOUNT_VALUE = "0x00000000000000000100000000000000ba31bc09df123864f700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
async function updateChainSpec(inputFile, outputFile) {
if (fs.existsSync(outputFile)) {
console.log(`Output file ${outputFile} already exists, skipping processing...`);
return;
}
console.log('Starting the chain spec update script...');
let chainSpec;
try {
chainSpec = JSON.parse(fs.readFileSync(inputFile, 'utf8'));
} catch (err) {
console.error('Error reading the chain spec file:', err);
process.exit(1);
}
// Create a new registry with custom types
const registry = new TypeRegistry();
registry.register({
HydraDxMathRatio: {
n: 'u128',
d: 'u128',
},
HydradxTraitsOracleVolume: {
aIn: 'u128',
bOut: 'u128',
aOut: 'u128',
bIn: 'u128',
},
HydradxTraitsOracleLiquidity: {
a: 'u128',
b: 'u128',
},
EmaOracleEntry: {
price: 'HydraDxMathRatio',
volume: 'HydradxTraitsOracleVolume',
liquidity: 'HydradxTraitsOracleLiquidity',
sharesIssuance: 'Option<u128>',
updatedAt: 'u32',
},
OracleValue: '(EmaOracleEntry, u32)',
PalletLiquidityMiningFarmState: {
_enum: ['Active', 'Terminated']
},
Perquintill: 'u64',
FixedU128: 'u128',
AccountId32: '[u8; 32]',
PalletLiquidityMiningGlobalFarmData: {
id: 'u32',
owner: 'AccountId32',
updatedAt: 'u32',
totalSharesZ: 'u128',
accumulatedRpz: 'FixedU128',
rewardCurrency: 'u32',
pendingRewards: 'u128',
accumulatedPaidRewards: 'u128',
yieldPerPeriod: 'Perquintill',
plannedYieldingPeriods: 'u32',
blocksPerPeriod: 'u32',
incentivizedAsset: 'u32',
maxRewardPerPeriod: 'u128',
minDeposit: 'u128',
liveYieldFarmsCount: 'u32',
totalYieldFarmsCount: 'u32',
priceAdjustment: 'FixedU128',
state: 'PalletLiquidityMiningFarmState'
},
PalletLiquidityMiningLoyaltyCurve: {
initialRewardPercentage: 'FixedU128',
scaleCoef: 'u32',
},
PalletLiquidityMiningYieldFarmData: {
id: 'u32',
updatedAt: 'u32',
totalShares: 'u128',
totalValuedShares: 'u128',
accumulatedRpvs: 'FixedU128',
accumulatedRpz: 'FixedU128',
loyaltyCurve: 'Option<PalletLiquidityMiningLoyaltyCurve>',
multiplier: 'FixedU128',
state: 'PalletLiquidityMiningFarmState',
entriesCount: 'u64',
leftToDistribute: 'u128',
totalStopped: 'u32',
},
});
const governance = process.env.KEEP_GOVERNANCE ? {} : {
"0xaebd463ed9925c488c112434d61debc0ba7fb8745735dc3be2a2c61a72c39e78": COUNCIL_AND_TECHNICAL_COMMITTEE_VALUE, // Council.members
"0xed25f63942de25ac5253ba64b5eb64d1ba7fb8745735dc3be2a2c61a72c39e78": COUNCIL_AND_TECHNICAL_COMMITTEE_VALUE, // TechnicalCommittee.members
}
const deployer = process.env.NO_DEPLOYER ? {} : {
// EvmAccounts.ContractDeployer - allows deploying contracts
"0x2c2b3fbb4fc221c42de8259db454678f7c36c2657df4391d8b829a0c1347bd1ef065818ad972112ab4e06ea85b354e36222222ff7be76052e023ec1a306fcca8f9659d80": "0x", // ContractDeployer 0x222222ff7Be76052e023Ec1a306fCca8F9659D80
// EvmAccounts.ApprovedContract - allows managing balances/tokens
"0x2c2b3fbb4fc221c42de8259db454678fe74405d2678f6b81824443771f6fa86af065818ad972112ab4e06ea85b354e36222222ff7be76052e023ec1a306fcca8f9659d80": "0x", // ApprovedContract 0x222222ff7Be76052e023Ec1a306fCca8F9659D80
// System account balance for 0x222222
"0x99971b5749ac43e0235e41b0d37869188ee7418a6531173d60d1f6a82d8f4d5173d3a4140c3587d7bc56f1a1c01a1c5e45544800222222ff7be76052e023ec1a306fcca8f9659d8000000000000000001f0e76f06ebd150314000000": "0x000064a7b3b6e00d00000000000000000000000000000000000000000000000000000000000000000000000000000000", // 1 ETH for 0x222222
}
// Define replacements
const REPLACEMENTS = {
"0x0d715f2646c8f85767b5d2764bb2782604a74d81251e398fd8a0a4d55023bb3f": "0xf2070000", // parachainInfo.parachainId = 2034
"0x57f8dc2f5ab09467896f47300f0424385e0621c4869aa60c02be9adcc98a0d1d": AURA_AUTHORITIES_VALUE, // aura.authorities
"0x3c311d57d4daf52904616cf69648081e5e0621c4869aa60c02be9adcc98a0d1d": AURA_AUTHORITIES_VALUE, // auraExt.authorities
"0xcec5070d609dd3497f72bde07fc96ba088dcde934c658227ee1dfafcd6e16903": AURA_AUTHORITIES_VALUE, // Session validators
"0x15464cac3378d46f113cd5b7a4d71c845579297f4dfb9609e7e4c2ebab9ce40a": AURA_AUTHORITIES_VALUE, // CollatorSelection.invulnerables
...governance,
"0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9de1e86a9a8c739864cf3cc5ec2bea59fd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d": SYSTEM_ACCOUNT_VALUE, // System account
...deployer,
};
// Set Configuration.IsTestnet to 1
const IS_TESTNET_KEY =
xxhashAsHex('Parameters', 128).replace('0x', '') +
xxhashAsHex('IsTestnet', 128).replace('0x', '');
REPLACEMENTS[`0x${IS_TESTNET_KEY}`] = '0x01';
// Define keys to delete
const KEYS_TO_DELETE = [
"0x26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac", // System.Number
"0x26aa394eea5630e07c48ae0c9558cef799e354094e5f3f9eddda2206fb22e261", // System.ParentHash
"0x45323df7cc47150b3930e2666b0aa313911a5dd3f1155f5b7d0c5aa102a757f9", // ParachainSystem.lastDmqMqcHead
"0x45323df7cc47150b3930e2666b0aa3133dca42deb008c6559ee789c9b9f70a2c", // ParachainSystem.lastHrmpMqcHeads
"0x45323df7cc47150b3930e2666b0aa313a2bca190d36bd834cc73a38fc213ecbd", // ParachainSystem.lastRelayChainBlockNumber
"0x7cda3cfa86b349fdafce4979b197118f948ece45793d7f15c9c0b9574ddbc665", // Elections.CandidateQueue
"0x7cda3cfa86b349fdafce4979b197118f7657ad2ff3a6742e1071bbb898ce5431", // Elections.Members
"0x7cda3cfa86b349fdafce4979b197118fba7fb8745735dc3be2a2c61a72c39e78", // Elections.RunnersUp
"0x7cda3cfa86b349fdafce4979b197118f40982df579bdf1315224f41e5f482063", // Elections.Votes
"0x5258a12472693b34a3ed25509781e55f3ffefddfbe00a43e565ba6114d1589ea", // Elections.StakeOf
"0xcec5070d609dd3497f72bde07fc96ba0e0cdd062e6eaf24295ad4ccfc41d4609", // Session.queuedKeys
"0xcec5070d609dd3497f72bde07fc96ba072763800a36a99fdfc7c10f6415f6ee6", // Session.currentIndex
];
// Define prefixes to delete
const PREFIXES_TO_DELETE = [
"0x7cda3cfa86b349fdafce4979b197118f71cd3068e6118bfb392b798317f63a89", // Elections.voting
"0x5258a12472693b34a3ed25509781e55f3ffefddfbe00a43e565ba6114d1589ea", // emaOracle.accumulator
"0xcec5070d609dd3497f72bde07fc96ba04c014e6bf8b8c2c011e7290b85696bb3", // Session.nextKeys
];
KEYS_TO_DELETE.forEach((key) => delete chainSpec.genesis.raw.top[key]);
// Process prefix-based deletions
for (const prefix of PREFIXES_TO_DELETE) {
for (const key of Object.keys(chainSpec.genesis.raw.top)) {
if (key.startsWith(prefix)) {
delete chainSpec.genesis.raw.top[key];
}
}
}
// Process EmaOracleEntry updates
console.log('Processing EmaOracleEntry & GlobalFarm updates...');
for (const [key, value] of Object.entries(chainSpec.genesis.raw.top)) {
if (key.startsWith("0x5258a12472693b34a3ed25509781e55fb79")) {
try {
const originalBytes = hexToU8a(value);
// ✅ Decode as (EmaOracleEntry, u32) tuple
const decoded = registry.createType('(EmaOracleEntry, u32)', originalBytes);
const [entry, _blockNumber] = decoded;
console.log(`🔍 Key: ${key}`);
//console.log(`🔍 entry (human):`, entry.toHuman());
//console.log(`🔍 blockNumber (u32):`, blockNumber.toHuman());
entry.set('updatedAt', registry.createType('u32', 0));
// Update: reset blockNumber to 0
const updated = registry.createType('(EmaOracleEntry, u32)', [entry, 0]);
const reEncodedBytes = updated.toU8a();
chainSpec.genesis.raw.top[key] = u8aToHex(reEncodedBytes);
console.log(`✅ Updated ${key} → blockNumber reset to 0`);
} catch (err) {
console.error(`❌ Error processing oracle key ${key}:`, err);
}
} else if (key.startsWith("0xa1a851f6ddab88c23c6615f42a0062df8d84255c07d18453a739a171ac5cf629") || key.startsWith("0xae438efb85a5af0e340133650eccd7638d84255c07d18453a739a171ac5cf629")) {
//NOTE: update omnipool's and xyk's LM global farms
try {
const decoded = registry.createType('PalletLiquidityMiningGlobalFarmData', hexToU8a(value));
const json = decoded.toJSON();
json.updatedAt = 0;
const updated = registry.createType('PalletLiquidityMiningGlobalFarmData', json);
chainSpec.genesis.raw.top[key] = u8aToHex(updated.toU8a());
} catch (err) {
console.error(`Error processing globalFarm for key ${key}:`, err);
}
} else if (key.startsWith("0xa1a851f6ddab88c23c6615f42a0062df7e1045c712fe23a3e89096e70b7ea444") || key.startsWith("0xae438efb85a5af0e340133650eccd7637e1045c712fe23a3e89096e70b7ea444")) {
//NOTE: update omnipool's and xyk's LM yield farms
try {
const decoded = registry.createType('PalletLiquidityMiningYieldFarmData', hexToU8a(value));
const json = decoded.toJSON();
json.updatedAt = 0;
const updated = registry.createType('PalletLiquidityMiningYieldFarmData', json);
chainSpec.genesis.raw.top[key] = u8aToHex(updated.toU8a());
} catch (err) {
console.error(`Error processing yeildFarm for key ${key}:`, err);
}
}
}
for (const [key, value] of Object.entries(REPLACEMENTS)) {
chainSpec.genesis.raw.top[key] = value;
}
// Optional: preauthorize a runtime upgrade so the fork comes up with
// System.AuthorizedUpgrade populated. A single system.applyAuthorizedUpgrade(code)
// call is then enough to enact the new wasm — no governance step required.
if (process.env.AUTHORIZE_UPGRADE_CODE_HASH) {
const raw = process.env.AUTHORIZE_UPGRADE_CODE_HASH.toLowerCase().replace(/^0x/, '');
if (!/^[0-9a-f]{64}$/.test(raw)) {
throw new Error(`AUTHORIZE_UPGRADE_CODE_HASH must be a 0x-prefixed 32-byte hex string, got: ${process.env.AUTHORIZE_UPGRADE_CODE_HASH}`);
}
const checkVersion = process.env.AUTHORIZE_UPGRADE_CHECK_VERSION === 'false' ? '00' : '01';
const key = '0x' +
xxhashAsHex('System', 128).replace('0x', '') +
xxhashAsHex('AuthorizedUpgrade', 128).replace('0x', '');
chainSpec.genesis.raw.top[key] = '0x' + raw + checkVersion;
console.log(`✅ Preauthorized runtime upgrade: code_hash=0x${raw} check_version=${checkVersion === '01'}`);
}
// Update metadata fields
chainSpec.name = NEW_NAME;
chainSpec.id = NEW_ID;
chainSpec.relay_chain = NEW_RELAY_CHAIN;
chainSpec.para_id = 2034;
// Save the updated chain spec
try {
fs.writeFileSync(outputFile, JSON.stringify(chainSpec));
console.log(`Chain spec updated successfully and saved to ${outputFile}`);
} catch (err) {
console.error('Error writing the updated chain spec file:', err);
}
console.log('Chain spec update script completed.');
}
const inputFile = process.argv[2];
const outputFile = process.argv[3];
if (!inputFile || !outputFile) {
console.error('Usage: node updateChainSpec.js <inputFile> <outputFile>');
process.exit(1);
}
updateChainSpec(inputFile, outputFile).catch((error) => {
console.error('Error updating chain spec:', error);
process.exit(1);
});