Skip to content

Commit

Permalink
add handleLog test; change block.number --> _blockNum() (#9746)
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanRHall authored Jul 6, 2023
1 parent ea89f08 commit b698a3f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
4 changes: 2 additions & 2 deletions contracts/src/v0.8/dev/automation/2_1/KeeperRegistry2_1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ contract KeeperRegistry2_1 is KeeperRegistryBase2_1, OCR2Abstract, Chainable, ER
}
if (
(trigger.blockHash != bytes32("") && _blockHash(trigger.blockNum) != trigger.blockHash) ||
trigger.blockNum >= block.number
trigger.blockNum >= _blockNum()
) {
// There are two cases of reorged report
// 1. trigger block number is in future: this is an edge case during extreme deep reorgs of chain
Expand All @@ -464,7 +464,7 @@ contract KeeperRegistry2_1 is KeeperRegistryBase2_1, OCR2Abstract, Chainable, ER
LogTrigger memory trigger = abi.decode(rawTrigger, (LogTrigger));
if (
(trigger.blockHash != bytes32("") && _blockHash(trigger.blockNum) != trigger.blockHash) ||
trigger.blockNum >= block.number
trigger.blockNum >= _blockNum()
) {
// Reorg protection is same as conditional trigger upkeeps
emit ReorgedUpkeepReport(upkeepId, rawTrigger);
Expand Down
43 changes: 40 additions & 3 deletions contracts/test/v0.8/automation/KeeperRegistry2_1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { UpkeepAutoFunder__factory as UpkeepAutoFunderFactory } from '../../../t
import { UpkeepTranscoder__factory as UpkeepTranscoderFactory } from '../../../typechain/factories/UpkeepTranscoder__factory'
import { MockArbGasInfo__factory as MockArbGasInfoFactory } from '../../../typechain/factories/MockArbGasInfo__factory'
import { MockOVMGasPriceOracle__factory as MockOVMGasPriceOracleFactory } from '../../../typechain/factories/MockOVMGasPriceOracle__factory'
import { ILogAutomation__factory as ILogAutomationactory } from '../../../typechain/factories/ILogAutomation__factory'
import { MockArbSys__factory as MockArbSysFactory } from '../../../typechain/factories/MockArbSys__factory'
import { AutomationUtils2_1 as AutomationUtils } from '../../../typechain/AutomationUtils2_1'
import { MercuryUpkeep } from '../../../typechain/MercuryUpkeep'
Expand All @@ -39,6 +40,10 @@ import {
InsufficientFundsUpkeepReportEvent,
CancelledUpkeepReportEvent,
} from '../../../typechain/IKeeperRegistryMaster'
import {
deployMockContract,
MockContract,
} from '@ethereum-waffle/mock-contract'
import { deployRegistry21 } from './helpers'

const describeMaybe = process.env.SKIP_SLOW ? describe.skip : describe
Expand Down Expand Up @@ -76,6 +81,7 @@ type Report = Parameters<AutomationUtils['_report']>[0]
type OnChainConfig = Parameters<AutomationUtils['_onChainConfig']>[0]
type LogTrigger = Parameters<AutomationUtils['_logTrigger']>[0]
type ConditionalTrigger = Parameters<AutomationUtils['_conditionalTrigger']>[0]
type Log = Parameters<AutomationUtils['_log']>[0]

// -----------------------------------------------------------------------------------------------
// These are the gas overheads that off chain systems should provide to check upkeep / transmit
Expand Down Expand Up @@ -153,7 +159,7 @@ let mgRegistry: IKeeperRegistry // "migrate registry" used in migration tests
let blankRegistry: IKeeperRegistry // used to test initial configurations
let mock: UpkeepMock
let autoFunderUpkeep: UpkeepAutoFunder
let ltUpkeep: UpkeepMock
let ltUpkeep: MockContract
let transcoder: UpkeepTranscoder
let mockArbGasInfo: MockArbGasInfo
let mockOVMGasPriceOracle: MockOVMGasPriceOracle
Expand Down Expand Up @@ -219,6 +225,12 @@ const encodeLogTrigger = (logTrigger: LogTrigger) => {
)
}

const encodeLog = (log: Log) => {
return (
'0x' + automationUtils.interface.encodeFunctionData('_log', [log]).slice(10)
)
}

const encodeReport = (report: Report) => {
return (
'0x' +
Expand Down Expand Up @@ -968,7 +980,7 @@ describe('KeeperRegistry2_1', () => {
)
afUpkeepId = await getUpkeepID(tx)

ltUpkeep = await upkeepMockFactory.deploy()
ltUpkeep = await deployMockContract(owner, ILogAutomationactory.abi)
tx = await registry
.connect(owner)
['registerUpkeep(address,uint32,address,uint8,bytes,bytes,bytes)'](
Expand Down Expand Up @@ -3032,8 +3044,9 @@ describe('KeeperRegistry2_1', () => {

context('when the registration is funded', () => {
beforeEach(async () => {
await linkToken.connect(admin).approve(registry.address, toWei('100'))
await linkToken.connect(admin).approve(registry.address, toWei('200'))
await registry.connect(admin).addFunds(upkeepId, toWei('100'))
await registry.connect(admin).addFunds(logUpkeepId, toWei('100'))
})

it('returns false, error code, and revert data if the target check reverts', async () => {
Expand Down Expand Up @@ -3145,6 +3158,30 @@ describe('KeeperRegistry2_1', () => {
assert.isTrue(checkUpkeepResult.linkNative.eq(linkEth))
})

it('calls checkLog for log-trigger upkeeps', async () => {
const log: Log = {
index: 0,
txIndex: 0,
txHash: ethers.utils.randomBytes(32),
blockNumber: 100,
blockHash: ethers.utils.randomBytes(32),
source: randomAddress(),
topics: [ethers.utils.randomBytes(32), ethers.utils.randomBytes(32)],
data: ethers.utils.randomBytes(1000),
}

await ltUpkeep.mock.checkLog.withArgs(log).returns(true, '0x1234')

const checkData = encodeLog(log)

const checkUpkeepResult = await registry
.connect(zeroAddress)
.callStatic['checkUpkeep(uint256,bytes)'](logUpkeepId, checkData)

expect(checkUpkeepResult.upkeepNeeded).to.be.true
expect(checkUpkeepResult.performData).to.equal('0x1234')
})

itMaybe(
'has a large enough gas overhead to cover upkeeps that use all their gas [ @skip-coverage ]',
async () => {
Expand Down
Loading

0 comments on commit b698a3f

Please sign in to comment.