@@ -2,37 +2,36 @@ pragma solidity ^0.6.4;
2
2
3
3
import "@openzeppelin/contracts/math/SafeMath.sol " ;
4
4
5
- import "./governance/Governed.sol " ;
5
+ import "../governance/Governed.sol " ;
6
+ import "../upgrades/GraphProxy.sol " ;
7
+
8
+ import "./EpochManagerStorage.sol " ;
9
+ import "./IEpochManager.sol " ;
6
10
7
11
/**
8
12
* @title EpochManager contract
9
13
* @dev Tracks epochs based on its block duration to sync contracts in the protocol.
10
14
*/
11
- contract EpochManager is Governed {
15
+ contract EpochManager is EpochManagerV1Storage , IEpochManager , Governed {
12
16
using SafeMath for uint256 ;
13
17
14
- // -- State --
15
-
16
- // Epoch length in blocks
17
- uint256 public epochLength;
18
-
19
- // Epoch that was last run
20
- uint256 public lastRunEpoch;
21
-
22
- // Block and epoch when epoch length was last updated
23
- uint256 public lastLengthUpdateEpoch;
24
- uint256 public lastLengthUpdateBlock;
25
-
26
18
// -- Events --
27
19
28
20
event EpochRun (uint256 indexed epoch , address caller );
29
21
event EpochLengthUpdate (uint256 indexed epoch , uint256 epochLength );
30
22
31
23
/**
32
- * @dev Contract Constructor.
33
- * @param _epochLength Epoch length in blocks
24
+ * @dev Check if the caller is the governor or initializing the implementation.
34
25
*/
35
- constructor (uint256 _epochLength ) public {
26
+ modifier onlyGovernorOrInit {
27
+ require (msg .sender == governor || msg .sender == implementation, "Only Governor can call " );
28
+ _;
29
+ }
30
+
31
+ /**
32
+ * @dev Initialize this contract.
33
+ */
34
+ function initialize (uint256 _epochLength ) external onlyGovernorOrInit {
36
35
require (_epochLength > 0 , "Epoch length cannot be 0 " );
37
36
38
37
lastLengthUpdateEpoch = 0 ;
@@ -42,12 +41,27 @@ contract EpochManager is Governed {
42
41
emit EpochLengthUpdate (lastLengthUpdateEpoch, epochLength);
43
42
}
44
43
44
+ /**
45
+ * @dev Accept to be an implementation of proxy and run initializer.
46
+ * @param _proxy Graph proxy delegate caller
47
+ * @param _epochLength Epoch length in blocks
48
+ */
49
+ function acceptProxy (GraphProxy _proxy , uint256 _epochLength ) external {
50
+ require (msg .sender == _proxy.governor (), "Only proxy governor can upgrade " );
51
+
52
+ // Accept to be the implementation for this proxy
53
+ _proxy.acceptImplementation ();
54
+
55
+ // Initialization
56
+ EpochManager (address (_proxy)).initialize (_epochLength);
57
+ }
58
+
45
59
/**
46
60
* @dev Set the epoch length.
47
61
* @notice Set epoch length to `_epochLength` blocks
48
62
* @param _epochLength Epoch length in blocks
49
63
*/
50
- function setEpochLength (uint256 _epochLength ) external onlyGovernor {
64
+ function setEpochLength (uint256 _epochLength ) external override onlyGovernor {
51
65
require (_epochLength > 0 , "Epoch length cannot be 0 " );
52
66
require (_epochLength != epochLength, "Epoch length must be different to current " );
53
67
@@ -62,7 +76,7 @@ contract EpochManager is Governed {
62
76
* @dev Run a new epoch, should be called once at the start of any epoch.
63
77
* @notice Perform state changes for the current epoch
64
78
*/
65
- function runEpoch () external {
79
+ function runEpoch () external override {
66
80
// Check if already called for the current epoch
67
81
require (! isCurrentEpochRun (), "Current epoch already run " );
68
82
@@ -77,23 +91,23 @@ contract EpochManager is Governed {
77
91
* @dev Return true if the current epoch has already run.
78
92
* @return Return true if epoch has run
79
93
*/
80
- function isCurrentEpochRun () public view returns (bool ) {
94
+ function isCurrentEpochRun () public override view returns (bool ) {
81
95
return lastRunEpoch == currentEpoch ();
82
96
}
83
97
84
98
/**
85
99
* @dev Return current block number.
86
100
* @return Block number
87
101
*/
88
- function blockNum () public view returns (uint256 ) {
102
+ function blockNum () public override view returns (uint256 ) {
89
103
return block .number ;
90
104
}
91
105
92
106
/**
93
107
* @dev Return blockhash for a block.
94
108
* @return BlockHash for `_block` number
95
109
*/
96
- function blockHash (uint256 _block ) public view returns (bytes32 ) {
110
+ function blockHash (uint256 _block ) public override view returns (bytes32 ) {
97
111
uint256 currentBlock = blockNum ();
98
112
99
113
require (_block < currentBlock, "Can only retrieve past block hashes " );
@@ -109,23 +123,23 @@ contract EpochManager is Governed {
109
123
* @dev Return the current epoch, it may have not been run yet.
110
124
* @return The current epoch based on epoch length
111
125
*/
112
- function currentEpoch () public view returns (uint256 ) {
126
+ function currentEpoch () public override view returns (uint256 ) {
113
127
return lastLengthUpdateEpoch.add (epochsSinceUpdate ());
114
128
}
115
129
116
130
/**
117
131
* @dev Return block where the current epoch started.
118
132
* @return The block number when the current epoch started
119
133
*/
120
- function currentEpochBlock () public view returns (uint256 ) {
134
+ function currentEpochBlock () public override view returns (uint256 ) {
121
135
return lastLengthUpdateBlock.add (epochsSinceUpdate ().mul (epochLength));
122
136
}
123
137
124
138
/**
125
139
* @dev Return the number of blocks that passed since current epoch started.
126
140
* @return Blocks that passed since start of epoch
127
141
*/
128
- function currentEpochBlockSinceStart () public view returns (uint256 ) {
142
+ function currentEpochBlockSinceStart () public override view returns (uint256 ) {
129
143
return blockNum () - currentEpochBlock ();
130
144
}
131
145
@@ -134,7 +148,7 @@ contract EpochManager is Governed {
134
148
* @param _epoch Epoch to use as since epoch value
135
149
* @return Number of epochs and current epoch
136
150
*/
137
- function epochsSince (uint256 _epoch ) public view returns (uint256 ) {
151
+ function epochsSince (uint256 _epoch ) public override view returns (uint256 ) {
138
152
uint256 epoch = currentEpoch ();
139
153
return _epoch < epoch ? epoch.sub (_epoch) : 0 ;
140
154
}
@@ -143,7 +157,7 @@ contract EpochManager is Governed {
143
157
* @dev Return number of epochs passed since last epoch length update.
144
158
* @return The number of epoch that passed since last epoch length update
145
159
*/
146
- function epochsSinceUpdate () public view returns (uint256 ) {
160
+ function epochsSinceUpdate () public override view returns (uint256 ) {
147
161
return blockNum ().sub (lastLengthUpdateBlock).div (epochLength);
148
162
}
149
163
}
0 commit comments