Skip to content

Commit

Permalink
Merge branch 'master' into Mint_Method
Browse files Browse the repository at this point in the history
  • Loading branch information
SkandaBhat committed Jun 2, 2021
2 parents cb71955 + f35aa12 commit 83a0eff
Show file tree
Hide file tree
Showing 24 changed files with 257 additions and 97 deletions.
2 changes: 1 addition & 1 deletion .env.ganache
Expand Up @@ -9,7 +9,7 @@ PROVIDER_HOST=127.0.0.1
PROVIDER_PORT=8545

# Schelling Coin Address (if want to re-use already deployed instance)
SCHELLING_COIN_ADDRESS=0xF4c08930165661f13CEb5671CB0B0EC372b08151
SCHELLING_COIN_ADDRESS=

# List of Stakers
STAKER_ADDRESSES=0x1D68ad204637173b2d8656B7972c57dcE41Bc80e,0x9FF5085aa345C019cDF2A427B02Bd6746DeF549B,0xc4695904751Ad8414c75798d6Ff2579f55e61522,0x40d57C3F5c3BAbac3033E2D50AB7C6886A595F46,0xa2B827aCF6073f5D9e2350cbf0646Ba2535a5B0C
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -8,4 +8,5 @@ deployed/ganache
.env.goerli
node_modules/
.DS_Store
.previous-deployment-addresses
.previous-deployment-addresses
.contract-deployment.tmp.json
2 changes: 0 additions & 2 deletions .solhint.json
Expand Up @@ -6,9 +6,7 @@
"const-name-snakecase": "error",
"contract-name-camelcase": "error",
"event-name-camelcase": "error",
"expression-indent": "error",
"func-name-mixedcase": "error",
"func-order": "error",
"func-visibility": ["error", { "ignoreConstructors": true }],
"imports-on-top": "warn",
"mark-callable-contracts": "off",
Expand Down
36 changes: 19 additions & 17 deletions contracts/Core/BlockManager.sol
Expand Up @@ -8,10 +8,11 @@ import "./interface/IJobManager.sol";
import "./storage/BlockStorage.sol";
import "../lib/Constants.sol";
import "../lib/Random.sol";
import "../Initializable.sol";
import "./ACL.sol";


