|
| 1 | +// SPDX-License-Identifier: BUSL 1.1 |
| 2 | +pragma solidity 0.8.24; |
| 3 | + |
| 4 | +import {ITypeAndVersion} from "../../shared/interfaces/ITypeAndVersion.sol"; |
| 5 | + |
| 6 | +import {Ownable2StepMsgSender} from "../../shared/access/Ownable2StepMsgSender.sol"; |
| 7 | + |
| 8 | +contract WorkflowRegistry is Ownable2StepMsgSender, ITypeAndVersion { |
| 9 | + string public constant override typeAndVersion = "WorkflowRegistry 2.0.0-dev"; |
| 10 | + |
| 11 | + enum WorkflowStatus { |
| 12 | + ACTIVE, |
| 13 | + PAUSED |
| 14 | + } |
| 15 | + |
| 16 | + struct WorkflowMetadata { |
| 17 | + bytes32 workflowID; // Unique identifier from hash of owner address, WASM binary content, config content and secrets URL. |
| 18 | + bytes32 donLabel; // Label for the DON that is used when distributing the workflow across DONs. |
| 19 | + address owner; // ─────────╮ Workflow owner. |
| 20 | + uint64 index; // │ Global primary key identifier for the Workflow. |
| 21 | + WorkflowStatus status; // ─╯ Current status of the workflow (active, paused). |
| 22 | + string workflowName; // Human readable string capped at 64 characters length. |
| 23 | + string binaryURL; // URL to the WASM binary. |
| 24 | + string configURL; // URL to the config. |
| 25 | + string secretsURL; // URL to the encrypted secrets. Workflow DON applies a default refresh period (e.g. daily). |
| 26 | + } |
| 27 | + |
| 28 | + /// @dev Global auto‐incrementing counter for next index. Starts at 0. |
| 29 | + uint64 private s_nextWorkflowIndex; |
| 30 | + |
| 31 | + constructor() { |
| 32 | + // Intialize with default limits for Config. |
| 33 | + s_cfg.defaultsPacked = _packDefaults(200, 500, 200); |
| 34 | + } |
| 35 | + |
| 36 | + // ================================================================ |
| 37 | + // | Limits Config | |
| 38 | + // ================================================================ |
| 39 | + |
| 40 | + /// @dev Instead of a big struct with mappings, we store |
| 41 | + /// defaults in a single 32-byte slot, and use nested mappings |
| 42 | + /// for overrides. |
| 43 | + struct Config { |
| 44 | + // Pack three uint32 defaults into a single 96-bit field: |
| 45 | + // Layout in bits: [maxUser(0..31) | maxDON(32..63) | maxUserDON(64..95)] |
| 46 | + uint96 defaultsPacked; |
| 47 | + // 1) user-specific overrides: address -> limit |
| 48 | + mapping(address => uint32) userOverride; |
| 49 | + // 2) DON-specific overrides: donLabel -> limit |
| 50 | + mapping(bytes32 => uint32) donOverride; |
| 51 | + // 3) user+don override: user => (donLabel => limit) |
| 52 | + mapping(address => mapping(bytes32 => uint32)) userDONOverride; |
| 53 | + } |
| 54 | + |
| 55 | + // Our single config instance |
| 56 | + Config private s_cfg; |
| 57 | + |
| 58 | + // ───────────────────────────────────────────────────────────────────────── |
| 59 | + // Limits Config - External Setters: Owner can set defaults and individual overrides |
| 60 | + // ───────────────────────────────────────────────────────────────────────── |
| 61 | + |
| 62 | + /// @notice Update the three default limits in a single call. |
| 63 | + function setDefaults(uint32 maxPerUser, uint32 maxPerDON, uint32 maxPerUserDON) external onlyOwner { |
| 64 | + s_cfg.defaultsPacked = _packDefaults(maxPerUser, maxPerDON, maxPerUserDON); |
| 65 | + } |
| 66 | + |
| 67 | + /// @notice Override the maximum # of workflows a specific user can register. |
| 68 | + function setUserOverride(address user, uint32 limit) external onlyOwner { |
| 69 | + s_cfg.userOverride[user] = limit; |
| 70 | + } |
| 71 | + |
| 72 | + /// @notice Override the maximum # of workflows allowed for a specific DON label |
| 73 | + /// @dev donLabel is a bytes32 value of the string, which should not exceed 32 ASCII characters. |
| 74 | + function setDONOverride(bytes32 donLabel, uint32 limit) external onlyOwner { |
| 75 | + s_cfg.donOverride[donLabel] = limit; |
| 76 | + } |
| 77 | + |
| 78 | + /// @notice Override the max # of workflows for a specific (user, DON) pair. |
| 79 | + function setUserDONOverride(address user, bytes32 donLabel, uint32 limit) external onlyOwner { |
| 80 | + s_cfg.userDONOverride[user][donLabel] = limit; |
| 81 | + } |
| 82 | + |
| 83 | + // ───────────────────────────────────────────────────────────────────────── |
| 84 | + // Limits Config - Public Getters that return the *effective* limit |
| 85 | + // (override if set, else default) |
| 86 | + // ───────────────────────────────────────────────────────────────────────── |
| 87 | + |
| 88 | + /// @notice Effective max # of workflows for a particular user. |
| 89 | + function getMaxWorkflowsPerUser( |
| 90 | + address user |
| 91 | + ) public view returns (uint32) { |
| 92 | + uint32 overrideVal = s_cfg.userOverride[user]; |
| 93 | + if (overrideVal != 0) { |
| 94 | + return overrideVal; |
| 95 | + } |
| 96 | + // fallback to the default |
| 97 | + (uint32 defUser,,) = _unpackDefaults(s_cfg.defaultsPacked); |
| 98 | + return defUser; |
| 99 | + } |
| 100 | + |
| 101 | + /// @notice Effective max # of workflows for a particular DON. |
| 102 | + function getMaxWorkflowsPerDON( |
| 103 | + bytes32 donLabel |
| 104 | + ) public view returns (uint32) { |
| 105 | + uint32 overrideVal = s_cfg.donOverride[donLabel]; |
| 106 | + if (overrideVal != 0) { |
| 107 | + return overrideVal; |
| 108 | + } |
| 109 | + // fallback to the default |
| 110 | + (, uint32 defDON,) = _unpackDefaults(s_cfg.defaultsPacked); |
| 111 | + return defDON; |
| 112 | + } |
| 113 | + |
| 114 | + /// @notice Effective max # of workflows for a (user, DON) combo. |
| 115 | + function getMaxWorkflowsPerUserDON(address user, bytes32 donLabel) public view returns (uint32) { |
| 116 | + uint32 overrideVal = s_cfg.userDONOverride[user][donLabel]; |
| 117 | + if (overrideVal != 0) { |
| 118 | + return overrideVal; |
| 119 | + } |
| 120 | + // fallback to the default |
| 121 | + (,, uint32 defUserDON) = _unpackDefaults(s_cfg.defaultsPacked); |
| 122 | + return defUserDON; |
| 123 | + } |
| 124 | + |
| 125 | + /// @notice Returns the three default limits: |
| 126 | + /// (maxWorkflowsPerUser, maxWorkflowsPerDon, maxWorkflowsPerUserDon) |
| 127 | + function getDefaults() external view returns (uint32 maxPerUser, uint32 maxPerDon, uint32 maxPerUserDon) { |
| 128 | + (maxPerUser, maxPerDon, maxPerUserDon) = _unpackDefaults(s_cfg.defaultsPacked); |
| 129 | + return (maxPerUser, maxPerDon, maxPerUserDon); |
| 130 | + } |
| 131 | + |
| 132 | + // ───────────────────────────────────────────────────────────────────────── |
| 133 | + // Limits Config - Internal Helpers: set/read defaults (packed into one 96-bit variable) |
| 134 | + // ───────────────────────────────────────────────────────────────────────── |
| 135 | + |
| 136 | + /// @dev Store 3 uint32 values in a single 96-bit field. |
| 137 | + function _packDefaults( |
| 138 | + uint32 maxPerUser, |
| 139 | + uint32 maxPerDON, |
| 140 | + uint32 maxPerUserDON |
| 141 | + ) internal pure returns (uint96 packed) { |
| 142 | + // lower 32 bits: maxPerUser |
| 143 | + // middle 32 bits: maxPerDON |
| 144 | + // top 32 bits: maxPerUserDON |
| 145 | + packed = uint96(maxPerUser) | (uint96(maxPerDON) << 32) | (uint96(maxPerUserDON) << 64); |
| 146 | + return packed; |
| 147 | + } |
| 148 | + |
| 149 | + /// @dev Extract the 3 defaults from the packed value. |
| 150 | + function _unpackDefaults( |
| 151 | + uint96 packed |
| 152 | + ) internal pure returns (uint32 maxPerUser, uint32 maxPerDON, uint32 maxPerUserDON) { |
| 153 | + maxPerUser = uint32(packed); |
| 154 | + maxPerDON = uint32(packed >> 32); |
| 155 | + maxPerUserDON = uint32(packed >> 64); |
| 156 | + return (maxPerUser, maxPerDON, maxPerUserDON); |
| 157 | + } |
| 158 | +} |
0 commit comments