Skip to content

Commit

Permalink
auto23 cleanup (#12630)
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanRHall authored Mar 29, 2024
1 parent 9c2764a commit 49a51b7
Show file tree
Hide file tree
Showing 11 changed files with 420 additions and 403 deletions.

Large diffs are not rendered by default.

125 changes: 53 additions & 72 deletions contracts/src/v0.8/automation/dev/v2_3/AutomationRegistry2_3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {Chainable} from "../../Chainable.sol";
import {IERC677Receiver} from "../../../shared/interfaces/IERC677Receiver.sol";
import {OCR2Abstract} from "../../../shared/ocr2/OCR2Abstract.sol";
import {IERC20} from "../../../vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/IERC20.sol";
import {SafeCast} from "../../../vendor/openzeppelin-solidity/v4.8.3/contracts/utils/math/SafeCast.sol";

/**
* @notice Registry for adding work for Chainlink nodes to perform on client
Expand Down Expand Up @@ -77,7 +78,7 @@ contract AutomationRegistry2_3 is AutomationRegistryBase2_3, OCR2Abstract, Chain
}

// ================================================================
// | ACTIONS |
// | HOT PATH ACTIONS |
// ================================================================

/**
Expand Down Expand Up @@ -113,6 +114,15 @@ contract AutomationRegistry2_3 is AutomationRegistryBase2_3, OCR2Abstract, Chain
}
}

/**
* @notice handles the report by performing the upkeeps and updating the state
* @param hotVars the hot variables of the registry
* @param report the report to be handled (already verified and decoded)
* @param gasOverhead the running tally of gas overhead to be split across the upkeeps
* @dev had to split this function from transmit() to avoid stack too deep errors
* @dev all other internal / private functions are generally defined in the Base contract
* we leave this here because it is essentially a continuation of the transmit() function,
*/
function _handleReport(HotVars memory hotVars, Report memory report, uint256 gasOverhead) private {
UpkeepTransmitInfo[] memory upkeepTransmitInfo = new UpkeepTransmitInfo[](report.upkeepIds.length);
TransmitVars memory transmitVars = TransmitVars({
Expand Down Expand Up @@ -158,7 +168,7 @@ contract AutomationRegistry2_3 is AutomationRegistryBase2_3, OCR2Abstract, Chain
(TRANSMIT_CALLDATA_PER_SIGNER_BYTES_OVERHEAD * (hotVars.f + 1));
transmitVars.totalCalldataWeight += upkeepTransmitInfo[i].calldataWeight;

// Deduct that gasUsed by upkeep from our running counter
// Deduct the gasUsed by upkeep from the overhead tally - upkeeps pay for their own gas individually
gasOverhead -= upkeepTransmitInfo[i].gasUsed;

// Store last perform block number / deduping key for upkeep
Expand Down Expand Up @@ -219,22 +229,34 @@ contract AutomationRegistry2_3 is AutomationRegistryBase2_3, OCR2Abstract, Chain
}

/**
* @notice simulates the upkeep with the perform data returned from checkUpkeep
* @param id identifier of the upkeep to execute the data with.
* @param performData calldata parameter to be passed to the target upkeep.
* @return success whether the call reverted or not
* @return gasUsed the amount of gas the target contract consumed
* @notice adds fund to an upkeep
* @param id the upkeepID
* @param amount the amount of funds to add, in the upkeep's billing token
*/
function simulatePerformUpkeep(
uint256 id,
bytes calldata performData
) external returns (bool success, uint256 gasUsed) {
_preventExecution();

if (s_hotVars.paused) revert RegistryPaused();
function addFunds(uint256 id, uint96 amount) external payable {
Upkeep memory upkeep = s_upkeep[id];
(success, gasUsed) = _performUpkeep(upkeep.forwarder, upkeep.performGas, performData);
return (success, gasUsed);
if (upkeep.maxValidBlocknumber != UINT32_MAX) revert UpkeepCancelled();

if (msg.value != 0) {
if (upkeep.billingToken != IERC20(i_wrappedNativeToken)) {
revert InvalidToken();
}
amount = SafeCast.toUint96(msg.value);
}

s_upkeep[id].balance = upkeep.balance + amount;
s_reserveAmounts[upkeep.billingToken] = s_reserveAmounts[upkeep.billingToken] + amount;

if (msg.value == 0) {
// ERC20 payment
bool success = upkeep.billingToken.transferFrom(msg.sender, address(this), amount);
if (!success) revert TransferFailed();
} else {
// native payment
i_wrappedNativeToken.deposit{value: amount}();
}

emit FundsAdded(id, msg.sender, amount);
}

/**
Expand All @@ -255,12 +277,13 @@ contract AutomationRegistry2_3 is AutomationRegistryBase2_3, OCR2Abstract, Chain
}

// ================================================================
// | SETTERS |
// | OCR2ABSTRACT |
// ================================================================

/**
* @inheritdoc OCR2Abstract
* @dev prefer the type-safe version of setConfig (below) whenever possible. The OnchainConfig could differ between registry versions
* @dev this function takes up precious space on the root contract, but must be implemented to conform to the OCR2Abstract interface
*/
function setConfig(
address[] memory signers,
Expand All @@ -287,6 +310,17 @@ contract AutomationRegistry2_3 is AutomationRegistryBase2_3, OCR2Abstract, Chain
);
}

/**
* @notice sets the configuration for the registry
* @param signers the list of permitted signers
* @param transmitters the list of permitted transmitters
* @param f the maximum tolerance for faulty nodes
* @param onchainConfig configuration values that are used on-chain
* @param offchainConfigVersion the version of the offchainConfig
* @param offchainConfig configuration values that are used off-chain
* @param billingTokens the list of valid billing tokens
* @param billingConfigs the configurations for each billing token
*/
function setConfigTypeSafe(
address[] memory signers,
address[] memory transmitters,
Expand Down Expand Up @@ -372,63 +406,9 @@ contract AutomationRegistry2_3 is AutomationRegistryBase2_3, OCR2Abstract, Chain
);
}

function _updateTransmitters(address[] memory signers, address[] memory transmitters) internal {
// move all pooled payments out of the pool to each transmitter's balance
for (uint256 i = 0; i < s_transmittersList.length; i++) {
_updateTransmitterBalanceFromPool(
s_transmittersList[i],
s_hotVars.totalPremium,
uint96(s_transmittersList.length)
);
}

// remove any old signer/transmitter addresses
address transmitterAddress;
PayoutMode mode = s_payoutMode;
for (uint256 i = 0; i < s_transmittersList.length; i++) {
transmitterAddress = s_transmittersList[i];
delete s_signers[s_signersList[i]];
// Do not delete the whole transmitter struct as it has balance information stored
s_transmitters[transmitterAddress].active = false;
if (mode == PayoutMode.OFF_CHAIN && s_transmitters[transmitterAddress].balance > 0) {
s_deactivatedTransmitters.add(transmitterAddress);
}
}
delete s_signersList;
delete s_transmittersList;

// add new signer/transmitter addresses
Transmitter memory transmitter;
for (uint256 i = 0; i < signers.length; i++) {
if (s_signers[signers[i]].active) revert RepeatedSigner();
if (signers[i] == ZERO_ADDRESS) revert InvalidSigner();
s_signers[signers[i]] = Signer({active: true, index: uint8(i)});

transmitterAddress = transmitters[i];
if (transmitterAddress == ZERO_ADDRESS) revert InvalidTransmitter();
transmitter = s_transmitters[transmitterAddress];
if (transmitter.active) revert RepeatedTransmitter();
transmitter.active = true;
transmitter.index = uint8(i);
// new transmitters start afresh from current totalPremium
// some spare change of premium from previous pool will be forfeited
transmitter.lastCollected = s_hotVars.totalPremium;
s_transmitters[transmitterAddress] = transmitter;
if (mode == PayoutMode.OFF_CHAIN) {
s_deactivatedTransmitters.remove(transmitterAddress);
}
}

s_signersList = signers;
s_transmittersList = transmitters;
}

// ================================================================
// | GETTERS |
// ================================================================

/**
* @inheritdoc OCR2Abstract
* @dev this function takes up precious space on the root contract, but must be implemented to conform to the OCR2Abstract interface
*/
function latestConfigDetails()
external
Expand All @@ -441,6 +421,7 @@ contract AutomationRegistry2_3 is AutomationRegistryBase2_3, OCR2Abstract, Chain

/**
* @inheritdoc OCR2Abstract
* @dev this function takes up precious space on the root contract, but must be implemented to conform to the OCR2Abstract interface
*/
function latestConfigDigestAndEpoch()
external
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,11 @@ abstract contract AutomationRegistryBase2_3 is ConfirmedOwner {
event AdminPrivilegeConfigSet(address indexed admin, bytes privilegeConfig);
event BillingConfigOverridden(uint256 indexed id, BillingOverrides overrides);
event BillingConfigOverrideRemoved(uint256 indexed id);
event BillingConfigSet(IERC20 indexed token, BillingConfig config);
event CancelledUpkeepReport(uint256 indexed id, bytes trigger);
event ChainSpecificModuleUpdated(address newModule);
event DedupKeyAdded(bytes32 indexed dedupKey);
event FeesWithdrawn(address indexed assetAddress, address indexed recipient, uint256 amount);
event FundsAdded(uint256 indexed id, address indexed from, uint96 amount);
event FundsWithdrawn(uint256 indexed id, uint256 amount, address to);
event InsufficientFundsUpkeepReport(uint256 indexed id, bytes trigger);
Expand Down Expand Up @@ -483,9 +485,6 @@ abstract contract AutomationRegistryBase2_3 is ConfirmedOwner {
event UpkeepTriggerConfigSet(uint256 indexed id, bytes triggerConfig);
event UpkeepUnpaused(uint256 indexed id);
event Unpaused(address account);
// Event to emit when a billing configuration is set
event BillingConfigSet(IERC20 indexed token, BillingConfig config);
event FeesWithdrawn(address indexed assetAddress, address indexed recipient, uint256 amount);

/**
* @param link address of the LINK Token
Expand Down Expand Up @@ -1091,4 +1090,66 @@ abstract contract AutomationRegistryBase2_3 is ConfirmedOwner {
emit BillingConfigSet(token, config);
}
}

/**
* @notice updates the signers and transmitters lists
*/
function _updateTransmitters(address[] memory signers, address[] memory transmitters) internal {
// move all pooled payments out of the pool to each transmitter's balance
for (uint256 i = 0; i < s_transmittersList.length; i++) {
_updateTransmitterBalanceFromPool(
s_transmittersList[i],
s_hotVars.totalPremium,
uint96(s_transmittersList.length)
);
}

// remove any old signer/transmitter addresses
address transmitterAddress;
PayoutMode mode = s_payoutMode;
for (uint256 i = 0; i < s_transmittersList.length; i++) {
transmitterAddress = s_transmittersList[i];
delete s_signers[s_signersList[i]];
// Do not delete the whole transmitter struct as it has balance information stored
s_transmitters[transmitterAddress].active = false;
if (mode == PayoutMode.OFF_CHAIN && s_transmitters[transmitterAddress].balance > 0) {
s_deactivatedTransmitters.add(transmitterAddress);
}
}
delete s_signersList;
delete s_transmittersList;

// add new signer/transmitter addresses
Transmitter memory transmitter;
for (uint256 i = 0; i < signers.length; i++) {
if (s_signers[signers[i]].active) revert RepeatedSigner();
if (signers[i] == ZERO_ADDRESS) revert InvalidSigner();
s_signers[signers[i]] = Signer({active: true, index: uint8(i)});

transmitterAddress = transmitters[i];
if (transmitterAddress == ZERO_ADDRESS) revert InvalidTransmitter();
transmitter = s_transmitters[transmitterAddress];
if (transmitter.active) revert RepeatedTransmitter();
transmitter.active = true;
transmitter.index = uint8(i);
// new transmitters start afresh from current totalPremium
// some spare change of premium from previous pool will be forfeited
transmitter.lastCollected = s_hotVars.totalPremium;
s_transmitters[transmitterAddress] = transmitter;
if (mode == PayoutMode.OFF_CHAIN) {
s_deactivatedTransmitters.remove(transmitterAddress);
}
}

s_signersList = signers;
s_transmittersList = transmitters;
}

/**
* @notice returns the size of the LINK liquidity pool
# @dev LINK max supply < 2^96, so casting to int256 is safe
*/
function _linkAvailableForPayment() internal view returns (int256) {
return int256(i_link.balanceOf(address(this))) - int256(s_reserveAmounts[IERC20(address(i_link))]);
}
}
Loading

0 comments on commit 49a51b7

Please sign in to comment.