contract.sol 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // ReleaseOracle is an Ethereum contract to store the current and previous
  17. // versions of the go-ethereum implementation. Its goal is to allow Geth to
  18. // check for new releases automatically without the need to consult a central
  19. // repository.
  20. //
  21. // The contract takes a vote based approach on both assigning authorised signers
  22. // as well as signing off on new Geth releases.
  23. //
  24. // Note, when a signer is demoted, the currently pending release is auto-nuked.
  25. // The reason is to prevent suprises where a demotion actually tilts the votes
  26. // in favor of one voter party and pushing out a new release as a consequence of
  27. // a simple demotion.
  28. contract ReleaseOracle {
  29. // Votes is an internal data structure to count votes on a specific proposal
  30. struct Votes {
  31. address[] pass; // List of signers voting to pass a proposal
  32. address[] fail; // List of signers voting to fail a proposal
  33. }
  34. // Version is the version details of a particular Geth release
  35. struct Version {
  36. uint32 major; // Major version component of the release
  37. uint32 minor; // Minor version component of the release
  38. uint32 patch; // Patch version component of the release
  39. bytes20 commit; // Git SHA1 commit hash of the release
  40. uint64 time; // Timestamp of the release approval
  41. Votes votes; // Votes that passed this release
  42. }
  43. // Oracle authorization details
  44. mapping(address => bool) authorised; // Set of accounts allowed to vote on updating the contract
  45. address[] voters; // List of addresses currently accepted as signers
  46. // Various proposals being voted on
  47. mapping(address => Votes) authProps; // Currently running user authorization proposals
  48. address[] authPend; // List of addresses being voted on (map indexes)
  49. Version verProp; // Currently proposed release being voted on
  50. Version[] releases; // All the positively voted releases
  51. // isSigner is a modifier to authorize contract transactions.
  52. modifier isSigner() {
  53. if (authorised[msg.sender]) {
  54. _
  55. }
  56. }
  57. // Constructor to assign the initial set of signers.
  58. function ReleaseOracle(address[] signers) {
  59. // If no signers were specified, assign the creator as the sole signer
  60. if (signers.length == 0) {
  61. authorised[msg.sender] = true;
  62. voters.push(msg.sender);
  63. return;
  64. }
  65. // Otherwise assign the individual signers one by one
  66. for (uint i = 0; i < signers.length; i++) {
  67. authorised[signers[i]] = true;
  68. voters.push(signers[i]);
  69. }
  70. }
  71. // signers is an accessor method to retrieve all te signers (public accessor
  72. // generates an indexed one, not a retreive-all version).
  73. function signers() constant returns(address[]) {
  74. return voters;
  75. }
  76. // authProposals retrieves the list of addresses that authorization proposals
  77. // are currently being voted on.
  78. function authProposals() constant returns(address[]) {
  79. return authPend;
  80. }
  81. // authVotes retrieves the current authorization votes for a particular user
  82. // to promote him into the list of signers, or demote him from there.
  83. function authVotes(address user) constant returns(address[] promote, address[] demote) {
  84. return (authProps[user].pass, authProps[user].fail);
  85. }
  86. // currentVersion retrieves the semantic version, commit hash and release time
  87. // of the currently votec active release.
  88. function currentVersion() constant returns (uint32 major, uint32 minor, uint32 patch, bytes20 commit, uint time) {
  89. if (releases.length == 0) {
  90. return (0, 0, 0, 0, 0);
  91. }
  92. var release = releases[releases.length - 1];
  93. return (release.major, release.minor, release.patch, release.commit, release.time);
  94. }
  95. // proposedVersion retrieves the semantic version, commit hash and the current
  96. // votes for the next proposed release.
  97. function proposedVersion() constant returns (uint32 major, uint32 minor, uint32 patch, bytes20 commit, address[] pass, address[] fail) {
  98. return (verProp.major, verProp.minor, verProp.patch, verProp.commit, verProp.votes.pass, verProp.votes.fail);
  99. }
  100. // promote pitches in on a voting campaign to promote a new user to a signer
  101. // position.
  102. function promote(address user) {
  103. updateSigner(user, true);
  104. }
  105. // demote pitches in on a voting campaign to demote an authorised user from
  106. // its signer position.
  107. function demote(address user) {
  108. updateSigner(user, false);
  109. }
  110. // release votes for a particular version to be included as the next release.
  111. function release(uint32 major, uint32 minor, uint32 patch, bytes20 commit) {
  112. updateRelease(major, minor, patch, commit, true);
  113. }
  114. // nuke votes for the currently proposed version to not be included as the next
  115. // release. Nuking doesn't require a specific version number for simplicity.
  116. function nuke() {
  117. updateRelease(0, 0, 0, 0, false);
  118. }
  119. // updateSigner marks a vote for changing the status of an Ethereum user, either
  120. // for or against the user being an authorised signer.
  121. function updateSigner(address user, bool authorize) internal isSigner {
  122. // Gather the current votes and ensure we don't double vote
  123. Votes votes = authProps[user];
  124. for (uint i = 0; i < votes.pass.length; i++) {
  125. if (votes.pass[i] == msg.sender) {
  126. return;
  127. }
  128. }
  129. for (i = 0; i < votes.fail.length; i++) {
  130. if (votes.fail[i] == msg.sender) {
  131. return;
  132. }
  133. }
  134. // If no authorization proposal is open, add the user to the index for later lookups
  135. if (votes.pass.length == 0 && votes.fail.length == 0) {
  136. authPend.push(user);
  137. }
  138. // Cast the vote and return if the proposal cannot be resolved yet
  139. if (authorize) {
  140. votes.pass.push(msg.sender);
  141. if (votes.pass.length <= voters.length / 2) {
  142. return;
  143. }
  144. } else {
  145. votes.fail.push(msg.sender);
  146. if (votes.fail.length <= voters.length / 2) {
  147. return;
  148. }
  149. }
  150. // Proposal resolved in our favor, execute whatever we voted on
  151. if (authorize && !authorised[user]) {
  152. authorised[user] = true;
  153. voters.push(user);
  154. } else if (!authorize && authorised[user]) {
  155. authorised[user] = false;
  156. for (i = 0; i < voters.length; i++) {
  157. if (voters[i] == user) {
  158. voters[i] = voters[voters.length - 1];
  159. voters.length--;
  160. delete verProp; // Nuke any version proposal (no suprise releases!)
  161. break;
  162. }
  163. }
  164. }
  165. // Finally delete the resolved proposal, index and garbage collect
  166. delete authProps[user];
  167. for (i = 0; i < authPend.length; i++) {
  168. if (authPend[i] == user) {
  169. authPend[i] = authPend[authPend.length - 1];
  170. authPend.length--;
  171. break;
  172. }
  173. }
  174. }
  175. // updateRelease votes for a particular version to be included as the next release,
  176. // or for the currently proposed release to be nuked out.
  177. function updateRelease(uint32 major, uint32 minor, uint32 patch, bytes20 commit, bool release) internal isSigner {
  178. // Skip nuke votes if no proposal is pending
  179. if (!release && verProp.votes.pass.length == 0) {
  180. return;
  181. }
  182. // Mark a new release if no proposal is pending
  183. if (verProp.votes.pass.length == 0) {
  184. verProp.major = major;
  185. verProp.minor = minor;
  186. verProp.patch = patch;
  187. verProp.commit = commit;
  188. }
  189. // Make sure positive votes match the current proposal
  190. if (release && (verProp.major != major || verProp.minor != minor || verProp.patch != patch || verProp.commit != commit)) {
  191. return;
  192. }
  193. // Gather the current votes and ensure we don't double vote
  194. Votes votes = verProp.votes;
  195. for (uint i = 0; i < votes.pass.length; i++) {
  196. if (votes.pass[i] == msg.sender) {
  197. return;
  198. }
  199. }
  200. for (i = 0; i < votes.fail.length; i++) {
  201. if (votes.fail[i] == msg.sender) {
  202. return;
  203. }
  204. }
  205. // Cast the vote and return if the proposal cannot be resolved yet
  206. if (release) {
  207. votes.pass.push(msg.sender);
  208. if (votes.pass.length <= voters.length / 2) {
  209. return;
  210. }
  211. } else {
  212. votes.fail.push(msg.sender);
  213. if (votes.fail.length <= voters.length / 2) {
  214. return;
  215. }
  216. }
  217. // Proposal resolved in our favor, execute whatever we voted on
  218. if (release) {
  219. verProp.time = uint64(now);
  220. releases.push(verProp);
  221. delete verProp;
  222. } else {
  223. delete verProp;
  224. }
  225. }
  226. }