GovernanceStructs.sol 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // contracts/GovernanceStructs.sol
  2. // SPDX-License-Identifier: Apache 2
  3. pragma solidity ^0.8.0;
  4. import "./libraries/external/BytesLib.sol";
  5. import "./Structs.sol";
  6. contract GovernanceStructs {
  7. using BytesLib for bytes;
  8. enum GovernanceAction {
  9. UpgradeContract,
  10. UpgradeGuardianset
  11. }
  12. struct ContractUpgrade {
  13. bytes32 module;
  14. uint8 action;
  15. uint16 chain;
  16. address newContract;
  17. }
  18. struct GuardianSetUpgrade {
  19. bytes32 module;
  20. uint8 action;
  21. uint16 chain;
  22. Structs.GuardianSet newGuardianSet;
  23. uint32 newGuardianSetIndex;
  24. }
  25. struct SetMessageFee {
  26. bytes32 module;
  27. uint8 action;
  28. uint16 chain;
  29. uint256 messageFee;
  30. }
  31. struct TransferFees {
  32. bytes32 module;
  33. uint8 action;
  34. uint16 chain;
  35. uint256 amount;
  36. bytes32 recipient;
  37. }
  38. function parseContractUpgrade(bytes memory encodedUpgrade) public pure returns (ContractUpgrade memory cu) {
  39. uint index = 0;
  40. cu.module = encodedUpgrade.toBytes32(index);
  41. index += 32;
  42. cu.action = encodedUpgrade.toUint8(index);
  43. index += 1;
  44. require(cu.action == 1, "invalid ContractUpgrade");
  45. cu.chain = encodedUpgrade.toUint16(index);
  46. index += 2;
  47. cu.newContract = address(uint160(uint256(encodedUpgrade.toBytes32(index))));
  48. index += 32;
  49. require(encodedUpgrade.length == index, "invalid ContractUpgrade");
  50. }
  51. function parseGuardianSetUpgrade(bytes memory encodedUpgrade) public pure returns (GuardianSetUpgrade memory gsu) {
  52. uint index = 0;
  53. gsu.module = encodedUpgrade.toBytes32(index);
  54. index += 32;
  55. gsu.action = encodedUpgrade.toUint8(index);
  56. index += 1;
  57. require(gsu.action == 2, "invalid GuardianSetUpgrade");
  58. gsu.chain = encodedUpgrade.toUint16(index);
  59. index += 2;
  60. gsu.newGuardianSetIndex = encodedUpgrade.toUint32(index);
  61. index += 4;
  62. uint8 guardianLength = encodedUpgrade.toUint8(index);
  63. index += 1;
  64. gsu.newGuardianSet = Structs.GuardianSet({
  65. keys : new address[](guardianLength),
  66. expirationTime : 0
  67. });
  68. for(uint i = 0; i < guardianLength; i++) {
  69. gsu.newGuardianSet.keys[i] = encodedUpgrade.toAddress(index);
  70. index += 20;
  71. }
  72. require(encodedUpgrade.length == index, "invalid GuardianSetUpgrade");
  73. }
  74. function parseSetMessageFee(bytes memory encodedSetMessageFee) public pure returns (SetMessageFee memory smf) {
  75. uint index = 0;
  76. smf.module = encodedSetMessageFee.toBytes32(index);
  77. index += 32;
  78. smf.action = encodedSetMessageFee.toUint8(index);
  79. index += 1;
  80. require(smf.action == 3, "invalid SetMessageFee");
  81. smf.chain = encodedSetMessageFee.toUint16(index);
  82. index += 2;
  83. smf.messageFee = encodedSetMessageFee.toUint256(index);
  84. index += 32;
  85. require(encodedSetMessageFee.length == index, "invalid SetMessageFee");
  86. }
  87. function parseTransferFees(bytes memory encodedTransferFees) public pure returns (TransferFees memory tf) {
  88. uint index = 0;
  89. tf.module = encodedTransferFees.toBytes32(index);
  90. index += 32;
  91. tf.action = encodedTransferFees.toUint8(index);
  92. index += 1;
  93. require(tf.action == 4, "invalid TransferFees");
  94. tf.chain = encodedTransferFees.toUint16(index);
  95. index += 2;
  96. tf.amount = encodedTransferFees.toUint256(index);
  97. index += 32;
  98. tf.recipient = encodedTransferFees.toBytes32(index);
  99. index += 32;
  100. require(encodedTransferFees.length == index, "invalid TransferFees");
  101. }
  102. }