Skip to content

Commit 57f35c6

Browse files
HoOngEemajecty
authored andcommitted
Add test case: ban from the candidates
1 parent 015509a commit 57f35c6

File tree

1 file changed

+130
-1
lines changed

1 file changed

+130
-1
lines changed

test/src/e2e.dynval/dv.double-vote.test.ts

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import {
2525
H256,
2626
signSchnorr,
2727
blake256,
28-
H512
28+
H512,
29+
PlatformAddress
2930
} from "codechain-primitives/lib";
3031
import "mocha";
3132
import * as RLP from "rlp";
@@ -339,6 +340,134 @@ describe("Report Double Vote", function() {
339340
});
340341
});
341342

343+
describe("Ban from the candidate state", async function() {
344+
const { nodes } = withNodes(this, {
345+
promiseExpect,
346+
validators: allDynValidators.map((signer, index) => ({
347+
signer,
348+
delegation: 5_000,
349+
deposit: 10_000_000 - index // tie-breaker
350+
})),
351+
overrideParams: {
352+
minNumOfValidators: 3
353+
}
354+
});
355+
356+
async function ensureAliceIsACandidate(sdk: SDK, blockNumber?: number) {
357+
const candidatesBefore = (await stake.getCandidates(
358+
sdk,
359+
blockNumber
360+
)).map(candidate =>
361+
PlatformAddress.fromPublic(candidate.pubkey, {
362+
networkId: "tc"
363+
}).toString()
364+
);
365+
expect(candidatesBefore).to.includes(
366+
alice.platformAddress.toString()
367+
);
368+
}
369+
370+
async function ensureAliceIsNotACandidate(
371+
sdk: SDK,
372+
blockNumber?: number
373+
) {
374+
const candidatesAfter = (await stake.getCandidates(
375+
sdk,
376+
blockNumber
377+
)).map(candidate =>
378+
PlatformAddress.fromPublic(candidate.pubkey, {
379+
networkId: "tc"
380+
}).toString()
381+
);
382+
expect(candidatesAfter).not.to.includes(
383+
alice.platformAddress.toString()
384+
);
385+
}
386+
387+
it("alice should be banned from the candidates", async function() {
388+
const termWaiter = setTermTestTimeout(this, {
389+
terms: 1.5
390+
});
391+
392+
const checkingNode = nodes[1];
393+
const blockNumber = await checkingNode.sdk.rpc.chain.getBestBlockNumber();
394+
const termMetadata = await stake.getTermMetadata(
395+
checkingNode.sdk,
396+
blockNumber
397+
);
398+
const currentTermInitialBlockNumber =
399+
termMetadata!.lastTermFinishedBlockNumber + 1;
400+
await checkingNode.waitBlockNumber(
401+
currentTermInitialBlockNumber + 1
402+
);
403+
404+
const aliceIdx = await getAliceIndex(
405+
checkingNode.sdk,
406+
currentTermInitialBlockNumber
407+
);
408+
409+
const revoketx = stake
410+
.createRevokeTransaction(
411+
checkingNode.sdk,
412+
alice.platformAddress,
413+
4_500
414+
)
415+
.sign({
416+
secret: faucetSecret,
417+
seq: await checkingNode.sdk.rpc.chain.getSeq(faucetAddress),
418+
fee: 10
419+
});
420+
const revokeTxHash = await checkingNode.sdk.rpc.chain.sendSignedTransaction(
421+
revoketx
422+
);
423+
await checkingNode.waitForTx(revokeTxHash);
424+
await termWaiter.waitNodeUntilTerm(checkingNode, {
425+
target: 2,
426+
termPeriods: 1
427+
});
428+
await expectPossibleAuthors(checkingNode.sdk, otherDynValidators);
429+
await ensureAliceIsACandidate(checkingNode.sdk);
430+
431+
const [message1, message2] = createDoubleVoteMessages(
432+
{
433+
height: currentTermInitialBlockNumber,
434+
view: 0,
435+
step: "precommit",
436+
privKey: alice.privateKey,
437+
signerIdx: aliceIdx
438+
},
439+
H256.ensure(
440+
"a3ea5219612cde721a61f099fadda0d23007e561b4c3a50d04097e5ac7ef1e24"
441+
),
442+
H256.ensure(
443+
"03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4"
444+
)
445+
);
446+
447+
const reportTx = createReportDoubleVoteTransaction(
448+
checkingNode.sdk,
449+
message1,
450+
message2
451+
);
452+
const reportTxHash = await checkingNode.sdk.rpc.chain.sendSignedTransaction(
453+
reportTx.sign({
454+
secret: faucetSecret,
455+
seq: await checkingNode.sdk.rpc.chain.getSeq(faucetAddress),
456+
fee: 10
457+
})
458+
);
459+
const blockNumberAfterReport = await waitUntilAliceGetBanned(
460+
checkingNode,
461+
reportTxHash
462+
);
463+
await ensureAliceIsBanned(checkingNode.sdk, blockNumberAfterReport);
464+
await ensureAliceIsNotACandidate(
465+
checkingNode.sdk,
466+
blockNumberAfterReport
467+
);
468+
});
469+
});
470+
342471
afterEach(async function() {
343472
promiseExpect.checkFulfilled();
344473
});

0 commit comments

Comments
 (0)