From 6f96c5c1844f643741208f2d43c1afd601ff8241 Mon Sep 17 00:00:00 2001 From: rezed Date: Wed, 8 Feb 2023 00:22:19 +0800 Subject: [PATCH 1/3] test: basic setup for report processor testing and first simple asserts --- .../hash-consensus-report-processor.test.js | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 test/0.8.9/oracle/hash-consensus-report-processor.test.js diff --git a/test/0.8.9/oracle/hash-consensus-report-processor.test.js b/test/0.8.9/oracle/hash-consensus-report-processor.test.js new file mode 100644 index 000000000..a0e23ecb2 --- /dev/null +++ b/test/0.8.9/oracle/hash-consensus-report-processor.test.js @@ -0,0 +1,64 @@ +const { assert } = require('../../helpers/assert') +const { toNum } = require('../../helpers/utils') +const { assertBn, assertEvent, assertAmountOfEvents } = require('@aragon/contract-helpers-test/src/asserts') +const { assertRevert } = require('../../helpers/assertThrow') +const { ZERO_ADDRESS, bn } = require('@aragon/contract-helpers-test') + +const { + SLOTS_PER_EPOCH, SECONDS_PER_SLOT, GENESIS_TIME, EPOCHS_PER_FRAME, + SECONDS_PER_EPOCH, SECONDS_PER_FRAME, SLOTS_PER_FRAME, + computeSlotAt, computeEpochAt, computeEpochFirstSlot, computeEpochFirstSlotAt, + computeTimestampAtSlot, computeTimestampAtEpoch, + ZERO_HASH, HASH_1, HASH_2, HASH_3, HASH_4, HASH_5, + CONSENSUS_VERSION, deployHashConsensus} = require('./hash-consensus-deploy.test') + +const HashConsensus = artifacts.require('HashConsensusTimeTravellable') +const MockReportProcessor = artifacts.require('MockReportProcessor') + +contract('HashConsensus', ([admin, member1, member2, member3, member4, member5, stranger]) => { + let consensus + let reportProcessor + + context('Report Processor', () => { + let consensus + let reportProcessor + let reportProcessor2 + + const deploy = async (options = undefined) => { + const deployed = await deployHashConsensus(admin, options) + consensus = deployed.consensus + reportProcessor = deployed.reportProcessor + reportProcessor2 = await MockReportProcessor.new(CONSENSUS_VERSION, { from: admin }) + } + + context('setReportProcessor', () => { + before(deploy) + + it('checks new processor is not zero', async () => { + await assertRevert( + consensus.setReportProcessor(ZERO_ADDRESS), + 'AddressCannotBeZero()', + ) + }) + + it('checks next processor is not the same as previous', async () => { + await assertRevert( + consensus.setReportProcessor(reportProcessor.address), + 'NewProcessorCannotBeTheSame()', + ) + }) + + it('checks tx sender for MANAGE_REPORT_PROCESSOR_ROLE', async () => { + await assertRevert( + consensus.setReportProcessor(reportProcessor2.address, {from: stranger}), + `AccessControl: account ${stranger.toLowerCase()} is missing role ${await consensus.MANAGE_REPORT_PROCESSOR_ROLE()}`, + ) + }) + + it('emits ReportProcessorSet event', async () => { + const tx = await consensus.setReportProcessor(reportProcessor2.address) + assertEvent(tx, 'ReportProcessorSet', {expectedArgs: {processor: reportProcessor2.address, prevProcessor: reportProcessor.address}}) + }) + }) + }) +}) From 60357afe57fe7ab4dfe989c8379c711de3451217 Mon Sep 17 00:00:00 2001 From: rezed Date: Thu, 9 Feb 2023 00:09:23 +0700 Subject: [PATCH 2/3] test: initial zero processor values in hash consensus --- .../oracle/HashConsensusTimeTravellable.sol | 4 ++ .../oracle/hash-consensus-deploy.test.js | 5 +- .../hash-consensus-report-processor.test.js | 72 +++++++++++++------ test/helpers/assert.js | 10 ++- 4 files changed, 66 insertions(+), 25 deletions(-) diff --git a/contracts/0.8.9/test_helpers/oracle/HashConsensusTimeTravellable.sol b/contracts/0.8.9/test_helpers/oracle/HashConsensusTimeTravellable.sol index cb7cda2f5..bc6fc74db 100644 --- a/contracts/0.8.9/test_helpers/oracle/HashConsensusTimeTravellable.sol +++ b/contracts/0.8.9/test_helpers/oracle/HashConsensusTimeTravellable.sol @@ -72,4 +72,8 @@ contract HashConsensusTimeTravellable is HashConsensus { function advanceTimeByEpochs(uint256 numEpochs) external { _time += SECONDS_PER_SLOT * SLOTS_PER_EPOCH * numEpochs; } + + function getReportProcessor() external view returns (address) { + return _reportProcessor; + } } diff --git a/test/0.8.9/oracle/hash-consensus-deploy.test.js b/test/0.8.9/oracle/hash-consensus-deploy.test.js index a9c66f89b..5aa0b7a97 100644 --- a/test/0.8.9/oracle/hash-consensus-deploy.test.js +++ b/test/0.8.9/oracle/hash-consensus-deploy.test.js @@ -34,6 +34,7 @@ const CONSENSUS_VERSION = 1 async function deployHashConsensus(admin, { reportProcessor = null, + reportProcessorAddress = null, slotsPerEpoch = SLOTS_PER_EPOCH, secondsPerSlot = SECONDS_PER_SLOT, genesisTime = GENESIS_TIME, @@ -41,7 +42,7 @@ async function deployHashConsensus(admin, { fastLaneLengthSlots = 0, initialEpoch = 1 } = {}) { - if (!reportProcessor) { + if (!reportProcessor && !reportProcessorAddress) { reportProcessor = await MockReportProcessor.new(CONSENSUS_VERSION, { from: admin }) } @@ -53,7 +54,7 @@ async function deployHashConsensus(admin, { initialEpoch, fastLaneLengthSlots, admin, - reportProcessor.address, + reportProcessorAddress || reportProcessor.address, { from: admin } ) diff --git a/test/0.8.9/oracle/hash-consensus-report-processor.test.js b/test/0.8.9/oracle/hash-consensus-report-processor.test.js index a0e23ecb2..eee9c0371 100644 --- a/test/0.8.9/oracle/hash-consensus-report-processor.test.js +++ b/test/0.8.9/oracle/hash-consensus-report-processor.test.js @@ -1,4 +1,4 @@ -const { assert } = require('../../helpers/assert') +const { assert, getAccessControlMessage } = require('../../helpers/assert') const { toNum } = require('../../helpers/utils') const { assertBn, assertEvent, assertAmountOfEvents } = require('@aragon/contract-helpers-test/src/asserts') const { assertRevert } = require('../../helpers/assertThrow') @@ -21,43 +21,71 @@ contract('HashConsensus', ([admin, member1, member2, member3, member4, member5, context('Report Processor', () => { let consensus - let reportProcessor + let reportProcessor1 let reportProcessor2 const deploy = async (options = undefined) => { const deployed = await deployHashConsensus(admin, options) consensus = deployed.consensus - reportProcessor = deployed.reportProcessor + reportProcessor1 = deployed.reportProcessor reportProcessor2 = await MockReportProcessor.new(CONSENSUS_VERSION, { from: admin }) } - context('setReportProcessor', () => { - before(deploy) + const deployProcessorZero = async () => { + await deploy({ reportProcessorAddress: ZERO_ADDRESS }) + } - it('checks new processor is not zero', async () => { - await assertRevert( - consensus.setReportProcessor(ZERO_ADDRESS), - 'AddressCannotBeZero()', - ) - }) + context('without initial processor', () => { + before(deployProcessorZero) - it('checks next processor is not the same as previous', async () => { - await assertRevert( - consensus.setReportProcessor(reportProcessor.address), - 'NewProcessorCannotBeTheSame()', + it('has no initial processor', async () => { + assert.addressEqual( + await consensus.getReportProcessor(), + ZERO_ADDRESS, + 'there should be zero address for processor', ) }) + }) - it('checks tx sender for MANAGE_REPORT_PROCESSOR_ROLE', async () => { - await assertRevert( - consensus.setReportProcessor(reportProcessor2.address, {from: stranger}), - `AccessControl: account ${stranger.toLowerCase()} is missing role ${await consensus.MANAGE_REPORT_PROCESSOR_ROLE()}`, + context('with initial processor', () => { + before(deploy) + + it('properly set initial report processor', async () => { + assert.addressEqual( + await consensus.getReportProcessor(), + reportProcessor1.address, + 'processor address differs', ) }) - it('emits ReportProcessorSet event', async () => { - const tx = await consensus.setReportProcessor(reportProcessor2.address) - assertEvent(tx, 'ReportProcessorSet', {expectedArgs: {processor: reportProcessor2.address, prevProcessor: reportProcessor.address}}) + context('method setReportProcessor', () => { + before(deploy) + + it('checks next processor is not zero', async () => { + await assertRevert( + consensus.setReportProcessor(ZERO_ADDRESS), + 'AddressCannotBeZero()', + ) + }) + + it('checks next processor is not the same as previous', async () => { + await assertRevert( + consensus.setReportProcessor(reportProcessor1.address), + 'NewProcessorCannotBeTheSame()', + ) + }) + + it('checks tx sender for MANAGE_REPORT_PROCESSOR_ROLE', async () => { + await assertRevert( + consensus.setReportProcessor(reportProcessor2.address, {from: stranger}), + getAccessControlMessage(stranger.toLowerCase(), await consensus.MANAGE_REPORT_PROCESSOR_ROLE()) + ) + }) + + it('emits ReportProcessorSet event', async () => { + const tx = await consensus.setReportProcessor(reportProcessor2.address) + assertEvent(tx, 'ReportProcessorSet', {expectedArgs: {processor: reportProcessor2.address, prevProcessor: reportProcessor1.address}}) + }) }) }) }) diff --git a/test/helpers/assert.js b/test/helpers/assert.js index 3c312a3aa..6976856a9 100644 --- a/test/helpers/assert.js +++ b/test/helpers/assert.js @@ -55,6 +55,10 @@ chai.util.addMethod(chai.assert, 'notEquals', function (actual, expected, errorM this.notEqual(actual.toString(), expected.toString(), `${errorMsg || ""} expected ${expected.toString()} to not equal ${actual.toString()}`) }) +chai.util.addMethod(chai.assert, 'addressEqual', function (actual, expected, errorMsg) { + assert.equal(toChecksumAddress(actual), toChecksumAddress(expected), errorMsg) +}) + chai.util.addMethod(chai.assert, 'revertsWithCustomError', async function (receipt, reason) { try { await receipt @@ -104,4 +108,8 @@ function normalizeArg(arg) { return arg } -module.exports = { assert: chai.assert } +function getAccessControlMessage(address, role) { + return `AccessControl: account ${address} is missing role ${role}` +} + +module.exports = { assert: chai.assert, getAccessControlMessage } From 7cba25bc5b79be69af1c3b21b0e523e9e22317f2 Mon Sep 17 00:00:00 2001 From: rezed Date: Thu, 9 Feb 2023 00:36:12 +0700 Subject: [PATCH 3/3] test: hash consensus version getter --- .../oracle/HashConsensusTimeTravellable.sol | 4 +++ .../hash-consensus-report-processor.test.js | 30 ++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/contracts/0.8.9/test_helpers/oracle/HashConsensusTimeTravellable.sol b/contracts/0.8.9/test_helpers/oracle/HashConsensusTimeTravellable.sol index bc6fc74db..238cc9506 100644 --- a/contracts/0.8.9/test_helpers/oracle/HashConsensusTimeTravellable.sol +++ b/contracts/0.8.9/test_helpers/oracle/HashConsensusTimeTravellable.sol @@ -76,4 +76,8 @@ contract HashConsensusTimeTravellable is HashConsensus { function getReportProcessor() external view returns (address) { return _reportProcessor; } + + function getConsensusVersion() external view returns (uint256) { + return _getConsensusVersion(); + } } diff --git a/test/0.8.9/oracle/hash-consensus-report-processor.test.js b/test/0.8.9/oracle/hash-consensus-report-processor.test.js index eee9c0371..25790e55b 100644 --- a/test/0.8.9/oracle/hash-consensus-report-processor.test.js +++ b/test/0.8.9/oracle/hash-consensus-report-processor.test.js @@ -12,6 +12,8 @@ const { ZERO_HASH, HASH_1, HASH_2, HASH_3, HASH_4, HASH_5, CONSENSUS_VERSION, deployHashConsensus} = require('./hash-consensus-deploy.test') +const CONSENSUS_VERSION_2 = 2 + const HashConsensus = artifacts.require('HashConsensusTimeTravellable') const MockReportProcessor = artifacts.require('MockReportProcessor') @@ -28,7 +30,7 @@ contract('HashConsensus', ([admin, member1, member2, member3, member4, member5, const deployed = await deployHashConsensus(admin, options) consensus = deployed.consensus reportProcessor1 = deployed.reportProcessor - reportProcessor2 = await MockReportProcessor.new(CONSENSUS_VERSION, { from: admin }) + reportProcessor2 = await MockReportProcessor.new(CONSENSUS_VERSION_2, { from: admin }) } const deployProcessorZero = async () => { @@ -45,6 +47,13 @@ contract('HashConsensus', ([admin, member1, member2, member3, member4, member5, 'there should be zero address for processor', ) }) + + it('consensus version equals zero', async () => { + assert.equal( + await consensus.getConsensusVersion(), + 0, + ) + }) }) context('with initial processor', () => { @@ -87,6 +96,25 @@ contract('HashConsensus', ([admin, member1, member2, member3, member4, member5, assertEvent(tx, 'ReportProcessorSet', {expectedArgs: {processor: reportProcessor2.address, prevProcessor: reportProcessor1.address}}) }) }) + + context('consensus version', () => { + beforeEach(deploy) + + it('equals to version of initial processor', async () => { + assert.equal( + await consensus.getConsensusVersion(), + CONSENSUS_VERSION, + ) + }) + + it('equals to new processor version after it was changed', async () => { + await consensus.setReportProcessor(reportProcessor2.address) + assert.equal( + await consensus.getConsensusVersion(), + CONSENSUS_VERSION_2, + ) + }) + }) }) }) })