From d9e8570ebbf3eb457357a39c1dd8266cc96c6d0c Mon Sep 17 00:00:00 2001 From: Ricardo Guilherme Schmidt <3esmit@gmail.com> Date: Sat, 20 May 2017 02:32:44 -0300 Subject: [PATCH] longtail and resume --- contracts/GitHubOracle.sol | 50 ++++++--- contracts/GitHubPoints.sol | 223 ++++++++++++------------------------- 2 files changed, 103 insertions(+), 170 deletions(-) diff --git a/contracts/GitHubOracle.sol b/contracts/GitHubOracle.sol index 0092317..2935c8d 100644 --- a/contracts/GitHubOracle.sol +++ b/contracts/GitHubOracle.sol @@ -60,14 +60,27 @@ contract GitHubOracle is Controlled, DGitI { function __changeController(address _newController) onlyController { userReg.changeController(_newController); - repoReg.changeController(newContract); - gitHubPoints.changeController(newContract); + repositoryReg.changeController(_newController); + gitHubPoints.changeController(_newController); } function update(string _repository, string _token) payable { uint256 repoId = repositoryReg.getId(_repository); if(repoId == 0) throw; - gitHubPoints.update.value(msg.value)(_repository, "master", repositories[repoId].head,_token); + gitHubPoints.update.value(msg.value)(_repository, repositoryReg.getBranch(repoId), repositories[repoId].head, _token); + } + function resume(string _repository, string _pendingTail, string _token) payable { + uint256 repoId = repositoryReg.getId(_repository); + if(repoId == 0) throw; + string claimedCommit = repositories[repoid].pending[_pendingTail]; + if(bytes(claimedCommit).length == 0) throw; + delete repositories[repoid].pending[_pendingTail]; + gitHubPoints.resume.value(msg.value)(_repository, repositoryReg.getBranch(repoId), _pendingTail, claimedCommit, _token); + } + function longtail(string _repository, string _token) payable { + uint256 repoId = repositoryReg.getId(_repository); + if(repoId == 0) throw; + gitHubPoints.longtail.value(msg.value)(_repository, repositoryReg.getBranch(repoId), repositories[repoId].tail, _token); } function issue(string _repository, string _issue, string _token) payable { @@ -91,21 +104,26 @@ contract GitHubOracle is Controlled, DGitI { repo.setBounty(_issueId, _state, _closedAt); } - function __setIssuePoints(uint256 _projectId, uint256 _issueId, uint256 _userId, uint256 _points) oraclized { + function __setIssuePoints(uint256 _projectId, uint256 _issueId, uint256[] _userId, uint256[] _points) oraclized { GitRepositoryI repo = GitRepositoryI(repositoryReg.getAddr(_projectId)); - repo.setBountyPoints(_issueId, userReg.getAddr(_userId), _points); + uint len = _userId.length; + for(uint i = 0; i < len; i++){ + address addr = userReg.getAddr(_userId[i]); + repo.setBountyPoints(_issueId, addr, _points[i]); + } } - event NewPoints(uint repoId, uint userId, uint total, bool claimed); - - function __newPoints(uint _repoId, uint _userId, uint _points) - oraclized { - GitRepositoryI repoaddr = GitRepositoryI(repositoryReg.getAddr(_repoId)); - bool claimed = repoaddr.claim(userReg.getAddr(_userId), _points); - if(!claimed){ //try to claim points - pending[_userId][_repoId] += _points; //set as a pending points + function __newPoints(uint _repoId, uint[] _userIds, uint[] _points) oraclized { + GitRepositoryI repo = GitRepositoryI(repositoryReg.getAddr(_repoId)); + uint len = _userIds.length; + for(uint i = 0; i < len; i++){ + uint _userId = _userIds[i]; + uint _uPoints = _points[i]; + address addr = userReg.getAddr(_userId); + if(addr == 0x0 || !repo.claim(addr, _uPoints)){ + pending[_userId][_repoId] += _uPoints; + } } - NewPoints(_repoId, _userId, _points, claimed); } //claims pending points @@ -113,9 +131,7 @@ contract GitHubOracle is Controlled, DGitI { GitRepositoryI repoaddr = GitRepositoryI(repositoryReg.getAddr(_repoId)); uint total = pending[_userId][_repoId]; delete pending[_userId][_repoId]; - if(repoaddr.claim(userReg.getAddr(_userId), total)) { - NewPoints(_repoId,_userId,total,true); - } else throw; + if(!repoaddr.claim(userReg.getAddr(_userId), total)) throw; } } \ No newline at end of file diff --git a/contracts/GitHubPoints.sol b/contracts/GitHubPoints.sol index 1ae94d6..02dba77 100644 --- a/contracts/GitHubPoints.sol +++ b/contracts/GitHubPoints.sol @@ -7,10 +7,10 @@ pragma solidity ^0.4.11; contract DGitI { function __setHead(uint256 projectId, string head); function __setTail(uint256 projectId, string tail); - function __newPoints(uint256 projectId, uint256 userId, uint total); function __pendingScan(uint256 projectId, string lastCommit, string pendingTail); function __setIssue(uint256 projectId, uint256 issueId, bool state, uint256 closedAt); - function __setIssuePoints(uint256 projectId, uint256 issueId, uint256 userId, uint256 points); + function __newPoints(uint256 projectId, uint256[] userIds, uint[] totals); + function __setIssuePoints(uint256 projectId, uint256 issueId, uint256[] userIds, uint256[] points); } contract GitHubPoints is Controlled, usingOraclize{ @@ -21,45 +21,45 @@ contract GitHubPoints is Controlled, usingOraclize{ string private cred = ""; string private script = ""; - enum Command { START, UPDATE, RESUME, ISSUE } - mapping (bytes32 => Command) command; //temporary db enumerating oraclize calls - mapping (bytes32 => string) lastCommits; //temporary db for oraclize commit token claim calls - mapping (bytes32 => string) branches; - //stores temporary data for oraclize repository commit claim - struct CommitClaim { - string repository; - bytes20 commitid; + enum Command { ISSUE, START, UPDATE, RESUME, LONGTAIL } + mapping (bytes32 => Claim) claim; // temporary db + struct Claim { + Command command; + string lastCommit; + string branch; } function start(string _repository, string _branch, string _cred) payable onlyController { if(bytes(_cred).length == 0) _cred = cred; bytes32 ocid = oraclize_query("nested", _query_start(_repository,_branch,_cred)); - command[ocid] = Command.START; - branches[ocid] = _branch; + claim[ocid] = Claim({command: Command.START, branch: _branch, lastCommit: ""}); } function update(string _repository, string _branch, string _lastCommit, string _cred) payable onlyController { if(bytes(_cred).length == 0) _cred = cred; bytes32 ocid = oraclize_query("nested", _query_update(_repository,_branch,_lastCommit,_cred)); - command[ocid] = Command.UPDATE; - lastCommits[ocid] = _lastCommit; - branches[ocid] = _branch; + claim[ocid] = Claim({command: Command.UPDATE, branch: _branch, lastCommit: _lastCommit}); } - function resume(string _repository, string _branch, string _lastCommit, string _limitCommit, string _cred) + function longtail(string _repository, string _branch, string _claimedTail, string _cred) payable onlyController { if(bytes(_cred).length == 0) _cred = cred; - bytes32 ocid = oraclize_query("nested", _query_resume(_repository,_branch,_lastCommit,_limitCommit,_cred)); - command[ocid] = Command.RESUME; - lastCommits[ocid] = _lastCommit; - branches[ocid] = _branch; + bytes32 ocid = oraclize_query("nested", _query_longtail(_repository,_branch,_claimedTail,_cred)); + claim[ocid] = Claim({command: Command.LONGTAIL, branch: _branch, lastCommit: ""}); + } + + function resume(string _repository, string _branch, string _pendingTail, string _claimedCommit, string _cred) + payable onlyController { + if(bytes(_cred).length == 0) _cred = cred; + bytes32 ocid = oraclize_query("nested", _query_resume(_repository,_branch,_pendingTail,_claimedCommit,_cred)); + claim[ocid] = Claim({command: Command.RESUME, branch: _branch, lastCommit: _claimedCommit}); } function issue(string _repository, string _issue, string _cred) payable onlyController { if(bytes(_cred).length == 0) _cred = cred; - command[ocid] = Command.ISSUE; bytes32 ocid = oraclize_query("nested", _query_issue(_repository,_issue,_cred)); + claim[ocid].command = Command.ISSUE; } event OracleEvent(bytes32 myid, string result, bytes proof); @@ -67,134 +67,59 @@ contract GitHubPoints is Controlled, usingOraclize{ function __callback(bytes32 myid, string result, bytes proof) { OracleEvent(myid, result, proof); if (msg.sender != oraclize.cbAddress()) throw; - Command comm = command[myid]; - if(comm == Command.UPDATE) { - _update(branches[myid],lastCommits[myid], result); - delete branches[myid]; - }else if(comm == Command.ISSUE) { - _issue(result); - }else if (comm == Command.RESUME) { - _resume(branches[myid],lastCommits[myid], result); - delete lastCommits[myid]; - delete branches[myid]; - }else if (comm == Command.START) { - _start(branches[myid],result); - delete branches[myid]; - } - delete command[myid]; + _process(bytes(result),claim[myid]); + delete claim[myid]; } - function _start(string _branch, string result) internal { - DGitI dGit = DGitI(owner); - bytes memory v = bytes(result); + function _process(bytes v, Claim claim) internal { + DGitI dGit = DGitI(controller); uint8 pos = 0; string memory temp; uint256 projectId; - (projectId,pos) = getNextUInt(v,pos); - (temp,pos) = getNextString(v,pos); //branch - if(sha3(_branch) != sha3(temp)) return; - (temp,pos) = getNextString(v,pos); //head - dGit.__setHead(projectId,temp); //head - (temp,pos) = getNextString(v,pos); //tail - - dGit.__setTail(projectId,temp); - uint numAuthors; - (numAuthors,pos) = getNextUInt(v,pos); - uint userId; - uint points; - for(uint i; i < numAuthors; i++){ - (userId,pos) = getNextUInt(v,pos); - (points,pos) = getNextUInt(v,pos); - dGit.__newPoints(projectId,userId,points); - } - } - - function _update(string _branch, string _lastCommit, string result) internal { - DGitI dGit = DGitI(owner); - bytes memory v = bytes(result); - uint8 pos = 0; - string memory temp; - uint256 projectId; - (projectId,pos) = getNextUInt(v,pos); - (temp,pos) = getNextString(v,pos); //branch - if(sha3(_branch) != sha3(temp)) return; - (temp,pos) = getNextString(v,pos); //head - dGit.__setHead(projectId,temp); //head - - (temp,pos) = getNextString(v,pos); //tail - if(bytes(_lastCommit).length == 0){ - dGit.__setTail(projectId,temp); - } - if (sha3(_lastCommit) != sha3(temp)){ //update didn't reached _lastCommit - dGit.__pendingScan(projectId,_lastCommit,temp); - } - uint numAuthors; - (numAuthors,pos) = getNextUInt(v,pos); - uint userId; - uint points; - for(uint i; i < numAuthors; i++){ - (userId,pos) = getNextUInt(v,pos); - (points,pos) = getNextUInt(v,pos); - dGit.__newPoints(projectId,userId,points); - } - } - - function _resume(string _branch, string _lastCommit, string result) internal { - DGitI dGit = DGitI(owner); - bytes memory v = bytes(result); - uint8 pos = 0; - string memory temp; - uint256 projectId; - (projectId,pos) = getNextUInt(v,pos); - string memory branch; - (branch,pos) = getNextString(v,pos); - if(sha3(_branch) != sha3(branch)) return; - string memory head; - (head,pos) = getNextString(v,pos); - string memory tail; - (tail,pos) = getNextString(v,pos); - dGit.__setTail(projectId,tail); - uint numAuthors; - (numAuthors,pos) = getNextUInt(v,pos); - uint userId; - uint points; - for(uint i; i < numAuthors; i++){ - (userId,pos) = getNextUInt(v,pos); - (points,pos) = getNextUInt(v,pos); - dGit.__newPoints(projectId,userId,points); - } - } - - function _issue(string result) internal { - DGitI dGit = DGitI(owner); - bytes memory v = bytes(result); - uint8 pos = 0; - string memory temp; - uint256 projectId; - (projectId,pos) = getNextUInt(v,pos); uint256 issueId; - (issueId,pos) = getNextUInt(v,pos); - bool state; - (temp,pos) = getNextString(v,pos); - state = (sha3("open") == sha3(temp)); - uint256 closedAt; - (closedAt,pos) = getNextUInt(v,pos); + (projectId,pos) = getNextUInt(v,pos); + + if(claim.command == Command.ISSUE){ + (issueId,pos) = getNextUInt(v,pos); + (temp,pos) = getNextString(v,pos);//temp = issue state + uint256 closedAt; + (closedAt,pos) = getNextUInt(v,pos); + dGit.__setIssue(projectId,issueId,(sha3("open") == sha3(temp)),closedAt); + } else { + (temp,pos) = getNextString(v,pos); //temp = branch + if(sha3(claim.branch) != sha3(temp)) return; + + if(claim.command == Command.START || claim.command == Command.UPDATE){ + (temp,pos) = getNextString(v,pos); //temp = scan head + dGit.__setHead(projectId,temp); + } + (temp,pos) = getNextString(v,pos); //temp = scan tail + if(claim.command == Command.START || claim.command == Command.LONGTAIL){ + dGit.__setTail(projectId,temp); + } + if((claim.command == Command.RESUME || claim.command == Command.UPDATE) && sha3(claim.lastCommit) != sha3(temp)){ + //update didn't reached _lastCommit + dGit.__pendingScan(projectId,temp,claim.lastCommit); + } + } uint numAuthors; (numAuthors,pos) = getNextUInt(v,pos); - uint userId; - uint points; - dGit.__setIssue(projectId,issueId,state,closedAt); + uint[] memory userId = new uint[](numAuthors); + uint[] memory points = new uint[](numAuthors); for(uint i; i < numAuthors; i++){ - (userId,pos) = getNextUInt(v,pos); - (points,pos) = getNextUInt(v,pos); + (userId[i],pos) = getNextUInt(v,pos); + (points[i],pos) = getNextUInt(v,pos); + } + if(claim.command == Command.ISSUE){ dGit.__setIssuePoints(projectId,issueId,userId,points); + }else{ + dGit.__newPoints(projectId,userId,points); } } //owner management function GitHubPoints(string _script){ script = _script; - oraclize_setProof(proofType_TLSNotary | proofStorage_IPFS); } function setAPICredentials(string _client_id_comma_client_secret) onlyController { @@ -241,16 +166,25 @@ contract GitHubPoints is Controlled, usingOraclize{ return _query_script("update",comma.join(cm),_cred); } - function _query_resume(string _repository, string _branch, string _lastCommit, string _limitCommit, string _cred) internal constant returns (string){ + function _query_resume(string _repository, string _branch, string _tail, string _claimedCommit, string _cred) internal constant returns (string){ strings.slice memory comma = strings.toSlice(","); strings.slice [] memory cm = new strings.slice[](4); cm[0] = _repository.toSlice(); cm[1] = _branch.toSlice(); - cm[2] = _lastCommit.toSlice(); - cm[3] = _limitCommit.toSlice(); + cm[2] = _tail.toSlice(); + cm[3] = _claimedCommit.toSlice(); return _query_script("resume",comma.join(cm),_cred); } + function _query_longtail(string _repository, string _branch, string _claimedTail, string _cred) internal constant returns (string){ + strings.slice memory comma = strings.toSlice(","); + strings.slice [] memory cm = new strings.slice[](3); + cm[0] = _repository.toSlice(); + cm[1] = _branch.toSlice(); + cm[2] = _claimedTail.toSlice(); + return _query_script("longtail",comma.join(cm),_cred); + } + function _query_issue(string _repository, string _issue, string _cred) internal returns(string){ strings.slice memory comma = strings.toSlice(","); strings.slice [] memory cm = new strings.slice[](2); @@ -266,23 +200,6 @@ contract GitHubPoints is Controlled, usingOraclize{ } } - function toString(bytes20 self) internal constant returns (string) { - bytes memory bytesString = new bytes(20); - uint charCount = 0; - for (uint j = 0; j < 20; j++) { - byte char = byte(bytes20(uint(self) * 2 ** (8 * j))); - if (char != 0) { - bytesString[charCount] = char; - charCount++; - } - } - bytes memory bytesStringTrimmed = new bytes(charCount); - for (j = 0; j < charCount; j++) { - bytesStringTrimmed[j] = bytesString[j]; - } - return string(bytesStringTrimmed); - } - function getNextString(bytes _str, uint8 _pos) internal constant returns (string, uint8) { uint8 start = 0; uint8 end = 0;