Skip to content

Commit

Permalink
Merge pull request #112 from skalenetwork/feature/SKALE-1642-delegation
Browse files Browse the repository at this point in the history
Feature/skale 1642 delegation
  • Loading branch information
DimaStebaev authored Mar 3, 2020
2 parents e8e88e3 + f0c81ad commit a3ddef5
Show file tree
Hide file tree
Showing 54 changed files with 2,434 additions and 1,843 deletions.
8 changes: 4 additions & 4 deletions .openzeppelin/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
"contracts": {
"DelegationController": "DelegationController",
"DelegationPeriodManager": "DelegationPeriodManager",
"DelegationRequestManager": "DelegationRequestManager",
"DelegationService": "DelegationService",
"Distributor": "Distributor",
"SkaleBalances": "SkaleBalances",
"TimeHelpers": "TimeHelpers",
"TokenSaleManager": "TokenSaleManager",
"TokenLaunchManager": "TokenLaunchManager",
"TokenState": "TokenState",
"ValidatorService": "ValidatorService",
"ContractManager": "ContractManager",
Expand All @@ -27,7 +25,9 @@
"ManagerData": "ManagerData",
"SkaleManager": "SkaleManager",
"Pricing": "Pricing",
"SlashingTable": "SlashingTable"
"SlashingTable": "SlashingTable",
"Punisher": "Punisher",
"TokenLaunchLocker": "TokenLaunchLocker"
},
"dependencies": {},
"name": "skale-manager",
Expand Down
3 changes: 2 additions & 1 deletion .soliumrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"function-order": "error",
"uppercase": "error",
"error-reason": "error",
"security/no-inline-assembly": "warning"
"security/no-inline-assembly": "warning",
"security/no-block-members": "off"
}
}
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0
1.1.0
7 changes: 7 additions & 0 deletions contracts/ConstantsHolder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ contract ConstantsHolder is IConstants, Permissions {

//Need to add minimal allowed parameters for verdicts

uint public launchTimestamp;

uint public rotationDelay;

/**
Expand Down Expand Up @@ -158,6 +160,10 @@ contract ConstantsHolder is IConstants, Permissions {
msr = newMSR;
}

function setLaunchTimestamp(uint timestamp) external onlyOwner {
launchTimestamp = timestamp;
}

function setRotationDelay(uint newDelay) external onlyOwner {
rotationDelay = newDelay;
}
Expand All @@ -176,6 +182,7 @@ contract ConstantsHolder is IConstants, Permissions {
checkTime = 120; // Test parameters
lastTimeUnderloaded = 0;
lastTimeOverloaded = 0;
launchTimestamp = now;
rotationDelay = 12 hours;
}
}
17 changes: 10 additions & 7 deletions contracts/ERC777/LockableERC777.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/introspection/IERC1820Registry.sol";
// Added by SKALE
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
// --------------


/**
* @dev Implementation of the `IERC777` interface.
* Modified version to support delegation
*/
contract LockableERC777 is IERC777, IERC20 {
contract LockableERC777 is IERC777, IERC20 /* Added by SKALE */, ReentrancyGuard { // End of added by SKALE
using SafeMath for uint256;
using Address for address;

Expand Down Expand Up @@ -88,7 +91,7 @@ contract LockableERC777 is IERC777, IERC20 {
*
* Also emits a `Sent` event.
*/
function transfer(address recipient, uint256 amount) external returns (bool) {
function transfer(address recipient, uint256 amount) external /* Added by SKALE */ nonReentrant /* End of added by SKALE */ returns (bool) {
require(recipient != address(0), "ERC777: transfer to the zero address");

address from = msg.sender;
Expand Down Expand Up @@ -204,7 +207,7 @@ contract LockableERC777 is IERC777, IERC20 {
*
* Emits `Sent` and `Transfer` events.
*/
function transferFrom(address holder, address recipient, uint256 amount) external returns (bool) {
function transferFrom(address holder, address recipient, uint256 amount) external /* Added by SKALE */ nonReentrant /*End*/ returns (bool) {
require(recipient != address(0), "ERC777: transfer to the zero address");
require(holder != address(0), "ERC777: transfer from the zero address");

Expand Down Expand Up @@ -347,7 +350,7 @@ contract LockableERC777 is IERC777, IERC20 {
}

// Added by SKALE
function _getLockedOf(address wallet) internal returns (uint);
function _getAndUpdateLockedAmount(address wallet) internal returns (uint);
// --------------

/**
Expand All @@ -370,6 +373,7 @@ contract LockableERC777 is IERC777, IERC20 {
bool requireReceptionAck
)
private
/* Added by SKALE */ nonReentrant /* End of added by SKALE */
{
require(from != address(0), "ERC777: send from the zero address");
require(to != address(0), "ERC777: send to the zero address");
Expand Down Expand Up @@ -406,7 +410,7 @@ contract LockableERC777 is IERC777, IERC20 {
{
require(from != address(0), "ERC777: burn from the zero address");
// Added by SKALE----------------------------------------------------------
uint locked = _getLockedOf(from);
uint locked = _getAndUpdateLockedAmount(from);
if (locked > 0) {
require(_balances[from] >= locked.add(amount), "Token should be unlocked for burning");
}
Expand All @@ -420,7 +424,6 @@ contract LockableERC777 is IERC777, IERC20 {
_totalSupply = _totalSupply.sub(amount);
_balances[from] = _balances[from].sub(amount);


emit Burned(
operator, from, amount, data, operatorData
);
Expand All @@ -438,7 +441,7 @@ contract LockableERC777 is IERC777, IERC20 {
private
{
// Added by SKALE----------------------------------------------------------
uint locked = _getLockedOf(from);
uint locked = _getAndUpdateLockedAmount(from);
if (locked > 0) {
require(_balances[from] >= locked.add(amount), "Token should be unlocked for transferring");
}
Expand Down
6 changes: 3 additions & 3 deletions contracts/SkaleDKG.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import "./interfaces/IGroupsData.sol";
import "./interfaces/INodesData.sol";
import "./interfaces/ISchainsFunctionality.sol";
import "./interfaces/ISchainsFunctionalityInternal.sol";
import "./delegation/DelegationService.sol";
import "./delegation/Punisher.sol";
import "./NodesData.sol";
import "./SlashingTable.sol";
import "./SchainsFunctionality.sol";
Expand Down Expand Up @@ -353,11 +353,11 @@ contract SkaleDKG is Permissions {
delete channels[groupIndex];
}

DelegationService delegationService = DelegationService(contractManager.getContract("DelegationService"));
Punisher punisher = Punisher(contractManager.getContract("Punisher"));
NodesData nodesData = NodesData(contractManager.getContract("NodesData"));
SlashingTable slashingTable = SlashingTable(contractManager.getContract("SlashingTable"));

delegationService.slash(nodesData.getValidatorId(badNode), slashingTable.getPenalty("FailedDKG"));
punisher.slash(nodesData.getValidatorId(badNode), slashingTable.getPenalty("FailedDKG"));
}

function verify(
Expand Down
33 changes: 15 additions & 18 deletions contracts/SkaleManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import "./interfaces/INodesFunctionality.sol";
import "./interfaces/ISchainsFunctionality.sol";
import "./interfaces/IManagerData.sol";
import "./delegation/DelegationService.sol";
import "./delegation/Distributor.sol";
import "./delegation/ValidatorService.sol";
import "./MonitorsFunctionality.sol";
import "./NodesFunctionality.sol";
Expand Down Expand Up @@ -55,25 +56,23 @@ contract SkaleManager is IERC777Recipient, Permissions {
);

function tokensReceived(
address operator,
address, // operator
address from,
address to,
uint256 value,
bytes calldata userData,
bytes calldata operatorData
bytes calldata // operator data
)
external
allow("SkaleToken")
{
if (from == contractManager.getContract("SkaleBalances")) {
// skip parsing of user data
return;
}

TransactionOperation operationType = fallbackOperationTypeConvert(userData);
if (operationType == TransactionOperation.CreateSchain) {
address schainsFunctionalityAddress = contractManager.getContract("SchainsFunctionality");
ISchainsFunctionality(schainsFunctionalityAddress).addSchain(from, value, userData);
require(to == address(this), "Receiver is incorrect");
if (userData.length > 0) {
TransactionOperation operationType = fallbackOperationTypeConvert(userData);
if (operationType == TransactionOperation.CreateSchain) {
address schainsFunctionalityAddress = contractManager.getContract("SchainsFunctionality");
ISchainsFunctionality(schainsFunctionalityAddress).addSchain(from, value, userData);
}
}
}

Expand Down Expand Up @@ -202,8 +201,7 @@ contract SkaleManager is IERC777Recipient, Permissions {
msg.sender,
nodeIndex,
averageDowntime,
averageLatency,
nodesDataAddress);
averageLatency);
INodesData(nodesDataAddress).changeNodeLastRewardDate(nodeIndex);
monitorsFunctionality.upgradeMonitor(nodeIndex);
emit BountyGot(
Expand All @@ -226,8 +224,7 @@ contract SkaleManager is IERC777Recipient, Permissions {
address from,
uint nodeIndex,
uint32 downtime,
uint32 latency,
address nodesDataAddress) internal returns (uint)
uint32 latency) internal returns (uint)
{
uint commonBounty;
IConstants constants = IConstants(contractManager.getContract("ConstantsHolder"));
Expand Down Expand Up @@ -276,14 +273,14 @@ contract SkaleManager is IERC777Recipient, Permissions {
function payBounty(uint bountyForMiner, address miner, uint nodeIndex) internal returns (bool) {
ValidatorService validatorService = ValidatorService(contractManager.getContract("ValidatorService"));
SkaleToken skaleToken = SkaleToken(contractManager.getContract("SkaleToken"));
DelegationService delegationService = DelegationService(contractManager.getContract("DelegationService"));
Distributor distributor = Distributor(contractManager.getContract("Distributor"));

uint validatorId = validatorService.getValidatorId(miner);
uint bounty = bountyForMiner;
if (!validatorService.checkPossibilityToMaintainNode(validatorId, nodeIndex)) {
bounty /= 2;
}
delegationService.withdrawBounty(address(this), bounty);
skaleToken.send(address(delegationService), bounty, abi.encode(validatorId));
skaleToken.send(address(distributor), bounty, abi.encode(validatorId));
}

function fallbackOperationTypeConvert(bytes memory data) internal pure returns (TransactionOperation) {
Expand Down
19 changes: 10 additions & 9 deletions contracts/SkaleToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ pragma solidity ^0.5.3;
import "./ERC777/LockableERC777.sol";
import "./Permissions.sol";
import "./interfaces/delegation/IDelegatableToken.sol";
import "./delegation/DelegationService.sol";
import "./delegation/Punisher.sol";
import "./delegation/TokenState.sol";


/**
Expand Down Expand Up @@ -88,21 +89,21 @@ contract SkaleToken is LockableERC777, Permissions, IDelegatableToken {
return true;
}

function getDelegatedOf(address wallet) external returns (uint) {
return DelegationService(contractManager.getContract("DelegationService")).getDelegatedOf(wallet);
function getAndUpdateDelegatedAmount(address wallet) external returns (uint) {
return DelegationController(contractManager.getContract("DelegationController")).getAndUpdateDelegatedAmount(wallet);
}

function getSlashedOf(address wallet) external returns (uint) {
return DelegationService(contractManager.getContract("DelegationService")).getSlashedOf(wallet);
function getAndUpdateSlashedAmount(address wallet) external returns (uint) {
return Punisher(contractManager.getContract("Punisher")).getAndUpdateLockedAmount(wallet);
}

function getLockedOf(address wallet) public returns (uint) {
return DelegationService(contractManager.getContract("DelegationService")).getLockedOf(wallet);
function getAndUpdateLockedAmount(address wallet) public returns (uint) {
return TokenState(contractManager.getContract("TokenState")).getAndUpdateLockedAmount(wallet);
}

// private

function _getLockedOf(address wallet) internal returns (uint) {
return getLockedOf(wallet);
function _getAndUpdateLockedAmount(address wallet) internal returns (uint) {
return getAndUpdateLockedAmount(wallet);
}
}
Loading

0 comments on commit a3ddef5

Please sign in to comment.