Skip to content

Commit

Permalink
re-write automation forwarder tests with foundry and remove old tests (
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanRHall authored Jan 26, 2024
1 parent a9e27b0 commit 12c4e0a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 206 deletions.
105 changes: 45 additions & 60 deletions contracts/src/v0.8/automation/test/AutomationForwarder.t.sol
Original file line number Diff line number Diff line change
@@ -1,89 +1,74 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.16;

import {IAutomationRegistryConsumer} from "../interfaces/IAutomationRegistryConsumer.sol";
import {IAutomationForwarder} from "../interfaces/IAutomationForwarder.sol";
import {AutomationForwarder} from "../AutomationForwarder.sol";
import {AutomationForwarderLogic} from "../AutomationForwarderLogic.sol";
import {MockKeeperRegistry2_1} from "../mocks/MockKeeperRegistry2_1.sol";
import {UpkeepCounter} from "../testhelpers/UpkeepCounter.sol";
import {BaseTest} from "./BaseTest.t.sol";
import "forge-std/Test.sol";

// in contracts directory, run
// forge test --match-path src/v0.8/automation/test/AutomationForwarder.t.sol

contract AutomationForwarderSetUp is BaseTest {
IAutomationForwarder internal forwarder;
AutomationForwarderLogic internal logicContract;
IAutomationRegistryConsumer internal default_registry;
UpkeepCounter internal default_target;
uint256 constant GAS = 1e18;
contract Target {
function handler() external pure {}

function setUp() public override {
// BaseTest.setUp() not called since we want calls to iniatiate from default_registry, not from some predefined owner
default_registry = IAutomationRegistryConsumer(new MockKeeperRegistry2_1());
default_target = new UpkeepCounter(10000, 1);
vm.startPrank(address(default_registry));
logicContract = new AutomationForwarderLogic();
forwarder = IAutomationForwarder(
address(new AutomationForwarder(address(default_target), address(default_registry), address(logicContract)))
);
// OWNER not necessary?
OWNER = address(default_registry);
function handlerRevert() external pure {
revert("revert");
}
}

function getSelector(string memory _func, bytes memory myData) public pure returns (bytes memory) {
bytes4 functionSignature = bytes4(keccak256(bytes(_func)));
return abi.encodeWithSelector(functionSignature, myData);
contract AutomationForwarderTestSetUp is Test {
address internal constant REGISTRY = 0x3e19ef5Aaa2606655f5A677A97E085cf3811067c;
address internal constant STRANGER = 0x618fae5d04963B2CEf533F247Eb2C46Bf1801D3b;

IAutomationForwarder internal forwarder;
address internal TARGET;

function setUp() public {
TARGET = address(new Target());
AutomationForwarderLogic logicContract = new AutomationForwarderLogic();
forwarder = IAutomationForwarder(address(new AutomationForwarder(TARGET, REGISTRY, address(logicContract))));
}
}

contract AutomationForwarder_forward is AutomationForwarderSetUp {
function testBasicSuccess() public {
uint256 prevCount = default_target.counter();
bytes memory selector = getSelector("performUpkeep(bytes)", "performDataHere");
(bool val, uint256 gasUsed) = forwarder.forward(GAS, selector);
assertEq(val, true);
assertGt(gasUsed, 0);
uint256 newCount = default_target.counter();
assertEq(newCount, prevCount + 1);
contract AutomationForwarderTest_constructor is AutomationForwarderTestSetUp {
function testInitialValues() external {
assertEq(address(forwarder.getRegistry()), REGISTRY);
assertEq(forwarder.getTarget(), TARGET);
}

function testWrongFunctionSelectorSuccess() public {
uint256 prevCount = default_target.counter();
bytes memory selector = getSelector("performUpkeep(bytes calldata data)", "");
(bool val, uint256 gasUsed) = forwarder.forward(GAS, selector);
assertFalse(val);
assertGt(gasUsed, 0);
uint256 newCount = default_target.counter();
assertEq(newCount, prevCount);
function testTypeAndVersion() external {
assertEq(forwarder.typeAndVersion(), "AutomationForwarder 1.0.0");
}
}

function testNotAuthorizedReverts() public {
uint256 prevCount = default_target.counter();
bytes memory selector = getSelector("performUpkeep(bytes)", "");
changePrank(STRANGER);
contract AutomationForwarderTest_forward is AutomationForwarderTestSetUp {
function testOnlyCallableByTheRegistry() external {
vm.prank(REGISTRY);
forwarder.forward(100000, abi.encodeWithSelector(Target.handler.selector));
vm.prank(STRANGER);
vm.expectRevert();
(bool val, uint256 gasUsed) = forwarder.forward(GAS, selector);
assertFalse(val);
assertEq(gasUsed, 0);
uint256 newCount = default_target.counter();
assertEq(newCount, prevCount);
forwarder.forward(100000, abi.encodeWithSelector(Target.handler.selector));
}
}

contract AutomationForwarder_updateRegistry is AutomationForwarderSetUp {
function testBasicSuccess() public {
address newRegistry = address(1);
forwarder.updateRegistry(address(newRegistry));
IAutomationRegistryConsumer newReg = forwarder.getRegistry();
assertEq(address(newReg), newRegistry);
function testReturnsSuccessValueAndGasUsed() external {
vm.startPrank(REGISTRY);
(bool success, uint256 gasUsed) = forwarder.forward(100000, abi.encodeWithSelector(Target.handler.selector));
assertTrue(success);
assertGt(gasUsed, 0);
(success, gasUsed) = forwarder.forward(100000, abi.encodeWithSelector(Target.handlerRevert.selector));
assertFalse(success);
assertGt(gasUsed, 0);
}
}

function testNotFromRegistryNotAuthorizedReverts() public {
contract AutomationForwarderTest_updateRegistry is AutomationForwarderTestSetUp {
function testOnlyCallableByTheActiveRegistry() external {
address newRegistry = address(1);
changePrank(STRANGER);
vm.startPrank(REGISTRY);
forwarder.updateRegistry(newRegistry);
assertEq(address(forwarder.getRegistry()), newRegistry);
vm.expectRevert();
forwarder.updateRegistry(address(newRegistry));
forwarder.updateRegistry(REGISTRY);
}
}
146 changes: 0 additions & 146 deletions contracts/test/v0.8/automation/AutomationForwarder.test.ts

This file was deleted.

0 comments on commit 12c4e0a

Please sign in to comment.