Skip to content

Commit

Permalink
Add balance transfer to zombie upgrade tests (#854)
Browse files Browse the repository at this point in the history
Co-authored-by: girazoki <[email protected]>
  • Loading branch information
tmpolaczyk and girazoki authored Feb 10, 2025
1 parent 4ba208f commit cc6e642
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MoonwallContext, beforeAll, describeSuite, expect } from "@moonwall/cli";
import { KeyringPair } from "@moonwall/util";
import { KeyringPair, generateKeyringPair } from "@moonwall/util";
import { ApiPromise, Keyring } from "@polkadot/api";
import fs from "node:fs";

Expand Down Expand Up @@ -55,13 +55,15 @@ describeSuite({
return;
} else {
log("Runtime not upgraded, proceeding with test");
log("Current runtime hash: " + rtHex.slice(0, 10) + "..." + rtHex.slice(-10));
log("New runtime hash: " + codeString.slice(0, 10) + "..." + codeString.slice(-10));
log("Current runtime spec version:", rtBefore);
log("Current runtime bytes: " + rtHex.slice(0, 10) + "..." + rtHex.slice(-10));
log("New runtime bytes: " + codeString.slice(0, 10) + "..." + codeString.slice(-10));
}

await context.upgradeRuntime({ from: alice, logger: log });
await context.waitBlock(2);
const rtafter = paraApi.consts.system.version.specVersion.toNumber();
log("New runtime spec version:", rtafter);
if (rtBefore === rtafter) {
throw new Error("Runtime upgrade failed");
}
Expand All @@ -70,5 +72,39 @@ describeSuite({
expect(blockNumberAfter, "Block number did not increase").to.be.greaterThan(blockNumberBefore);
},
});

it({
id: "T03",
title: "Can send balance transfers",
timeout: 600000,
test: async function () {
const randomAccount = generateKeyringPair("sr25519");

let tries = 0;
const balanceBefore = (await paraApi.query.system.account(randomAccount.address)).data.free.toBigInt();

/// It might happen that by accident we hit a session change
/// A block in which a session change occurs cannot hold any tx
/// Chopsticks does not have the notion of tx pool either, so we need to retry
/// Therefore we just retry at most MAX_BALANCE_TRANSFER_TRIES
const MAX_BALANCE_TRANSFER_TRIES = 5;
while (tries < MAX_BALANCE_TRANSFER_TRIES) {
const txHash = await paraApi.tx.balances
.transferAllowDeath(randomAccount.address, 1_000_000_000)
.signAndSend(alice);
await context.waitBlock(1);

const block = await paraApi.rpc.chain.getBlock();
const includedTxHashes = block.block.extrinsics.map((x) => x.hash.toString());
if (includedTxHashes.includes(txHash.toString())) {
break;
}
tries++;
}

const balanceAfter = (await paraApi.query.system.account(randomAccount.address)).data.free.toBigInt();
expect(balanceBefore < balanceAfter).to.be.true;
},
});
},
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MoonwallContext, beforeAll, describeSuite, expect } from "@moonwall/cli";
import { KeyringPair } from "@moonwall/util";
import { generateKeyringPair, KeyringPair } from "@moonwall/util";
import { ApiPromise, Keyring } from "@polkadot/api";
import fs from "node:fs";

Expand Down Expand Up @@ -50,13 +50,15 @@ describeSuite({
return;
} else {
log("Runtime not upgraded, proceeding with test");
log("Current runtime hash: " + rtHex.slice(0, 10) + "..." + rtHex.slice(-10));
log("New runtime hash: " + codeString.slice(0, 10) + "..." + codeString.slice(-10));
log("Current runtime spec version:", rtBefore);
log("Current runtime bytes: " + rtHex.slice(0, 10) + "..." + rtHex.slice(-10));
log("New runtime bytes: " + codeString.slice(0, 10) + "..." + codeString.slice(-10));
}

await context.upgradeRuntime({ from: alice, logger: log });
await context.waitBlock(2);
const rtafter = relayApi.consts.system.version.specVersion.toNumber();
log("New runtime spec version:", rtafter);
if (rtBefore === rtafter) {
throw new Error("Runtime upgrade failed");
}
Expand All @@ -65,5 +67,39 @@ describeSuite({
expect(blockNumberAfter, "Block number did not increase").to.be.greaterThan(blockNumberBefore);
},
});

