{"action":"created","comment":{"url":"https://api.github.com/repos/maticnetwork/contracts/pulls/comments/219664477","pull_request_review_id":157902491,"id":219664477,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIxOTY2NDQ3Nw==","diff_hunk":"@@ -6,134 +6,194 @@ import { SafeMath } from \"openzeppelin-solidity/contracts/math/SafeMath.sol\";\n \n import { ECVerify } from \"../lib/ECVerify.sol\";\n import { BytesLib } from \"../lib/BytesLib.sol\";\n+import { PriorityQueue } from \"../lib/PriorityQueue.sol\";\n+\n import { Lockable } from \"../mixin/Lockable.sol\";\n import { RootChainable } from \"../mixin/RootChainable.sol\";\n \n import { StakeManagerInterface } from \"./StakeManagerInterface.sol\";\n import { RootChain } from \"./RootChain.sol\";\n+import { ValidatorSet } from \"./ValidatorSet.sol\"; \n \n \n contract StakeManager is StakeManagerInterface, RootChainable, Lockable {\n   using SafeMath for uint256;\n+  using SafeMath for uint8;\n   using ECVerify for bytes32;\n \n-  // token object\n-  ERC20 public tokenObj;\n-\n-  // validator threshold\n-  uint256 public _validatorThreshold = 0;\n-\n-  // pool of stakers and validators\n-  struct Stake {\n-    uint256 amount;\n-    address staker;\n-  }\n+  PriorityQueue stakeQueue;\n+  ValidatorSet validatorSet;\n \n-  // map of stakers (address => staked index)\n-  mapping(address => uint256) public stakers;\n+  ERC20 public tokenObj;\n \n-  // stake list\n-  Stake[] public stakeList;\n+  event ThresholdChange(uint256 newThreshold, uint256 oldThreshold);\n+  //optional event to ack unstaking\n+  event UnstakeInit(address indexed user, uint256 amount, uint256 total, bytes data); \n+  event NewValidatorSet(uint256 validatorThreshold, uint256 totalPower, bytes data);\n \n-  // total stake\n-  uint256 public totalStake;\n+  uint256 public _validatorThreshold = 0;\n+  uint256 public totalStake = 0;\n+  uint256 public currentEpoch = 1;\n+  uint256 public totalEpoch;\n \n-  // The randomness seed of the epoch.\n-  // This is used to determine the proposer and the validator pool\n-  bytes32 public epochSeed = keccak256(abi.encodePacked(block.difficulty + block.number + now));\n+  //Todo: dynamically update\n+  uint256 public minStakeAmount = 1;  // ETH\n+  uint256 public minLockInPeriod = 100; //(unit epochs)\n+  uint256 public stakingIdCount = 0;  // just a counter/index to map it with PQ w/address\n \n-  //\n-  // Events\n-  //\n+  enum ValidatorStatus { WAITING, VALIDATOR, UNSTAKING } // need update \n \n-  event ThresholdChange(uint256 newThreshold, uint256 oldThreshold);\n+  struct Staker {\n+    uint256 epoch;  // init 0 \n+    uint256 amount;\n+    bytes data;\n+    ValidatorStatus status;\n+    uint256 stakingId; \n+  }\n \n-  // ERC900\n-  event Staked(address indexed user, uint256 amount, uint256 total, bytes data);\n-  event Unstaked(address indexed user, uint256 amount, uint256 total, bytes data);\n+  address[] exiterList;\n+  address[] stakersList;\n \n-  //\n-  // Constructor\n-  //\n+  mapping (address => Staker) stakers; \n+  mapping (uint256 => address) stakingIdToAddress;\n \n-  constructor (address _token) public {\n+  constructor(address _token) public {\n     require(_token != 0x0);\n     tokenObj = ERC20(_token);\n+    stakeQueue = new PriorityQueue();\n   }\n \n-  //\n-  // Modifiers\n-  //\n-\n   // only staker\n   modifier onlyStaker() {\n     require(totalStakedFor(msg.sender) > 0);\n     _;\n   }\n \n-  //\n-  // ERC900 implementation\n-  //\n+  \n+  function _priority(address user, uint256 amount, bytes data) private view returns (uint256) {\n+    // return amount.mul(10).add(currentEpoch.mul(1000).add(amount.mul(100).div(user.balance)));\n+    return amount;\n+  }\n \n   function stake(uint256 amount, bytes data) public {\n+    // no second time staking \n+    // maybe restrict entry after n staker\n+    require(stakers[msg.sender].epoch == 0);\n     stakeFor(msg.sender, amount, data);\n   }\n \n   function stakeFor(address user, uint256 amount, bytes data) public onlyWhenUnlocked {\n-    require(amount > 0);\n-\n-    // transfer tokens to stake manager\n-    require(tokenObj.transferFrom(msg.sender, address(this), amount));\n-\n-    // check staker is present or not\n-    if (stakers[user] == 0) {\n-      // actual staker cannot be on index 0\n-      if (stakeList.length == 0) {\n-        stakeList.push(Stake({\n-          amount: 0,\n-          staker: address(0x0)\n-        }));\n-      }\n-\n-      // add new stake\n-      stakeList.push(Stake({\n-        amount: 0,\n-        staker: user\n-      }));\n-      stakers[user] = stakeList.length - 1;\n+    require(amount >= minStakeAmount); \n+\n+    // actual staker cannot be on index 0\n+    if (stakersList.length == 0) {\n+      stakersList.push(address(0x0));\n+      stakers[address(0x0)] = Staker(0, 0, new bytes(0), ValidatorStatus.WAITING, stakingIdCount);\n+      stakingIdToAddress[stakingIdCount] = address(0x0);\n+      stakingIdCount.add(1);\n     }\n \n-    // add amount\n-    uint256 stakedAmount = totalStakedFor(user).add(amount);\n-    stakeList[stakers[user]].amount = stakedAmount;\n-\n+    // transfer tokens to stake manager\n+    require(tokenObj.transferFrom(user, address(this), amount));\n+    \n     // update total stake\n     totalStake = totalStake.add(amount);\n \n-    // broadcast event\n-    emit Staked(user, amount, stakedAmount, data);\n+    stakers[user] = Staker(currentEpoch, amount, data, ValidatorStatus.WAITING, stakingIdCount);","path":"contracts/root/StakeManager.sol","position":162,"original_position":162,"commit_id":"a6718a34cd407f0da6932ae13747ad18674e9b86","original_commit_id":"a6718a34cd407f0da6932ae13747ad18674e9b86","user":{"login":"jdkanani","id":1692593,"node_id":"MDQ6VXNlcjE2OTI1OTM=","avatar_url":"https://avatars1.githubusercontent.com/u/1692593?v=4","gravatar_id":"","url":"https://api.github.com/users/jdkanani","html_url":"https://github.com/jdkanani","followers_url":"https://api.github.com/users/jdkanani/followers","following_url":"https://api.github.com/users/jdkanani/following{/other_user}","gists_url":"https://api.github.com/users/jdkanani/gists{/gist_id}","starred_url":"https://api.github.com/users/jdkanani/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jdkanani/subscriptions","organizations_url":"https://api.github.com/users/jdkanani/orgs","repos_url":"https://api.github.com/users/jdkanani/repos","events_url":"https://api.github.com/users/jdkanani/events{/privacy}","received_events_url":"https://api.github.com/users/jdkanani/received_events","type":"User","site_admin":false},"body":"Same as above. Make obj creation more readable","created_at":"2018-09-22T07:43:38Z","updated_at":"2018-09-22T07:43:38Z","html_url":"https://github.com/maticnetwork/contracts/pull/20#discussion_r219664477","pull_request_url":"https://api.github.com/repos/maticnetwork/contracts/pulls/20","author_association":"MEMBER","_links":{"self":{"href":"https://api.github.com/repos/maticnetwork/contracts/pulls/comments/219664477"},"html":{"href":"https://github.com/maticnetwork/contracts/pull/20#discussion_r219664477"},"pull_request":{"href":"https://api.github.com/repos/maticnetwork/contracts/pulls/20"}}},"pull_request":{"url":"https://api.github.com/repos/maticnetwork/contracts/pulls/20","id":214665599,"node_id":"MDExOlB1bGxSZXF1ZXN0MjE0NjY1NTk5","html_url":"https://github.com/maticnetwork/contracts/pull/20","diff_url":"https://github.com/maticnetwork/contracts/pull/20.diff","patch_url":"https://github.com/maticnetwork/contracts/pull/20.patch","issue_url":"https://api.github.com/repos/maticnetwork/contracts/issues/20","number":20,"state":"open","locked":false,"title":"Priority queue based stakeManager","user":{"login":"ashishrp","id":14859266,"node_id":"MDQ6VXNlcjE0ODU5MjY2","avatar_url":"https://avatars1.githubusercontent.com/u/14859266?v=4","gravatar_id":"","url":"https://api.github.com/users/ashishrp","html_url":"https://github.com/ashishrp","followers_url":"https://api.github.com/users/ashishrp/followers","following_url":"https://api.github.com/users/ashishrp/following{/other_user}","gists_url":"https://api.github.com/users/ashishrp/gists{/gist_id}","starred_url":"https://api.github.com/users/ashishrp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ashishrp/subscriptions","organizations_url":"https://api.github.com/users/ashishrp/orgs","repos_url":"https://api.github.com/users/ashishrp/repos","events_url":"https://api.github.com/users/ashishrp/events{/privacy}","received_events_url":"https://api.github.com/users/ashishrp/received_events","type":"User","site_admin":false},"body":"","created_at":"2018-09-11T15:06:19Z","updated_at":"2018-09-22T07:43:38Z","closed_at":null,"merged_at":null,"merge_commit_sha":"da5150da6e2057519d65b4f8ef7e26af8ccb5920","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/maticnetwork/contracts/pulls/20/commits","review_comments_url":"https://api.github.com/repos/maticnetwork/contracts/pulls/20/comments","review_comment_url":"https://api.github.com/repos/maticnetwork/contracts/pulls/comments{/number}","comments_url":"https://api.github.com/repos/maticnetwork/contracts/issues/20/comments","statuses_url":"https://api.github.com/repos/maticnetwork/contracts/statuses/a6718a34cd407f0da6932ae13747ad18674e9b86","head":{"label":"ashishrp:master","ref":"master","sha":"a6718a34cd407f0da6932ae13747ad18674e9b86","user":{"login":"ashishrp","id":14859266,"node_id":"MDQ6VXNlcjE0ODU5MjY2","avatar_url":"https://avatars1.githubusercontent.com/u/14859266?v=4","gravatar_id":"","url":"https://api.github.com/users/ashishrp","html_url":"https://github.com/ashishrp","followers_url":"https://api.github.com/users/ashishrp/followers","following_url":"https://api.github.com/users/ashishrp/following{/other_user}","gists_url":"https://api.github.com/users/ashishrp/gists{/gist_id}","starred_url":"https://api.github.com/users/ashishrp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ashishrp/subscriptions","organizations_url":"https://api.github.com/users/ashishrp/orgs","repos_url":"https://api.github.com/users/ashishrp/repos","events_url":"https://api.github.com/users/ashishrp/events{/privacy}","received_events_url":"https://api.github.com/users/ashishrp/received_events","type":"User","site_admin":false},"repo":{"id":143138873,"node_id":"MDEwOlJlcG9zaXRvcnkxNDMxMzg4NzM=","name":"contracts","full_name":"ashishrp/contracts","private":false,"owner":{"login":"ashishrp","id":14859266,"node_id":"MDQ6VXNlcjE0ODU5MjY2","avatar_url":"https://avatars1.githubusercontent.com/u/14859266?v=4","gravatar_id":"","url":"https://api.github.com/users/ashishrp","html_url":"https://github.com/ashishrp","followers_url":"https://api.github.com/users/ashishrp/followers","following_url":"https://api.github.com/users/ashishrp/following{/other_user}","gists_url":"https://api.github.com/users/ashishrp/gists{/gist_id}","starred_url":"https://api.github.com/users/ashishrp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ashishrp/subscriptions","organizations_url":"https://api.github.com/users/ashishrp/orgs","repos_url":"https://api.github.com/users/ashishrp/repos","events_url":"https://api.github.com/users/ashishrp/events{/privacy}","received_events_url":"https://api.github.com/users/ashishrp/received_events","type":"User","site_admin":false},"html_url":"https://github.com/ashishrp/contracts","description":"Smart contracts comprising the business logic of the Matic Network","fork":true,"url":"https://api.github.com/repos/ashishrp/contracts","forks_url":"https://api.github.com/repos/ashishrp/contracts/forks","keys_url":"https://api.github.com/repos/ashishrp/contracts/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ashishrp/contracts/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ashishrp/contracts/teams","hooks_url":"https://api.github.com/repos/ashishrp/contracts/hooks","issue_events_url":"https://api.github.com/repos/ashishrp/contracts/issues/events{/number}","events_url":"https://api.github.com/repos/ashishrp/contracts/events","assignees_url":"https://api.github.com/repos/ashishrp/contracts/assignees{/user}","branches_url":"https://api.github.com/repos/ashishrp/contracts/branches{/branch}","tags_url":"https://api.github.com/repos/ashishrp/contracts/tags","blobs_url":"https://api.github.com/repos/ashishrp/contracts/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ashishrp/contracts/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ashishrp/contracts/git/refs{/sha}","trees_url":"https://api.github.com/repos/ashishrp/contracts/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ashishrp/contracts/statuses/{sha}","languages_url":"https://api.github.com/repos/ashishrp/contracts/languages","stargazers_url":"https://api.github.com/repos/ashishrp/contracts/stargazers","contributors_url":"https://api.github.com/repos/ashishrp/contracts/contributors","subscribers_url":"https://api.github.com/repos/ashishrp/contracts/subscribers","subscription_url":"https://api.github.com/repos/ashishrp/contracts/subscription","commits_url":"https://api.github.com/repos/ashishrp/contracts/commits{/sha}","git_commits_url":"https://api.github.com/repos/ashishrp/contracts/git/commits{/sha}","comments_url":"https://api.github.com/repos/ashishrp/contracts/comments{/number}","issue_comment_url":"https://api.github.com/repos/ashishrp/contracts/issues/comments{/number}","contents_url":"https://api.github.com/repos/ashishrp/contracts/contents/{+path}","compare_url":"https://api.github.com/repos/ashishrp/contracts/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ashishrp/contracts/merges","archive_url":"https://api.github.com/repos/ashishrp/contracts/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ashishrp/contracts/downloads","issues_url":"https://api.github.com/repos/ashishrp/contracts/issues{/number}","pulls_url":"https://api.github.com/repos/ashishrp/contracts/pulls{/number}","milestones_url":"https://api.github.com/repos/ashishrp/contracts/milestones{/number}","notifications_url":"https://api.github.com/repos/ashishrp/contracts/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ashishrp/contracts/labels{/name}","releases_url":"https://api.github.com/repos/ashishrp/contracts/releases{/id}","deployments_url":"https://api.github.com/repos/ashishrp/contracts/deployments","created_at":"2018-08-01T10:08:40Z","updated_at":"2018-09-21T15:17:55Z","pushed_at":"2018-09-21T15:17:53Z","git_url":"git://github.com/ashishrp/contracts.git","ssh_url":"git@github.com:ashishrp/contracts.git","clone_url":"https://github.com/ashishrp/contracts.git","svn_url":"https://github.com/ashishrp/contracts","homepage":"https://matic.network","size":521,"stargazers_count":0,"watchers_count":0,"language":"Solidity","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"gpl-3.0","name":"GNU General Public License v3.0","spdx_id":"GPL-3.0","url":"https://api.github.com/licenses/gpl-3.0","node_id":"MDc6TGljZW5zZTk="},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"maticnetwork:master","ref":"master","sha":"e59e3fb9f7b4ed946e50dccb59a5b4b4d53685fa","user":{"login":"maticnetwork","id":30753617,"node_id":"MDEyOk9yZ2FuaXphdGlvbjMwNzUzNjE3","avatar_url":"https://avatars2.githubusercontent.com/u/30753617?v=4","gravatar_id":"","url":"https://api.github.com/users/maticnetwork","html_url":"https://github.com/maticnetwork","followers_url":"https://api.github.com/users/maticnetwork/followers","following_url":"https://api.github.com/users/maticnetwork/following{/other_user}","gists_url":"https://api.github.com/users/maticnetwork/gists{/gist_id}","starred_url":"https://api.github.com/users/maticnetwork/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maticnetwork/subscriptions","organizations_url":"https://api.github.com/users/maticnetwork/orgs","repos_url":"https://api.github.com/users/maticnetwork/repos","events_url":"https://api.github.com/users/maticnetwork/events{/privacy}","received_events_url":"https://api.github.com/users/maticnetwork/received_events","type":"Organization","site_admin":false},"repo":{"id":111664047,"node_id":"MDEwOlJlcG9zaXRvcnkxMTE2NjQwNDc=","name":"contracts","full_name":"maticnetwork/contracts","private":false,"owner":{"login":"maticnetwork","id":30753617,"node_id":"MDEyOk9yZ2FuaXphdGlvbjMwNzUzNjE3","avatar_url":"https://avatars2.githubusercontent.com/u/30753617?v=4","gravatar_id":"","url":"https://api.github.com/users/maticnetwork","html_url":"https://github.com/maticnetwork","followers_url":"https://api.github.com/users/maticnetwork/followers","following_url":"https://api.github.com/users/maticnetwork/following{/other_user}","gists_url":"https://api.github.com/users/maticnetwork/gists{/gist_id}","starred_url":"https://api.github.com/users/maticnetwork/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maticnetwork/subscriptions","organizations_url":"https://api.github.com/users/maticnetwork/orgs","repos_url":"https://api.github.com/users/maticnetwork/repos","events_url":"https://api.github.com/users/maticnetwork/events{/privacy}","received_events_url":"https://api.github.com/users/maticnetwork/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/maticnetwork/contracts","description":"Smart contracts comprising the business logic of the Matic Network","fork":false,"url":"https://api.github.com/repos/maticnetwork/contracts","forks_url":"https://api.github.com/repos/maticnetwork/contracts/forks","keys_url":"https://api.github.com/repos/maticnetwork/contracts/keys{/key_id}","collaborators_url":"https://api.github.com/repos/maticnetwork/contracts/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/maticnetwork/contracts/teams","hooks_url":"https://api.github.com/repos/maticnetwork/contracts/hooks","issue_events_url":"https://api.github.com/repos/maticnetwork/contracts/issues/events{/number}","events_url":"https://api.github.com/repos/maticnetwork/contracts/events","assignees_url":"https://api.github.com/repos/maticnetwork/contracts/assignees{/user}","branches_url":"https://api.github.com/repos/maticnetwork/contracts/branches{/branch}","tags_url":"https://api.github.com/repos/maticnetwork/contracts/tags","blobs_url":"https://api.github.com/repos/maticnetwork/contracts/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/maticnetwork/contracts/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/maticnetwork/contracts/git/refs{/sha}","trees_url":"https://api.github.com/repos/maticnetwork/contracts/git/trees{/sha}","statuses_url":"https://api.github.com/repos/maticnetwork/contracts/statuses/{sha}","languages_url":"https://api.github.com/repos/maticnetwork/contracts/languages","stargazers_url":"https://api.github.com/repos/maticnetwork/contracts/stargazers","contributors_url":"https://api.github.com/repos/maticnetwork/contracts/contributors","subscribers_url":"https://api.github.com/repos/maticnetwork/contracts/subscribers","subscription_url":"https://api.github.com/repos/maticnetwork/contracts/subscription","commits_url":"https://api.github.com/repos/maticnetwork/contracts/commits{/sha}","git_commits_url":"https://api.github.com/repos/maticnetwork/contracts/git/commits{/sha}","comments_url":"https://api.github.com/repos/maticnetwork/contracts/comments{/number}","issue_comment_url":"https://api.github.com/repos/maticnetwork/contracts/issues/comments{/number}","contents_url":"https://api.github.com/repos/maticnetwork/contracts/contents/{+path}","compare_url":"https://api.github.com/repos/maticnetwork/contracts/compare/{base}...{head}","merges_url":"https://api.github.com/repos/maticnetwork/contracts/merges","archive_url":"https://api.github.com/repos/maticnetwork/contracts/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/maticnetwork/contracts/downloads","issues_url":"https://api.github.com/repos/maticnetwork/contracts/issues{/number}","pulls_url":"https://api.github.com/repos/maticnetwork/contracts/pulls{/number}","milestones_url":"https://api.github.com/repos/maticnetwork/contracts/milestones{/number}","notifications_url":"https://api.github.com/repos/maticnetwork/contracts/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/maticnetwork/contracts/labels{/name}","releases_url":"https://api.github.com/repos/maticnetwork/contracts/releases{/id}","deployments_url":"https://api.github.com/repos/maticnetwork/contracts/deployments","created_at":"2017-11-22T09:34:25Z","updated_at":"2018-09-05T12:07:24Z","pushed_at":"2018-09-21T15:17:54Z","git_url":"git://github.com/maticnetwork/contracts.git","ssh_url":"git@github.com:maticnetwork/contracts.git","clone_url":"https://github.com/maticnetwork/contracts.git","svn_url":"https://github.com/maticnetwork/contracts","homepage":"https://matic.network","size":617,"stargazers_count":28,"watchers_count":28,"language":"Solidity","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":8,"mirror_url":null,"archived":false,"open_issues_count":6,"license":{"key":"gpl-3.0","name":"GNU General Public License v3.0","spdx_id":"GPL-3.0","url":"https://api.github.com/licenses/gpl-3.0","node_id":"MDc6TGljZW5zZTk="},"forks":8,"open_issues":6,"watchers":28,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/maticnetwork/contracts/pulls/20"},"html":{"href":"https://github.com/maticnetwork/contracts/pull/20"},"issue":{"href":"https://api.github.com/repos/maticnetwork/contracts/issues/20"},"comments":{"href":"https://api.github.com/repos/maticnetwork/contracts/issues/20/comments"},"review_comments":{"href":"https://api.github.com/repos/maticnetwork/contracts/pulls/20/comments"},"review_comment":{"href":"https://api.github.com/repos/maticnetwork/contracts/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/maticnetwork/contracts/pulls/20/commits"},"statuses":{"href":"https://api.github.com/repos/maticnetwork/contracts/statuses/a6718a34cd407f0da6932ae13747ad18674e9b86"}},"author_association":"MEMBER"}}
{"actor":{"display_login":"jdkanani"}}