Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto-11214: migrate more tests to foundry #13934

Merged
merged 13 commits into from
Aug 7, 2024
Prev Previous commit
Next Next commit
cancel upkeep tests
  • Loading branch information
FelixFan1992 committed Aug 2, 2024
commit b7c141c6fa42273d206937f5eb13a545236644f6
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ contract Withdraw is SetUp {

// default is ON_CHAIN mode
function test_WithdrawERC20Fees_RevertsWhen_LinkAvailableForPaymentIsNegative() public {
_transmit(usdUpkeepID18, registry); // adds USD token to finance withdrawable, and gives NOPs a LINK balance
_transmit(usdUpkeepID18, registry, bytes4(0)); // adds USD token to finance withdrawable, and gives NOPs a LINK balance
require(registry.linkAvailableForPayment() < 0, "linkAvailableForPayment should be negative");
require(
registry.getAvailableERC20ForPayment(address(usdToken18)) > 0,
Expand All @@ -375,7 +375,7 @@ contract Withdraw is SetUp {
registry.addFunds(id, 1e20);

// manually create a transmit so transmitters earn some rewards
_transmit(id, registry);
_transmit(id, registry, bytes4(0));
require(registry.linkAvailableForPayment() < 0, "linkAvailableForPayment should be negative");
vm.prank(FINANCE_ADMIN);
registry.withdrawERC20Fees(address(usdToken18), aMockAddress, 1); // finance can withdraw
Expand Down Expand Up @@ -1000,7 +1000,7 @@ contract NOPsSettlement is SetUp {
registry.addFunds(id, 1e20);

// manually create a transmit so transmitters earn some rewards
_transmit(id, registry);
_transmit(id, registry, bytes4(0));

// verify transmitters have positive balances
uint256[] memory payments = new uint256[](TRANSMITTERS.length);
Expand Down Expand Up @@ -1037,7 +1037,7 @@ contract NOPsSettlement is SetUp {
vm.startPrank(UPKEEP_ADMIN);
vm.roll(100 + block.number);
// manually create a transmit so transmitters earn some rewards
_transmit(id, registry);
_transmit(id, registry, bytes4(0));

uint256 erc20ForPayment2 = registry.getAvailableERC20ForPayment(address(usdToken18));
require(erc20ForPayment2 > erc20ForPayment1, "ERC20AvailableForPayment should be greater after another transmit");
Expand Down Expand Up @@ -1080,13 +1080,13 @@ contract NOPsSettlement is SetUp {
registry.addFunds(id, 1e20);

// manually create a transmit so TRANSMITTERS earn some rewards
_transmit(id, registry);
_transmit(id, registry, bytes4(0));

// TRANSMITTERS have positive balance now
// configure the registry to use NEW_TRANSMITTERS
_configureWithNewTransmitters(registry, registrar);

_transmit(id, registry);
_transmit(id, registry, bytes4(0));

// verify all transmitters have positive balances
address[] memory expectedPayees = new address[](6);
Expand Down Expand Up @@ -1195,7 +1195,7 @@ contract NOPsSettlement is SetUp {
registry.addFunds(id, 1e20);

// manually create a transmit so transmitters earn some rewards
_transmit(id, registry);
_transmit(id, registry, bytes4(0));

// disable offchain payments
_mintLink(address(registry), 1e19);
Expand Down Expand Up @@ -1235,7 +1235,7 @@ contract NOPsSettlement is SetUp {
// manually call transmit so transmitters earn some rewards
for (uint256 i = 0; i < 50; i++) {
vm.roll(100 + block.number);
_transmit(id, registry);
_transmit(id, registry, bytes4(0));
}

// disable offchain payments
Expand All @@ -1246,7 +1246,7 @@ contract NOPsSettlement is SetUp {
// manually call transmit after offchain payment is disabled
for (uint256 i = 0; i < 50; i++) {
vm.roll(100 + block.number);
_transmit(id, registry);
_transmit(id, registry, bytes4(0));
}

// payees should be able to withdraw onchain
Expand Down Expand Up @@ -1671,7 +1671,7 @@ contract Transmit is SetUp {
require(registry.getAvailableERC20ForPayment(address(weth)) == 0, "ERC20AvailableForPayment should be 0");

// do the thing
_transmit(upkeepIDs, registry);
_transmit(upkeepIDs, registry, bytes4(0));

// withdraw-able by the finance team should be positive
require(
Expand Down Expand Up @@ -1726,7 +1726,7 @@ contract Transmit is SetUp {

// manually create a transmit
vm.recordLogs();
_transmit(upkeepID, registry);
_transmit(upkeepID, registry, bytes4(0));
Vm.Log[] memory entries = vm.getRecordedLogs();

assertEq(entries.length, 3);
Expand Down Expand Up @@ -1777,7 +1777,7 @@ contract Transmit is SetUp {

// manually create a transmit
vm.recordLogs();
_transmit(upkeepID, registry);
_transmit(upkeepID, registry, bytes4(0));
Vm.Log[] memory entries = vm.getRecordedLogs();

assertEq(entries.length, 3);
Expand Down Expand Up @@ -2010,13 +2010,12 @@ contract Pause is SetUp {
);
}

// function test_revertsWhen_transmitInPausedRegistry() external {
// vm.startPrank(registry.owner());
// registry.pause();
//
// vm.expectRevert(Registry.RegistryPaused.selector);
// _transmit(usdUpkeepID18, registry);
// }
function test_revertsWhen_transmitInPausedRegistry() external {
vm.startPrank(registry.owner());
registry.pause();

_transmit(usdUpkeepID18, registry, Registry.RegistryPaused.selector);
}
FelixFan1992 marked this conversation as resolved.
Show resolved Hide resolved
}

contract Unpause is SetUp {
Expand Down Expand Up @@ -2067,31 +2066,35 @@ contract CancelUpkeep is SetUp {
vm.startPrank(UPKEEP_ADMIN);
registry.cancelUpkeep(linkUpkeepID);

vm.roll(50 + bn);

vm.startPrank(registry.owner());
vm.expectRevert(Registry.UpkeepCancelled.selector);
registry.cancelUpkeep(linkUpkeepID);
}

function test_RevertsWhen_UpkeepAlreadyCanceled_CalledByAdmin() external {
function test_RevertsWhen_UpkeepAlreadyCanceledByOwner_CalledByAdmin() external {
uint256 bn = block.number;
vm.startPrank(registry.owner());
registry.cancelUpkeep(linkUpkeepID);

vm.startPrank(UPKEEP_ADMIN);
vm.expectRevert(Registry.UpkeepCancelled.selector);
registry.cancelUpkeep(linkUpkeepID);
}

vm.roll(10 + bn);
function test_RevertsWhen_UpkeepAlreadyCanceledByAdmin_CalledByAdmin() external {
uint256 bn = block.number;
vm.startPrank(UPKEEP_ADMIN);
registry.cancelUpkeep(linkUpkeepID);

vm.expectRevert(Registry.UpkeepCancelled.selector);
registry.cancelUpkeep(linkUpkeepID);
}

function test_RevertsWhen_UpkeepAlreadyCanceled_CalledByOwner() external {
function test_RevertsWhen_UpkeepAlreadyCanceledByOwner_CalledByOwner() external {
uint256 bn = block.number;
vm.startPrank(registry.owner());
registry.cancelUpkeep(linkUpkeepID);

vm.roll(10 + bn);

vm.expectRevert(Registry.UpkeepCancelled.selector);
registry.cancelUpkeep(linkUpkeepID);
}
Expand All @@ -2101,7 +2104,6 @@ contract CancelUpkeep is SetUp {
vm.startPrank(UPKEEP_ADMIN);
registry.cancelUpkeep(linkUpkeepID);

vm.roll(10 + bn);
uint256 maxValidBlocknumber = uint256(registry.getUpkeep(linkUpkeepID).maxValidBlocknumber);

// 50 is the cancellation delay
Expand All @@ -2113,7 +2115,6 @@ contract CancelUpkeep is SetUp {
vm.startPrank(registry.owner());
registry.cancelUpkeep(linkUpkeepID);

vm.roll(10 + bn);
uint256 maxValidBlocknumber = uint256(registry.getUpkeep(linkUpkeepID).maxValidBlocknumber);

// cancellation by registry owner is immediate and no cancellation delay is applied
Expand Down
63 changes: 35 additions & 28 deletions contracts/src/v0.8/automation/test/v2_3/BaseTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -356,40 +356,44 @@ contract BaseTest is Test {
);
}

function _transmit(uint256 id, Registry registry) internal {
function _transmit(uint256 id, Registry registry, bytes4 selector) internal {
uint256[] memory ids = new uint256[](1);
ids[0] = id;
_transmit(ids, registry);
_transmit(ids, registry, selector);
}

function _transmit(uint256[] memory ids, Registry registry) internal {
uint256[] memory upkeepIds = new uint256[](ids.length);
uint256[] memory gasLimits = new uint256[](ids.length);
bytes[] memory performDatas = new bytes[](ids.length);
bytes[] memory triggers = new bytes[](ids.length);
for (uint256 i = 0; i < ids.length; i++) {
upkeepIds[i] = ids[i];
gasLimits[i] = registry.getUpkeep(ids[i]).performGas;
performDatas[i] = new bytes(0);
uint8 triggerType = registry.getTriggerType(ids[i]);
if (triggerType == 0) {
triggers[i] = _encodeConditionalTrigger(
AutoBase.ConditionalTrigger(uint32(block.number - 1), blockhash(block.number - 1))
);
} else {
revert("not implemented");
function _transmit(uint256[] memory ids, Registry registry, bytes4 selector) internal {
FelixFan1992 marked this conversation as resolved.
Show resolved Hide resolved
bytes memory reportBytes;
{
uint256[] memory upkeepIds = new uint256[](ids.length);
uint256[] memory gasLimits = new uint256[](ids.length);
bytes[] memory performDatas = new bytes[](ids.length);
bytes[] memory triggers = new bytes[](ids.length);
for (uint256 i = 0; i < ids.length; i++) {
upkeepIds[i] = ids[i];
gasLimits[i] = registry.getUpkeep(ids[i]).performGas;
performDatas[i] = new bytes(0);
uint8 triggerType = registry.getTriggerType(ids[i]);
if (triggerType == 0) {
triggers[i] = _encodeConditionalTrigger(
AutoBase.ConditionalTrigger(uint32(block.number - 1), blockhash(block.number - 1))
);
} else {
revert("not implemented");
}
}
}
AutoBase.Report memory report = AutoBase.Report(
uint256(1000000000),
uint256(2000000000),
upkeepIds,
gasLimits,
triggers,
performDatas
);

bytes memory reportBytes = _encodeReport(report);
AutoBase.Report memory report = AutoBase.Report(
uint256(1000000000),
uint256(2000000000),
upkeepIds,
gasLimits,
triggers,
performDatas
);

reportBytes = _encodeReport(report);
}
(, , bytes32 configDigest) = registry.latestConfigDetails();
bytes32[3] memory reportContext = [configDigest, configDigest, configDigest];
uint256[] memory signerPKs = new uint256[](2);
Expand All @@ -398,6 +402,9 @@ contract BaseTest is Test {
(bytes32[] memory rs, bytes32[] memory ss, bytes32 vs) = _signReport(reportBytes, reportContext, signerPKs);

vm.startPrank(TRANSMITTERS[0]);
if (selector != bytes4(0)) {
vm.expectRevert(selector);
}
registry.transmit(reportContext, reportBytes, rs, ss, vs);
vm.stopPrank();
}
Expand Down
32 changes: 0 additions & 32 deletions contracts/test/v0.8/automation/AutomationRegistry2_3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4299,18 +4299,6 @@ describe('AutomationRegistry2_3', () => {
})
})

describe('#pause', () => {
it('Does not allow transmits when paused', async () => {
await registry.connect(owner).pause()

await evmRevertCustomError(
getTransmitTx(registry, keeper1, [upkeepId]),
registry,
'RegistryPaused',
)
})
})

describe('#setPayees', () => {
const IGNORE_ADDRESS = '0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF'

Expand Down Expand Up @@ -4415,26 +4403,6 @@ describe('AutomationRegistry2_3', () => {
// exactly 1 CancelledUpkeepReport log should be emitted
assert.equal(cancelledUpkeepReportLogs.length, 1)
})

describe('when called by the owner when the admin has just canceled', () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
// @ts-ignore
let oldExpiration: BigNumber

beforeEach(async () => {
await registry.connect(admin).cancelUpkeep(upkeepId)
const registration = await registry.getUpkeep(upkeepId)
oldExpiration = registration.maxValidBlocknumber
})

it('reverts with proper error', async () => {
await evmRevertCustomError(
registry.connect(owner).cancelUpkeep(upkeepId),
registry,
'UpkeepCancelled',
)
})
})
})

describe('when called by the admin', async () => {
Expand Down
Loading