From 4a67b0fc2fd15c2d0aa3dcefc8413e5415c6bed1 Mon Sep 17 00:00:00 2001 From: girazoki Date: Thu, 25 Jan 2024 17:40:32 +0100 Subject: [PATCH] more tests --- ...ices_pament_credit_buying_free_combined.ts | 90 +++++++++++++++++++ .../test_services_payment credit_buying.ts | 88 ++++++++++++++++++ .../services-payment/test_services_payment.ts | 6 +- 3 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 test/suites/common-tanssi/services-payment/test_services_pament_credit_buying_free_combined.ts create mode 100644 test/suites/common-tanssi/services-payment/test_services_payment credit_buying.ts diff --git a/test/suites/common-tanssi/services-payment/test_services_pament_credit_buying_free_combined.ts b/test/suites/common-tanssi/services-payment/test_services_pament_credit_buying_free_combined.ts new file mode 100644 index 000000000..1a87941e6 --- /dev/null +++ b/test/suites/common-tanssi/services-payment/test_services_pament_credit_buying_free_combined.ts @@ -0,0 +1,90 @@ +import "@tanssi/api-augment"; +import { describeSuite, expect, beforeAll } from "@moonwall/cli"; +import { ApiPromise } from "@polkadot/api"; +import { KeyringPair } from "@moonwall/util"; +import { jumpSessions } from "util/block"; + +describeSuite({ + id: "CT0603", + title: "Services payment test suite", + foundationMethods: "dev", + testCases: ({ it, context }) => { + let polkadotJs: ApiPromise; + let alice: KeyringPair; + const blocksPerSession = 5n; + const paraId2000 = 2000n; + const paraId2001 = 2001n; + const costPerBlock = 1_000_000n; + beforeAll(async () => { + polkadotJs = context.polkadotJs(); + alice = context.keyring.alice; + }); + + it({ + id: "E01", + title: "Collators are unassigned when a container chain does not have enough credits", + test: async function () { + // Create blocks until authorNoting.blockNum does not increase anymore. + // Check that collatorAssignment does not have collators and num credits is less than 2 sessions. + + const tx2000free = polkadotJs.tx.servicesPayment.setCredits(paraId2000, 0n); + const tx2001free = polkadotJs.tx.servicesPayment.setCredits(paraId2001, 0n); + + await context.createBlock([await polkadotJs.tx.sudo.sudo(tx2000free).signAsync(alice)]); + await context.createBlock([await polkadotJs.tx.sudo.sudo(tx2001free).signAsync(alice)]); + + // 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(); + expect( + collators.toJSON().containerChains[paraId2000], + `Container chain ${paraId2000} should have 0 collators` + ).toBeUndefined(); + }, + }); + it({ + id: "E02", + title: "Collators are not assigned when we buy 2 session + ED -1", + test: async function () { + const tx2000OneSession = polkadotJs.tx.servicesPayment.setCredits(paraId2000, blocksPerSession); + await context.createBlock([await polkadotJs.tx.sudo.sudo(tx2000OneSession).signAsync(alice)]); + const existentialDeposit = await polkadotJs.consts.balances.existentialDeposit.toBigInt(); + // Now, buy some credits for container chain 2000. we only buy ones session -1 + const purchasedCredits = blocksPerSession * costPerBlock + existentialDeposit - 1n; + // Check that after 2 sessions, container chain 2000 has not collators + const tx = polkadotJs.tx.servicesPayment.purchaseCredits(paraId2000, purchasedCredits); + await context.createBlock([await tx.signAsync(alice)]); + + // 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(); + expect( + collators.toJSON().containerChains[paraId2000], + `Container chain ${paraId2000} should have 0 collators` + ).toBeUndefined(); + }, + }); + it({ + id: "E03", + title: "Collators are assigned when we buy at least 2 session + ED", + test: async function () { + // Now, buy the remaining + const purchasedCredits = 1n; + // Purchase the remaining 1 + const tx = polkadotJs.tx.servicesPayment.purchaseCredits(paraId2000, purchasedCredits); + await context.createBlock([await tx.signAsync(alice)]); + + // 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(); + expect( + collators.toJSON().containerChains[paraId2000].length, + `Container chain ${paraId2000} has 0 collators` + ).toBeGreaterThan(0); + }, + }); + }, +}); diff --git a/test/suites/common-tanssi/services-payment/test_services_payment credit_buying.ts b/test/suites/common-tanssi/services-payment/test_services_payment credit_buying.ts new file mode 100644 index 000000000..5d3e02c1e --- /dev/null +++ b/test/suites/common-tanssi/services-payment/test_services_payment credit_buying.ts @@ -0,0 +1,88 @@ +import "@tanssi/api-augment"; +import { describeSuite, expect, beforeAll } from "@moonwall/cli"; +import { ApiPromise } from "@polkadot/api"; +import { KeyringPair } from "@moonwall/util"; +import { jumpSessions } from "util/block"; + +describeSuite({ + id: "CT0603", + title: "Services payment test suite", + foundationMethods: "dev", + testCases: ({ it, context }) => { + let polkadotJs: ApiPromise; + let alice: KeyringPair; + const blocksPerSession = 5n; + const paraId2000 = 2000n; + const paraId2001 = 2001n; + const costPerBlock = 1_000_000n; + beforeAll(async () => { + polkadotJs = context.polkadotJs(); + alice = context.keyring.alice; + }); + + it({ + id: "E01", + title: "Collators are unassigned when a container chain does not have enough credits", + test: async function () { + // Create blocks until authorNoting.blockNum does not increase anymore. + // Check that collatorAssignment does not have collators and num credits is less than 2 sessions. + + const tx2000free = polkadotJs.tx.servicesPayment.setCredits(paraId2000, 0n); + const tx2001free = polkadotJs.tx.servicesPayment.setCredits(paraId2001, 0n); + + await context.createBlock([await polkadotJs.tx.sudo.sudo(tx2000free).signAsync(alice)]); + await context.createBlock([await polkadotJs.tx.sudo.sudo(tx2001free).signAsync(alice)]); + + // 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(); + expect( + collators.toJSON().containerChains[paraId2000], + `Container chain ${paraId2000} should have 0 collators` + ).toBeUndefined(); + }, + }); + it({ + id: "E02", + title: "Collators are not assigned when we buy 2 session + ED -1", + test: async function () { + const existentialDeposit = await polkadotJs.consts.balances.existentialDeposit.toBigInt(); + // Now, buy some credits for container chain 2000 + const purchasedCredits = 2n * blocksPerSession * costPerBlock + existentialDeposit - 1n; + // Check that after 2 sessions, container chain 2000 has not collators + const tx = polkadotJs.tx.servicesPayment.purchaseCredits(paraId2000, purchasedCredits); + await context.createBlock([await tx.signAsync(alice)]); + + // 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(); + expect( + collators.toJSON().containerChains[paraId2000], + `Container chain ${paraId2000} should have 0 collators` + ).toBeUndefined(); + }, + }); + it({ + id: "E03", + title: "Collators are assigned when we buy at least 2 session + ED", + test: async function () { + // Now, buy the remaining + const purchasedCredits = 1n; + // Check that after 2 sessions, container chain 2000 has collators and is producing blocks + const tx = polkadotJs.tx.servicesPayment.purchaseCredits(paraId2000, purchasedCredits); + await context.createBlock([await tx.signAsync(alice)]); + + // 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(); + expect( + collators.toJSON().containerChains[paraId2000].length, + `Container chain ${paraId2000} has 0 collators` + ).toBeGreaterThan(0); + }, + }); + }, +}); diff --git a/test/suites/common-tanssi/services-payment/test_services_payment.ts b/test/suites/common-tanssi/services-payment/test_services_payment.ts index 28c5fdf61..bd2170426 100644 --- a/test/suites/common-tanssi/services-payment/test_services_payment.ts +++ b/test/suites/common-tanssi/services-payment/test_services_payment.ts @@ -203,7 +203,7 @@ describeSuite({ await polkadotJs.query.system.account(randomAccount.address) ).data.free.toBigInt(); expect(balanceAfter).toBeLessThan(balanceBefore); - + const balanceTank = (await polkadotJs.query.system.account(paraIdTank(paraId))).data.free.toBigInt(); expect(balanceTank).toBe(requiredBalance); @@ -227,7 +227,9 @@ describeSuite({ expect(containerBlockNum3, "container chain 2000 did not create a block").toBeLessThan( containerBlockNum4 ); - const balanceTankAfter = (await polkadotJs.query.system.account(paraIdTank(paraId))).data.free.toBigInt(); + const balanceTankAfter = ( + await polkadotJs.query.system.account(paraIdTank(paraId)) + ).data.free.toBigInt(); expect(balanceTank, "container chain 2000 created a block without burning any credits").toBeGreaterThan( balanceTankAfter );