Skip to content

Commit

Permalink
Migrate CLI validator, validatorgroup and election commands to anvil (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shazarre authored Aug 20, 2024
1 parent 13ffd59 commit 2478aa6
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 116 deletions.
24 changes: 15 additions & 9 deletions packages/cli/src/commands/election/activate.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { newKitFromWeb3 } from '@celo/contractkit'
import { testWithGanache } from '@celo/dev-utils/lib/ganache-test'
import { testWithAnvilL1 } from '@celo/dev-utils/lib/anvil-test'
import { ux } from '@oclif/core'
import BigNumber from 'bignumber.js'
import Web3 from 'web3'
Expand All @@ -10,18 +10,20 @@ import {
setupGroupAndAffiliateValidator,
voteForGroupFrom,
} from '../../test-utils/chain-setup'
import { stripAnsiCodesAndTxHashes, testLocally } from '../../test-utils/cliUtils'
import { stripAnsiCodesAndTxHashes, testLocallyWithWeb3Node } from '../../test-utils/cliUtils'
import ElectionActivate from './activate'

process.env.NO_SYNCCHECK = 'true'

testWithGanache('election:activate', (web3: Web3) => {
testWithAnvilL1('election:activate', (web3: Web3) => {
afterEach(async () => {
jest.clearAllMocks()
})

it('fails when no flags are provided', async () => {
await expect(testLocally(ElectionActivate, [])).rejects.toThrow('Missing required flag from')
await expect(testLocallyWithWeb3Node(ElectionActivate, [], web3)).rejects.toThrow(
'Missing required flag from'
)
})

it('shows no pending votes', async () => {
Expand All @@ -31,7 +33,7 @@ testWithGanache('election:activate', (web3: Web3) => {

await registerAccount(kit, userAddress)

await testLocally(ElectionActivate, ['--from', userAddress])
await testLocallyWithWeb3Node(ElectionActivate, ['--from', userAddress], web3)

expect(writeMock.mock.calls).toMatchInlineSnapshot(`
[
Expand All @@ -53,7 +55,7 @@ testWithGanache('election:activate', (web3: Web3) => {
await registerAccountWithLockedGold(kit, userAddress)

await voteForGroupFrom(kit, userAddress, groupAddress, new BigNumber(10))
await testLocally(ElectionActivate, ['--from', userAddress])
await testLocallyWithWeb3Node(ElectionActivate, ['--from', userAddress], web3)

expect(writeMock.mock.calls).toMatchInlineSnapshot(`
[
Expand Down Expand Up @@ -83,7 +85,7 @@ testWithGanache('election:activate', (web3: Web3) => {
new BigNumber(0)
)

await testLocally(ElectionActivate, ['--from', userAddress])
await testLocallyWithWeb3Node(ElectionActivate, ['--from', userAddress], web3)

expect(writeMock.mock.calls).toMatchInlineSnapshot(`[]`)
expect((await election.getVotesForGroupByAccount(userAddress, groupAddress)).active).toEqual(
Expand All @@ -109,7 +111,7 @@ testWithGanache('election:activate', (web3: Web3) => {
)

await Promise.all([
testLocally(ElectionActivate, ['--from', userAddress, '--wait']),
testLocallyWithWeb3Node(ElectionActivate, ['--from', userAddress, '--wait'], web3),
new Promise<void>((resolve) => {
// at least the amount the --wait flag waits in the check
setTimeout(async () => {
Expand Down Expand Up @@ -167,7 +169,11 @@ testWithGanache('election:activate', (web3: Web3) => {
(await election.getVotesForGroupByAccount(otherUserAddress, groupAddress)).active
).toEqual(new BigNumber(0))

await testLocally(ElectionActivate, ['--from', otherUserAddress, '--for', userAddress])
await testLocallyWithWeb3Node(
ElectionActivate,
['--from', otherUserAddress, '--for', userAddress],
web3
)

expect(writeMock.mock.calls).toMatchInlineSnapshot(`[]`)
expect((await election.getVotesForGroupByAccount(userAddress, groupAddress)).active).toEqual(
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/src/commands/election/current.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { newKitFromWeb3 } from '@celo/contractkit'
import { WrapperCache } from '@celo/contractkit/lib/contract-cache'
import { ElectionWrapper } from '@celo/contractkit/lib/wrappers/Election'
import { ValidatorsWrapper } from '@celo/contractkit/lib/wrappers/Validators'
import { testWithGanache } from '@celo/dev-utils/lib/ganache-test'
import { testWithAnvilL1 } from '@celo/dev-utils/lib/anvil-test'
import { ux } from '@oclif/core'
import BigNumber from 'bignumber.js'
import Web3 from 'web3'
import { registerAccount, setupGroupAndAffiliateValidator } from '../../test-utils/chain-setup'
import { testLocally } from '../../test-utils/cliUtils'
import { testLocallyWithWeb3Node } from '../../test-utils/cliUtils'
import Current from './current'

process.env.NO_SYNCCHECK = 'true'
Expand All @@ -17,7 +17,7 @@ afterEach(async () => {
jest.restoreAllMocks()
})

testWithGanache('election:current cmd', async (web3: Web3) => {
testWithAnvilL1('election:current cmd', async (web3: Web3) => {
it('shows list with no --valset provided', async () => {
const kit = newKitFromWeb3(web3)
const [
Expand Down Expand Up @@ -75,7 +75,7 @@ testWithGanache('election:current cmd', async (web3: Web3) => {
const warnMock = jest.spyOn(console, 'warn')
const writeMock = jest.spyOn(ux.write, 'stdout')

await testLocally(Current, ['--csv'])
await testLocallyWithWeb3Node(Current, ['--csv'], web3)

expect(writeMock.mock.calls).toMatchInlineSnapshot(`
[
Expand Down Expand Up @@ -152,7 +152,7 @@ testWithGanache('election:current cmd', async (web3: Web3) => {
const warnMock = jest.spyOn(console, 'warn')
const writeMock = jest.spyOn(ux.write, 'stdout')

await testLocally(Current, ['--csv', '--valset'])
await testLocallyWithWeb3Node(Current, ['--csv', '--valset'], web3)

expect(writeMock.mock.calls).toMatchInlineSnapshot(`
[
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/election/list.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ElectionWrapper, ValidatorGroupVote } from '@celo/contractkit/lib/wrappers/Election'
import { testWithGanache } from '@celo/dev-utils/lib/ganache-test'
import { testWithAnvilL1 } from '@celo/dev-utils/lib/anvil-test'
import { ux } from '@oclif/core'
import BigNumber from 'bignumber.js'
import Web3 from 'web3'
Expand All @@ -8,7 +8,7 @@ import ElectionList from './list'

process.env.NO_SYNCCHECK = 'true'

testWithGanache('election:list cmd', (web3: Web3) => {
testWithAnvilL1('election:list cmd', (web3: Web3) => {
test('shows list when no arguments provided', async () => {
const getValidatorGroupsVotesMock = jest.spyOn(
ElectionWrapper.prototype,
Expand Down
46 changes: 24 additions & 22 deletions packages/cli/src/commands/election/revoke.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { newKitFromWeb3 } from '@celo/contractkit'
import { testWithGanache } from '@celo/dev-utils/lib/ganache-test'
import { testWithAnvilL1 } from '@celo/dev-utils/lib/anvil-test'
import BigNumber from 'bignumber.js'
import Web3 from 'web3'
import {
Expand All @@ -8,26 +8,30 @@ import {
setupGroupAndAffiliateValidator,
voteForGroupFromAndActivateVotes,
} from '../../test-utils/chain-setup'
import { testLocally } from '../../test-utils/cliUtils'
import { testLocallyWithWeb3Node } from '../../test-utils/cliUtils'
import Revoke from './revoke'

process.env.NO_SYNCCHECK = 'true'

testWithGanache('election:revoke', (web3: Web3) => {
testWithAnvilL1('election:revoke', (web3: Web3) => {
afterEach(async () => {
jest.clearAllMocks()
})

it('fails when no flags are provided', async () => {
await expect(testLocally(Revoke, [])).rejects.toThrow('Missing required flag')
await expect(testLocallyWithWeb3Node(Revoke, [], web3)).rejects.toThrow('Missing required flag')
})

it('fails when address is not an account', async () => {
const logMock = jest.spyOn(console, 'log')
const [fromAddress, groupAddress] = await web3.eth.getAccounts()

await expect(
testLocally(Revoke, ['--from', fromAddress, '--for', groupAddress, '--value', '1'])
testLocallyWithWeb3Node(
Revoke,
['--from', fromAddress, '--for', groupAddress, '--value', '1'],
web3
)
).rejects.toThrow()
expect(logMock.mock.calls[1][0]).toContain(
`${fromAddress} is not a signer or registered as an account`
Expand All @@ -41,7 +45,11 @@ testWithGanache('election:revoke', (web3: Web3) => {
await registerAccount(kit, fromAddress)

await expect(
testLocally(Revoke, ['--from', fromAddress, '--for', groupAddress, '--value', '1'])
testLocallyWithWeb3Node(
Revoke,
['--from', fromAddress, '--for', groupAddress, '--value', '1'],
web3
)
).rejects.toThrow(
`can't revoke more votes for ${groupAddress} than have been made by ${fromAddress}`
)
Expand All @@ -61,14 +69,11 @@ testWithGanache('election:revoke', (web3: Web3) => {
amount
)

await testLocally(Revoke, [
'--from',
fromAddress,
'--for',
groupAddress,
'--value',
amount.toFixed(),
])
await testLocallyWithWeb3Node(
Revoke,
['--from', fromAddress, '--for', groupAddress, '--value', amount.toFixed()],
web3
)

expect((await election.getVotesForGroupByAccount(fromAddress, groupAddress)).active).toEqual(
new BigNumber(0)
Expand All @@ -90,14 +95,11 @@ testWithGanache('election:revoke', (web3: Web3) => {
amount
)

await testLocally(Revoke, [
'--from',
fromAddress,
'--for',
groupAddress,
'--value',
revokeAmount.toFixed(),
])
await testLocallyWithWeb3Node(
Revoke,
['--from', fromAddress, '--for', groupAddress, '--value', revokeAmount.toFixed()],
web3
)

expect((await election.getVotesForGroupByAccount(fromAddress, groupAddress)).active).toEqual(
amount.minus(revokeAmount)
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/src/commands/election/run.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { newKitFromWeb3 } from '@celo/contractkit'
import { ElectionWrapper } from '@celo/contractkit/lib/wrappers/Election'
import { ValidatorsWrapper } from '@celo/contractkit/lib/wrappers/Validators'
import { testWithGanache } from '@celo/dev-utils/lib/ganache-test'
import { testWithAnvilL1 } from '@celo/dev-utils/lib/anvil-test'
import { ux } from '@oclif/core'
import BigNumber from 'bignumber.js'
import Web3 from 'web3'
import { setupGroupAndAffiliateValidator } from '../../test-utils/chain-setup'
import { stripAnsiCodesAndTxHashes, testLocally } from '../../test-utils/cliUtils'
import { stripAnsiCodesAndTxHashes, testLocallyWithWeb3Node } from '../../test-utils/cliUtils'
import Run from './run'

process.env.NO_SYNCCHECK = 'true'

testWithGanache('election:run', (web3: Web3) => {
testWithAnvilL1('election:run', (web3: Web3) => {
afterEach(async () => {
jest.clearAllMocks()
})
Expand All @@ -21,7 +21,7 @@ testWithGanache('election:run', (web3: Web3) => {
const warnMock = jest.spyOn(console, 'warn')
const writeMock = jest.spyOn(ux.write, 'stdout')

await testLocally(Run, ['--csv'])
await testLocallyWithWeb3Node(Run, ['--csv'], web3)

expect(writeMock.mock.calls).toMatchInlineSnapshot(`
[
Expand Down Expand Up @@ -89,7 +89,7 @@ testWithGanache('election:run', (web3: Web3) => {
signer: anotherSignerAddress,
}))

await testLocally(Run, ['--csv'])
await testLocallyWithWeb3Node(Run, ['--csv'], web3)

expect(writeMock.mock.calls).toMatchInlineSnapshot(`
[
Expand Down
20 changes: 11 additions & 9 deletions packages/cli/src/commands/election/show.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import { newKitFromWeb3 } from '@celo/contractkit'
import { ElectionWrapper, ValidatorGroupVote, Voter } from '@celo/contractkit/lib/wrappers/Election'
import { testWithGanache } from '@celo/dev-utils/lib/ganache-test'
import { testWithAnvilL1 } from '@celo/dev-utils/lib/anvil-test'
import { ux } from '@oclif/core'
import BigNumber from 'bignumber.js'
import Web3 from 'web3'
import { registerAccount, setupGroup } from '../../test-utils/chain-setup'
import { stripAnsiCodesAndTxHashes, testLocally } from '../../test-utils/cliUtils'
import { stripAnsiCodesAndTxHashes, testLocallyWithWeb3Node } from '../../test-utils/cliUtils'
import Show from './show'

process.env.NO_SYNCCHECK = 'true'

testWithGanache('election:show', (web3: Web3) => {
testWithAnvilL1('election:show', (web3: Web3) => {
afterEach(async () => {
jest.clearAllMocks()
})

it('fails when no args are provided', async () => {
await expect(testLocally(Show, [])).rejects.toThrow("Voter or Validator Groups's address")
await expect(testLocallyWithWeb3Node(Show, [], web3)).rejects.toThrow(
"Voter or Validator Groups's address"
)
})

it('fails when no flags are provided', async () => {
const [groupAddress] = await web3.eth.getAccounts()
await expect(testLocally(Show, [groupAddress])).rejects.toThrow(
await expect(testLocallyWithWeb3Node(Show, [groupAddress], web3)).rejects.toThrow(
'Must select --voter or --group'
)
})
Expand All @@ -30,7 +32,7 @@ testWithGanache('election:show', (web3: Web3) => {
const logMock = jest.spyOn(console, 'log')
const [groupAddress] = await web3.eth.getAccounts()

await expect(testLocally(Show, [groupAddress, '--group'])).rejects.toThrow(
await expect(testLocallyWithWeb3Node(Show, [groupAddress, '--group'], web3)).rejects.toThrow(
"Some checks didn't pass!"
)
expect(stripAnsiCodesAndTxHashes(logMock.mock.calls[1][0])).toContain(
Expand All @@ -42,7 +44,7 @@ testWithGanache('election:show', (web3: Web3) => {
const logMock = jest.spyOn(console, 'log')
const [voterAddress] = await web3.eth.getAccounts()

await expect(testLocally(Show, [voterAddress, '--voter'])).rejects.toThrow(
await expect(testLocallyWithWeb3Node(Show, [voterAddress, '--voter'], web3)).rejects.toThrow(
"Some checks didn't pass!"
)
expect(stripAnsiCodesAndTxHashes(logMock.mock.calls[1][0])).toContain(
Expand Down Expand Up @@ -72,7 +74,7 @@ testWithGanache('election:show', (web3: Web3) => {
} as ValidatorGroupVote
})

await testLocally(Show, [groupAddress, '--group'])
await testLocallyWithWeb3Node(Show, [groupAddress, '--group'], web3)

expect(writeMock.mock.calls).toMatchInlineSnapshot(`[]`)
expect(logMock.mock.calls.map((args) => args.map(stripAnsiCodesAndTxHashes)))
Expand Down Expand Up @@ -125,7 +127,7 @@ testWithGanache('election:show', (web3: Web3) => {
} as Voter
})

await testLocally(Show, [voterAddress, '--voter'])
await testLocallyWithWeb3Node(Show, [voterAddress, '--voter'], web3)

expect(writeMock.mock.calls).toMatchInlineSnapshot(`[]`)
expect(logMock.mock.calls.map((args) => args.map(stripAnsiCodesAndTxHashes)))
Expand Down
Loading

0 comments on commit 2478aa6

Please sign in to comment.