Skip to content

Commit

Permalink
Merge pull request #86 from skalenetwork/feature/SKALE-215-safemath
Browse files Browse the repository at this point in the history
Feature/skale 215 safemath
  • Loading branch information
yavrsky committed Feb 13, 2020
2 parents 54487fe + f85950c commit 5f65668
Show file tree
Hide file tree
Showing 29 changed files with 168 additions and 208 deletions.
87 changes: 17 additions & 70 deletions buidler.config.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions contracts/ECDH.sol
Expand Up @@ -18,9 +18,11 @@
*/

pragma solidity ^0.5.3;
import "@openzeppelin/contracts/math/SafeMath.sol";


contract ECDH {
using SafeMath for uint256;

uint256 constant GX = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798;
uint256 constant GY = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8;
Expand Down Expand Up @@ -88,7 +90,7 @@ contract ECDH {
pure
returns (uint256 x3, uint256 z3)
{
(x3, z3) = (addmod(mulmod(z2, x1, N), mulmod(N - x2, z1, N), N), mulmod(z1, z2, N));
(x3, z3) = (addmod(mulmod(z2, x1, N), mulmod(N.sub(x2), z1, N), N), mulmod(z1, z2, N));
}

function jMul(
Expand Down Expand Up @@ -124,9 +126,9 @@ contract ECDH {
uint256 newR = a;
uint256 q;
while (newR != 0) {
q = r / newR;
(t, newT) = (newT, addmod(t, (N - mulmod(q, newT, N)), N));
(r, newR) = (newR, r - q * newR);
q = r.div(newR);
(t, newT) = (newT, addmod(t, (N.sub(mulmod(q, newT, N))), N));
(r, newR) = (newR, r.sub(q.mul(newR)));
}
return t;
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/ERC777/LockableERC777.sol
Expand Up @@ -433,7 +433,7 @@ contract LockableERC777 is IERC777, IERC20 {
// Property of the company SKALE Labs inc.---------------------------------
uint locked = _getLockedOf(from);
if (locked > 0) {
require(_balances[from] >= locked + amount, "Token should be unlocked for transferring");
require(_balances[from] >= locked.add(amount), "Token should be unlocked for transferring");
}
//-------------------------------------------------------------------------
_balances[from] = _balances[from].sub(amount);
Expand Down
2 changes: 1 addition & 1 deletion contracts/MonitorsData.sol
Expand Up @@ -47,7 +47,7 @@ contract MonitorsData is GroupsData {
require(data.length >= indexLength, "data is too small");
for (uint i = 0; i < checkedNodes[monitorIndex].length; ++i) {
require(checkedNodes[monitorIndex][i].length >= indexLength, "checked nodes data is too small");
uint shift = (32 - indexLength) * 8;
uint shift = (32 - indexLength).mul(8);
bool equalIndex = checkedNodes[monitorIndex][i] >> shift == data >> shift;
if (equalIndex) {
checkedNodes[monitorIndex][i] = data;
Expand Down
20 changes: 10 additions & 10 deletions contracts/MonitorsFunctionality.sol
Expand Up @@ -138,7 +138,7 @@ contract MonitorsFunctionality is GroupsFunctionality {
MonitorsData data = MonitorsData(contractManager.getContract("MonitorsData"));
data.removeCheckedNode(monitorIndex, index);
address constantsAddress = contractManager.getContract("ConstantsHolder");
bool receiveVerdict = time + IConstants(constantsAddress).deltaPeriod() > uint32(block.timestamp);
bool receiveVerdict = time.add(IConstants(constantsAddress).deltaPeriod()) > uint32(block.timestamp);
if (receiveVerdict) {
data.addVerdict(keccak256(abi.encodePacked(toNodeIndex)), downtime, latency);
}
Expand Down Expand Up @@ -211,22 +211,22 @@ contract MonitorsFunctionality is GroupsFunctionality {
uint exceptionNode = uint(IGroupsData(dataAddress).getGroupData(groupIndex));
uint[] memory activeNodes = INodesData(nodesDataAddress).getActiveNodeIds();
uint numberOfNodesInGroup = IGroupsData(dataAddress).getRecommendedNumberOfNodes(groupIndex);
uint availableAmount = activeNodes.length - (INodesData(nodesDataAddress).isNodeActive(exceptionNode) ? 1 : 0);
uint availableAmount = activeNodes.length.sub((INodesData(nodesDataAddress).isNodeActive(exceptionNode)) ? 1 : 0);
if (numberOfNodesInGroup > availableAmount) {
numberOfNodesInGroup = availableAmount;
}
uint[] memory nodesInGroup = new uint[](numberOfNodesInGroup);
uint ignoringTail = 0;
uint random = uint(keccak256(abi.encodePacked(uint(blockhash(block.number - 1)), groupIndex)));
for (uint i = 0; i < nodesInGroup.length; ++i) {
uint index = random % (activeNodes.length - ignoringTail);
uint index = random % (activeNodes.length.sub(ignoringTail));
if (activeNodes[index] == exceptionNode) {
swap(activeNodes, index, activeNodes.length - ignoringTail - 1);
swap(activeNodes, index, activeNodes.length.sub(ignoringTail) - 1);
++ignoringTail;
index = random % (activeNodes.length - ignoringTail);
index = random % (activeNodes.length.sub(ignoringTail));
}
nodesInGroup[i] = activeNodes[index];
swap(activeNodes, index, activeNodes.length - ignoringTail - 1);
swap(activeNodes, index, activeNodes.length.sub(ignoringTail) - 1);
++ignoringTail;
IGroupsData(dataAddress).setNodeInGroup(groupIndex, nodesInGroup[i]);
}
Expand All @@ -253,8 +253,8 @@ contract MonitorsFunctionality is GroupsFunctionality {
uint numberOfActiveNodes = INodesData(nodesDataAddress).numberOfActiveNodes();
uint numberOfExceptionNodes = (INodesData(nodesDataAddress).isNodeActive(uint(groupData)) ? 1 : 0);
uint recommendedNumberOfNodes = IGroupsData(dataAddress).getRecommendedNumberOfNodes(groupIndex);
finish = (recommendedNumberOfNodes > numberOfActiveNodes - numberOfExceptionNodes ?
numberOfActiveNodes - numberOfExceptionNodes : recommendedNumberOfNodes);
finish = (recommendedNumberOfNodes > numberOfActiveNodes.sub(numberOfExceptionNodes) ?
numberOfActiveNodes.sub(numberOfExceptionNodes) : recommendedNumberOfNodes);
}
function comparator(bytes32 groupIndex, uint indexOfNode) internal view returns (bool) {
Expand Down Expand Up @@ -298,7 +298,7 @@ contract MonitorsFunctionality is GroupsFunctionality {
function quickSort(uint32[] memory array, uint left, uint right) internal pure {
uint leftIndex = left;
uint rightIndex = right;
uint32 middle = array[(right + left) / 2];
uint32 middle = array[(right.add(left)) / 2];
while (leftIndex <= rightIndex) {
while (array[leftIndex] < middle) {
leftIndex++;
Expand Down Expand Up @@ -337,7 +337,7 @@ contract MonitorsFunctionality is GroupsFunctionality {
bytes memory tempData = new bytes(32);
bytes14 bytesOfIndex = bytes14(uint112(nodeIndex));
bytes14 bytesOfTime = bytes14(
uint112(INodesData(nodesDataAddress).getNodeNextRewardDate(nodeIndex) - IConstants(constantsAddress).deltaPeriod())
uint112(INodesData(nodesDataAddress).getNodeNextRewardDate(nodeIndex).sub(IConstants(constantsAddress).deltaPeriod()))
);
bytes4 ip = INodesData(nodesDataAddress).getNodeIP(nodeIndex);
assembly {
Expand Down
6 changes: 3 additions & 3 deletions contracts/NodesData.sol
Expand Up @@ -103,7 +103,7 @@ contract NodesData is INodesData, Permissions {
function countNodesWithFreeSpace(uint8 freeSpace) external view returns (uint count) {
count = 0;
for (uint8 i = freeSpace; i <= 128; ++i) {
count += spaceToNodes[i].length;
count = count.add(spaceToNodes[i].length);
}
}

Expand Down Expand Up @@ -276,7 +276,7 @@ contract NodesData is INodesData, Permissions {
*/
function isTimeForReward(uint nodeIndex) external view returns (bool) {
address constantsAddress = contractManager.getContract("ConstantsHolder");
return nodes[nodeIndex].lastRewardDate + IConstants(constantsAddress).rewardPeriod() <= block.timestamp;
return nodes[nodeIndex].lastRewardDate.add(IConstants(constantsAddress).rewardPeriod()) <= block.timestamp;
}

/**
Expand Down Expand Up @@ -355,7 +355,7 @@ contract NodesData is INodesData, Permissions {
* @return number of active nodes plus number of leaving nodes
*/
function getNumberOnlineNodes() external view returns (uint) {
return numberOfActiveNodes + numberOfLeavingNodes;
return numberOfActiveNodes.add(numberOfLeavingNodes);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions contracts/Permissions.sol
Expand Up @@ -20,13 +20,16 @@
pragma solidity ^0.5.3;

import "./ContractManager.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";


/**
* @title Permissions - connected module for Upgradeable approach, knows ContractManager
* @author Artem Payvin
*/
contract Permissions is Ownable {
using SafeMath for uint;
using SafeMath for uint32;

ContractManager contractManager;

Expand Down
21 changes: 10 additions & 11 deletions contracts/Pricing.sol
Expand Up @@ -41,25 +41,24 @@ contract Pricing is Permissions {
}

function adjustPrice() external {
require(now > lastUpdated + COOLDOWN_TIME, "It's not a time to update a price");
require(now > lastUpdated.add(COOLDOWN_TIME), "It's not a time to update a price");
checkAllNodes();
uint loadPercentage = getTotalLoadPercentage();
uint priceChange;
uint timeSkipped;
if (loadPercentage < OPTIMAL_LOAD_PERCENTAGE) {
priceChange = (ADJUSTMENT_SPEED * price) * (OPTIMAL_LOAD_PERCENTAGE - loadPercentage) / 10**6;
timeSkipped = (now - lastUpdated) / COOLDOWN_TIME;
require(price - priceChange * timeSkipped < price, "New price should be less than old price");
price -= priceChange * timeSkipped;
priceChange = (ADJUSTMENT_SPEED.mul(price)).mul((OPTIMAL_LOAD_PERCENTAGE.sub(loadPercentage))) / 10**6;
timeSkipped = (now.sub(lastUpdated)).div(COOLDOWN_TIME);
price = price.sub(priceChange.mul(timeSkipped));
if (price < MIN_PRICE) {
price = MIN_PRICE;
}
} else {
priceChange = (ADJUSTMENT_SPEED * price) * (loadPercentage - OPTIMAL_LOAD_PERCENTAGE) / 10**6;
timeSkipped = (now - lastUpdated) / COOLDOWN_TIME;
require(price + priceChange * timeSkipped > price, "New price should be greater than old price");
price += priceChange * timeSkipped;
priceChange = (ADJUSTMENT_SPEED.mul(price)).mul((loadPercentage.sub(OPTIMAL_LOAD_PERCENTAGE))) / 10**6;
timeSkipped = (now.sub(lastUpdated)).div(COOLDOWN_TIME);
require(price.add(priceChange.mul(timeSkipped)) > price, "New price should be greater than old price");
price = price.add(priceChange.mul(timeSkipped));
}
lastUpdated = now;
}
Expand Down Expand Up @@ -89,8 +88,8 @@ contract Pricing is Permissions {
bytes32 schain = SchainsData(schainsDataAddress).schainsAtSystem(i);
uint numberOfNodesInGroup = IGroupsData(schainsDataAddress).getNumberOfNodesInGroup(schain);
uint part = SchainsData(schainsDataAddress).getSchainsPartOfNode(schain);
sumLoadSchain += (numberOfNodesInGroup*10**7)/part;
sumLoadSchain = sumLoadSchain.add((numberOfNodesInGroup*10**7).div(part));
}
return uint(sumLoadSchain/(10**5*numberOfNodes));
return uint(sumLoadSchain.div(10**5*numberOfNodes));
}
}
8 changes: 4 additions & 4 deletions contracts/SchainsData.sol
Expand Up @@ -129,7 +129,7 @@ contract SchainsData is GroupsData {
function setSchainPartOfNode(bytes32 schainId, uint8 partOfNode) external allow(executorName) {
schains[schainId].partOfNode = partOfNode;
if (partOfNode > 0) {
sumOfSchainsResources += (128 / partOfNode) * groups[schainId].nodesInGroup.length;
sumOfSchainsResources = sumOfSchainsResources.add((128 / partOfNode) * groups[schainId].nodesInGroup.length);
}
}

Expand All @@ -141,8 +141,8 @@ contract SchainsData is GroupsData {
* @param deposit - amount of SKL which payed for this time
*/
function changeLifetime(bytes32 schainId, uint lifetime, uint deposit) external allow("SchainsFunctionality") {
schains[schainId].deposit += deposit;
schains[schainId].lifetime += lifetime;
schains[schainId].deposit = schains[schainId].deposit.add(deposit);
schains[schainId].lifetime = schains[schainId].lifetime.add(lifetime);
}

/**
Expand Down Expand Up @@ -284,7 +284,7 @@ contract SchainsData is GroupsData {
* @return if expired - true, else - false
*/
function isTimeExpired(bytes32 schainId) external view returns (bool) {
return schains[schainId].startDate + schains[schainId].lifetime < block.timestamp;
return schains[schainId].startDate.add(schains[schainId].lifetime) < block.timestamp;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions contracts/SchainsFunctionality.sol
Expand Up @@ -213,9 +213,9 @@ contract SchainsFunctionality is Permissions, ISchainsFunctionality {
if (divisor == 0) {
return 1e18;
} else {
uint up = nodeDeposit * numberOfNodes * 2 * lifetime;
uint down = uint(uint(IConstants(constantsAddress).TINY_DIVISOR() / divisor) * uint(IConstants(constantsAddress).SECONDS_TO_YEAR()));
return up / down;
uint up = nodeDeposit.mul(numberOfNodes.mul(lifetime.mul(2)));
uint down = uint(uint(IConstants(constantsAddress).TINY_DIVISOR()).div(divisor).mul(uint(IConstants(constantsAddress).SECONDS_TO_YEAR())));
return up.div(down);
}
}

Expand Down
6 changes: 3 additions & 3 deletions contracts/SchainsFunctionalityInternal.sol
Expand Up @@ -139,7 +139,7 @@ contract SchainsFunctionalityInternal is GroupsFunctionality {
}
}
if (counter < nodesWithFreeSpace.length) {
result = new uint[](nodesWithFreeSpace.length - counter);
result = new uint[](nodesWithFreeSpace.length.sub(counter));
counter = 0;
for (uint i = 0; i < nodesWithFreeSpace.length; i++) {
if (!groupsData.isExceptionNode(groupIndex, nodesWithFreeSpace[i]) && nodesData.isNodeActive(nodesWithFreeSpace[i])) {
Expand Down Expand Up @@ -218,10 +218,10 @@ contract SchainsFunctionalityInternal is GroupsFunctionality {
uint ignoringTail = 0;
uint random = uint(keccak256(abi.encodePacked(uint(blockhash(block.number - 1)), groupIndex)));
for (uint i = 0; i < nodesInGroup.length; ++i) {
uint index = random % (possibleNodes.length - ignoringTail);
uint index = random % (possibleNodes.length.sub(ignoringTail));
uint node = possibleNodes[index];
nodesInGroup[i] = node;
swap(possibleNodes, index, possibleNodes.length - ignoringTail - 1);
swap(possibleNodes, index, possibleNodes.length.sub(ignoringTail) - 1);
++ignoringTail;

groupsData.setException(groupIndex, node);
Expand Down

0 comments on commit 5f65668

Please sign in to comment.