ReceiverGovernance.sol 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // contracts/Governance.sol
  2. // SPDX-License-Identifier: Apache 2
  3. pragma solidity ^0.8.0;
  4. import "./ReceiverStructs.sol";
  5. import "./ReceiverGovernanceStructs.sol";
  6. import "./ReceiverMessages.sol";
  7. import "./ReceiverSetters.sol";
  8. import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Upgrade.sol";
  9. abstract contract ReceiverGovernance is
  10. ReceiverGovernanceStructs,
  11. ReceiverMessages,
  12. ReceiverSetters,
  13. ERC1967Upgrade
  14. {
  15. event ContractUpgraded(
  16. address indexed oldContract,
  17. address indexed newContract
  18. );
  19. event OwnershipTransfered(
  20. address indexed oldOwner,
  21. address indexed newOwner
  22. );
  23. // "Core" (left padded)
  24. bytes32 constant module =
  25. 0x00000000000000000000000000000000000000000000000000000000436f7265;
  26. function submitNewGuardianSet(bytes memory _vm) public {
  27. ReceiverStructs.VM memory vm = parseVM(_vm);
  28. (bool isValid, string memory reason) = verifyGovernanceVM(vm);
  29. require(isValid, reason);
  30. ReceiverGovernanceStructs.GuardianSetUpgrade
  31. memory upgrade = parseGuardianSetUpgrade(vm.payload);
  32. require(upgrade.module == module, "invalid Module");
  33. require(
  34. upgrade.newGuardianSet.keys.length > 0,
  35. "new guardian set is empty"
  36. );
  37. require(
  38. upgrade.newGuardianSetIndex == getCurrentGuardianSetIndex() + 1,
  39. "index must increase in steps of 1"
  40. );
  41. setGovernanceActionConsumed(vm.hash);
  42. expireGuardianSet(getCurrentGuardianSetIndex());
  43. storeGuardianSet(upgrade.newGuardianSet, upgrade.newGuardianSetIndex);
  44. updateGuardianSetIndex(upgrade.newGuardianSetIndex);
  45. }
  46. function upgradeImplementation(address newImplementation) public onlyOwner {
  47. address currentImplementation = _getImplementation();
  48. _upgradeTo(newImplementation);
  49. // Call initialize function of the new implementation
  50. (bool success, bytes memory reason) = newImplementation.delegatecall(
  51. abi.encodeWithSignature("initialize()")
  52. );
  53. require(success, string(reason));
  54. emit ContractUpgraded(currentImplementation, newImplementation);
  55. }
  56. function verifyGovernanceVM(
  57. ReceiverStructs.VM memory vm
  58. ) internal view returns (bool, string memory) {
  59. // validate vm
  60. (bool isValid, string memory reason) = verifyVM(vm);
  61. if (!isValid) {
  62. return (false, reason);
  63. }
  64. // only current guardianset can sign governance packets
  65. if (vm.guardianSetIndex != getCurrentGuardianSetIndex()) {
  66. return (false, "not signed by current guardian set");
  67. }
  68. // verify source
  69. if (uint16(vm.emitterChainId) != governanceChainId()) {
  70. return (false, "wrong governance chain");
  71. }
  72. if (vm.emitterAddress != governanceContract()) {
  73. return (false, "wrong governance contract");
  74. }
  75. // prevent re-entry
  76. if (governanceActionIsConsumed(vm.hash)) {
  77. return (false, "governance action already consumed");
  78. }
  79. return (true, "");
  80. }
  81. function transferOwnership(address newOwner) public onlyOwner {
  82. require(newOwner != address(0), "new owner cannot be the zero address");
  83. address currentOwner = owner();
  84. setOwner(newOwner);
  85. emit OwnershipTransfered(currentOwner, newOwner);
  86. }
  87. modifier onlyOwner() {
  88. require(owner() == msg.sender, "caller is not the owner");
  89. _;
  90. }
  91. }