From c904a1a90959dfc8e87b603c779948c2ecc64a0b Mon Sep 17 00:00:00 2001 From: Nisheeth Barthwal Date: Tue, 25 Oct 2022 17:34:46 +0200 Subject: [PATCH] use suffixes to clear storage --- ...0-fix-staking-reserve-to-lock-migration.ts | 100 ++++++++++-------- 1 file changed, 54 insertions(+), 46 deletions(-) diff --git a/src/hotfixes/runtime-1900-fix-staking-reserve-to-lock-migration.ts b/src/hotfixes/runtime-1900-fix-staking-reserve-to-lock-migration.ts index e0a56e3..5af6611 100644 --- a/src/hotfixes/runtime-1900-fix-staking-reserve-to-lock-migration.ts +++ b/src/hotfixes/runtime-1900-fix-staking-reserve-to-lock-migration.ts @@ -18,6 +18,7 @@ import "@moonbeam-network/api-augment"; import { ApiPromise, Keyring } from "@polkadot/api"; import { getApiFor, NETWORK_YARGS_OPTIONS } from "../utils/networks"; import { blake2AsHex, xxhashAsHex } from "@polkadot/util-crypto"; +import { numberToHex } from "@polkadot/util"; const argv = yargs(process.argv.slice(2)) .usage("Usage: $0") @@ -86,57 +87,64 @@ async function main() { return keys.concat(await getAllKeys(api, prefix, keys[keys.length - 1])); } - const delegatorPrefix = + const delegatorPrefixBase = xxhashAsHex("ParachainStaking", 128) + xxhashAsHex("DelegatorReserveToLockMigrations", 128).slice(2); - const collatorPrefix = + const collatorPrefixBase = xxhashAsHex("ParachainStaking", 128) + xxhashAsHex("CollatorReserveToLockMigrations", 128).slice(2); - const delegatorKeys = await getAllKeys(api, delegatorPrefix); - const collatorKeys = await getAllKeys(api, collatorPrefix); - - console.log( - `DelegatorReserveToLockMigrations: ${delegatorKeys.length - .toString() - .padStart(6, " ")} (prefix: ${delegatorPrefix})` - ); - console.log( - ` CollatorReserveToLockMigrations: ${collatorKeys.length - .toString() - .padStart(6, " ")} (prefix: ${collatorPrefix})` - ); - - const toPropose = api.tx.utility.batch([ - api.tx.system.remarkWithEvent("State cleanup: reserve-to-lock storage items 1/1"), - api.tx.system.killPrefix(delegatorPrefix, delegatorKeys.length), - api.tx.system.killPrefix(collatorPrefix, collatorKeys.length), - ]); - let encodedProposal = toPropose?.method.toHex() || ""; - let encodedHash = blake2AsHex(encodedProposal); - console.log("Encoded proposal after schedule is", encodedProposal); - console.log("Encoded proposal hash after schedule is", encodedHash); - console.log("Encoded length", encodedProposal.length); - - if (argv["sudo"]) { - await api.tx.sudo.sudo(toPropose).signAndSend(account, { nonce: nonce++ }); - } else { - if (argv["send-preimage-hash"]) { - await api.tx.democracy - .notePreimage(encodedProposal) - .signAndSend(account, { nonce: nonce++ }); - } - - if (argv["send-proposal-as"] == "democracy") { - await api.tx.democracy - .propose(encodedHash, proposalAmount) - .signAndSend(account, { nonce: nonce++ }); - } else if (argv["send-proposal-as"] == "council-external") { - let external = api.tx.democracy.externalProposeMajority(encodedHash); - - await api.tx.councilCollective - .propose(collectiveThreshold, external, external.length) - .signAndSend(account, { nonce: nonce++ }); + for (let i = 0; i < 256; i++) { + const delegatorPrefix = `${delegatorPrefixBase}${numberToHex(i).slice(2)}`; + const collatorPrefix = `${collatorPrefixBase}${numberToHex(i).slice(2)}`; + + const delegatorKeys = await getAllKeys(api, delegatorPrefix); + const collatorKeys = await getAllKeys(api, collatorPrefix); + + console.log( + `DelegatorReserveToLockMigrations: ${delegatorKeys.length + .toString() + .padStart(6, " ")} (prefix: ${delegatorPrefix})` + ); + console.log( + ` CollatorReserveToLockMigrations: ${collatorKeys.length + .toString() + .padStart(6, " ")} (prefix: ${collatorPrefix})` + ); + + const toPropose = api.tx.scheduler.scheduleAfter(i + 1, null, 0, { + Value: api.tx.utility.batch([ + api.tx.system.remarkWithEvent("State cleanup: reserve-to-lock storage items 1/1"), + api.tx.system.killPrefix(delegatorPrefix, delegatorKeys.length), + api.tx.system.killPrefix(collatorPrefix, collatorKeys.length), + ]), + }); + let encodedProposal = toPropose?.method.toHex() || ""; + let encodedHash = blake2AsHex(encodedProposal); + console.log("Encoded proposal after schedule is", encodedProposal); + console.log("Encoded proposal hash after schedule is", encodedHash); + console.log("Encoded length", encodedProposal.length); + + if (argv["sudo"]) { + await api.tx.sudo.sudo(toPropose).signAndSend(account, { nonce: nonce++ }); + } else { + if (argv["send-preimage-hash"]) { + await api.tx.democracy + .notePreimage(encodedProposal) + .signAndSend(account, { nonce: nonce++ }); + } + + if (argv["send-proposal-as"] == "democracy") { + await api.tx.democracy + .propose(encodedHash, proposalAmount) + .signAndSend(account, { nonce: nonce++ }); + } else if (argv["send-proposal-as"] == "council-external") { + let external = api.tx.democracy.externalProposeMajority(encodedHash); + + await api.tx.councilCollective + .propose(collectiveThreshold, external, external.length) + .signAndSend(account, { nonce: nonce++ }); + } } } } finally {