Skip to content

Commit

Permalink
add tests for reorg protection flag
Browse files Browse the repository at this point in the history
  • Loading branch information
infiloop2 committed Feb 16, 2024
1 parent 943fb32 commit de686ea
Showing 1 changed file with 125 additions and 0 deletions.
125 changes: 125 additions & 0 deletions contracts/test/v0.8/automation/AutomationRegistry2_2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,81 @@ describe('AutomationRegistry2_2', () => {
}
})

it('allows bypassing reorg protection with reorgProtectionEnabled false config', async () => {
const tests: [string, BigNumber][] = [
['conditional', upkeepId],
['log-trigger', logUpkeepId],
]
let newConfig = config

Check warning on line 1329 in contracts/test/v0.8/automation/AutomationRegistry2_2.test.ts

View workflow job for this annotation

GitHub Actions / Solidity Lint

'newConfig' is never reassigned. Use 'const' instead
newConfig.reorgProtectionEnabled = false
await registry // used to test initial configurations
.connect(owner)
.setConfigTypeSafe(
signerAddresses,
keeperAddresses,
f,
newConfig,
offchainVersion,
offchainBytes,
)

for (const [type, id] of tests) {
const latestBlock = await ethers.provider.getBlock('latest')
// Try to transmit a report which has incorrect checkBlockHash
const tx = await getTransmitTx(registry, keeper1, [id], {
checkBlockNum: latestBlock.number - 1,
checkBlockHash: latestBlock.hash, // should be latestBlock.parentHash
})

const receipt = await tx.wait()
const upkeepPerformedLogs = parseUpkeepPerformedLogs(receipt)
assert.equal(
upkeepPerformedLogs.length,
1,
`wrong log count for ${type} upkeep`,
)
}
})

it('allows very old trigger block numbers when bypassing reorg protection with reorgProtectionEnabled config', async () => {
let newConfig = config

Check warning on line 1361 in contracts/test/v0.8/automation/AutomationRegistry2_2.test.ts

View workflow job for this annotation

GitHub Actions / Solidity Lint

'newConfig' is never reassigned. Use 'const' instead
newConfig.reorgProtectionEnabled = false
await registry // used to test initial configurations
.connect(owner)
.setConfigTypeSafe(
signerAddresses,
keeperAddresses,
f,
newConfig,
offchainVersion,
offchainBytes,
)
for (let i = 0; i < 256; i++) {
await ethers.provider.send('evm_mine', [])
}
const tests: [string, BigNumber][] = [
['conditional', upkeepId],
['log-trigger', logUpkeepId],
]
for (const [type, id] of tests) {
const latestBlock = await ethers.provider.getBlock('latest')
const old = await ethers.provider.getBlock(latestBlock.number - 256)
// Try to transmit a report which has incorrect checkBlockHash
const tx = await getTransmitTx(registry, keeper1, [id], {
checkBlockNum: old.number,
checkBlockHash: old.hash,
})

const receipt = await tx.wait()
const upkeepPerformedLogs = parseUpkeepPerformedLogs(receipt)
assert.equal(
upkeepPerformedLogs.length,
1,
`wrong log count for ${type} upkeep`,
)
}
})

it('allows very old trigger block numbers when bypassing reorg protection with empty blockhash', async () => {
// mine enough blocks so that blockhash(1) is unavailable
for (let i = 0; i <= 256; i++) {
Expand Down Expand Up @@ -1383,6 +1458,56 @@ describe('AutomationRegistry2_2', () => {
}
})

it('returns early when future block number is provided as trigger, irrespective of reorgProtectionEnabled config', async () => {
let newConfig = config

Check warning on line 1462 in contracts/test/v0.8/automation/AutomationRegistry2_2.test.ts

View workflow job for this annotation

GitHub Actions / Solidity Lint

'newConfig' is never reassigned. Use 'const' instead
newConfig.reorgProtectionEnabled = false
await registry // used to test initial configurations
.connect(owner)
.setConfigTypeSafe(
signerAddresses,
keeperAddresses,
f,
newConfig,
offchainVersion,
offchainBytes,
)
const tests: [string, BigNumber][] = [
['conditional', upkeepId],
['log-trigger', logUpkeepId],
]
for (const [type, id] of tests) {
const latestBlock = await ethers.provider.getBlock('latest')

// Should fail when blockhash is empty
let tx = await getTransmitTx(registry, keeper1, [id], {
checkBlockNum: latestBlock.number + 100,
checkBlockHash: emptyBytes32,
})
let receipt = await tx.wait()
let reorgedUpkeepReportLogs = parseReorgedUpkeepReportLogs(receipt)
// exactly 1 ReorgedUpkeepReportLogs log should be emitted
assert.equal(
reorgedUpkeepReportLogs.length,
1,
`wrong log count for ${type} upkeep`,
)

// Should also fail when blockhash is not empty
tx = await getTransmitTx(registry, keeper1, [id], {
checkBlockNum: latestBlock.number + 100,
checkBlockHash: latestBlock.hash,
})
receipt = await tx.wait()
reorgedUpkeepReportLogs = parseReorgedUpkeepReportLogs(receipt)
// exactly 1 ReorgedUpkeepReportLogs log should be emitted
assert.equal(
reorgedUpkeepReportLogs.length,
1,
`wrong log count for ${type} upkeep`,
)
}
})

it('returns early when upkeep is cancelled and cancellation delay has gone', async () => {
const latestBlockReport = await makeLatestBlockReport([upkeepId])
await registry.connect(admin).cancelUpkeep(upkeepId)
Expand Down

0 comments on commit de686ea

Please sign in to comment.