github.com/codingfuture/orig-energi3@v0.8.4/energi/contracts/src/CheckpointRegistryV2.sol (about) 1 // Copyright 2019 The Energi Core Authors 2 // This file is part of Energi Core. 3 // 4 // Energi Core is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU 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 // Energi Core 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 General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Energi Core. If not, see <http://www.gnu.org/licenses/>. 16 17 // Energi Governance system is the fundamental part of Energi Core. 18 19 // NOTE: It's not allowed to change the compiler due to byte-to-byte 20 // match requirement. 21 pragma solidity 0.5.16; 22 //pragma experimental SMTChecker; 23 pragma experimental ABIEncoderV2; 24 25 import { IGovernedContract } from "./GovernedContract.sol"; 26 import { IGovernedProxy } from "./IGovernedProxy.sol"; 27 import { ICheckpoint } from "./ICheckpoint.sol"; 28 29 import { 30 StorageCheckpointRegistryV1, 31 CheckpointV1, 32 CheckpointRegistryV1 33 } from "./CheckpointRegistryV1.sol"; 34 35 // solium-disable-next-line no-empty-blocks 36 37 /** 38 * Checkpoint V2 object 39 */ 40 contract CheckpointV2 is CheckpointV1 { 41 constructor( 42 IGovernedProxy _mnregistry_proxy, 43 uint _number, 44 bytes32 _hash, 45 bytes32 _sigbase, 46 bytes memory _cpp_sig 47 ) 48 public 49 CheckpointV1(_mnregistry_proxy, _number, _hash, _sigbase) 50 { 51 require(_cpp_sig.length == 65, "Invalid signature length"); 52 (bytes32 r, bytes32 s) = abi.decode(_cpp_sig, (bytes32, bytes32)); 53 address signer = ecrecover(_sigbase, uint8(_cpp_sig[64]), r, s); 54 55 signature_list.push(_cpp_sig); 56 signers[signer] = signature_list.length; 57 } 58 59 function canVote(address masternode) external view returns(bool) { 60 if (signers[masternode] != 0) { 61 return false; 62 } 63 64 if ((block.number - since) >= SIGNING_PERIOD) { 65 return false; 66 } 67 68 return true; 69 } 70 } 71 72 /** 73 * Genesis hardcoded version of CheckpointRegistry 74 * 75 * NOTE: it MUST NOT change after blockchain launch! 76 */ 77 contract CheckpointRegistryV2 is CheckpointRegistryV1 { 78 constructor(address _proxy, IGovernedProxy _mnregistry_proxy, address _cpp_signer) 79 public CheckpointRegistryV1(_proxy, _mnregistry_proxy, _cpp_signer) 80 {} 81 82 // IGovernedContract 83 //--------------------------------- 84 function _migrate(IGovernedContract _oldImpl) internal { 85 v1storage.kill(); 86 v1storage = CheckpointRegistryV1(address(_oldImpl)).v1storage(); 87 } 88 89 function _destroy(IGovernedContract _newImpl) internal { 90 v1storage.setOwner(_newImpl); 91 } 92 93 // ICheckpointRegistry 94 //--------------------------------- 95 function propose(uint number, bytes32 hash, bytes calldata signature) external returns(ICheckpoint checkpoint) { 96 bytes32 sigbase = signatureBase(number, hash); 97 require(signature.length == 65, "Invalid signature length"); 98 (bytes32 r, bytes32 s) = abi.decode(signature, (bytes32, bytes32)); 99 require(ecrecover(sigbase, uint8(signature[64]), r, s) == CPP_signer, "Invalid signer"); 100 101 checkpoint = new CheckpointV2(mnregistry_proxy, number, hash, sigbase, signature); 102 v1storage.add(checkpoint); 103 104 emit Checkpoint( 105 number, 106 hash, 107 checkpoint 108 ); 109 } 110 }