From 6e88e7225359d885a8f34d4c1c092fe5ba00d8cc Mon Sep 17 00:00:00 2001 From: Sebastian Gerske <13647606+H34D@users.noreply.github.com> Date: Tue, 10 Dec 2019 16:31:32 +0100 Subject: [PATCH 1/7] make error output more understandable --- packages/protocol/runTests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/protocol/runTests.js b/packages/protocol/runTests.js index ca608d819a0..f5fabdbb74e 100644 --- a/packages/protocol/runTests.js +++ b/packages/protocol/runTests.js @@ -71,7 +71,7 @@ async function test() { const testFiles = glob.readdirSync(testGlob) if (testFiles.length === 0) { // tslint:disable-next-line: no-console - console.error(`No tests matched with ${argv._}`) + console.error(`No test files matched with ${testGlob}`) process.exit(1) } testArgs = testArgs.concat(testFiles) From 9053b97a664b3c7e5bbc8135450543fc03e09d93 Mon Sep 17 00:00:00 2001 From: Sebastian Gerske <13647606+H34D@users.noreply.github.com> Date: Tue, 10 Dec 2019 16:32:49 +0100 Subject: [PATCH 2/7] activate ts build again for tests, it is ts code and attract glitches when not being build --- packages/protocol/tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/protocol/tsconfig.json b/packages/protocol/tsconfig.json index 33321908982..08064c34136 100644 --- a/packages/protocol/tsconfig.json +++ b/packages/protocol/tsconfig.json @@ -15,6 +15,6 @@ "target": "es5", "downlevelIteration": true }, - "exclude": ["test/", "node_modules"], + "exclude": ["node_modules"], "references": [{ "path": "../utils" }] } From ed31c6ce49eeaf9f1386a74abae820d472e00f95 Mon Sep 17 00:00:00 2001 From: Sebastian Gerske <13647606+H34D@users.noreply.github.com> Date: Tue, 10 Dec 2019 16:33:16 +0100 Subject: [PATCH 3/7] fix ts errors --- .../protocol/test/governance/epochrewards.ts | 7 ++++--- .../protocol/test/identity/attestations.ts | 21 +++++++------------ 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/packages/protocol/test/governance/epochrewards.ts b/packages/protocol/test/governance/epochrewards.ts index 3106f052aab..5e20f402cf2 100644 --- a/packages/protocol/test/governance/epochrewards.ts +++ b/packages/protocol/test/governance/epochrewards.ts @@ -1,3 +1,4 @@ +/* globals artifacts, web3, beforeEach, it, */ import { CeloContractName } from '@celo/protocol/lib/registry-utils' import { assertContainSubset, @@ -6,20 +7,20 @@ import { assertRevert, timeTravel, } from '@celo/protocol/lib/test-utils' +import { fromFixed, toFixed } from '@celo/utils/lib/fixidity' import BigNumber from 'bignumber.js' import { + EpochRewardsTestContract, + EpochRewardsTestInstance, MockElectionContract, MockElectionInstance, MockGoldTokenContract, MockGoldTokenInstance, MockSortedOraclesContract, MockSortedOraclesInstance, - EpochRewardsTestContract, - EpochRewardsTestInstance, RegistryContract, RegistryInstance, } from 'types' -import { fromFixed, toFixed } from '@celo/utils/lib/fixidity' const EpochRewards: EpochRewardsTestContract = artifacts.require('EpochRewardsTest') const MockElection: MockElectionContract = artifacts.require('MockElection') diff --git a/packages/protocol/test/identity/attestations.ts b/packages/protocol/test/identity/attestations.ts index b99f84c1857..d2c19ec29be 100644 --- a/packages/protocol/test/identity/attestations.ts +++ b/packages/protocol/test/identity/attestations.ts @@ -497,10 +497,10 @@ contract('Attestations', (accounts: string[]) => { }) it('should no longer list the attestations in getCompletableAttestations', async () => { - const [ - attestationBlockNumbers, - _attestationIssuers, - ] = await attestations.getCompletableAttestations(phoneHash, caller) + const [attestationBlockNumbers] = await attestations.getCompletableAttestations( + phoneHash, + caller + ) assert.lengthOf(attestationBlockNumbers, 0) }) @@ -591,11 +591,8 @@ contract('Attestations', (accounts: string[]) => { it('should no longer list the attestation in getCompletableAttestationStats', async () => { await attestations.complete(phoneHash, v, r, s) - const [ - _attestationBlockNumbers, - attestationIssuers, - ] = await attestations.getCompletableAttestations(phoneHash, caller) - assert.equal(attestationIssuers.indexOf(issuer), -1) + const [attestationIssuers] = await attestations.getCompletableAttestations(phoneHash, caller) + assert.equal(attestationIssuers.indexOf(new BigNumber(issuer)), -1) }) it('should emit the AttestationCompleted event', async () => { @@ -635,11 +632,7 @@ contract('Attestations', (accounts: string[]) => { it('should mark the attestation by the issuer as complete', async () => { await attestations.complete(phoneHash, v, r, s) - const [status, _blockNumber] = await attestations.getAttestationState( - phoneHash, - caller, - issuer - ) + const [status] = await attestations.getAttestationState(phoneHash, caller, issuer) assert.equal(status.toNumber(), 2) }) }) From 20dd109a903993c2f7ad5a3efbc53b93c41f614e Mon Sep 17 00:00:00 2001 From: Sebastian Gerske <13647606+H34D@users.noreply.github.com> Date: Tue, 10 Dec 2019 17:17:20 +0100 Subject: [PATCH 4/7] fix ts issues --- packages/protocol/test/baklava/freezable.ts | 6 ++--- packages/protocol/test/common/accounts.ts | 16 ++++++------ .../protocol/test/common/gaspriceminimum.ts | 6 ++--- packages/protocol/test/common/linkedlist.ts | 3 ++- .../test/governance/blockchainparams.ts | 4 +-- packages/protocol/test/governance/election.ts | 8 +++--- .../protocol/test/governance/governance.ts | 22 ++++++++-------- .../protocol/test/governance/lockedgold.ts | 2 +- .../protocol/test/governance/validators.ts | 26 +++++++++---------- .../protocol/test/identity/attestations.ts | 8 +++--- packages/protocol/test/identity/random.ts | 12 ++++----- packages/protocol/test/stability/exchange.ts | 2 +- packages/protocol/test/stability/reserve.ts | 2 +- .../protocol/test/stability/sortedoracles.ts | 4 +-- .../protocol/test/stability/stabletoken.ts | 2 +- 15 files changed, 62 insertions(+), 61 deletions(-) diff --git a/packages/protocol/test/baklava/freezable.ts b/packages/protocol/test/baklava/freezable.ts index 1c7431a80f6..0ba2e3be3e8 100644 --- a/packages/protocol/test/baklava/freezable.ts +++ b/packages/protocol/test/baklava/freezable.ts @@ -1,4 +1,4 @@ -import { assertSameAddress, assertLogMatches2, assertRevert } from '@celo/protocol/lib/test-utils' +import { assertLogMatches2, assertRevert, assertSameAddress } from '@celo/protocol/lib/test-utils' import { FreezableTestInstance } from 'types' contract('Freezable', (accounts: string[]) => { @@ -7,7 +7,7 @@ contract('Freezable', (accounts: string[]) => { beforeEach(async () => { freezableTest = await FreezableTest.new() - freezableTest.setFreezer(accounts[0]) + await freezableTest.setFreezer(accounts[0]) }) describe('_setFreezer', () => { @@ -55,7 +55,7 @@ contract('Freezable', (accounts: string[]) => { describe('unfreeze', () => { beforeEach(async () => { - freezableTest.freeze() + await freezableTest.freeze() }) it('should allow freezer to unfreeze the contract', async () => { diff --git a/packages/protocol/test/common/accounts.ts b/packages/protocol/test/common/accounts.ts index e2baf094fb7..b35d70b9120 100644 --- a/packages/protocol/test/common/accounts.ts +++ b/packages/protocol/test/common/accounts.ts @@ -1,5 +1,3 @@ -import { parseSolidityStringArray } from '@celo/utils/lib/parsing' -import { upperFirst } from 'lodash' import { CeloContractName } from '@celo/protocol/lib/registry-utils' import { getParsedSignatureOfAddress } from '@celo/protocol/lib/signing-utils' import { @@ -8,6 +6,8 @@ import { assertRevert, NULL_ADDRESS, } from '@celo/protocol/lib/test-utils' +import { parseSolidityStringArray } from '@celo/utils/lib/parsing' +import { upperFirst } from 'lodash' import { AccountsContract, AccountsInstance, @@ -243,7 +243,7 @@ contract('Accounts', (accounts: string[]) => { describe('when the account has been created', () => { beforeEach(async () => { - accountsInstance.createAccount() + await accountsInstance.createAccount() }) it('should set the walletAddress', async () => { @@ -279,7 +279,7 @@ contract('Accounts', (accounts: string[]) => { describe('when the account has been created', () => { beforeEach(async () => { - accountsInstance.createAccount() + await accountsInstance.createAccount() }) it('should set the metadataURL', async () => { @@ -304,9 +304,9 @@ contract('Accounts', (accounts: string[]) => { it('returns multiple metadata URLs', async () => { const randomStrings = accounts.map((_) => web3.utils.randomHex(20).slice(2)) await Promise.all( - accounts.map(async (account, i) => { - await accountsInstance.createAccount({ from: account }) - await accountsInstance.setMetadataURL(randomStrings[i], { from: account }) + accounts.map(async (mappedAccount, i) => { + await accountsInstance.createAccount({ from: mappedAccount }) + await accountsInstance.setMetadataURL(randomStrings[i], { from: mappedAccount }) }) ) const [stringLengths, data] = await accountsInstance.batchGetMetadataURL(accounts) @@ -329,7 +329,7 @@ contract('Accounts', (accounts: string[]) => { describe('when the account has been created', () => { beforeEach(async () => { - accountsInstance.createAccount() + await accountsInstance.createAccount() }) it('should set the name', async () => { diff --git a/packages/protocol/test/common/gaspriceminimum.ts b/packages/protocol/test/common/gaspriceminimum.ts index 6b15f21d38d..8c3a0ae4342 100644 --- a/packages/protocol/test/common/gaspriceminimum.ts +++ b/packages/protocol/test/common/gaspriceminimum.ts @@ -160,14 +160,14 @@ contract('GasPriceMinimum', (accounts: string[]) => { const getUpdatedGasPriceMinimum = ( previousGasPriceMinimum, density, - targetDensity, - adjustmentSpeed + _targetDensity, + _adjustmentSpeed ) => { const one = new BigNumber(1) return previousGasPriceMinimum .times( one.plus( - fromFixed(adjustmentSpeed).times(fromFixed(density).minus(fromFixed(targetDensity))) + fromFixed(_adjustmentSpeed).times(fromFixed(density).minus(fromFixed(_targetDensity))) ) ) .plus(one) diff --git a/packages/protocol/test/common/linkedlist.ts b/packages/protocol/test/common/linkedlist.ts index 0ba0f660739..e2b44764812 100644 --- a/packages/protocol/test/common/linkedlist.ts +++ b/packages/protocol/test/common/linkedlist.ts @@ -45,8 +45,9 @@ contract('LinkedListTest', () => { describe('when inserting to a list with more items', () => { beforeEach(async () => { await linkedListTest.insert(firstKey, NULL_KEY, NULL_KEY) - for (let i = 1; i < keys.length; i++) + for (let i = 1; i < keys.length; i++) { await linkedListTest.insert(keys[i], NULL_KEY, keys[i - 1]) + } }) it('should revert if next is equal to key (beginning)', async () => { diff --git a/packages/protocol/test/governance/blockchainparams.ts b/packages/protocol/test/governance/blockchainparams.ts index 9e196c8203d..eba70d539ab 100644 --- a/packages/protocol/test/governance/blockchainparams.ts +++ b/packages/protocol/test/governance/blockchainparams.ts @@ -1,6 +1,6 @@ -import { assertContainSubset, assertRevert, assertEqualBN } from '@celo/protocol/lib/test-utils' -import { BlockchainParametersContract, BlockchainParametersInstance } from 'types' +import { assertContainSubset, assertEqualBN, assertRevert } from '@celo/protocol/lib/test-utils' import { BigNumber } from 'bignumber.js' +import { BlockchainParametersContract, BlockchainParametersInstance } from 'types' const BlockchainParameters: BlockchainParametersContract = artifacts.require('BlockchainParameters') diff --git a/packages/protocol/test/governance/election.ts b/packages/protocol/test/governance/election.ts index c1ad34520dc..4f2add73473 100644 --- a/packages/protocol/test/governance/election.ts +++ b/packages/protocol/test/governance/election.ts @@ -375,10 +375,10 @@ contract('Election', (accounts: string[]) => { }) describe('when the voter has already voted for this group', () => { - let resp: any + let response: any beforeEach(async () => { await mockLockedGold.incrementNonvotingAccountBalance(voter, value) - resp = await election.vote(group, value, NULL_ADDRESS, NULL_ADDRESS) + response = await election.vote(group, value, NULL_ADDRESS, NULL_ADDRESS) }) it('should not change the list of groups the account has voted for', async () => { @@ -416,8 +416,8 @@ contract('Election', (accounts: string[]) => { }) it('should emit the ValidatorGroupVoteCast event', async () => { - assert.equal(resp.logs.length, 1) - const log = resp.logs[0] + assert.equal(response.logs.length, 1) + const log = response.logs[0] assertContainSubset(log, { event: 'ValidatorGroupVoteCast', args: { diff --git a/packages/protocol/test/governance/governance.ts b/packages/protocol/test/governance/governance.ts index 8164c7d43b7..77c0e15cdc1 100644 --- a/packages/protocol/test/governance/governance.ts +++ b/packages/protocol/test/governance/governance.ts @@ -642,7 +642,7 @@ contract('Governance', (accounts: string[]) => { args: { destination, functionId: web3.utils.padRight(functionId, 64), - threshold: threshold, + threshold, }, }) }) @@ -675,7 +675,7 @@ contract('Governance', (accounts: string[]) => { args: { destination, functionId: web3.utils.padRight(functionId, 64), - threshold: threshold, + threshold, }, }) }) @@ -777,7 +777,7 @@ contract('Governance', (accounts: string[]) => { proposalId: new BigNumber(1), proposer: accounts[0], deposit: new BigNumber(minDeposit), - timestamp: timestamp, + timestamp, transactionCount: 0, }, }) @@ -842,7 +842,7 @@ contract('Governance', (accounts: string[]) => { proposalId: new BigNumber(1), proposer: accounts[0], deposit: new BigNumber(minDeposit), - timestamp: timestamp, + timestamp, transactionCount: 1, }, }) @@ -915,7 +915,7 @@ contract('Governance', (accounts: string[]) => { proposalId: new BigNumber(1), proposer: accounts[0], deposit: new BigNumber(minDeposit), - timestamp: timestamp, + timestamp, transactionCount: 2, }, }) @@ -1039,10 +1039,10 @@ contract('Governance', (accounts: string[]) => { // @ts-ignore: TODO(mcortesi) fix typings for TransactionDetails { value: minDeposit } ) - const otherAccount = accounts[1] - await accountsInstance.createAccount({ from: otherAccount }) - await mockLockedGold.setAccountTotalLockedGold(otherAccount, weight) - await governance.upvote(otherProposalId, proposalId, 0, { from: otherAccount }) + const otherAccount1 = accounts[1] + await accountsInstance.createAccount({ from: otherAccount1 }) + await mockLockedGold.setAccountTotalLockedGold(otherAccount1, weight) + await governance.upvote(otherProposalId, proposalId, 0, { from: otherAccount1 }) await timeTravel(queueExpiry, web3) }) @@ -1105,11 +1105,11 @@ contract('Governance', (accounts: string[]) => { describe('when the previously upvoted proposal is in the queue and expired', () => { const upvotedProposalId = 2 // Expire the upvoted proposal without dequeueing it. - const queueExpiry = 60 + const queueExpiry1 = 60 beforeEach(async () => { await governance.setQueueExpiry(60) await governance.upvote(proposalId, 0, 0) - await timeTravel(queueExpiry, web3) + await timeTravel(queueExpiry1, web3) await governance.propose( [transactionSuccess1.value], [transactionSuccess1.destination], diff --git a/packages/protocol/test/governance/lockedgold.ts b/packages/protocol/test/governance/lockedgold.ts index d34083225d2..c5351c8c5c5 100644 --- a/packages/protocol/test/governance/lockedgold.ts +++ b/packages/protocol/test/governance/lockedgold.ts @@ -40,7 +40,7 @@ const HOUR = 60 * 60 const DAY = 24 * HOUR contract('LockedGold', (accounts: string[]) => { - let account = accounts[0] + const account = accounts[0] const nonOwner = accounts[1] const unlockingPeriod = 3 * DAY let accountsInstance: AccountsInstance diff --git a/packages/protocol/test/governance/validators.ts b/packages/protocol/test/governance/validators.ts index cc2edc38115..16b15ad0eab 100644 --- a/packages/protocol/test/governance/validators.ts +++ b/packages/protocol/test/governance/validators.ts @@ -151,7 +151,7 @@ contract('Validators', (accounts: string[]) => { for (const validator of members) { await registerValidator(validator) await validators.affiliate(group, { from: validator }) - if (validator == members[0]) { + if (validator === members[0]) { await validators.addFirstMember(validator, NULL_ADDRESS, NULL_ADDRESS, { from: group }) } else { await validators.addMember(validator, { from: group }) @@ -606,7 +606,7 @@ contract('Validators', (accounts: string[]) => { args: { validator, ecdsaPublicKey: publicKey, - blsPublicKey: blsPublicKey, + blsPublicKey, }, }) }) @@ -907,7 +907,7 @@ contract('Validators', (accounts: string[]) => { await validators.getMembershipHistory(validator) ) let expectedEntries = 1 - if (registrationEpoch != additionEpoch || additionEpoch != affiliationEpoch) { + if (registrationEpoch !== additionEpoch || additionEpoch !== affiliationEpoch) { expectedEntries = 2 } assert.equal(membershipHistory.epochs.length, expectedEntries) @@ -1035,7 +1035,7 @@ contract('Validators', (accounts: string[]) => { await validators.getMembershipHistory(validator) ) let expectedEntries = 1 - if (registrationEpoch != additionEpoch || additionEpoch != deaffiliationEpoch) { + if (registrationEpoch !== additionEpoch || additionEpoch !== deaffiliationEpoch) { expectedEntries = 2 } assert.equal(membershipHistory.epochs.length, expectedEntries) @@ -1433,7 +1433,7 @@ contract('Validators', (accounts: string[]) => { }) it("should update the member's membership history", async () => { - const expectedEntries = registrationEpoch == additionEpoch ? 1 : 2 + const expectedEntries = registrationEpoch === additionEpoch ? 1 : 2 const membershipHistory = parseMembershipHistory( await validators.getMembershipHistory(validator) ) @@ -1479,14 +1479,14 @@ contract('Validators', (accounts: string[]) => { assert.equal(expectedSizeHistory.length, 1) for (let i = 2; i < maxGroupSize.toNumber() + 1; i++) { const numMembers = i - const validator = accounts[i] - await registerValidator(validator) - await validators.affiliate(group, { from: validator }) + const validator1 = accounts[i] + await registerValidator(validator1) + await validators.affiliate(group, { from: validator1 }) await mockLockedGold.setAccountTotalLockedGold( group, groupLockedGoldRequirements.value.times(numMembers) ) - await validators.addMember(validator) + await validators.addMember(validator1) expectedSizeHistory.push((await web3.eth.getBlock('latest')).timestamp) const parsedGroup = parseValidatorGroupParams( await validators.getValidatorGroup(group) @@ -1578,9 +1578,9 @@ contract('Validators', (accounts: string[]) => { // Depending on test timing, we may or may not span an epoch boundary between registration // and removal. const numEntries = membershipHistory.epochs.length - assert.isTrue(numEntries == 1 || numEntries == 2) + assert.isTrue(numEntries === 1 || numEntries === 2) assert.equal(membershipHistory.groups.length, numEntries) - if (numEntries == 1) { + if (numEntries === 1) { assertEqualBN(membershipHistory.epochs[0], expectedEpoch) assertSameAddress(membershipHistory.groups[0], NULL_ADDRESS) } else { @@ -1880,7 +1880,7 @@ contract('Validators', (accounts: string[]) => { from: groups[i], }) - if (i == 0) { + if (i === 0) { assert.equal(await validators.getMembershipInLastEpoch(validator), NULL_ADDRESS) } else { assert.equal(await validators.getMembershipInLastEpoch(validator), groups[i - 1]) @@ -1912,7 +1912,7 @@ contract('Validators', (accounts: string[]) => { group, groupLockedGoldRequirements.value.times(i) ) - if (i == 1) { + if (i === 1) { await validators.addFirstMember(validator, NULL_ADDRESS, NULL_ADDRESS) } else { await validators.addMember(validator) diff --git a/packages/protocol/test/identity/attestations.ts b/packages/protocol/test/identity/attestations.ts index d2c19ec29be..a49f0b29c7f 100644 --- a/packages/protocol/test/identity/attestations.ts +++ b/packages/protocol/test/identity/attestations.ts @@ -136,7 +136,7 @@ contract('Attestations', (accounts: string[]) => { const mockValidators = await MockValidators.new() attestations = await Attestations.new() random = await Random.new() - random.addTestRandomness(0, '0x00') + await random.addTestRandomness(0, '0x00') mockLockedGold = await MockLockedGold.new() registry = await Registry.new() await accountsInstance.initialize(registry.address) @@ -554,12 +554,12 @@ contract('Attestations', (accounts: string[]) => { }) it('should increment the number of completed verification requests', async () => { - let [numCompleted, numTotal] = await attestations.getAttestationStats(phoneHash, caller) + const [numCompleted] = await attestations.getAttestationStats(phoneHash, caller) assert.equal(numCompleted.toNumber(), 0) await attestations.complete(phoneHash, v, r, s) - ;[numCompleted, numTotal] = await attestations.getAttestationStats(phoneHash, caller) - assert.equal(numCompleted.toNumber(), 1) + const [numCompleted2, numTotal] = await attestations.getAttestationStats(phoneHash, caller) + assert.equal(numCompleted2.toNumber(), 1) assert.equal(numTotal.toNumber(), attestationsRequested) }) diff --git a/packages/protocol/test/identity/random.ts b/packages/protocol/test/identity/random.ts index 2ffca631849..84f047db049 100644 --- a/packages/protocol/test/identity/random.ts +++ b/packages/protocol/test/identity/random.ts @@ -1,7 +1,7 @@ -import { assertRevert, assertEqualBN, assertContainSubset } from '@celo/protocol/lib/test-utils' +import { assertContainSubset, assertEqualBN, assertRevert } from '@celo/protocol/lib/test-utils' -import { TestRandomContract, TestRandomInstance } from 'types' import { BigNumber } from 'bignumber.js' +import { TestRandomContract, TestRandomInstance } from 'types' const Random: TestRandomContract = artifacts.require('TestRandom') @@ -14,7 +14,7 @@ contract('Random', (accounts: string[]) => { beforeEach(async () => { random = await Random.new() - random.initialize(256) + await random.initialize(256) }) describe('#setRandomnessRetentionWindow()', () => { @@ -36,7 +36,7 @@ contract('Random', (accounts: string[]) => { }) it('only owner can set', async () => { - assertRevert(random.setRandomnessBlockRetentionWindow(1000, { from: accounts[1] })) + await assertRevert(random.setRandomnessBlockRetentionWindow(1000, { from: accounts[1] })) }) }) @@ -75,7 +75,7 @@ contract('Random', (accounts: string[]) => { assert.equal(randomValues[5], await random.getTestRandomness(5, 5)) }) it('cannot read old blocks', async () => { - assertRevert(random.getTestRandomness(3, 5)) + await assertRevert(random.getTestRandomness(3, 5)) }) }) @@ -93,7 +93,7 @@ contract('Random', (accounts: string[]) => { assert.equal(randomValues[5], await random.getTestRandomness(5, 5)) }) it('cannot read old blocks', async () => { - assertRevert(random.getTestRandomness(1, 5)) + await assertRevert(random.getTestRandomness(1, 5)) }) it('old values are preserved', async () => { await random.addTestRandomness(5, randomValues[5]) diff --git a/packages/protocol/test/stability/exchange.ts b/packages/protocol/test/stability/exchange.ts index a75de0bb0bd..ce3c922403b 100644 --- a/packages/protocol/test/stability/exchange.ts +++ b/packages/protocol/test/stability/exchange.ts @@ -6,7 +6,7 @@ import { isSameAddress, timeTravel, } from '@celo/protocol/lib/test-utils' -import { fixed1, toFixed, fromFixed, multiply } from '@celo/utils/lib/fixidity' +import { fixed1, fromFixed, multiply, toFixed } from '@celo/utils/lib/fixidity' import BigNumber from 'bignumber.js' import { ExchangeInstance, diff --git a/packages/protocol/test/stability/reserve.ts b/packages/protocol/test/stability/reserve.ts index 9ed594436ac..6edd67a6279 100644 --- a/packages/protocol/test/stability/reserve.ts +++ b/packages/protocol/test/stability/reserve.ts @@ -6,6 +6,7 @@ import { timeTravel, } from '@celo/protocol/lib/test-utils' import BigNumber from 'bignumber.js' +import BN = require('bn.js') import { MockGoldTokenInstance, MockSortedOraclesInstance, @@ -13,7 +14,6 @@ import { RegistryInstance, ReserveInstance, } from 'types' -import BN = require('bn.js') const Registry: Truffle.Contract = artifacts.require('Registry') const Reserve: Truffle.Contract = artifacts.require('Reserve') diff --git a/packages/protocol/test/stability/sortedoracles.ts b/packages/protocol/test/stability/sortedoracles.ts index bac7cce8526..2260e0128fb 100644 --- a/packages/protocol/test/stability/sortedoracles.ts +++ b/packages/protocol/test/stability/sortedoracles.ts @@ -4,8 +4,8 @@ import { assertRevert, matchAddress, matchAny, - timeTravel, NULL_ADDRESS, + timeTravel, } from '@celo/protocol/lib/test-utils' import BigNumber from 'bignumber.js' import { SortedOraclesContract, SortedOraclesInstance } from 'types' @@ -386,7 +386,7 @@ contract('SortedOracles', (accounts: string[]) => { ) beforeEach(async () => { - sortedOracles.addOracle(aToken, anotherOracle) + await sortedOracles.addOracle(aToken, anotherOracle) await sortedOracles.report(aToken, anotherOracleNumerator, 1, NULL_ADDRESS, NULL_ADDRESS, { from: anotherOracle, }) diff --git a/packages/protocol/test/stability/stabletoken.ts b/packages/protocol/test/stability/stabletoken.ts index 87557e3dbec..e25a5ccba89 100644 --- a/packages/protocol/test/stability/stabletoken.ts +++ b/packages/protocol/test/stability/stabletoken.ts @@ -236,7 +236,7 @@ contract('StableToken', (accounts: string[]) => { assertLogMatches2(res.logs[0], { event: 'InflationFactorUpdated', args: { - factor: factor, + factor, lastUpdated, }, }) From a9f785707a1a589c64757e1b54acf763a76e6bc6 Mon Sep 17 00:00:00 2001 From: Sebastian Gerske <13647606+H34D@users.noreply.github.com> Date: Tue, 10 Dec 2019 17:32:27 +0100 Subject: [PATCH 5/7] ignore js in the right folder --- .prettierignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.prettierignore b/.prettierignore index 07955f8eef4..a5147dbb317 100644 --- a/.prettierignore +++ b/.prettierignore @@ -21,7 +21,7 @@ packages/protocol/types/ !packages/protocol/lib/**/*.ts packages/protocol/scripts/**/*.js packages/protocol/migrations/**/*.js -packages/protocol/tests/**/*.js +packages/protocol/test/**/*.js packages/verification-pool-api/contracts/*.ts From 40b69d28727d76207531e4977ebbe11c3029e3ba Mon Sep 17 00:00:00 2001 From: Sebastian Gerske <13647606+H34D@users.noreply.github.com> Date: Tue, 10 Dec 2019 21:46:25 +0100 Subject: [PATCH 6/7] make time travel more reliant --- packages/protocol/lib/test-utils.ts | 10 ++++++---- .../protocol/test/governance/epochrewards.ts | 17 ++++++++++------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/packages/protocol/lib/test-utils.ts b/packages/protocol/lib/test-utils.ts index 0605852eaed..46373ad6770 100644 --- a/packages/protocol/lib/test-utils.ts +++ b/packages/protocol/lib/test-utils.ts @@ -41,7 +41,9 @@ export async function jsonRpc(web3: Web3, method: string, params: any[] = []): P jsonrpc: '2.0', method, params, - id: new Date().getTime(), + // salt id generation, milliseconds might not be + // enough to generate unique ids + id: new Date().getTime() + Math.floor(Math.random() * ( 1 + 100 - 1 )), }, // @ts-ignore (err: any, result: any) => { @@ -205,13 +207,13 @@ export function assertLogMatches( } export function assertEqualBN( - value: number | BN | BigNumber, + actual: number | BN | BigNumber, expected: number | BN | BigNumber, msg?: string ) { assert( - web3.utils.toBN(value).eq(web3.utils.toBN(expected)), - `expected ${expected.toString()} and got ${value.toString()}. ${msg || ''}` + web3.utils.toBN(actual).eq(web3.utils.toBN(expected)), + `expected ${expected.toString(10)} and got ${actual.toString(10)}. ${msg || ''}` ) } diff --git a/packages/protocol/test/governance/epochrewards.ts b/packages/protocol/test/governance/epochrewards.ts index 5e20f402cf2..a297028aa2d 100644 --- a/packages/protocol/test/governance/epochrewards.ts +++ b/packages/protocol/test/governance/epochrewards.ts @@ -1,10 +1,10 @@ -/* globals artifacts, web3, beforeEach, it, */ import { CeloContractName } from '@celo/protocol/lib/registry-utils' import { assertContainSubset, assertEqualBN, assertEqualDpBN, assertRevert, + jsonRpc, timeTravel, } from '@celo/protocol/lib/test-utils' import { fromFixed, toFixed } from '@celo/utils/lib/fixidity' @@ -35,7 +35,7 @@ EpochRewards.numberFormat = 'BigNumber' const YEAR = new BigNumber(365 * 24 * 60 * 60) const SUPPLY_CAP = new BigNumber(web3.utils.toWei('1000000000')) -const getExpectedTargetTotalSupply = (timeDelta: BigNumber) => { +const getExpectedTargetTotalSupply = (timeDelta: BigNumber): BigNumber => { const genesisSupply = new BigNumber(web3.utils.toWei('600000000')) const linearRewards = new BigNumber(web3.utils.toWei('200000000')) return genesisSupply @@ -69,10 +69,13 @@ contract('EpochRewards', (accounts: string[]) => { const mockStableTokenAddress = web3.utils.randomHex(20) const sortedOraclesDenominator = new BigNumber('0x10000000000000000') const timeTravelToDelta = async (timeDelta: BigNumber) => { - const currentTime = new BigNumber((await web3.eth.getBlock('latest')).timestamp) - const startTime = await epochRewards.startTime() - const desiredTime = startTime.plus(timeDelta) - await timeTravel(desiredTime.minus(currentTime).toNumber(), web3) + // mine beforehand, just in case + await jsonRpc(web3, 'evm_mine', []) + const currentTime: BigNumber = new BigNumber((await web3.eth.getBlock('latest')).timestamp) + const startTime: BigNumber = await epochRewards.startTime() + const desiredTime: BigNumber = startTime.plus(timeDelta) + const delta: number = desiredTime.minus(currentTime).toNumber() + await timeTravel(delta, web3) } beforeEach(async () => { @@ -355,7 +358,7 @@ contract('EpochRewards', (accounts: string[]) => { describe('#getTargetGoldTotalSupply()', () => { describe('when it has been fewer than 15 years since genesis', () => { - const timeDelta = YEAR.times(10) + const timeDelta: BigNumber = YEAR.times(10) beforeEach(async () => { await timeTravelToDelta(timeDelta) }) From 316537c7dd9c76e75d124cc3a0f633daf14e9b9d Mon Sep 17 00:00:00 2001 From: Sebastian Gerske <13647606+H34D@users.noreply.github.com> Date: Wed, 11 Dec 2019 10:12:46 +0100 Subject: [PATCH 7/7] lint free --- packages/protocol/test/governance/validators.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/protocol/test/governance/validators.ts b/packages/protocol/test/governance/validators.ts index 1f42512c93a..4db673d0595 100644 --- a/packages/protocol/test/governance/validators.ts +++ b/packages/protocol/test/governance/validators.ts @@ -3,8 +3,8 @@ import { getParsedSignatureOfAddress } from '@celo/protocol/lib/signing-utils' import { assertContainSubset, assertEqualBN, - assertEqualDpBN, assertEqualBNArray, + assertEqualDpBN, assertRevert, assertSameAddress, mineBlocks, @@ -1758,15 +1758,15 @@ contract('Validators', (accounts: string[]) => { describe('#calculateGroupEpochScore', () => { describe('when all uptimes are in the interval [0, 1.0]', () => { - const testGroupUptimeCalculation = (uptimes) => { - const expected = uptimes + const testGroupUptimeCalculation = (_uptimes) => { + const expected = _uptimes .map((uptime) => new BigNumber(uptime)) .map((uptime) => uptime.pow(validatorScoreParameters.exponent.toNumber())) .reduce((sum, n) => sum.plus(n)) - .div(uptimes.length) + .div(_uptimes.length) it('should calculate the group score correctly', async () => { assertEqualDpBN( - fromFixed(await validators.calculateGroupEpochScore(uptimes.map(toFixed))), + fromFixed(await validators.calculateGroupEpochScore(_uptimes.map(toFixed))), expected, 8 )