-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathManaged.sol
196 lines (166 loc) · 6 KB
/
Managed.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.7.6;
import "./IController.sol";
import "../curation/ICuration.sol";
import "../epochs/IEpochManager.sol";
import "../rewards/IRewardsManager.sol";
import "../staking/IStaking.sol";
import "../token/IGraphToken.sol";
import "../arbitrum/ITokenGateway.sol";
/**
* @title Graph Managed contract
* @dev The Managed contract provides an interface to interact with the Controller.
* It also provides local caching for contract addresses. This mechanism relies on calling the
* public `syncAllContracts()` function whenever a contract changes in the controller.
*
* Inspired by Livepeer:
* https://github.com/livepeer/protocol/blob/streamflow/contracts/Controller.sol
*/
contract Managed {
// -- State --
// Controller that contract is registered with
IController public controller;
mapping(bytes32 => address) private addressCache;
uint256[10] private __gap;
// -- Events --
event ParameterUpdated(string param);
event SetController(address controller);
/**
* @dev Emitted when contract with `nameHash` is synced to `contractAddress`.
*/
event ContractSynced(bytes32 indexed nameHash, address contractAddress);
// -- Modifiers --
function _notPartialPaused() internal view {
require(!controller.paused(), "Paused");
require(!controller.partialPaused(), "Partial-paused");
}
function _notPaused() internal view virtual {
require(!controller.paused(), "Paused");
}
function _onlyGovernor() internal view {
require(msg.sender == controller.getGovernor(), "Caller must be Controller governor");
}
function _onlyController() internal view {
require(msg.sender == address(controller), "Caller must be Controller");
}
modifier notPartialPaused() {
_notPartialPaused();
_;
}
modifier notPaused() {
_notPaused();
_;
}
// Check if sender is controller.
modifier onlyController() {
_onlyController();
_;
}
// Check if sender is the governor.
modifier onlyGovernor() {
_onlyGovernor();
_;
}
// -- Functions --
/**
* @dev Initialize the controller.
*/
function _initialize(address _controller) internal {
_setController(_controller);
}
/**
* @notice Set Controller. Only callable by current controller.
* @param _controller Controller contract address
*/
function setController(address _controller) external onlyController {
_setController(_controller);
}
/**
* @dev Set controller.
* @param _controller Controller contract address
*/
function _setController(address _controller) internal {
require(_controller != address(0), "Controller must be set");
controller = IController(_controller);
emit SetController(_controller);
}
/**
* @dev Return Curation interface.
* @return Curation contract registered with Controller
*/
function curation() internal view returns (ICuration) {
return ICuration(_resolveContract(keccak256("Curation")));
}
/**
* @dev Return EpochManager interface.
* @return Epoch manager contract registered with Controller
*/
function epochManager() internal view returns (IEpochManager) {
return IEpochManager(_resolveContract(keccak256("EpochManager")));
}
/**
* @dev Return RewardsManager interface.
* @return Rewards manager contract registered with Controller
*/
function rewardsManager() internal view returns (IRewardsManager) {
return IRewardsManager(_resolveContract(keccak256("RewardsManager")));
}
/**
* @dev Return Staking interface.
* @return Staking contract registered with Controller
*/
function staking() internal view returns (IStaking) {
return IStaking(_resolveContract(keccak256("Staking")));
}
/**
* @dev Return GraphToken interface.
* @return Graph token contract registered with Controller
*/
function graphToken() internal view returns (IGraphToken) {
return IGraphToken(_resolveContract(keccak256("GraphToken")));
}
/**
* @dev Return GraphTokenGateway (L1 or L2) interface.
* @return Graph token gateway contract registered with Controller
*/
function graphTokenGateway() internal view returns (ITokenGateway) {
return ITokenGateway(_resolveContract(keccak256("GraphTokenGateway")));
}
/**
* @dev Resolve a contract address from the cache or the Controller if not found.
* @return Address of the contract
*/
function _resolveContract(bytes32 _nameHash) internal view returns (address) {
address contractAddress = addressCache[_nameHash];
if (contractAddress == address(0)) {
contractAddress = controller.getContractProxy(_nameHash);
}
return contractAddress;
}
/**
* @dev Cache a contract address from the Controller registry.
* @param _name Name of the contract to sync into the cache
*/
function _syncContract(string memory _name) internal {
bytes32 nameHash = keccak256(abi.encodePacked(_name));
address contractAddress = controller.getContractProxy(nameHash);
if (addressCache[nameHash] != contractAddress) {
addressCache[nameHash] = contractAddress;
emit ContractSynced(nameHash, contractAddress);
}
}
/**
* @dev Sync protocol contract addresses from the Controller registry.
* This function will cache all the contracts using the latest addresses
* Anyone can call the function whenever a Proxy contract change in the
* controller to ensure the protocol is using the latest version
*/
function syncAllContracts() external {
_syncContract("Curation");
_syncContract("EpochManager");
_syncContract("RewardsManager");
_syncContract("Staking");
_syncContract("GraphToken");
_syncContract("GraphTokenGateway");
}
}