it({
id: "T03",
title: "Can send balance transfers",
timeout: 600000,
test: async function () {
const randomAccount = generateKeyringPair("sr25519");

let tries = 0;
const balanceBefore = (await relayApi.query.system.account(randomAccount.address)).data.free.toBigInt();

/// It might happen that by accident we hit a session change
/// A block in which a session change occurs cannot hold any tx
/// Chopsticks does not have the notion of tx pool either, so we need to retry
/// Therefore we just retry at most MAX_BALANCE_TRANSFER_TRIES
const MAX_BALANCE_TRANSFER_TRIES = 5;
while (tries < MAX_BALANCE_TRANSFER_TRIES) {
const txHash = await relayApi.tx.balances
.transferAllowDeath(randomAccount.address, 1_000_000_000)
.signAndSend(alice);
await context.waitBlock(1);

const block = await relayApi.rpc.chain.getBlock();
const includedTxHashes = block.block.extrinsics.map((x) => x.hash.toString());
if (includedTxHashes.includes(txHash.toString())) {
break;
}
tries++;
}

const balanceAfter = (await relayApi.query.system.account(randomAccount.address)).data.free.toBigInt();
expect(balanceBefore < balanceAfter).to.be.true;
},
});
},
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MoonwallContext, beforeAll, describeSuite, expect } from "@moonwall/cli";
import { KeyringPair } from "@moonwall/util";
import { generateKeyringPair, KeyringPair } from "@moonwall/util";
import { ApiPromise, Keyring } from "@polkadot/api";
import { alith } from "@moonwall/util";

Expand Down Expand Up @@ -42,23 +42,25 @@ describeSuite({
const blockNumberBefore = (await paraApi.rpc.chain.getBlock()).block.header.number.toNumber();
const currentCode = await paraApi.rpc.state.getStorage(":code");
const codeString = currentCode.toString();
const rtBefore = paraApi.consts.system.version.specVersion.toNumber();

const wasm = fs.readFileSync((await MoonwallContext.getContext()).rtUpgradePath);
const rtHex = `0x${wasm.toString("hex")}`;
const rtBefore = paraApi.consts.system.version.specVersion.toNumber();

if (rtHex === codeString) {
log("Runtime already upgraded, skipping test");
return;
} else {
log("Runtime not upgraded, proceeding with test");
log("Current runtime hash: " + rtHex.slice(0, 10) + "..." + rtHex.slice(-10));
log("New runtime hash: " + codeString.slice(0, 10) + "..." + codeString.slice(-10));
log("Current runtime spec version:", rtBefore);
log("Current runtime bytes: " + rtHex.slice(0, 10) + "..." + rtHex.slice(-10));
log("New runtime bytes: " + codeString.slice(0, 10) + "..." + codeString.slice(-10));
}

await context.upgradeRuntime({ from: alice_or_alith, logger: log });
await context.waitBlock(2);
const rtafter = paraApi.consts.system.version.specVersion.toNumber();
log("New runtime spec version:", rtafter);
if (rtBefore === rtafter) {
throw new Error("Runtime upgrade failed");
}
Expand All @@ -67,5 +69,39 @@ describeSuite({
expect(blockNumberAfter, "Block number did not increase").to.be.greaterThan(blockNumberBefore);
},
});

it({
id: "T03",
title: "Can send balance transfers",
timeout: 600000,
test: async function () {
const randomAccount = generateKeyringPair("sr25519");

let tries = 0;
const balanceBefore = (await paraApi.query.system.account(randomAccount.address)).data.free.toBigInt();

/// It might happen that by accident we hit a session change
/// A block in which a session change occurs cannot hold any tx
/// Chopsticks does not have the notion of tx pool either, so we need to retry
/// Therefore we just retry at most MAX_BALANCE_TRANSFER_TRIES
const MAX_BALANCE_TRANSFER_TRIES = 5;
while (tries < MAX_BALANCE_TRANSFER_TRIES) {
const txHash = await paraApi.tx.balances
.transferAllowDeath(randomAccount.address, 1_000_000_000)
.signAndSend(alice_or_alith);
await context.waitBlock(1);

const block = await paraApi.rpc.chain.getBlock();
const includedTxHashes = block.block.extrinsics.map((x) => x.hash.toString());
if (includedTxHashes.includes(txHash.toString())) {
break;
}
tries++;
}

const balanceAfter = (await paraApi.query.system.account(randomAccount.address)).data.free.toBigInt();
expect(balanceBefore < balanceAfter).to.be.true;
},
});
},
});

0 comments on commit cc6e642

Please sign in to comment.