|
| 1 | +// Copyright 2019 Kodebox, Inc. |
| 2 | +// This file is part of CodeChain. |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as |
| 6 | +// published by the Free Software Foundation, either version 3 of the |
| 7 | +// License, or (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +import * as chai from "chai"; |
| 18 | +import { expect } from "chai"; |
| 19 | +import * as chaiAsPromised from "chai-as-promised"; |
| 20 | +import { H160, H512 } from "codechain-primitives/lib"; |
| 21 | +import * as stake from "codechain-stakeholder-sdk"; |
| 22 | +import "mocha"; |
| 23 | +import { validators as originalDynValidators } from "../../tendermint.dynval/constants"; |
| 24 | +import { PromiseExpect } from "../helper/promise"; |
| 25 | +import { selfNominate, withNodes } from "./setup"; |
| 26 | + |
| 27 | +chai.use(chaiAsPromised); |
| 28 | + |
| 29 | +const [alice, ...otherDynValidators] = originalDynValidators; |
| 30 | + |
| 31 | +describe("Nomination", function() { |
| 32 | + const promiseExpect = new PromiseExpect(); |
| 33 | + const NOMINATION_EXPIRATION = 2; |
| 34 | + const TERM_SECONDS = 20; |
| 35 | + const margin = 1.2; |
| 36 | + |
| 37 | + describe("Alice doesn't self nominate in NOMINATION_EXPIRATION", async function() { |
| 38 | + // alice : Elected as a validator, but does not send precommits and does not propose. |
| 39 | + // Alice should be jailed. |
| 40 | + // betty : Not elected as validator because of small delegation. She acquire more delegation in the first term. |
| 41 | + // betty should be a validator in the second term. |
| 42 | + const allDynNodes = withNodes(this, { |
| 43 | + promiseExpect, |
| 44 | + overrideParams: { |
| 45 | + nominationExpiration: NOMINATION_EXPIRATION, |
| 46 | + termSeconds: TERM_SECONDS |
| 47 | + }, |
| 48 | + validators: [ |
| 49 | + { signer: alice }, |
| 50 | + ...otherDynValidators.map((validator, index) => ({ |
| 51 | + signer: validator, |
| 52 | + delegation: 5000 - index, |
| 53 | + deposit: 100000 |
| 54 | + })) |
| 55 | + ] |
| 56 | + }); |
| 57 | + |
| 58 | + it("Alice be eligible after 2 terms", async function() { |
| 59 | + this.slow(TERM_SECONDS * 3 * margin * 1000); |
| 60 | + this.timeout(TERM_SECONDS * 4 * 1000); |
| 61 | + const [aliceNode, ...otherDynNodes] = allDynNodes; |
| 62 | + |
| 63 | + const selfNominationHash = await selfNominate( |
| 64 | + aliceNode.sdk, |
| 65 | + alice, |
| 66 | + 10 |
| 67 | + ); |
| 68 | + await aliceNode.waitForTx(selfNominationHash); |
| 69 | + |
| 70 | + const beforeCandidates = await stake.getCandidates( |
| 71 | + otherDynNodes[0].sdk |
| 72 | + ); |
| 73 | + |
| 74 | + expect( |
| 75 | + beforeCandidates.map(candidate => candidate.pubkey.toString()) |
| 76 | + ).to.includes(H512.ensure(alice.publicKey).toString()); |
| 77 | + |
| 78 | + await otherDynNodes[0].waitForTermChange( |
| 79 | + 4, |
| 80 | + TERM_SECONDS * margin * 3 |
| 81 | + ); |
| 82 | + |
| 83 | + const [validators, banned, candidates, jailed] = await Promise.all([ |
| 84 | + stake.getValidators(otherDynNodes[0].sdk), |
| 85 | + stake.getBanned(otherDynNodes[0].sdk), |
| 86 | + stake.getCandidates(otherDynNodes[0].sdk), |
| 87 | + stake.getJailed(otherDynNodes[0].sdk) |
| 88 | + ]); |
| 89 | + |
| 90 | + expect( |
| 91 | + validators.map(validator => validator.pubkey.toString()) |
| 92 | + ).not.to.includes(alice.publicKey); |
| 93 | + expect( |
| 94 | + banned.map(ban => ban.getAccountId().toString()) |
| 95 | + ).not.to.includes(alice.accountId); |
| 96 | + expect( |
| 97 | + candidates.map(canidate => canidate.pubkey.toString()) |
| 98 | + ).not.to.includes(alice.publicKey); |
| 99 | + expect(jailed.map(jail => jail.address)).not.to.includes( |
| 100 | + alice.platformAddress.toString() |
| 101 | + ); |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments