-
Notifications
You must be signed in to change notification settings - Fork 18
/
Master.sol
232 lines (199 loc) · 8.95 KB
/
Master.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* Copyright (C) 2017 GovBlocks.io
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/ */
pragma solidity 0.4.24;
import "./Upgradeable.sol";
import "./GovBlocksMaster.sol";
import "./imports/openzeppelin-solidity/contracts/ownership/Ownable.sol";
import "./GovernanceData.sol";
import "./Governed.sol";
contract Master is Ownable {
uint[] public versionDates;
bytes32 public dAppName;
bytes2[] internal allContractNames;
mapping(address => bool) public contractsActive;
mapping(uint => mapping(bytes2 => address)) internal allContractVersions;
address public dAppTokenProxy;
GovBlocksMaster internal gbm;
Upgradeable internal up;
Governed internal govern;
address public dAppToken;
function initMaster(address _ownerAddress, bytes32 _gbUserName) public {
require(address(gbm) == address(0));
contractsActive[address(this)] = true;
gbm = GovBlocksMaster(msg.sender);
dAppName = _gbUserName;
owner = _ownerAddress;
versionDates.push(now); //solhint-disable-line
addContractNames();
}
/// @dev Checks if the address is authorized to make changes.
/// owner allowed for debugging only, will be removed before launch.
function isAuth() public view returns(bool check) {
if (versionDates.length < 2) {
if (owner == msg.sender)
check = true;
} else {
if (getLatestAddress("SV") == msg.sender || owner == msg.sender)
check = true;
}
}
/// @dev Checks if the caller address is either one of its active contract address or owner.
/// @param _address address to be checked for internal
/// @return check returns true if the condition meets
function isInternal(address _address) public view returns(bool check) {
if (contractsActive[_address] || owner == _address)
check = true;
}
/// @dev returns gbm address
function gbmAddress() public view returns(address) {
return address(gbm);
}
/// @dev Creates a new version of contract addresses
/// @param _contractAddresses Array of contract addresses which will be generated
function addNewVersion(address[] _contractAddresses) public {
require(isAuth());
address gbt = gbm.gbtAddress();
if (versionDates.length < 2) {
govern = new Governed();
GovernChecker governChecker = GovernChecker(govern.governChecker());
if (getCodeSize(address(governChecker)) > 0) {
if (governChecker.authorizedAddressNumber(dAppName, _contractAddresses[3]) == 0)
governChecker.initializeAuthorized(dAppName, _contractAddresses[3]);
}
dAppToken = gbm.getDappTokenAddress(dAppName);
dAppTokenProxy = gbm.getDappTokenProxyAddress(dAppName);
}
for (uint i = 0; i < allContractNames.length - 2; i++) {
allContractVersions[versionDates.length][allContractNames[i+2]] = _contractAddresses[i];
}
allContractVersions[versionDates.length]["GS"] = gbt;
versionDates.push(now); //solhint-disable-line
changeMasterAddress(address(this));
changeAllAddress();
}
/// @dev upgrades a single contract
function upgradeContract(bytes2 _contractsName, address _contractsAddress) public {
require(isAuth());
allContractVersions[versionDates.length - 1][_contractsName] = _contractsAddress;
changeMasterAddress(address(this));
changeAllAddress();
}
/// @dev sets dAppTokenProxy address
/// @param _dAppTokenProxy token proxy address
function setDAppTokenProxy(address _dAppTokenProxy) public {
require(isAuth());
dAppTokenProxy = _dAppTokenProxy;
}
/// @dev changeGBTSAddress
function changeGBTSAddress(address _gbtAddress) public {
require(msg.sender == address(gbm));
contractsActive[allContractVersions[versionDates.length - 1]["GS"]] = false;
allContractVersions[versionDates.length - 1]["GS"] = _gbtAddress;
contractsActive[_gbtAddress] = true;
changeAllAddress();
}
/// @dev Changes Master contract address
function changeMasterAddress(address _masterAddress) public {
if (_masterAddress != address(this)) {
require(isAuth());
}
allContractVersions[versionDates.length - 1]["MS"] = _masterAddress;
for (uint i = 2; i < allContractNames.length; i++) {
up = Upgradeable(allContractVersions[versionDates.length - 1][allContractNames[i]]);
up.changeMasterAddress();
}
contractsActive[address(this)] = false;
contractsActive[_masterAddress] = true;
}
/// @dev Changes GovBlocks Master address
/// @param _gbmNewAddress New GovBlocks master address
function changeGBMAddress(address _gbmNewAddress) public {
require(msg.sender == address(gbm));
gbm = GovBlocksMaster(_gbmNewAddress);
}
/// @dev Gets current version amd its master address
/// @return versionNo Current version number that is active
function getCurrentVersion() public view returns(uint versionNo) {
return versionDates.length - 1;
}
/// @dev Gets latest version name and address
/// @param _versionNo Version number that data we want to fetch
/// @return versionNo Version number
/// @return contractsName Latest version's contract names
/// @return contractsAddress Latest version's contract addresses
function getVersionData(uint _versionNo)
public
view
returns(uint versionNo, bytes2[] contractsName, address[] contractsAddress)
{
versionNo = _versionNo;
contractsName = new bytes2[](allContractNames.length);
contractsAddress = new address[](allContractNames.length);
for (uint i = 0; i < allContractNames.length; i++) {
contractsName[i] = allContractNames[i];
contractsAddress[i] = allContractVersions[versionNo][allContractNames[i]];
}
}
/// @dev Gets latest contract address
/// @param _contractName Contract name to fetch
function getLatestAddress(bytes2 _contractName) public view returns(address contractAddress) {
contractAddress =
allContractVersions[versionDates.length - 1][_contractName];
}
/// @dev adds a new contract type to master
function addNewContract(bytes2 _contractName, address _contractAddress) public {
require(isAuth());
allContractNames.push(_contractName);
contractsActive[_contractAddress] = true;
allContractVersions[versionDates.length - 1][_contractName] = _contractAddress;
}
function getEventCallerAddress() public view returns(address) {
return gbm.eventCaller();
}
function getGovernCheckerAddress() public view returns(address) {
return govern.governChecker();
}
/// @dev Save the initials of all the contracts
function addContractNames() internal {
allContractNames.push("MS");
allContractNames.push("GS");
allContractNames.push("GD");
allContractNames.push("MR");
allContractNames.push("PC");
allContractNames.push("SV");
allContractNames.push("GV");
allContractNames.push("PL");
}
function getCodeSize(address _addr) internal view returns(uint _size) {
assembly { //solhint-disable-line
_size := extcodesize(_addr)
}
}
/// @dev Sets the older versions of contract addresses as inactive and the latest one as active.
function changeAllAddress() internal {
uint i;
if (versionDates.length < 3) {
for (i = 2; i < allContractNames.length; i++) {
contractsActive[allContractVersions[versionDates.length - 1][allContractNames[i]]] = true;
up = Upgradeable(allContractVersions[versionDates.length - 1][allContractNames[i]]);
up.updateDependencyAddresses();
}
} else {
for (i = 2; i < allContractNames.length; i++) {
contractsActive[allContractVersions[versionDates.length - 2][allContractNames[i]]] = false;
contractsActive[allContractVersions[versionDates.length - 1][allContractNames[i]]] = true;
up = Upgradeable(allContractVersions[versionDates.length - 1][allContractNames[i]]);
up.updateDependencyAddresses();
}
}
}
}