github.com/klaytn/klaytn@v1.12.1/contracts/reward/contract/AddressBookMock.sol (about) 1 // Copyright 2019 The klaytn Authors 2 // This file is part of the klaytn library. 3 // 4 // The klaytn library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The klaytn library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>. 16 17 pragma solidity ^0.4.24; 18 19 /** 20 * @title AddressBookMock 21 */ 22 23 contract AddressBookMock { 24 /* 25 * Constants 26 */ 27 uint256 constant public MAX_ADMIN = 50; 28 uint256 constant public MAX_PENDING_REQUEST = 100; 29 string constant public CONTRACT_TYPE = "AddressBookMock"; 30 uint8 constant public CN_NODE_ID_TYPE = 0; 31 uint8 constant public CN_STAKING_ADDRESS_TYPE = 1; 32 uint8 constant public CN_REWARD_ADDRESS_TYPE = 2; 33 uint8 constant public POC_CONTRACT_TYPE = 3; 34 uint8 constant public KIR_CONTRACT_TYPE = 4; 35 uint256 constant public ONE_WEEK = 1 weeks; 36 uint256 constant public TWO_WEEKS = 2 weeks; 37 uint256 constant public VERSION = 0; 38 39 enum Functions {Unknown, AddAdmin, DeleteAdmin, UpdateRequirement, ClearRequest, ActivateAddressBook, UpdatePocContract, UpdateKirContract, RegisterCnStakingContract, UnregisterCnStakingContract, UpdateSpareContract} 40 41 /* 42 * Storage 43 */ 44 address[] public adminList; 45 uint256 public requirement; 46 47 address public pocContractAddress; 48 address public kirContractAddress; 49 address public spareContractAddress; 50 51 bool public isActivated; 52 bool public isConstructed; 53 54 mapping(address => uint256) public cnIndexMap; 55 address[] public cnNodeIdList; 56 address[] public cnStakingContractList; 57 address[] public cnRewardAddressList; 58 59 /* 60 * setter functions 61 */ 62 function constructContract(address[] /* _adminList */, uint256 _requirement) external { 63 requirement = _requirement; 64 isConstructed = true; 65 } 66 67 function updateRequirement(uint256 _requirement) public { 68 requirement = _requirement; 69 } 70 71 function activateAddressBook() public { 72 require(isActivated == false, "Already activated."); 73 require(isConstructed, "AddressBookMock not constructed"); 74 require(pocContractAddress != address(0), "PoC contract is not registered."); 75 require(kirContractAddress != address(0), "KIR contract is not registered."); 76 require(cnNodeIdList.length != 0, "No node ID is listed."); 77 require(cnNodeIdList.length == cnStakingContractList.length, "Invalid length between node IDs and staking contracts."); 78 require(cnStakingContractList.length == cnRewardAddressList.length, "Invalid length between staking contracts and reward addresses."); 79 isActivated = true; 80 } 81 82 function updatePocContract(address _pocContractAddress, uint256 /* _version */) public 83 { 84 require(isConstructed, "AddressBookMock not constructed"); 85 pocContractAddress = _pocContractAddress; 86 } 87 88 function updateKirContract(address _kirContractAddress, uint256 /* _version */) public 89 { 90 require(isConstructed, "AddressBookMock not constructed"); 91 kirContractAddress = _kirContractAddress; 92 } 93 94 function updateSpareContract(address _spareContractAddress) public 95 { 96 require(isConstructed, "AddressBookMock not constructed"); 97 spareContractAddress = _spareContractAddress; 98 } 99 100 function registerCnStakingContract(address _cnNodeId, address _cnStakingContractAddress, address _cnRewardAddress) public 101 { 102 require(isConstructed, "AddressBookMock not constructed"); 103 if (cnNodeIdList.length > 0) { 104 require(cnNodeIdList[cnIndexMap[_cnNodeId]] != _cnNodeId, "CN node ID already exist."); 105 } 106 107 uint256 index = cnNodeIdList.length; 108 cnIndexMap[_cnNodeId] = index; 109 cnNodeIdList.push(_cnNodeId); 110 cnStakingContractList.push(_cnStakingContractAddress); 111 cnRewardAddressList.push(_cnRewardAddress); 112 } 113 114 function mockRegisterCnStakingContracts(address[] _cnNodeIdList, address[] _cnStakingContractAddressList, address[] _cnRewardAddressList) external 115 { 116 require(_cnNodeIdList.length == _cnStakingContractAddressList.length, "Different cnNodeId and cnStaking lengths"); 117 require(_cnNodeIdList.length == _cnRewardAddressList.length, "Different cnNodeId and reward lengths"); 118 for (uint i = 0; i < _cnNodeIdList.length; i++) { 119 // skip duplicate 120 address _cnNodeId = _cnNodeIdList[i]; 121 if (cnNodeIdList.length > 0 && cnNodeIdList[cnIndexMap[_cnNodeId]] == _cnNodeId) { 122 continue; 123 } 124 registerCnStakingContract(_cnNodeIdList[i], _cnStakingContractAddressList[i], _cnRewardAddressList[i]); 125 } 126 } 127 128 function unregisterCnStakingContract(address _cnNodeId) public 129 { 130 require(isConstructed, "AddressBookMock not constructed"); 131 132 uint256 index = cnIndexMap[_cnNodeId]; 133 require(cnNodeIdList[index] == _cnNodeId, "Invalid CN node ID."); 134 require(cnNodeIdList.length > 1, "CN should be more than one."); 135 136 if (index < cnNodeIdList.length - 1) { 137 cnNodeIdList[index] = cnNodeIdList[cnNodeIdList.length-1]; 138 cnStakingContractList[index] = cnStakingContractList[cnNodeIdList.length-1]; 139 cnRewardAddressList[index] = cnRewardAddressList[cnNodeIdList.length-1]; 140 141 cnIndexMap[cnNodeIdList[cnNodeIdList.length-1]] = index; 142 } 143 144 delete cnIndexMap[_cnNodeId]; 145 delete cnNodeIdList[cnNodeIdList.length-1]; 146 cnNodeIdList.length = cnNodeIdList.length - 1; 147 delete cnStakingContractList[cnStakingContractList.length-1]; 148 cnStakingContractList.length = cnStakingContractList.length - 1; 149 delete cnRewardAddressList[cnRewardAddressList.length-1]; 150 cnRewardAddressList.length = cnRewardAddressList.length - 1; 151 } 152 153 function mockUnregisterCnStakingContracts(address[] _cnNodeIdList) external 154 { 155 for (uint i = 0; i < _cnNodeIdList.length; i++) { 156 // skip duplicate 157 address _cnNodeId = _cnNodeIdList[i]; 158 uint256 index = cnIndexMap[_cnNodeId]; 159 if (cnNodeIdList[index] != _cnNodeId) { 160 continue; 161 } 162 unregisterCnStakingContract(_cnNodeIdList[i]); 163 } 164 } 165 166 function revokeRequest(Functions, bytes32, bytes32, bytes32) external { } 167 168 /* 169 * submit functions redirected to functions 170 */ 171 function submitAddAdmin(address /* _admin */) external { } 172 function submitDeleteAdmin(address /* _admin */) external { } 173 function submitUpdateRequirement(uint256 _requirement) external { updateRequirement(_requirement); } 174 function submitClearRequest() external { } 175 function submitActivateAddressBook() external { activateAddressBook(); } 176 function submitUpdatePocContract(address _pocContractAddress, uint256 _version) external { updatePocContract(_pocContractAddress, _version); } 177 function submitUpdateKirContract(address _kirContractAddress, uint256 _version) external { updateKirContract(_kirContractAddress, _version); } 178 function submitUpdateSpareContract(address _spareContractAddress) external { updateSpareContract(_spareContractAddress); } 179 function submitRegisterCnStakingContract(address _cnNodeId, address _cnStakingContractAddress, address _cnRewardAddress) external { registerCnStakingContract(_cnNodeId, _cnStakingContractAddress, _cnRewardAddress); } 180 function submitUnregisterCnStakingContract(address _cnNodeId) external { unregisterCnStakingContract(_cnNodeId); } 181 182 /* 183 * Getter functions 184 */ 185 function getState() external view returns(address[], uint256) { 186 return (adminList, 0); 187 } 188 189 function getAllAddress() external view returns(uint8[], address[]) { 190 uint8[] memory typeList; 191 address[] memory addressList; 192 if(isActivated == false) { 193 typeList = new uint8[](0); 194 addressList = new address[](0); 195 } else { 196 typeList = new uint8[](cnNodeIdList.length * 3 + 2); 197 addressList = new address[](cnNodeIdList.length * 3 + 2); 198 uint256 cnNodeCnt = cnNodeIdList.length; 199 for (uint256 i = 0; i < cnNodeCnt; i ++){ 200 //add node id and its type number to array 201 typeList[i * 3] = uint8(CN_NODE_ID_TYPE); 202 addressList[i * 3] = address(cnNodeIdList[i]); 203 //add staking address and its type number to array 204 typeList[i * 3 + 1] = uint8(CN_STAKING_ADDRESS_TYPE); 205 addressList[i * 3 + 1] = address(cnStakingContractList[i]); 206 //add reward address and its type number to array 207 typeList[i * 3 + 2] = uint8(CN_REWARD_ADDRESS_TYPE); 208 addressList[i * 3 + 2] = address(cnRewardAddressList[i]); 209 } 210 typeList[cnNodeCnt *3] = uint8(POC_CONTRACT_TYPE); 211 addressList[cnNodeCnt * 3] = address(pocContractAddress); 212 typeList[cnNodeCnt * 3 + 1] = uint8(KIR_CONTRACT_TYPE); 213 addressList[cnNodeCnt * 3 + 1] = address(kirContractAddress); 214 } 215 return (typeList, addressList); 216 } 217 218 function getAllAddressInfo() external view returns(address[], address[], address[], address, address) { 219 return (cnNodeIdList, cnStakingContractList, cnRewardAddressList, pocContractAddress, kirContractAddress); 220 } 221 222 function getCnInfo(address _cnNodeId) external view returns(address, address, address) { 223 uint256 index = cnIndexMap[_cnNodeId]; 224 require(cnNodeIdList[index] == _cnNodeId, "Invalid CN node ID."); 225 return(cnNodeIdList[index], cnStakingContractList[index], cnRewardAddressList[index]); 226 } 227 }