From 3c4b9ae76648f69fe3efbd202b4110110acfd76e Mon Sep 17 00:00:00 2001 From: Ricardo Guilherme Schmidt <3esmit@gmail.com> Date: Thu, 11 May 2017 16:56:39 -0300 Subject: [PATCH] fixed contracts scope --- contracts/src/DGit.sol | 96 ------------- contracts/src/GitHubOracle.sol | 247 ++++++++++----------------------- contracts/src/GitHubPoints.sol | 189 +++++++++++++++++++++++++ 3 files changed, 266 insertions(+), 266 deletions(-) delete mode 100644 contracts/src/DGit.sol create mode 100644 contracts/src/GitHubPoints.sol diff --git a/contracts/src/DGit.sol b/contracts/src/DGit.sol deleted file mode 100644 index 436468e..0000000 --- a/contracts/src/DGit.sol +++ /dev/null @@ -1,96 +0,0 @@ -pragma solidity ^0.4.8; - -/** - * Contract that oracle github API - * - * GitHubOracle register users and create GitHubToken contracts - * Registration requires user create a gist with only their account address - * GitHubOracle will create one GitHubToken contract per repository - * GitHubToken mint tokens by commit only for registered users in GitHubOracle - * GitHubToken is a LockableCoin, that accept donatations and can be withdrawn by Token Holders - * The lookups are done by Oraclize that charge a small fee - * The contract itself will never charge any fee - * - * By Ricardo Guilherme Schmidt - * Released under GPLv3 License - */ - -import "lib/oraclize/oraclizeAPI_0.4.sol"; -import "lib/ethereans/management/Owned.sol"; -import "./DGitDB.sol"; -import "./GitHubAPI.sol"; -import "./GitRepository.sol"; - -contract DGit is Owned, DGitI { - - DGitDBI public db; - GitHubAPI public gitHubApi; - - function initialize() only_owner { - db = DBFactory.newStorage(); - gitHubApi = QueryFactory.newGitHubAPI(); - } - - function register(string _github_user, string _gistid) payable{ - gitHubApi.register.value(msg.value)(msg.sender,_github_user,_gistid); - } - function updateCommits(string _repository) payable{ - gitHubApi.updateCommits.value(msg.value)(_repository,db.getClaimedHead(_repository)); - } - function addRepository(string _repository) payable{ - gitHubApi.addRepository.value(msg.value)(_repository); - } - function updateIssue(string _repository, string issue) payable{ - gitHubApi.updateIssue.value(msg.value)(_repository,issue); - } - function getRepository(uint projectId) constant returns (address){ - return db.getRepositoryAddress(projectId); - } - function getRepository(string full_name) constant returns (address){ - return db.getRepositoryAddress(full_name); - } - - modifier only_gitapi{ - if (msg.sender != address(gitHubApi)) throw; - _; - } - - event UserSet(string githubLogin); - function __register(address addrLoaded, uint256 userId, string login) - only_gitapi { - UserSet(login); - db.addUser(userId, login, 0, addrLoaded); - } - - event GitRepositoryRegistered(uint256 projectId, string full_name, uint256 watchers, uint256 subscribers); - function __setRepository(uint256 projectId, string full_name, uint256 watchers, uint256 subscribers) only_gitapi //[83725290, "ethereans/github-token", 4, 2] - { - uint256 ownerId; string memory name; //TODO - address repository = db.getRepositoryAddress(projectId); - if(repository == 0x0){ - GitRepositoryRegistered(projectId,full_name,watchers,subscribers); - repository = GitFactory.newGitRepository(projectId,full_name); - db.addRepository(projectId,ownerId,name,full_name,repository); - } - GitRepositoryI(repository).setStats(subscribers,watchers); - } - - event NewPoints(string repository, uint userId, uint total); - function __newPoints(string repository, uint userId, uint total) - only_gitapi { - NewPoints(repository,userId,total); - GitRepositoryI repoaddr = GitRepositoryI(db.getRepositoryAddress(repository)); - if(!repoaddr.claim(db.getUserAddress(userId), total)){ //try to claim points - db.setPending(repository, userId, total); //set as a pending points - } - } - - //claims pending points - function claimPending(uint repoId, uint userId){ - GitRepositoryI repoaddr = GitRepositoryI(db.getRepositoryAddress(repoId)); - uint total = db.claimPending(repoId,userId); - if(!repoaddr.claim(db.getUserAddress(userId), total)) throw; - - } - -} \ No newline at end of file diff --git a/contracts/src/GitHubOracle.sol b/contracts/src/GitHubOracle.sol index c8fec8e..436468e 100644 --- a/contracts/src/GitHubOracle.sol +++ b/contracts/src/GitHubOracle.sol @@ -1,189 +1,96 @@ -pragma solidity ^0.4.9; +pragma solidity ^0.4.8; +/** + * Contract that oracle github API + * + * GitHubOracle register users and create GitHubToken contracts + * Registration requires user create a gist with only their account address + * GitHubOracle will create one GitHubToken contract per repository + * GitHubToken mint tokens by commit only for registered users in GitHubOracle + * GitHubToken is a LockableCoin, that accept donatations and can be withdrawn by Token Holders + * The lookups are done by Oraclize that charge a small fee + * The contract itself will never charge any fee + * + * By Ricardo Guilherme Schmidt + * Released under GPLv3 License + */ + import "lib/oraclize/oraclizeAPI_0.4.sol"; -import "lib/StringLib.sol"; -import "lib/JSONLib.sol"; import "lib/ethereans/management/Owned.sol"; +import "./DGitDB.sol"; +import "./GitHubAPI.sol"; +import "./GitRepository.sol"; +contract DGit is Owned, DGitI { -contract GitHubAPI{ - function updateCommits(string _full_name, string _branch, bytes20 _commitid) payable; - function updateIssue(string _full_name, string _issue) payable; -} + DGitDBI public db; + GitHubAPI public gitHubApi; -contract DGitI { - function __addRepository(uint256 projectId, string full_name, string default_branch); - function __setHead(uint256 projectId, string branch, bytes20 head); - function __setTail(uint256 projectId, string branch, bytes20 tail); - function __newPoints(uint256 projectId, uint256 userId, uint total); - function __setIssue(uint256 projectId, uint256 issueId, bool state, uint256 closedAt); - function __setIssuePoints(uint256 projectId, uint256 issueId, uint256 userId, uint256 points); -} + function initialize() only_owner { + db = DBFactory.newStorage(); + gitHubApi = QueryFactory.newGitHubAPI(); + } -contract GitHubOracle is GitHubAPI, Owned, usingOraclize{ - using StringLib for string; - DGitI dGit - - string private cred = ""; - string private script = ""; - - enum OracleType { CLAIM_COMMIT, CLAIM_CONTINUE, UPDATE_ISSUE } - mapping (bytes32 => OracleType) claimType; //temporary db enumerating oraclize calls - mapping (bytes32 => CommitClaim) commitClaim; //temporary db for oraclize commit token claim calls - - //stores temporary data for oraclize repository commit claim - struct CommitClaim { - string repository; - bytes20 commitid; + function register(string _github_user, string _gistid) payable{ + gitHubApi.register.value(msg.value)(msg.sender,_github_user,_gistid); + } + function updateCommits(string _repository) payable{ + gitHubApi.updateCommits.value(msg.value)(_repository,db.getClaimedHead(_repository)); + } + function addRepository(string _repository) payable{ + gitHubApi.addRepository.value(msg.value)(_repository); + } + function updateIssue(string _repository, string issue) payable{ + gitHubApi.updateIssue.value(msg.value)(_repository,issue); + } + function getRepository(uint projectId) constant returns (address){ + return db.getRepositoryAddress(projectId); + } + function getRepository(string full_name) constant returns (address){ + return db.getRepositoryAddress(full_name); + } + + modifier only_gitapi{ + if (msg.sender != address(gitHubApi)) throw; + _; } - function GitHubOracle(string _script){ - script = _script; - dGit = DGitI(msg.sender); - oraclize_setProof(proofType_TLSNotary | proofStorage_IPFS); + event UserSet(string githubLogin); + function __register(address addrLoaded, uint256 userId, string login) + only_gitapi { + UserSet(login); + db.addUser(userId, login, 0, addrLoaded); } - function updateCommits(string _repository, string _branch, bytes20 _commitid) - payable only_owner{ - bytes32 ocid = oraclize_query("computation", [script, "update-new",_repository.concat(",", _branch,",",toString(_commitid)),cred]); - claimType[ocid] = OracleType.CLAIM_COMMIT; - commitClaim[ocid] = CommitClaim( { repository: _repository, commitid:_commitid}); - } - - function continueUpdateCommits(string _repository, string _branch, bytes20 _lastCommit,bytes20 _limitCommit) - payable only_owner{ - bytes32 ocid = oraclize_query("computation", [script, "update-old",_repository.concat(",", _branch,",",toString(_lastCommit)).concat(",",toString(_limitCommit)),cred]); - claimType[ocid] = OracleType.CLAIM_CONTINUE; - } - - function updateIssue(string _repository, string issue) payable only_owner{ - bytes32 ocid = oraclize_query("computation", [script, "issue-update",_repository.concat(",",issue),cred]); - } - - event OracleEvent(bytes32 myid, string result, bytes proof); - //oraclize response callback - function __callback(bytes32 myid, string result, bytes proof) { - OracleEvent(myid, result, proof); - if (msg.sender != oraclize.cbAddress()){ - throw; - }else if(claimType[myid]==OracleType.CLAIM_COMMIT){ - _updateCommits(myid, result, false); - }else if(claimType[myid]==OracleType.CLAIM_CONTINUE){ - _updateCommits(myid, result, true); - }else if(claimType[myid] == OracleType.UPDATE_ISSUE){ - _updateIssue(myid, result); + event GitRepositoryRegistered(uint256 projectId, string full_name, uint256 watchers, uint256 subscribers); + function __setRepository(uint256 projectId, string full_name, uint256 watchers, uint256 subscribers) only_gitapi //[83725290, "ethereans/github-token", 4, 2] + { + uint256 ownerId; string memory name; //TODO + address repository = db.getRepositoryAddress(projectId); + if(repository == 0x0){ + GitRepositoryRegistered(projectId,full_name,watchers,subscribers); + repository = GitFactory.newGitRepository(projectId,full_name); + db.addRepository(projectId,ownerId,name,full_name,repository); } - delete claimType[myid]; //should always be deleted + GitRepositoryI(repository).setStats(subscribers,watchers); } - function _addRepository(bytes32 myid, string result) internal {//[85743750, "ethereans/TheEtherian", "master"] - bytes memory v = bytes(result); - uint8 pos = 0; - string memory temp; - uint256 projectId; - (projectId,pos) = JSONLib.getNextUInt(v,pos); - string memory full_name; - (full_name,pos) = JSONLib.getNextString(v,pos); - string memory default_branch; - (default_branch,pos) = JSONLib.getNextString(v,pos); - dGit.__addRepository(projectId,full_name,default_branch); - } - - function _updateCommits(bytes32 myid, string result, bool continuing) internal { - bytes memory v = bytes(result); - uint8 pos = 0; - string memory temp; - uint256 projectId; - (projectId,pos) = JSONLib.getNextUInt(v,pos); - string memory branch; - (branch,pos) = JSONLib.getNextString(v,pos); - (temp,pos) = JSONLib.getNextString(v,pos); - bytes20 head = temp.toBytes20(); - (temp,pos) = JSONLib.getNextString(v,pos); - bytes20 tail = temp.toBytes20(); - uint numAuthors; - (numAuthors,pos) = JSONLib.getNextUInt(v,pos); - uint userId; - uint points; - dGit.__setHead(projectId,branch,head); - if(continuing){ - dGit.__setTail(projectId,branch,tail); - }else{ - bytes20 oldCommit = commitUpdate[myid].commitid; - if(oldCommit == 0x0){ - dGit.__setTail(projectId,branch,tail); - }else if (oldCommit != tail){ - //TODO: acceptContinueUpdateUntilLimit(tail,oldCommit) - } - } - for(uint i; i < numAuthors; i++){ - (userId,pos) = JSONLib.getNextUInt(v,pos); - (points,pos) = JSONLib.getNextUInt(v,pos); - dGit.__newPoints(projectId,userId,points); - } + event NewPoints(string repository, uint userId, uint total); + function __newPoints(string repository, uint userId, uint total) + only_gitapi { + NewPoints(repository,userId,total); + GitRepositoryI repoaddr = GitRepositoryI(db.getRepositoryAddress(repository)); + if(!repoaddr.claim(db.getUserAddress(userId), total)){ //try to claim points + db.setPending(repository, userId, total); //set as a pending points + } } - function _updateIssue(bytes32 myid, string result) internal { - bytes memory v = bytes(result); - uint8 pos = 0; - string memory temp; - uint256 projectId; - (projectId,pos) = JSONLib.getNextUInt(v,pos); - uint256 issueId; - (issueId,pos) = JSONLib.getNextUInt(v,pos); - bool state; - (temp,pos) = JSONLib.getNextString(v,pos); - state = (temp.compare("open") == 0); - uint256 closedAt; - (closedAt,pos) = JSONLib.getNextUInt(v,pos); - uint numAuthors; - (numAuthors,pos) = JSONLib.getNextUInt(v,pos); - uint userId; - uint points; - dGit.__setIssue(projectId,issueId,state,closedAt); - for(uint i; i < numAuthors; i++){ - (userId,pos) = JSONLib.getNextUInt(v,pos); - (points,pos) = JSONLib.getNextUInt(v,pos); - dGit.__setIssuePoints(projectId,issueId,userId,points); - } - } - - //owner management - function setAPICredentials(string _client_id, string _client_secret) only_owner { - cred = StringLib.concat(_client_id,",", _client_secret); - } - - function setScript(string _script) only_owner{ - script = _script; - } - - function clearAPICredentials() only_owner { - cred = ""; - } - - 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); - } - -} - -library QueryFactory { - - function newGitHubAPI() returns (GitHubAPI){ - return new GitHubOracle(); + //claims pending points + function claimPending(uint repoId, uint userId){ + GitRepositoryI repoaddr = GitRepositoryI(db.getRepositoryAddress(repoId)); + uint total = db.claimPending(repoId,userId); + if(!repoaddr.claim(db.getUserAddress(userId), total)) throw; + } } \ No newline at end of file diff --git a/contracts/src/GitHubPoints.sol b/contracts/src/GitHubPoints.sol new file mode 100644 index 0000000..c8fec8e --- /dev/null +++ b/contracts/src/GitHubPoints.sol @@ -0,0 +1,189 @@ +pragma solidity ^0.4.9; + +import "lib/oraclize/oraclizeAPI_0.4.sol"; +import "lib/StringLib.sol"; +import "lib/JSONLib.sol"; +import "lib/ethereans/management/Owned.sol"; + + +contract GitHubAPI{ + function updateCommits(string _full_name, string _branch, bytes20 _commitid) payable; + function updateIssue(string _full_name, string _issue) payable; +} + +contract DGitI { + function __addRepository(uint256 projectId, string full_name, string default_branch); + function __setHead(uint256 projectId, string branch, bytes20 head); + function __setTail(uint256 projectId, string branch, bytes20 tail); + function __newPoints(uint256 projectId, uint256 userId, uint total); + function __setIssue(uint256 projectId, uint256 issueId, bool state, uint256 closedAt); + function __setIssuePoints(uint256 projectId, uint256 issueId, uint256 userId, uint256 points); +} + +contract GitHubOracle is GitHubAPI, Owned, usingOraclize{ + using StringLib for string; + DGitI dGit + + string private cred = ""; + string private script = ""; + + enum OracleType { CLAIM_COMMIT, CLAIM_CONTINUE, UPDATE_ISSUE } + mapping (bytes32 => OracleType) claimType; //temporary db enumerating oraclize calls + mapping (bytes32 => CommitClaim) commitClaim; //temporary db for oraclize commit token claim calls + + //stores temporary data for oraclize repository commit claim + struct CommitClaim { + string repository; + bytes20 commitid; + } + + function GitHubOracle(string _script){ + script = _script; + dGit = DGitI(msg.sender); + oraclize_setProof(proofType_TLSNotary | proofStorage_IPFS); + } + + function updateCommits(string _repository, string _branch, bytes20 _commitid) + payable only_owner{ + bytes32 ocid = oraclize_query("computation", [script, "update-new",_repository.concat(",", _branch,",",toString(_commitid)),cred]); + claimType[ocid] = OracleType.CLAIM_COMMIT; + commitClaim[ocid] = CommitClaim( { repository: _repository, commitid:_commitid}); + } + + function continueUpdateCommits(string _repository, string _branch, bytes20 _lastCommit,bytes20 _limitCommit) + payable only_owner{ + bytes32 ocid = oraclize_query("computation", [script, "update-old",_repository.concat(",", _branch,",",toString(_lastCommit)).concat(",",toString(_limitCommit)),cred]); + claimType[ocid] = OracleType.CLAIM_CONTINUE; + } + + function updateIssue(string _repository, string issue) payable only_owner{ + bytes32 ocid = oraclize_query("computation", [script, "issue-update",_repository.concat(",",issue),cred]); + } + + event OracleEvent(bytes32 myid, string result, bytes proof); + //oraclize response callback + function __callback(bytes32 myid, string result, bytes proof) { + OracleEvent(myid, result, proof); + if (msg.sender != oraclize.cbAddress()){ + throw; + }else if(claimType[myid]==OracleType.CLAIM_COMMIT){ + _updateCommits(myid, result, false); + }else if(claimType[myid]==OracleType.CLAIM_CONTINUE){ + _updateCommits(myid, result, true); + }else if(claimType[myid] == OracleType.UPDATE_ISSUE){ + _updateIssue(myid, result); + } + delete claimType[myid]; //should always be deleted + } + + function _addRepository(bytes32 myid, string result) internal {//[85743750, "ethereans/TheEtherian", "master"] + bytes memory v = bytes(result); + uint8 pos = 0; + string memory temp; + uint256 projectId; + (projectId,pos) = JSONLib.getNextUInt(v,pos); + string memory full_name; + (full_name,pos) = JSONLib.getNextString(v,pos); + string memory default_branch; + (default_branch,pos) = JSONLib.getNextString(v,pos); + dGit.__addRepository(projectId,full_name,default_branch); + } + + function _updateCommits(bytes32 myid, string result, bool continuing) internal { + bytes memory v = bytes(result); + uint8 pos = 0; + string memory temp; + uint256 projectId; + (projectId,pos) = JSONLib.getNextUInt(v,pos); + string memory branch; + (branch,pos) = JSONLib.getNextString(v,pos); + (temp,pos) = JSONLib.getNextString(v,pos); + bytes20 head = temp.toBytes20(); + (temp,pos) = JSONLib.getNextString(v,pos); + bytes20 tail = temp.toBytes20(); + uint numAuthors; + (numAuthors,pos) = JSONLib.getNextUInt(v,pos); + uint userId; + uint points; + dGit.__setHead(projectId,branch,head); + if(continuing){ + dGit.__setTail(projectId,branch,tail); + }else{ + bytes20 oldCommit = commitUpdate[myid].commitid; + if(oldCommit == 0x0){ + dGit.__setTail(projectId,branch,tail); + }else if (oldCommit != tail){ + //TODO: acceptContinueUpdateUntilLimit(tail,oldCommit) + } + } + for(uint i; i < numAuthors; i++){ + (userId,pos) = JSONLib.getNextUInt(v,pos); + (points,pos) = JSONLib.getNextUInt(v,pos); + dGit.__newPoints(projectId,userId,points); + } + } + + function _updateIssue(bytes32 myid, string result) internal { + bytes memory v = bytes(result); + uint8 pos = 0; + string memory temp; + uint256 projectId; + (projectId,pos) = JSONLib.getNextUInt(v,pos); + uint256 issueId; + (issueId,pos) = JSONLib.getNextUInt(v,pos); + bool state; + (temp,pos) = JSONLib.getNextString(v,pos); + state = (temp.compare("open") == 0); + uint256 closedAt; + (closedAt,pos) = JSONLib.getNextUInt(v,pos); + uint numAuthors; + (numAuthors,pos) = JSONLib.getNextUInt(v,pos); + uint userId; + uint points; + dGit.__setIssue(projectId,issueId,state,closedAt); + for(uint i; i < numAuthors; i++){ + (userId,pos) = JSONLib.getNextUInt(v,pos); + (points,pos) = JSONLib.getNextUInt(v,pos); + dGit.__setIssuePoints(projectId,issueId,userId,points); + } + } + + //owner management + function setAPICredentials(string _client_id, string _client_secret) only_owner { + cred = StringLib.concat(_client_id,",", _client_secret); + } + + function setScript(string _script) only_owner{ + script = _script; + } + + function clearAPICredentials() only_owner { + cred = ""; + } + + 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); + } + +} + +library QueryFactory { + + function newGitHubAPI() returns (GitHubAPI){ + return new GitHubOracle(); + } + +} \ No newline at end of file