Skip to content

Commit

Permalink
chore(tools): adds 🛠️ script for clearing the storage previously used…
Browse files Browse the repository at this point in the history
… by LocalAssets pallet (#42)

Co-authored-by: root <[email protected]>
  • Loading branch information
RomarQ and noandrea authored Apr 11, 2024
1 parent 423a258 commit 543c776
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 149 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Use Node.js 16.x
uses: actions/setup-node@v2
uses: actions/setup-node@v4
with:
node-version: 16.x
node-version: 20.x
- name: Check with Prettier
run: npx prettier --check --ignore-path .prettierignore '**/*.(yml|js|ts|json)'
run: npx prettier --check --ignore-path .prettierignore '**/*.(yml|js|ts)'
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
This script will continuously call and wait for completion of unlockDemocracyFunds
extrinsic from pallet moonbeam-lazy-migrations until the migration is completed.
Ex: ./node_modules/.bin/ts-node unlock-democracy-funds.ts \
Ex: ./node_modules/.bin/ts-node src/lazy-migrations/001-unlock-democracy-funds.ts \
--url ws://127.0.0.1:34102 \
--account-priv-key <key> \
*/
Expand Down Expand Up @@ -50,7 +50,6 @@ const main = async () => {
).toHuman();
if (migrationCompleted === true) {
console.log("Democracy locks migration already completed. Exiting...");
return;
} else {
console.log("Unlocking democracy funds...");
while (migrationCompleted === false) {
Expand Down
91 changes: 91 additions & 0 deletions src/lazy-migrations/002-clear-local-assets-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Clears the unused storage of pallet LocalAssets removed in runtime 2800
Ex: ./node_modules/.bin/ts-node src/lazy-migrations/002-clear-local-assets-storage.ts \
--url ws://localhost:9944 \
--max-assets 10 \
--limit 1000 \
--account-priv-key <key> \
*/
import yargs from "yargs";
import "@polkadot/api-augment";
import "@moonbeam-network/api-augment";
import { Keyring } from "@polkadot/api";
import { KeyringPair } from "@polkadot/keyring/types";
import { getApiFor, NETWORK_YARGS_OPTIONS } from "../utils/networks";
import { monitorSubmittedExtrinsic, waitForAllMonitoredExtrinsics } from "../utils/monitoring";
import { ALITH_PRIVATE_KEY } from "../utils/constants";

const argv = yargs(process.argv.slice(2))
.usage("Usage: $0")
.version("1.0.0")
.options({
...NETWORK_YARGS_OPTIONS,
"account-priv-key": {
type: "string",
demandOption: false,
alias: "account",
},
"max-assets": {
type: "number",
default: 1,
describe: "The maximum number of assets to be removed by this call",
},
limit: {
type: "number",
default: 100,
describe: "The maximum number of storage entries to be removed by this call",
},
alith: {
type: "boolean",
demandOption: false,
conflicts: ["account-priv-key"],
},
})
.check((argv) => {
if (!(argv["account-priv-key"] || argv["alith"])) {
throw new Error("Missing --account-priv-key or --alith");
}
return true;
}).argv;

async function main() {
const api = await getApiFor(argv);
const keyring = new Keyring({ type: "ethereum" });

try {
const max_assets = argv["max-assets"];
const entries_to_remove = argv["limit"];

let account: KeyringPair;
let nonce;
const privKey = argv["alith"] ? ALITH_PRIVATE_KEY : argv["account-priv-key"];
if (privKey) {
account = keyring.addFromUri(privKey, null, "ethereum");
const { nonce: rawNonce, data: balance } = await api.query.system.account(account.address);
nonce = BigInt(rawNonce.toString());
}

const isMigrationCompleted = (
await api.query["moonbeamLazyMigrations"].localAssetsMigrationCompleted()
).toPrimitive();
if (isMigrationCompleted) {
throw new Error("Migration completed, all keys have been removed!");
}

const extrinsicCall = api.tx["moonbeamLazyMigrations"].clearLocalAssetsStorage(
max_assets,
entries_to_remove
);
await extrinsicCall.signAndSend(
account,
{ nonce: nonce++ },
monitorSubmittedExtrinsic(api, { id: "migration" })
);
} finally {
await waitForAllMonitoredExtrinsics();
await api.disconnect();
}
}

main().catch((err) => console.error("ERR!", err));
4 changes: 3 additions & 1 deletion src/libs/helpers/state-manipulator/genesis-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export interface StateManipulator {

// Will get executed for each line of the state file during the write phase
// Can decide to remove/keep the original line and also add extra lines
processWrite: (line: StateLine) => { action: Action; extraLines?: StateLine[] } | undefined | void;
processWrite: (
line: StateLine
) => { action: Action; extraLines?: StateLine[] } | undefined | void;
}

export function encodeStorageKey(module, name) {
Expand Down
Loading

0 comments on commit 543c776

Please sign in to comment.