|
2 | 2 | pragma solidity ^0.8.19; |
3 | 3 |
|
4 | 4 | import {ConfirmedOwner} from "../../../shared/access/ConfirmedOwner.sol"; |
| 5 | +import {EnumerableSet} from "@openzeppelin/[email protected]/utils/structs/EnumerableSet.sol"; |
5 | 6 |
|
6 | 7 | import {ITypeAndVersion} from "../../../shared/interfaces/ITypeAndVersion.sol"; |
7 | 8 | import {IChannelConfigStore} from "./interfaces/IChannelConfigStore.sol"; |
8 | 9 |
|
9 | 10 | contract ChannelConfigStore is ConfirmedOwner, IChannelConfigStore, ITypeAndVersion { |
| 11 | + using EnumerableSet for EnumerableSet.UintSet; |
| 12 | + |
10 | 13 | event NewChannelDefinition(uint256 indexed donId, uint32 version, string url, bytes32 sha); |
| 14 | + event ChannelDefinitionAdded(uint256 indexed donId, uint32 indexed channelAdderId, string url, bytes32 sha); |
| 15 | + event ChannelAdderSet(uint256 indexed donId, uint32 indexed channelAdderId, bool allowed); |
| 16 | + event ChannelAdderAddressSet(uint32 indexed channelAdderId, address adderAddress); |
| 17 | + |
| 18 | + error UnauthorizedChannelAdder(); |
11 | 19 |
|
12 | 20 | constructor() ConfirmedOwner(msg.sender) {} |
13 | 21 |
|
14 | 22 | /// @notice The version of a channel definition keyed by DON ID |
15 | 23 | // Increments by 1 on every update |
16 | 24 | mapping(uint256 => uint256) internal s_channelDefinitionVersions; |
| 25 | + |
| 26 | + /// @notice Mapping from channel adder ID to its corresponding address |
| 27 | + mapping(uint32 => address) internal s_channelAdderAddresses; |
| 28 | + |
| 29 | + /// @notice Mapping from DON ID to the set of allowed channel adder IDs |
| 30 | + mapping(uint256 => EnumerableSet.UintSet) internal s_allowedChannelAdders; |
17 | 31 |
|
18 | 32 | function setChannelDefinitions(uint32 donId, string calldata url, bytes32 sha) external onlyOwner { |
19 | 33 | uint32 newVersion = uint32(++s_channelDefinitionVersions[uint256(donId)]); |
20 | 34 | emit NewChannelDefinition(donId, newVersion, url, sha); |
21 | 35 | } |
22 | 36 |
|
| 37 | + /// @notice Allows a channel adder to add channel definitions to the specified DON. |
| 38 | + /// The DON enforces (in its consensus rules), that the channel definitions provided |
| 39 | + /// by the channel adder are well-formed, purely additive, and do not overlaod the DON. |
| 40 | + /// @param donId The DON ID |
| 41 | + /// @param channelAdderId The channel adder ID |
| 42 | + /// @param url The URL of the channel definition |
| 43 | + /// @param sha The SHA hash of the channel definition |
| 44 | + function addChannelDefinitions( |
| 45 | + uint256 donId, |
| 46 | + uint32 channelAdderId, |
| 47 | + string calldata url, |
| 48 | + bytes32 sha |
| 49 | + ) external { |
| 50 | + if (msg.sender != s_channelAdderAddresses[channelAdderId]) { |
| 51 | + revert UnauthorizedChannelAdder(); |
| 52 | + } |
| 53 | + if (!s_allowedChannelAdders[donId].contains(channelAdderId)) { |
| 54 | + revert UnauthorizedChannelAdder(); |
| 55 | + } |
| 56 | + emit ChannelDefinitionAdded(donId, channelAdderId, url, sha); |
| 57 | + } |
| 58 | + |
| 59 | + /// @notice Sets the address for a channel adder ID |
| 60 | + /// @param channelAdderId The channel adder ID |
| 61 | + /// @param adderAddress The address to associate with the channel adder ID |
| 62 | + function setChannelAdderAddress(uint32 channelAdderId, address adderAddress) external onlyOwner { |
| 63 | + s_channelAdderAddresses[channelAdderId] = adderAddress; |
| 64 | + emit ChannelAdderAddressSet(channelAdderId, adderAddress); |
| 65 | + } |
| 66 | + |
| 67 | + /// @notice Sets whether a channel adder ID is allowed for a DON |
| 68 | + /// @param donId The DON ID |
| 69 | + /// @param channelAdderId The channel adder ID |
| 70 | + /// @param allowed Whether the channel adder should be allowed or removed |
| 71 | + function setChannelAdder(uint256 donId, uint32 channelAdderId, bool allowed) external onlyOwner { |
| 72 | + if (allowed) { |
| 73 | + s_allowedChannelAdders[donId].add(channelAdderId); |
| 74 | + } else { |
| 75 | + s_allowedChannelAdders[donId].remove(channelAdderId); |
| 76 | + } |
| 77 | + emit ChannelAdderSet(donId, channelAdderId, allowed); |
| 78 | + } |
| 79 | + |
| 80 | + /// @notice Gets the address associated with a channel adder ID |
| 81 | + /// @param channelAdderId The channel adder ID |
| 82 | + /// @return The address associated with the channel adder ID |
| 83 | + function getChannelAdderAddress(uint32 channelAdderId) external view returns (address) { |
| 84 | + return s_channelAdderAddresses[channelAdderId]; |
| 85 | + } |
| 86 | + |
| 87 | + /// @notice Checks if a channel adder is allowed for a DON |
| 88 | + /// @param donId The DON ID |
| 89 | + /// @param channelAdderId The channel adder ID |
| 90 | + /// @return True if the channel adder is allowed for the DON |
| 91 | + function isChannelAdderAllowed(uint256 donId, uint32 channelAdderId) external view returns (bool) { |
| 92 | + return s_allowedChannelAdders[donId].contains(channelAdderId); |
| 93 | + } |
| 94 | + |
| 95 | + /// @notice Gets all allowed channel adder IDs for a DON |
| 96 | + /// @param donId The DON ID |
| 97 | + /// @return An array of allowed channel adder IDs |
| 98 | + function getAllowedChannelAdders(uint256 donId) external view returns (uint256[] memory) { |
| 99 | + return s_allowedChannelAdders[donId].values(); |
| 100 | + } |
| 101 | + |
23 | 102 | function typeAndVersion() external pure override returns (string memory) { |
24 | 103 | return "ChannelConfigStore 0.0.1"; |
25 | 104 | } |
|
0 commit comments