Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.4.13;
- pragma solidity ^0.4.13;
- /// @title Library implementing an array type which allows O(1) lookups on values.
- /// @author Piper Merriam <pipermerriam@gmail.com>, Eric Olszewski <eolszewski@gmail.com>
- /// Adapted from https://github.com/ethpm/ethereum-indexed-enumerable-set-lib/blob/master/contracts/IndexedEnumerableSetLib.sol
- library UIntSetLib {
- struct UIntSet {
- uint[] values;
- mapping(uint => bool) exists;
- mapping(uint => uint) indices;
- }
- modifier inBounds(UIntSet storage self, uint index) {
- require(index < self.values.length);
- _;
- }
- modifier notEmpty(UIntSet storage self) {
- require(self.values.length != 0);
- _;
- }
- function get(UIntSet storage self, uint index) public constant
- inBounds(self, index)
- returns (uint)
- {
- return self.values[index];
- }
- function set(UIntSet storage self, uint index, uint value) public
- inBounds(self, index)
- returns (bool)
- {
- if (self.exists[value])
- return false;
- self.values[index] = value;
- self.exists[value] = true;
- self.indices[value] = index;
- return true;
- }
- function add(UIntSet storage self, uint value) public
- returns (bool)
- {
- if (self.exists[value])
- return false;
- self.indices[value] = self.values.length;
- self.values.push(value);
- self.exists[value] = true;
- return true;
- }
- function remove(UIntSet storage self, uint value) public
- returns (bool)
- {
- if (!self.exists[value])
- return false;
- uint index = indexOf(self, value);
- pop(self, index);
- return true;
- }
- function pop(UIntSet storage self, uint index) public
- inBounds(self, index)
- returns (uint)
- {
- uint value = get(self, index);
- if (index != self.values.length - 1) {
- uint lastValue = last(self);
- self.exists[lastValue] = false;
- set(self, index, lastValue);
- self.indices[lastValue] = index;
- }
- self.values.length -= 1;
- delete self.indices[value];
- delete self.exists[value];
- return value;
- }
- function first(UIntSet storage self) public constant
- notEmpty(self)
- returns (uint)
- {
- return get(self, 0);
- }
- function last(UIntSet storage self) public constant
- notEmpty(self)
- returns (uint)
- {
- return get(self, self.values.length - 1);
- }
- function indexOf(UIntSet storage self, uint value) public constant
- returns (uint)
- {
- if (!self.exists[value])
- return uint(-1);
- return self.indices[value];
- }
- function contains(UIntSet storage self, uint value) public constant
- returns (bool)
- {
- return self.exists[value];
- }
- function size(UIntSet storage self) public constant
- returns (uint)
- {
- return self.values.length;
- }
- }
- pragma solidity ^0.4.13;
- /// @title Library implementing an array type which allows O(1) lookups on values.
- /// @author Piper Merriam <pipermerriam@gmail.com>, Eric Olszewski <eolszewski@gmail.com>
- /// Adapted from https://github.com/ethpm/ethereum-indexed-enumerable-set-lib/blob/master/contracts/IndexedEnumerableSetLib.sol
- library AddressSetLib {
- struct AddressSet {
- address[] values;
- mapping(address => bool) exists;
- mapping(address => uint) indices;
- }
- modifier inBounds(AddressSet storage self, uint index) {
- require(index < self.values.length);
- _;
- }
- modifier notEmpty(AddressSet storage self) {
- require(self.values.length != 0);
- _;
- }
- function get(AddressSet storage self, uint index) public constant
- inBounds(self, index)
- returns (address)
- {
- return self.values[index];
- }
- function set(AddressSet storage self, uint index, address value) public
- inBounds(self, index)
- returns (bool)
- {
- if (self.exists[value])
- return false;
- self.values[index] = value;
- self.exists[value] = true;
- self.indices[value] = index;
- return true;
- }
- function add(AddressSet storage self, address value) public
- returns (bool)
- {
- if (self.exists[value])
- return false;
- self.indices[value] = self.values.length;
- self.values.push(value);
- self.exists[value] = true;
- return true;
- }
- function remove(AddressSet storage self, address value) public
- returns (bool)
- {
- if (!self.exists[value])
- return false;
- uint index = indexOf(self, value);
- pop(self, index);
- return true;
- }
- function pop(AddressSet storage self, uint index) public
- inBounds(self, index)
- returns (address)
- {
- address value = get(self, index);
- if (index != self.values.length - 1) {
- address lastValue = last(self);
- self.exists[lastValue] = false;
- set(self, index, lastValue);
- self.indices[lastValue] = index;
- }
- self.values.length -= 1;
- delete self.indices[value];
- delete self.exists[value];
- return value;
- }
- function first(AddressSet storage self) public constant
- notEmpty(self)
- returns (address)
- {
- return get(self, 0);
- }
- function last(AddressSet storage self) public constant
- notEmpty(self)
- returns (address)
- {
- return get(self, self.values.length - 1);
- }
- function indexOf(AddressSet storage self, address value) public constant
- returns (uint)
- {
- if (!self.exists[value])
- return uint(-1);
- return self.indices[value];
- }
- function contains(AddressSet storage self, address value) public constant
- returns (bool)
- {
- return self.exists[value];
- }
- function size(AddressSet storage self) public constant
- returns (uint)
- {
- return self.values.length;
- }
- }
- pragma solidity ^0.4.13;
- // accepted from zeppelin-solidity https://github.com/OpenZeppelin/zeppelin-solidity
- /**
- * Math operations with safety checks
- */
- library SafeMath {
- function safeMul(uint a, uint b) internal returns (uint) {
- uint c = a * b;
- require(a == 0 || c / a == b);
- return c;
- }
- function safeDiv(uint a, uint b) internal returns (uint) {
- require(b > 0);
- uint c = a / b;
- require(a == b * c + a % b);
- return c;
- }
- function safeSub(uint a, uint b) internal returns (uint) {
- require(b <= a);
- return a - b;
- }
- function safeAdd(uint a, uint b) internal returns (uint) {
- uint c = a + b;
- require(c>=a && c>=b);
- return c;
- }
- function safeMul8(uint8 a, uint8 b) internal returns (uint8) {
- uint8 c = a * b;
- require(a == 0 || c / a == b);
- return c;
- }
- function safeDiv8(uint8 a, uint8 b) internal returns (uint8) {
- require(b > 0);
- uint8 c = a / b;
- require(a == b * c + a % b);
- return c;
- }
- function safeSub8(uint8 a, uint8 b) internal returns (uint8) {
- require(b <= a);
- return a - b;
- }
- function safeAdd8(uint8 a, uint8 b) internal returns (uint8) {
- uint8 c = a + b;
- require(c>=a && c>=b);
- return c;
- }
- function max64(uint64 a, uint64 b) internal constant returns (uint64) {
- return a >= b ? a : b;
- }
- function min64(uint64 a, uint64 b) internal constant returns (uint64) {
- return a < b ? a : b;
- }
- function max256(uint256 a, uint256 b) internal constant returns (uint256) {
- return a >= b ? a : b;
- }
- function min256(uint256 a, uint256 b) internal constant returns (uint256) {
- return a < b ? a : b;
- }
- }
- contract Contributors {
- using SafeMath for uint;
- using UIntSetLib for UIntSetLib.UIntSet;
- using AddressSetLib for AddressSetLib.AddressSet;
- address public peerReview;
- struct Contributor {
- address owner;
- uint256 HNR;
- uint256 CBN;
- UIntSetLib.UIntSet proposals; // proposalIds for submitted proposals
- UIntSetLib.UIntSet votes; // voteIds for submitted votes
- uint lastUpdated;
- bytes32 profile;
- }
- AddressSetLib.AddressSet contributors;
- mapping (address => Contributor) contributorForAddress;
- modifier onlyPeerReview {
- require(msg.sender == peerReview);
- _;
- }
- event CreatedContributor(address indexed contributorAddress);
- event ModifiedHNR(address indexed contributorAddress, uint newHNR);
- event ModifiedCBN(address indexed contributorAddress, uint newCBN);
- event ModifiedProfile(address indexed contributorAddress, bytes32 newProfile);
- event AddedProposal(address indexed contributorAddress, uint proposalId);
- event AddedVote(address indexed contributorAddress, uint voteId);
- function Contributors (address _peerReview) {
- peerReview = _peerReview;
- }
- /**
- * whole struct getters + setters
- */
- function addContributor(address contributor)
- public
- onlyPeerReview
- {
- createContributor(contributor);
- }
- function createContributor(address contributorAddr)
- internal
- {
- Contributor storage contributor = contributorForAddress[contributorAddr];
- // ensure that this contributor doesn't exist yet
- assert(contributor.owner == 0);
- contributor.owner = contributorAddr;
- contributor.HNR = 0;
- contributor.CBN = 0;
- contributor.lastUpdated = block.number;
- contributors.add(contributorAddr);
- CreatedContributor(contributorAddr);
- }
- function contributorExists(address contributorAddr)
- public
- constant
- returns (bool)
- {
- return contributors.contains(contributorAddr);
- }
- function getContributor(address contributorAddr)
- public
- constant
- returns (bool exists, uint HNR, uint CBN, uint[] proposalsValues, uint[] votesValues, uint lastUpdated)
- {
- exists = contributorExists(contributorAddr);
- Contributor storage contr = contributorForAddress[contributorAddr];
- HNR = contr.HNR;
- CBN = contr.CBN;
- proposalsValues = contr.proposals.values;
- votesValues = contr.votes.values;
- lastUpdated = contr.lastUpdated;
- }
- function getContributorIndex(uint index)
- public
- constant
- returns (bool exists, address owner, uint256 HNR, uint256 CBN, uint256[] proposalsValues, uint256[] votesValues, uint lastUpdated)
- {
- address contributorAddr = contributors.get(index);
- exists = contributorExists(contributorAddr);
- Contributor storage contributor = contributorForAddress[contributorAddr];
- owner = contributor.owner;
- HNR = contributor.HNR;
- CBN = contributor.CBN;
- proposalsValues = contributor.proposals.values;
- votesValues = contributor.votes.values;
- lastUpdated = contributor.lastUpdated;
- }
- /**
- * field: HNR
- */
- function getHNR(address contributorAddr)
- public
- constant
- returns (uint)
- {
- return contributorForAddress[contributorAddr].HNR;
- }
- function increaseHNR(address contributorAddr, uint amount)
- public
- onlyPeerReview
- {
- Contributor storage contributor = contributorForAddress[contributorAddr];
- contributor.HNR = contributor.HNR.safeAdd(amount);
- ModifiedHNR(contributorAddr, contributor.HNR);
- }
- function decreaseHNR(address contributorAddr, uint amount)
- public
- onlyPeerReview
- {
- Contributor storage contributor = contributorForAddress[contributorAddr];
- contributor.HNR = contributor.HNR.safeSub(amount);
- ModifiedHNR(contributorAddr, contributor.HNR);
- }
- /**
- * field: CBN
- */
- function getCBN(address contributorAddr)
- public
- constant
- returns (uint)
- {
- return contributorForAddress[contributorAddr].CBN;
- }
- function increaseCBN(address contributorAddr, uint amount)
- public
- onlyPeerReview
- {
- Contributor storage contributor = contributorForAddress[contributorAddr];
- contributor.CBN = contributor.CBN.safeAdd(amount);
- ModifiedCBN(contributorAddr, contributor.CBN);
- }
- function decreaseCBN(address contributorAddr, uint amount)
- public
- onlyPeerReview
- {
- Contributor storage contributor = contributorForAddress[contributorAddr];
- contributor.CBN = contributor.CBN.safeSub(amount);
- ModifiedCBN(contributorAddr, contributor.CBN);
- }
- /**
- * field: proposals
- */
- function getNumProposals(address contributor)
- public
- constant
- returns (uint)
- {
- return contributorForAddress[contributor].proposals.size();
- }
- function getProposals(address contributor, uint page)
- public
- constant
- returns (uint[32])
- {
- uint[32] memory ids;
- uint index;
- UIntSetLib.UIntSet storage proposals = contributorForAddress[contributor].proposals;
- for (uint i = 0; i < 32; i++)
- {
- index = page.safeMul(32).safeAdd(i);
- if (index >= proposals.size()) { break; }
- ids[i] = proposals.get(index);
- }
- return ids;
- }
- function addProposal(address contributorAddr, uint proposalId)
- public
- onlyPeerReview
- {
- if (contributorExists(contributorAddr) == false) {
- createContributor(contributorAddr);
- }
- // this is guaranteed to exist if createContributor() doesn't throw
- Contributor storage contributor = contributorForAddress[contributorAddr];
- contributor.proposals.add(proposalId);
- contributor.lastUpdated = block.number;
- AddedProposal(contributorAddr, proposalId);
- }
- /**
- * field: votes
- */
- function getNumVotes(address contributorAddr)
- public
- constant
- returns (uint)
- {
- // @@TODO: check existence
- return contributorForAddress[contributorAddr].votes.size();
- }
- function getVotes(address contributorAddr)
- public
- constant
- returns (uint[])
- {
- // @@TODO: check existence
- return contributorForAddress[contributorAddr].votes.values;
- }
- function addVote(address contributorAddr, uint voteId)
- public
- onlyPeerReview
- {
- // @@TODO: check existence
- Contributor storage contributor = contributorForAddress[contributorAddr];
- contributor.votes.add(voteId);
- contributor.lastUpdated = block.number;
- AddedVote(contributorAddr, voteId);
- }
- /**
- * field: lastUpdated
- */
- function getLastUpdated(address contributorAddr)
- public
- constant
- returns (uint)
- {
- // @@TODO: check existence
- return contributorForAddress[contributorAddr].lastUpdated;
- }
- function setLastUpdated(address contributorAddr, uint lastUpdated)
- public
- onlyPeerReview
- {
- // @@TODO: check existence
- contributorForAddress[contributorAddr].lastUpdated = lastUpdated;
- }
- /**
- * field: profile
- */
- function getProfile(address contributorAddr)
- public
- constant
- returns (bytes32)
- {
- // @@TODO: check existence
- return contributorForAddress[contributorAddr].profile;
- }
- function setProfile(address contributorAddr, bytes32 profile)
- public
- onlyPeerReview
- {
- // @@TODO: check existence
- contributorForAddress[contributorAddr].profile = profile;
- ModifiedProfile(contributorAddr, contributorForAddress[contributorAddr].profile);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement