Skip to content

Commit

Permalink
so far services payment tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
girazoki committed Sep 3, 2024
1 parent 66c75d2 commit 40919ae
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ import { describeSuite, expect, beforeAll } from "@moonwall/cli";
import { ApiPromise } from "@polkadot/api";
import { KeyringPair } from "@moonwall/util";
import { fetchCollatorAssignmentTip, jumpSessions } from "util/block";

import { paraIdTank } from "util/payment";
describeSuite({
id: "CT0608",
title: "Services payment collator assignment tip test suite",
foundationMethods: "dev",
testCases: ({ it, context }) => {
let polkadotJs: ApiPromise;
let alice: KeyringPair;

let collatorAssignmentAlias;
beforeAll(async () => {
polkadotJs = context.polkadotJs();
alice = context.keyring.alice;
const runtimeName = polkadotJs.runtimeVersion.specName.toString();
collatorAssignmentAlias = runtimeName.includes("light") ? polkadotJs.query.tanssiCollatorAssignment : polkadotJs.query.collatorAssignment;
});
it({
id: "E01",
Expand All @@ -33,7 +35,15 @@ describeSuite({
await context.createBlock([await polkadotJs.tx.sudo.sudo(txMaxTip).signAsync(alice)]);
await jumpSessions(context, 2);

const collators = await polkadotJs.query.collatorAssignment.collatorContainerChain();
const collators = await collatorAssignmentAlias.collatorContainerChain();

const balanceTank = (
await polkadotJs.query.system.account(paraIdTank(paraId))
).data.free.toBigInt();

console.log(balanceTank)
console.log(paraIdTank(paraId))

expect(
collators.toJSON().containerChains[paraId].length,
`Container chain ${paraId} should have 2 collators`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ describeSuite({
const paraId2001 = 2001n;
const costPerBlock = 1_000_000n;
let balanceTankBefore;
let registerAlias;
beforeAll(async () => {
polkadotJs = context.polkadotJs();
alice = context.keyring.alice;
const runtimeName = polkadotJs.runtimeVersion.specName.toString();
registerAlias = runtimeName.includes("light") ? polkadotJs.tx.containerRegistrar : polkadotJs.tx.registrar;

const tx2000OneSession = polkadotJs.tx.servicesPayment.setBlockProductionCredits(paraId2001, 0);
await context.createBlock([await polkadotJs.tx.sudo.sudo(tx2000OneSession).signAsync(alice)]);
const existentialDeposit = await polkadotJs.consts.balances.existentialDeposit.toBigInt();
Expand All @@ -34,7 +38,7 @@ describeSuite({
title: "We deregister 2000, check the issuance drops",
test: async function () {
// We deregister the chain
const deregister2001 = polkadotJs.tx.sudo.sudo(polkadotJs.tx.registrar.deregister(paraId2001));
const deregister2001 = polkadotJs.tx.sudo.sudo(registerAlias.deregister(paraId2001));
await context.createBlock([await deregister2001.signAsync(alice)]);
// Check that after 2 sessions, tank is empty and chain is deregistered
await jumpSessions(context, 2);
Expand All @@ -48,6 +52,7 @@ describeSuite({
const supplyBefore = (await apiAtBlockBefore.query.balances.totalIssuance()).toBigInt();
const supplyAfter = (await polkadotJs.query.balances.totalIssuance()).toBigInt();
const blockIssuance = await fetchIssuance(await polkadotJs.query.system.events());

const issuanceDiff = supplyAfter - supplyBefore;
expect(issuanceDiff, `Tank should have been removed`).toBe(
blockIssuance.amount.toBigInt() - balanceTankBefore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ describeSuite({
let refundAddress;
let balanceTankBefore;
let purchasedCredits;
let registerAlias;
beforeAll(async () => {
polkadotJs = context.polkadotJs();
alice = context.keyring.alice;
const runtimeName = polkadotJs.runtimeVersion.specName.toString();
registerAlias = runtimeName.includes("light") ? polkadotJs.tx.containerRegistrar : polkadotJs.tx.registrar;

refundAddress = generateKeyringPair("sr25519");
const tx2001OneSession = polkadotJs.tx.servicesPayment.setBlockProductionCredits(paraId2001, 0);
await context.createBlock([await polkadotJs.tx.sudo.sudo(tx2001OneSession).signAsync(alice)]);
Expand Down Expand Up @@ -51,7 +55,7 @@ describeSuite({
title: "On deregistration we refund the address",
test: async function () {
// We deregister the chain
const deregister2001 = polkadotJs.tx.sudo.sudo(polkadotJs.tx.registrar.deregister(paraId2001));
const deregister2001 = polkadotJs.tx.sudo.sudo(registerAlias.deregister(paraId2001));
await context.createBlock([await deregister2001.signAsync(alice)]);
// Check that after 2 sessions, tank is empty and chain is deregistered
await jumpSessions(context, 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ describeSuite({
const paraId2000 = 2000n;
const paraId2001 = 2001n;
const costPerBlock = 1_000_000n;
let collatorAssignmentAlias;
beforeAll(async () => {
polkadotJs = context.polkadotJs();
alice = context.keyring.alice;
const runtimeName = polkadotJs.runtimeVersion.specName.toString();
collatorAssignmentAlias = runtimeName.includes("light") ? polkadotJs.query.tanssiCollatorAssignment : polkadotJs.query.collatorAssignment;
});

it({
Expand All @@ -36,7 +39,7 @@ describeSuite({
// Check that after 2 sessions, container chain 2000 has collators and is producing blocks
await jumpSessions(context, 2);

const collators = await polkadotJs.query.collatorAssignment.collatorContainerChain();
const collators = await collatorAssignmentAlias.collatorContainerChain();
expect(
collators.toJSON().containerChains[paraId2000],
`Container chain ${paraId2000} should have 0 collators`
Expand All @@ -63,7 +66,7 @@ describeSuite({
// Check that after 2 sessions, container chain 2000 has 0 collators and is not producing blocks
await jumpSessions(context, 2);

const collators = await polkadotJs.query.collatorAssignment.collatorContainerChain();
const collators = await collatorAssignmentAlias.collatorContainerChain();
expect(
collators.toJSON().containerChains[paraId2000],
`Container chain ${paraId2000} should have 0 collators`
Expand All @@ -83,7 +86,7 @@ describeSuite({
// Check that after 2 sessions, container chain 2000 has collators and is producing blocks
await jumpSessions(context, 2);

const collators = await polkadotJs.query.collatorAssignment.collatorContainerChain();
const collators = await collatorAssignmentAlias.collatorContainerChain();
expect(
collators.toJSON().containerChains[paraId2000].length,
`Container chain ${paraId2000} has 0 collators`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ describeSuite({
const paraId2000 = 2000n;
const paraId2001 = 2001n;
const costPerSession = 100_000_000n;
let collatorAssignmentAlias
beforeAll(async () => {
polkadotJs = context.polkadotJs();
alice = context.keyring.alice;
const runtimeName = polkadotJs.runtimeVersion.specName.toString();
collatorAssignmentAlias = runtimeName.includes("light") ? polkadotJs.query.tanssiCollatorAssignment : polkadotJs.query.collatorAssignment;
});

it({
Expand All @@ -35,7 +38,7 @@ describeSuite({
// Check that after 2 sessions, container chain 2000 has collators and is producing blocks
await jumpSessions(context, 2);

const collators = await polkadotJs.query.collatorAssignment.collatorContainerChain();
const collators = await collatorAssignmentAlias.collatorContainerChain();
expect(
collators.toJSON().containerChains[paraId2000],
`Container chain ${paraId2000} should have 0 collators`
Expand All @@ -56,7 +59,7 @@ describeSuite({
// Check that after 2 sessions, container chain 2000 has 0 collators and is not producing blocks
await jumpSessions(context, 2);

const collators = await polkadotJs.query.collatorAssignment.collatorContainerChain();
const collators = await collatorAssignmentAlias.collatorContainerChain();
expect(
collators.toJSON().containerChains[paraId2000],
`Container chain ${paraId2000} should have 0 collators`
Expand All @@ -76,7 +79,7 @@ describeSuite({
// Check that after 2 sessions, container chain 2000 has collators and is producing blocks
await jumpSessions(context, 2);

const collators = await polkadotJs.query.collatorAssignment.collatorContainerChain();
const collators = await collatorAssignmentAlias.collatorContainerChain();
expect(
collators.toJSON().containerChains[paraId2000].length,
`Container chain ${paraId2000} has 0 collators`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ describeSuite({
const costPerSession = 100_000_000n;
const costPerBlock = 1_000_000n;
const blocksPerSession = 10n;
let collatorAssignmentAlias;
beforeAll(async () => {
polkadotJs = context.polkadotJs();
alice = context.keyring.alice;
const runtimeName = polkadotJs.runtimeVersion.specName.toString();
collatorAssignmentAlias = runtimeName.includes("light") ? polkadotJs.query.tanssiCollatorAssignment : polkadotJs.query.collatorAssignment;
});
it({
id: "E01",
title: "Genesis container chains have credits and collators and should have one less credit",
title: "Removing credits should make chains not get collators",
test: async function () {
const removeFreeCredits = polkadotJs.tx.utility.batch([
polkadotJs.tx.servicesPayment.setCollatorAssignmentCredits(paraId2000, 0n),
Expand All @@ -36,7 +39,7 @@ describeSuite({

await context.createBlock();
// Should not have assigned collators
const collators = await polkadotJs.query.collatorAssignment.collatorContainerChain();
const collators = await collatorAssignmentAlias.collatorContainerChain();

expect(
collators.toJSON().containerChains[paraId2000],
Expand All @@ -63,7 +66,7 @@ describeSuite({
// Check that after 2 sessions, container chain 2000 has 0 collators and is not producing blocks
await jumpSessions(context, 2);

const collators = await polkadotJs.query.collatorAssignment.collatorContainerChain();
const collators = await collatorAssignmentAlias.collatorContainerChain();
expect(
collators.toJSON().containerChains[paraId2000],
`Container chain ${paraId2000} should have 0 collators`
Expand All @@ -80,10 +83,10 @@ describeSuite({
const tx = polkadotJs.tx.servicesPayment.purchaseCredits(paraId2000, purchasedCredits);
await context.createBlock([await tx.signAsync(alice)]);

// Check that after 2 sessions, container chain 2000 has 0 collators and is not producing blocks
// Check that after 2 sessions, container chain 2000 has collators
await jumpSessions(context, 2);

const collators = await polkadotJs.query.collatorAssignment.collatorContainerChain();
const collators = await collatorAssignmentAlias.collatorContainerChain();
expect(
collators.toJSON().containerChains[paraId2000].length,
`Container chain ${paraId2000} has 0 collators`
Expand All @@ -97,7 +100,7 @@ describeSuite({
// Check that after 1 sessions
await jumpSessions(context, 1);

const collators = await polkadotJs.query.collatorAssignment.collatorContainerChain();
const collators = await collatorAssignmentAlias.collatorContainerChain();
expect(
collators.toJSON().containerChains[paraId2000],
`Container chain ${paraId2000} should have 0 collators`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import "@tanssi/api-augment";
import { describeSuite, expect, beforeAll } from "@moonwall/cli";
import { ApiPromise } from "@polkadot/api";
import { KeyringPair } from "@moonwall/util";
import { fetchCollatorAssignmentTip, jumpSessions } from "util/block";

describeSuite({
id: "CT0608",
title: "Services payment collator assignment tip test suite",
foundationMethods: "dev",
testCases: ({ it, context }) => {
let polkadotJs: ApiPromise;
let alice: KeyringPair;

beforeAll(async () => {
polkadotJs = context.polkadotJs();
alice = context.keyring.alice;
});
it({
id: "E01",
title: "Tip should prioritize collator assignment",
test: async function () {
await context.createBlock();

const paraId = 2001n;

const tip = 123;

const tx = polkadotJs.tx.servicesPayment.purchaseCredits(paraId, 1_000_000_000_000_000);
await context.createBlock([await tx.signAsync(alice)]);

const txMaxTip = polkadotJs.tx.servicesPayment.setMaxTip(paraId, tip);
// In genesis we have 4 collators, hence if we make 4 collators per para, we make sure the one
// with priority gets them
const changeCollatorsPerChain = polkadotJs.tx.collatorConfiguration.setCollatorsPerContainer(4);
await context.createBlock([await polkadotJs.tx.sudo.sudo(polkadotJs.tx.utility.batchAll([txMaxTip, changeCollatorsPerChain])).signAsync(alice)]);
await jumpSessions(context, 2);

const collators = await polkadotJs.query.tanssiCollatorAssignment.collatorContainerChain();
expect(
collators.toJSON().containerChains[paraId].length,
`Container chain ${paraId} should have 4 collators`
).toBe(4);

const events = await polkadotJs.query.system.events();
const tipEvent = fetchCollatorAssignmentTip(events);
expect(tipEvent.tip.toNumber()).to.be.equal(tip);
},
});
},
});
5 changes: 5 additions & 0 deletions test/util/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { filterAndApply } from "@moonwall/util";
import { ApiPromise } from "@polkadot/api";
import { AccountId32, EventRecord } from "@polkadot/types/interfaces";
import { Vec, u8, u32, bool } from "@polkadot/types-codec";
import { TypeRegistry } from '@polkadot/types';

export async function jumpSessions(context: DevModeContext, count: number): Promise<string | null> {
const session = (await context.polkadotJs().query.session.currentIndex()).addn(count.valueOf()).toNumber();

Expand Down Expand Up @@ -186,6 +188,9 @@ export function fetchIssuance(events: EventRecord[] = []) {
({ event }: EventRecord) => event.data as unknown as { amount: u128 }
);

if (filtered.length == 0) {
return { amount: (new TypeRegistry()).createType('u128', 0) }
}
return filtered[0];
}

Expand Down

0 comments on commit 40919ae

Please sign in to comment.