contract BlockManager is ACL, BlockStorage {
contract BlockManager is Initializable, ACL, BlockStorage {

IStakeManager public stakeManager;
IStateManager public stateManager;
Expand Down Expand Up @@ -50,17 +51,17 @@ contract BlockManager is ACL, BlockStorage {
_;
}

function init(
address _stakeManagerAddress,
address _stateManagerAddress,
address _voteManagerAddress,
address _jobManagerAddress
) external
function initialize (
address stakeManagerAddress,
address stateManagerAddress,
address voteManagerAddress,
address jobManagerAddress
) external initializer onlyRole(DEFAULT_ADMIN_ROLE)
{
stakeManager = IStakeManager(_stakeManagerAddress);
stateManager = IStateManager(_stateManagerAddress);
voteManager = IVoteManager(_voteManagerAddress);
jobManager = IJobManager(_jobManagerAddress);
stakeManager = IStakeManager(stakeManagerAddress);
stateManager = IStateManager(stateManagerAddress);
voteManager = IVoteManager(voteManagerAddress);
jobManager = IJobManager(jobManagerAddress);
}

function getBlock(uint256 epoch) external view returns(Structs.Block memory _block) {
Expand Down Expand Up @@ -132,10 +133,9 @@ contract BlockManager is ACL, BlockStorage {
uint256[] memory higherCutoffs,
uint256 iteration,
uint256 biggestStakerId
) public checkEpoch(epoch) checkState(Constants.propose())
) public initialized checkEpoch(epoch) checkState(Constants.propose())
{
uint256 proposerId = stakeManager.getStakerId(msg.sender);
// SchellingCoin sch = SchellingCoin(schAddress);
require(isElectedProposer(iteration, biggestStakerId, proposerId), "not elected");
require(
stakeManager.getStaker(proposerId).stake >= Constants.minStake(),
Expand Down Expand Up @@ -176,6 +176,7 @@ contract BlockManager is ACL, BlockStorage {
uint256[] memory sorted
)
public
initialized
checkEpoch(epoch)
checkState(Constants.dispute())
{
Expand Down Expand Up @@ -214,13 +215,13 @@ contract BlockManager is ACL, BlockStorage {
// //if any mistake made during giveSorted, resetDispute and start again
function resetDispute(
uint256 epoch
) public checkEpoch(epoch) checkState(Constants.dispute())
) public initialized checkEpoch(epoch) checkState(Constants.dispute())
{
disputes[epoch][msg.sender] = Structs.Dispute(0, 0, 0, 0, 0, 0);
}

function finalizeDispute (uint256 epoch, uint256 blockId)
public checkEpoch(epoch) checkState(Constants.dispute()) {
public initialized checkEpoch(epoch) checkState(Constants.dispute()) {
uint256 assetId = disputes[epoch][msg.sender].assetId;
require(
disputes[epoch][msg.sender].accWeight == voteManager.getTotalStakeRevealed(epoch, assetId),
Expand All @@ -242,7 +243,7 @@ contract BlockManager is ACL, BlockStorage {
}
}
function confirmBlock() public onlyRole(Constants.getBlockConfirmerHash()) {
function confirmBlock() public initialized onlyRole(Constants.getBlockConfirmerHash()) {
uint256 epoch = stateManager.getEpoch();
for (uint8 i=0; i < proposedBlocks[epoch - 1].length; i++) {
Expand Down Expand Up @@ -273,7 +274,8 @@ contract BlockManager is ACL, BlockStorage {
uint256 stakerId
)
public
view
view
initialized
returns (bool)
{
// generating pseudo random number (range 0..(totalstake - 1)), add (+1) to the result,
Expand Down
7 changes: 3 additions & 4 deletions contracts/Core/JobManager.sol
Expand Up @@ -37,11 +37,10 @@ contract JobManager is ACL, JobStorage {
uint256 timestamp
);

//disable after init.
function init(address _stateManagerAddress) external {
stateManager = IStateManager(_stateManagerAddress);
constructor(address stateManagerAddress) {
stateManager = IStateManager(stateManagerAddress);
}

function createJob(
string calldata url,
string calldata selector,
Expand Down
45 changes: 23 additions & 22 deletions contracts/Core/StakeManager.sol
Expand Up @@ -7,6 +7,7 @@ import "./interface/IVoteManager.sol";
import "./storage/StakeStorage.sol";
import "../lib/Constants.sol";
import "../lib/Constants.sol";
import "../Initializable.sol";
import "../SchellingCoin.sol";
import "./ACL.sol";

Expand All @@ -15,7 +16,7 @@ import "./ACL.sol";
/// @notice StakeManager handles stake, unstake, withdraw, reward, functions
/// for stakers

contract StakeManager is ACL, StakeStorage {
contract StakeManager is Initializable, ACL, StakeStorage {

SchellingCoin public sch;
IVoteManager public voteManager;
Expand Down Expand Up @@ -82,38 +83,37 @@ contract StakeManager is ACL, StakeStorage {
blockReward = _blockReward;
}

/// @param _schAddress The address of the Schelling token ERC20 contract
/// @param _voteManagerAddress The address of the VoteManager contract
/// @param _blockManagerAddress The address of the BlockManager contract
/// @param _stateManagerAddress The address of the StateManager contract
/// todo disable after init
function init (
address _schAddress,
address _voteManagerAddress,
address _blockManagerAddress,
address _stateManagerAddress
) external
/// @param schAddress The address of the Schelling token ERC20 contract
/// @param voteManagersAddress The address of the VoteManager contract
/// @param blockManagerAddress The address of the BlockManager contract
/// @param stateManagerAddress The address of the StateManager contract
function initialize (
address schAddress,
address voteManagersAddress,
address blockManagerAddress,
address stateManagerAddress
) external initializer onlyRole(DEFAULT_ADMIN_ROLE)
{
sch = SchellingCoin(_schAddress);
voteManager = IVoteManager(_voteManagerAddress);
blockManager = IBlockManager(_blockManagerAddress);
stateManager = IStateManager(_stateManagerAddress);
sch = SchellingCoin(schAddress);
voteManager = IVoteManager(voteManagersAddress);
blockManager = IBlockManager(blockManagerAddress);
stateManager = IStateManager(stateManagerAddress);
}

/// @param _id The ID of the staker
/// @param _epochLastRevealed The number of epoch that staker revealed asset values
function setStakerEpochLastRevealed(
uint256 _id,
uint256 _epochLastRevealed
) external onlyRole(Constants.getStakerActivityUpdaterHash())
) external initialized onlyRole(Constants.getStakerActivityUpdaterHash())
{
stakers[_id].epochLastRevealed = _epochLastRevealed;
}

/// @param stakerId The ID of the staker
function updateCommitmentEpoch(
uint256 stakerId
) external onlyRole(Constants.getStakerActivityUpdaterHash())
) external initialized onlyRole(Constants.getStakerActivityUpdaterHash())
{
stakers[stakerId].epochLastCommitted = stateManager.getEpoch();
}
Expand All @@ -132,6 +132,7 @@ contract StakeManager is ACL, StakeStorage {
uint256 amount
)
external
initialized
checkEpoch(epoch) checkState(Constants.commit())
{
// not allowed during reveal period
Expand Down Expand Up @@ -163,7 +164,7 @@ contract StakeManager is ACL, StakeStorage {
/// @notice staker must call unstake() and should wait for Constants.WITHDRAW_LOCK_PERIOD
/// after which she can call withdraw() to finally Withdraw
/// @param epoch The Epoch value for which staker is requesting to unstake
function unstake (uint256 epoch) external checkEpoch(epoch) checkState(Constants.commit()) {
function unstake (uint256 epoch) external initialized checkEpoch(epoch) checkState(Constants.commit()) {
uint256 stakerId = stakerIds[msg.sender];
Structs.Staker storage staker = stakers[stakerId];
require(staker.id != 0, "staker.id = 0");
Expand All @@ -177,7 +178,7 @@ contract StakeManager is ACL, StakeStorage {

/// @notice Helps stakers withdraw their stake if previously unstaked
/// @param epoch The Epoch value for which staker is requesting a withdraw
function withdraw (uint256 epoch) external checkEpoch(epoch) checkState(Constants.commit()) {
function withdraw (uint256 epoch) external initialized checkEpoch(epoch) checkState(Constants.commit()) {
uint256 stakerId = stakerIds[msg.sender];
Structs.Staker storage staker = stakers[stakerId];
require(staker.id != 0, "staker doesnt exist");
Expand Down Expand Up @@ -210,7 +211,7 @@ contract StakeManager is ACL, StakeStorage {
function givePenalties(
uint256 stakerId,
uint256 epoch
) external onlyRole(Constants.getStakeModifierHash())
) external initialized onlyRole(Constants.getStakeModifierHash())
{
_givePenalties(stakerId, epoch);
}
Expand Down Expand Up @@ -248,7 +249,7 @@ contract StakeManager is ACL, StakeStorage {
function giveRewards(
uint256 stakerId,
uint256 epoch
) external onlyRole(Constants.getStakeModifierHash())
) external initialized onlyRole(Constants.getStakeModifierHash())
{
if (stakeGettingReward == 0) return;
Structs.Staker memory thisStaker = stakers[stakerId];
Expand Down
41 changes: 28 additions & 13 deletions contracts/Core/VoteManager.sol
Expand Up @@ -7,14 +7,19 @@ import "./interface/IStateManager.sol";
import "./interface/IBlockManager.sol";
import "./storage/VoteStorage.sol";
import "../lib/Constants.sol";
import "../Initializable.sol";
import "./ACL.sol";


contract VoteManager is VoteStorage {
contract VoteManager is Initializable, ACL, VoteStorage {

IStakeManager public stakeManager;
IStateManager public stateManager;
IBlockManager public blockManager;

event Committed(uint256 epoch, uint256 stakerId, bytes32 commitment, uint256 timestamp);
event Revealed(uint256 epoch, uint256 stakerId, uint256 stake, uint256[] values, uint256 timestamp);

modifier checkEpoch (uint256 epoch) {
require(epoch == stateManager.getEpoch(), "incorrect epoch");
_;
Expand All @@ -25,15 +30,19 @@ contract VoteManager is VoteStorage {
_;
}

function init (address _stakeManagerAddress, address _stateManagerAddress, address _blockManagerAddress) public {
stakeManager = IStakeManager(_stakeManagerAddress);
stateManager = IStateManager(_stateManagerAddress);
blockManager = IBlockManager(_blockManagerAddress);
function initialize (
address stakeManagerAddress,
address stateManagerAddress,
address blockManagerAddress
) external initializer onlyRole(DEFAULT_ADMIN_ROLE)
{
stakeManager = IStakeManager(stakeManagerAddress);
stateManager = IStateManager(stateManagerAddress);
blockManager = IBlockManager(blockManagerAddress);
}


event Committed(uint256 epoch, uint256 stakerId, bytes32 commitment, uint256 timestamp);

function commit(uint256 epoch, bytes32 commitment) public checkEpoch(epoch) checkState(Constants.commit()) {
function commit(uint256 epoch, bytes32 commitment) public initialized checkEpoch(epoch) checkState(Constants.commit()) {
uint256 stakerId = stakeManager.getStakerId(msg.sender);
require(commitments[epoch][stakerId] == 0x0, "already commited");
Structs.Staker memory thisStaker = stakeManager.getStaker(stakerId);
Expand All @@ -53,12 +62,18 @@ contract VoteManager is VoteStorage {
}
}

event Revealed(uint256 epoch, uint256 stakerId, uint256 stake, uint256[] values, uint256 timestamp);

function reveal (uint256 epoch, bytes32 root, uint256[] memory values,
bytes32[][] memory proofs, bytes32 secret, address stakerAddress)
public
checkEpoch(epoch) {
function reveal (
uint256 epoch,
bytes32 root,
uint256[] memory values,
bytes32[][] memory proofs, bytes32 secret,
address stakerAddress
)
public
initialized
checkEpoch(epoch)
{
uint256 thisStakerId = stakeManager.getStakerId(stakerAddress);
require(thisStakerId > 0, "Structs.Staker does not exist");
Structs.Staker memory thisStaker = stakeManager.getStaker(thisStakerId);
Expand Down
7 changes: 0 additions & 7 deletions contracts/Core/interface/IBlockManager.sol
Expand Up @@ -6,13 +6,6 @@ import "../../lib/Structs.sol";

interface IBlockManager {

function init(
address _stakeManagerAddress,
address _stateManagerAddress,
address _voteManagerAddress,
address _jobManagerAddress
) external;

// elected proposer proposes block.
//we use a probabilistic method to elect stakers weighted by stake
// protocol works like this.
Expand Down
3 changes: 0 additions & 3 deletions contracts/Core/interface/IStakeManager.sol
Expand Up @@ -4,8 +4,6 @@ pragma solidity ^0.8.0;
import "../../lib/Structs.sol";

interface IStakeManager {
function init (address _schAddress, address _voteManagerAddress,
address _blockManagerAddress, address _stateManagerAddress) external;

function setStakerEpochLastRevealed(uint256 _id, uint256 _epochLastRevealed) external;
function updateCommitmentEpoch(uint256 stakerId) external;
Expand All @@ -21,5 +19,4 @@ interface IStakeManager {
function getNumStakers() external view returns(uint256);
function getRewardPool() external view returns(uint256);
function getStakeGettingReward() external view returns(uint256);

}
2 changes: 0 additions & 2 deletions contracts/Core/interface/IVoteManager.sol
Expand Up @@ -6,8 +6,6 @@ import "../../lib/Structs.sol";

interface IVoteManager {

function init(address _stakeManagerAddress, address _blockManagerAddress) external;

function commit(uint256 epoch, bytes32 commitment) external;

function reveal(
Expand Down
1 change: 1 addition & 0 deletions contracts/Faucet.sol
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";


Expand Down

0 comments on commit 83a0eff

Please sign in to comment.