Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.34 KB | None | 0 0
  1. pragma solidity ^0.4.13;
  2.  
  3. pragma solidity ^0.4.13;
  4.  
  5. /// @title Library implementing an array type which allows O(1) lookups on values.
  6. /// @author Piper Merriam <pipermerriam@gmail.com>, Eric Olszewski <eolszewski@gmail.com>
  7. /// Adapted from https://github.com/ethpm/ethereum-indexed-enumerable-set-lib/blob/master/contracts/IndexedEnumerableSetLib.sol
  8. library UIntSetLib {
  9.  
  10. struct UIntSet {
  11. uint[] values;
  12. mapping(uint => bool) exists;
  13. mapping(uint => uint) indices;
  14. }
  15.  
  16. modifier inBounds(UIntSet storage self, uint index) {
  17. require(index < self.values.length);
  18. _;
  19. }
  20.  
  21. modifier notEmpty(UIntSet storage self) {
  22. require(self.values.length != 0);
  23. _;
  24. }
  25.  
  26. function get(UIntSet storage self, uint index) public constant
  27. inBounds(self, index)
  28. returns (uint)
  29. {
  30. return self.values[index];
  31. }
  32.  
  33. function set(UIntSet storage self, uint index, uint value) public
  34. inBounds(self, index)
  35. returns (bool)
  36. {
  37. if (self.exists[value])
  38. return false;
  39. self.values[index] = value;
  40. self.exists[value] = true;
  41. self.indices[value] = index;
  42. return true;
  43. }
  44.  
  45. function add(UIntSet storage self, uint value) public
  46. returns (bool)
  47. {
  48. if (self.exists[value])
  49. return false;
  50. self.indices[value] = self.values.length;
  51. self.values.push(value);
  52. self.exists[value] = true;
  53. return true;
  54. }
  55.  
  56. function remove(UIntSet storage self, uint value) public
  57. returns (bool)
  58. {
  59. if (!self.exists[value])
  60. return false;
  61. uint index = indexOf(self, value);
  62. pop(self, index);
  63. return true;
  64. }
  65.  
  66. function pop(UIntSet storage self, uint index) public
  67. inBounds(self, index)
  68. returns (uint)
  69. {
  70. uint value = get(self, index);
  71.  
  72. if (index != self.values.length - 1) {
  73. uint lastValue = last(self);
  74. self.exists[lastValue] = false;
  75. set(self, index, lastValue);
  76. self.indices[lastValue] = index;
  77. }
  78. self.values.length -= 1;
  79.  
  80. delete self.indices[value];
  81. delete self.exists[value];
  82.  
  83. return value;
  84. }
  85.  
  86. function first(UIntSet storage self) public constant
  87. notEmpty(self)
  88. returns (uint)
  89. {
  90. return get(self, 0);
  91. }
  92.  
  93. function last(UIntSet storage self) public constant
  94. notEmpty(self)
  95. returns (uint)
  96. {
  97. return get(self, self.values.length - 1);
  98. }
  99.  
  100. function indexOf(UIntSet storage self, uint value) public constant
  101. returns (uint)
  102. {
  103. if (!self.exists[value])
  104. return uint(-1);
  105. return self.indices[value];
  106. }
  107.  
  108. function contains(UIntSet storage self, uint value) public constant
  109. returns (bool)
  110. {
  111. return self.exists[value];
  112. }
  113.  
  114. function size(UIntSet storage self) public constant
  115. returns (uint)
  116. {
  117. return self.values.length;
  118. }
  119. }
  120. pragma solidity ^0.4.13;
  121.  
  122. /// @title Library implementing an array type which allows O(1) lookups on values.
  123. /// @author Piper Merriam <pipermerriam@gmail.com>, Eric Olszewski <eolszewski@gmail.com>
  124. /// Adapted from https://github.com/ethpm/ethereum-indexed-enumerable-set-lib/blob/master/contracts/IndexedEnumerableSetLib.sol
  125. library AddressSetLib {
  126.  
  127. struct AddressSet {
  128. address[] values;
  129. mapping(address => bool) exists;
  130. mapping(address => uint) indices;
  131. }
  132.  
  133. modifier inBounds(AddressSet storage self, uint index) {
  134. require(index < self.values.length);
  135. _;
  136. }
  137.  
  138. modifier notEmpty(AddressSet storage self) {
  139. require(self.values.length != 0);
  140. _;
  141. }
  142.  
  143. function get(AddressSet storage self, uint index) public constant
  144. inBounds(self, index)
  145. returns (address)
  146. {
  147. return self.values[index];
  148. }
  149.  
  150. function set(AddressSet storage self, uint index, address value) public
  151. inBounds(self, index)
  152. returns (bool)
  153. {
  154. if (self.exists[value])
  155. return false;
  156. self.values[index] = value;
  157. self.exists[value] = true;
  158. self.indices[value] = index;
  159. return true;
  160. }
  161.  
  162. function add(AddressSet storage self, address value) public
  163. returns (bool)
  164. {
  165. if (self.exists[value])
  166. return false;
  167. self.indices[value] = self.values.length;
  168. self.values.push(value);
  169. self.exists[value] = true;
  170. return true;
  171. }
  172.  
  173. function remove(AddressSet storage self, address value) public
  174. returns (bool)
  175. {
  176. if (!self.exists[value])
  177. return false;
  178. uint index = indexOf(self, value);
  179. pop(self, index);
  180. return true;
  181. }
  182.  
  183. function pop(AddressSet storage self, uint index) public
  184. inBounds(self, index)
  185. returns (address)
  186. {
  187. address value = get(self, index);
  188.  
  189. if (index != self.values.length - 1) {
  190. address lastValue = last(self);
  191. self.exists[lastValue] = false;
  192. set(self, index, lastValue);
  193. self.indices[lastValue] = index;
  194. }
  195. self.values.length -= 1;
  196.  
  197. delete self.indices[value];
  198. delete self.exists[value];
  199.  
  200. return value;
  201. }
  202.  
  203. function first(AddressSet storage self) public constant
  204. notEmpty(self)
  205. returns (address)
  206. {
  207. return get(self, 0);
  208. }
  209.  
  210. function last(AddressSet storage self) public constant
  211. notEmpty(self)
  212. returns (address)
  213. {
  214. return get(self, self.values.length - 1);
  215. }
  216.  
  217. function indexOf(AddressSet storage self, address value) public constant
  218. returns (uint)
  219. {
  220. if (!self.exists[value])
  221. return uint(-1);
  222. return self.indices[value];
  223. }
  224.  
  225. function contains(AddressSet storage self, address value) public constant
  226. returns (bool)
  227. {
  228. return self.exists[value];
  229. }
  230.  
  231. function size(AddressSet storage self) public constant
  232. returns (uint)
  233. {
  234. return self.values.length;
  235. }
  236. }
  237. pragma solidity ^0.4.13;
  238.  
  239. // accepted from zeppelin-solidity https://github.com/OpenZeppelin/zeppelin-solidity
  240.  
  241. /**
  242. * Math operations with safety checks
  243. */
  244. library SafeMath {
  245. function safeMul(uint a, uint b) internal returns (uint) {
  246. uint c = a * b;
  247. require(a == 0 || c / a == b);
  248. return c;
  249. }
  250.  
  251. function safeDiv(uint a, uint b) internal returns (uint) {
  252. require(b > 0);
  253. uint c = a / b;
  254. require(a == b * c + a % b);
  255. return c;
  256. }
  257.  
  258. function safeSub(uint a, uint b) internal returns (uint) {
  259. require(b <= a);
  260. return a - b;
  261. }
  262.  
  263. function safeAdd(uint a, uint b) internal returns (uint) {
  264. uint c = a + b;
  265. require(c>=a && c>=b);
  266. return c;
  267. }
  268.  
  269. function safeMul8(uint8 a, uint8 b) internal returns (uint8) {
  270. uint8 c = a * b;
  271. require(a == 0 || c / a == b);
  272. return c;
  273. }
  274.  
  275. function safeDiv8(uint8 a, uint8 b) internal returns (uint8) {
  276. require(b > 0);
  277. uint8 c = a / b;
  278. require(a == b * c + a % b);
  279. return c;
  280. }
  281.  
  282. function safeSub8(uint8 a, uint8 b) internal returns (uint8) {
  283. require(b <= a);
  284. return a - b;
  285. }
  286.  
  287. function safeAdd8(uint8 a, uint8 b) internal returns (uint8) {
  288. uint8 c = a + b;
  289. require(c>=a && c>=b);
  290. return c;
  291. }
  292.  
  293. function max64(uint64 a, uint64 b) internal constant returns (uint64) {
  294. return a >= b ? a : b;
  295. }
  296.  
  297. function min64(uint64 a, uint64 b) internal constant returns (uint64) {
  298. return a < b ? a : b;
  299. }
  300.  
  301. function max256(uint256 a, uint256 b) internal constant returns (uint256) {
  302. return a >= b ? a : b;
  303. }
  304.  
  305. function min256(uint256 a, uint256 b) internal constant returns (uint256) {
  306. return a < b ? a : b;
  307. }
  308. }
  309.  
  310.  
  311. contract Contributors {
  312. using SafeMath for uint;
  313. using UIntSetLib for UIntSetLib.UIntSet;
  314. using AddressSetLib for AddressSetLib.AddressSet;
  315.  
  316. address public peerReview;
  317.  
  318. struct Contributor {
  319. address owner;
  320. uint256 HNR;
  321. uint256 CBN;
  322. UIntSetLib.UIntSet proposals; // proposalIds for submitted proposals
  323. UIntSetLib.UIntSet votes; // voteIds for submitted votes
  324. uint lastUpdated;
  325. bytes32 profile;
  326. }
  327.  
  328. AddressSetLib.AddressSet contributors;
  329. mapping (address => Contributor) contributorForAddress;
  330.  
  331. modifier onlyPeerReview {
  332. require(msg.sender == peerReview);
  333. _;
  334. }
  335.  
  336. event CreatedContributor(address indexed contributorAddress);
  337. event ModifiedHNR(address indexed contributorAddress, uint newHNR);
  338. event ModifiedCBN(address indexed contributorAddress, uint newCBN);
  339. event ModifiedProfile(address indexed contributorAddress, bytes32 newProfile);
  340. event AddedProposal(address indexed contributorAddress, uint proposalId);
  341. event AddedVote(address indexed contributorAddress, uint voteId);
  342.  
  343. function Contributors (address _peerReview) {
  344. peerReview = _peerReview;
  345. }
  346.  
  347. /**
  348. * whole struct getters + setters
  349. */
  350.  
  351. function addContributor(address contributor)
  352. public
  353. onlyPeerReview
  354. {
  355. createContributor(contributor);
  356. }
  357.  
  358. function createContributor(address contributorAddr)
  359. internal
  360. {
  361. Contributor storage contributor = contributorForAddress[contributorAddr];
  362.  
  363. // ensure that this contributor doesn't exist yet
  364. assert(contributor.owner == 0);
  365.  
  366. contributor.owner = contributorAddr;
  367. contributor.HNR = 0;
  368. contributor.CBN = 0;
  369. contributor.lastUpdated = block.number;
  370.  
  371. contributors.add(contributorAddr);
  372.  
  373. CreatedContributor(contributorAddr);
  374. }
  375.  
  376. function contributorExists(address contributorAddr)
  377. public
  378. constant
  379. returns (bool)
  380. {
  381. return contributors.contains(contributorAddr);
  382. }
  383.  
  384. function getContributor(address contributorAddr)
  385. public
  386. constant
  387. returns (bool exists, uint HNR, uint CBN, uint[] proposalsValues, uint[] votesValues, uint lastUpdated)
  388. {
  389. exists = contributorExists(contributorAddr);
  390.  
  391. Contributor storage contr = contributorForAddress[contributorAddr];
  392.  
  393. HNR = contr.HNR;
  394. CBN = contr.CBN;
  395. proposalsValues = contr.proposals.values;
  396. votesValues = contr.votes.values;
  397. lastUpdated = contr.lastUpdated;
  398. }
  399.  
  400. function getContributorIndex(uint index)
  401. public
  402. constant
  403. returns (bool exists, address owner, uint256 HNR, uint256 CBN, uint256[] proposalsValues, uint256[] votesValues, uint lastUpdated)
  404. {
  405. address contributorAddr = contributors.get(index);
  406. exists = contributorExists(contributorAddr);
  407.  
  408. Contributor storage contributor = contributorForAddress[contributorAddr];
  409.  
  410. owner = contributor.owner;
  411. HNR = contributor.HNR;
  412. CBN = contributor.CBN;
  413. proposalsValues = contributor.proposals.values;
  414. votesValues = contributor.votes.values;
  415. lastUpdated = contributor.lastUpdated;
  416. }
  417.  
  418. /**
  419. * field: HNR
  420. */
  421.  
  422. function getHNR(address contributorAddr)
  423. public
  424. constant
  425. returns (uint)
  426. {
  427. return contributorForAddress[contributorAddr].HNR;
  428. }
  429.  
  430. function increaseHNR(address contributorAddr, uint amount)
  431. public
  432. onlyPeerReview
  433. {
  434. Contributor storage contributor = contributorForAddress[contributorAddr];
  435.  
  436. contributor.HNR = contributor.HNR.safeAdd(amount);
  437. ModifiedHNR(contributorAddr, contributor.HNR);
  438. }
  439.  
  440. function decreaseHNR(address contributorAddr, uint amount)
  441. public
  442. onlyPeerReview
  443. {
  444. Contributor storage contributor = contributorForAddress[contributorAddr];
  445.  
  446. contributor.HNR = contributor.HNR.safeSub(amount);
  447. ModifiedHNR(contributorAddr, contributor.HNR);
  448. }
  449.  
  450. /**
  451. * field: CBN
  452. */
  453.  
  454. function getCBN(address contributorAddr)
  455. public
  456. constant
  457. returns (uint)
  458. {
  459. return contributorForAddress[contributorAddr].CBN;
  460. }
  461.  
  462. function increaseCBN(address contributorAddr, uint amount)
  463. public
  464. onlyPeerReview
  465. {
  466. Contributor storage contributor = contributorForAddress[contributorAddr];
  467.  
  468. contributor.CBN = contributor.CBN.safeAdd(amount);
  469. ModifiedCBN(contributorAddr, contributor.CBN);
  470. }
  471.  
  472. function decreaseCBN(address contributorAddr, uint amount)
  473. public
  474. onlyPeerReview
  475. {
  476. Contributor storage contributor = contributorForAddress[contributorAddr];
  477.  
  478. contributor.CBN = contributor.CBN.safeSub(amount);
  479. ModifiedCBN(contributorAddr, contributor.CBN);
  480. }
  481.  
  482. /**
  483. * field: proposals
  484. */
  485.  
  486. function getNumProposals(address contributor)
  487. public
  488. constant
  489. returns (uint)
  490. {
  491. return contributorForAddress[contributor].proposals.size();
  492. }
  493.  
  494. function getProposals(address contributor, uint page)
  495. public
  496. constant
  497. returns (uint[32])
  498. {
  499. uint[32] memory ids;
  500. uint index;
  501. UIntSetLib.UIntSet storage proposals = contributorForAddress[contributor].proposals;
  502.  
  503. for (uint i = 0; i < 32; i++)
  504. {
  505. index = page.safeMul(32).safeAdd(i);
  506. if (index >= proposals.size()) { break; }
  507.  
  508. ids[i] = proposals.get(index);
  509. }
  510. return ids;
  511. }
  512.  
  513. function addProposal(address contributorAddr, uint proposalId)
  514. public
  515. onlyPeerReview
  516. {
  517. if (contributorExists(contributorAddr) == false) {
  518. createContributor(contributorAddr);
  519. }
  520.  
  521. // this is guaranteed to exist if createContributor() doesn't throw
  522. Contributor storage contributor = contributorForAddress[contributorAddr];
  523.  
  524. contributor.proposals.add(proposalId);
  525. contributor.lastUpdated = block.number;
  526.  
  527. AddedProposal(contributorAddr, proposalId);
  528. }
  529.  
  530. /**
  531. * field: votes
  532. */
  533.  
  534. function getNumVotes(address contributorAddr)
  535. public
  536. constant
  537. returns (uint)
  538. {
  539. // @@TODO: check existence
  540. return contributorForAddress[contributorAddr].votes.size();
  541. }
  542.  
  543. function getVotes(address contributorAddr)
  544. public
  545. constant
  546. returns (uint[])
  547. {
  548. // @@TODO: check existence
  549. return contributorForAddress[contributorAddr].votes.values;
  550. }
  551.  
  552. function addVote(address contributorAddr, uint voteId)
  553. public
  554. onlyPeerReview
  555. {
  556. // @@TODO: check existence
  557. Contributor storage contributor = contributorForAddress[contributorAddr];
  558.  
  559. contributor.votes.add(voteId);
  560. contributor.lastUpdated = block.number;
  561.  
  562. AddedVote(contributorAddr, voteId);
  563. }
  564.  
  565. /**
  566. * field: lastUpdated
  567. */
  568.  
  569. function getLastUpdated(address contributorAddr)
  570. public
  571. constant
  572. returns (uint)
  573. {
  574. // @@TODO: check existence
  575. return contributorForAddress[contributorAddr].lastUpdated;
  576. }
  577.  
  578. function setLastUpdated(address contributorAddr, uint lastUpdated)
  579. public
  580. onlyPeerReview
  581. {
  582. // @@TODO: check existence
  583. contributorForAddress[contributorAddr].lastUpdated = lastUpdated;
  584. }
  585.  
  586. /**
  587. * field: profile
  588. */
  589.  
  590. function getProfile(address contributorAddr)
  591. public
  592. constant
  593. returns (bytes32)
  594. {
  595. // @@TODO: check existence
  596. return contributorForAddress[contributorAddr].profile;
  597. }
  598.  
  599. function setProfile(address contributorAddr, bytes32 profile)
  600. public
  601. onlyPeerReview
  602. {
  603. // @@TODO: check existence
  604. contributorForAddress[contributorAddr].profile = profile;
  605. ModifiedProfile(contributorAddr, contributorForAddress[contributorAddr].profile);
  606. }
  607. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement