Skip to content

Commit

Permalink
Merge v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaStebaev committed Mar 21, 2024
2 parents cd3fb71 + 29d6283 commit 4e3bc99
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 87 deletions.
6 changes: 3 additions & 3 deletions contracts/ConstantsHolder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ contract ConstantsHolder is Permissions, IConstantsHolder {
uint256 public constant ALRIGHT_DELTA = 134161;
uint256 public constant BROADCAST_DELTA = 177490;
uint256 public constant COMPLAINT_BAD_DATA_DELTA = 80995;
uint256 public constant PRE_RESPONSE_DELTA = 100061;
uint256 public constant COMPLAINT_DELTA = 106611;
uint256 public constant RESPONSE_DELTA = 48132;
uint256 public constant PRE_RESPONSE_DELTA = 114511;
uint256 public constant COMPLAINT_DELTA = 203288;
uint256 public constant RESPONSE_DELTA = 55002;

// MSR - Minimum staking requirement
uint256 public msr;
Expand Down
3 changes: 2 additions & 1 deletion contracts/NodeRotation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ contract NodeRotation is Permissions, INodeRotation {
)
public
override
allowTwo("SkaleDKG", "SkaleManager")
allowThree("SkaleDKG", "SkaleManager", "Schains")
returns (uint256 newNode)
{
ISchainsInternal schainsInternal = ISchainsInternal(contractManager.getContract("SchainsInternal"));
Expand Down Expand Up @@ -297,6 +297,7 @@ contract NodeRotation is Permissions, INodeRotation {
}
leavingHistory[nodeIndex].push(LeavingHistory({schainHash: schainHash, finishedRotation: finishTimestamp}));
require(_rotations[schainHash].newNodeIndexes.add(newNodeIndex), "New node was already added");
_rotations[schainHash].nodeIndex = nodeIndex;
_rotations[schainHash].newNodeIndex = newNodeIndex;
_rotations[schainHash].rotationCounter++;
_rotations[schainHash].previousNodes[newNodeIndex] = nodeIndex;
Expand Down
20 changes: 20 additions & 0 deletions contracts/Nodes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,26 @@ contract Nodes is Permissions, INodes {
return nodeExtras[nodeIndex].lastChangeIpTime;
}

function isNodeVisible(uint256 nodeIndex)
external
view
override
checkNodeExists(nodeIndex)
returns (bool visible)
{
return !_invisible[nodeIndex];
}

function getFreeSpace(uint256 nodeIndex)
external
view
override
checkNodeExists(nodeIndex)
returns (uint8 freeSpace)
{
return spaceOfNodes[nodeIndex].freeSpace;
}

/**
* @dev Returns the Validator ID for a given node.
*/
Expand Down
8 changes: 7 additions & 1 deletion contracts/Schains.sol
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,13 @@ contract Schains is Permissions, ISchains {
ISchainsInternal schainsInternal = ISchainsInternal(
contractManager.getContract("SchainsInternal"));
require(schainsInternal.isAnyFreeNode(schainHash), "No free Nodes for new group formation");
uint256 newNodeIndex = nodeRotation.selectNodeToGroup(schainHash);
uint256 newNodeIndex = nodeRotation.rotateNode(
skaleDKG.pendingToBeReplaced(schainHash),
schainHash,
false,
true
);
skaleDKG.resetPendingToBeReplaced(schainHash);
skaleDKG.openChannel(schainHash);
emit NodeAdded(schainHash, newNodeIndex);
}
Expand Down
40 changes: 13 additions & 27 deletions contracts/SchainsInternal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ pragma solidity 0.8.17;
import { EnumerableSetUpgradeable }
from "@openzeppelin/contracts-upgradeable/utils/structs/EnumerableSetUpgradeable.sol";

import { ISkaleDKG } from "@skalenetwork/skale-manager-interfaces/ISkaleDKG.sol";
import { INodes } from "@skalenetwork/skale-manager-interfaces/INodes.sol";
import { ISkaleDKG } from "@skalenetwork/skale-manager-interfaces/ISkaleDKG.sol";

import { IPruningSchainsInternal } from "./interfaces/IPruningSchainInternal.sol";
import { Permissions } from "./Permissions.sol";
import { ConstantsHolder } from "./ConstantsHolder.sol";
import { IPruningSchainsInternal } from "./interfaces/IPruningSchainInternal.sol";
import { IRandom, Random } from "./utils/Random.sol";
import { Permissions } from "./Permissions.sol";


/**
Expand Down Expand Up @@ -182,29 +182,6 @@ contract SchainsInternal is Permissions, IPruningSchainsInternal {
return _generateGroup(schainHash, numberOfNodes);
}

/**
* @dev Allows Schains contract to change the Schain lifetime through
* an additional SKL token deposit.
*
* Requirements:
*
* - Message sender is Schains smart contract
* - Schain must exist
*/
function changeLifetime(
bytes32 schainHash,
uint256 lifetime,
uint256 deposit
)
external
override
allow("Schains")
schainExists(schainHash)
{
schains[schainHash].deposit = schains[schainHash].deposit + deposit;
schains[schainHash].lifetime = schains[schainHash].lifetime + lifetime;
}

/**
* @dev Allows Schains contract to remove an schain from the network.
* Generally schains are not removed from the system; instead they are
Expand Down Expand Up @@ -755,9 +732,18 @@ contract SchainsInternal is Permissions, IPruningSchainsInternal {
* - Schain must exist
*/
function isAnyFreeNode(bytes32 schainHash) external view override schainExists(schainHash) returns (bool free) {
// TODO:
// Move this function to Nodes?
INodes nodes = INodes(contractManager.getContract("Nodes"));
uint8 space = schains[schainHash].partOfNode;
return nodes.countNodesWithFreeSpace(space) > 0;
uint256 numberOfCandidates = nodes.countNodesWithFreeSpace(space);
for (uint256 i = 0; i < _schainToExceptionNodes[schainHash].length; i++) {
uint256 nodeIndex = _schainToExceptionNodes[schainHash][i];
if (space <= nodes.getFreeSpace(nodeIndex) && nodes.isNodeVisible(nodeIndex)) {
--numberOfCandidates;
}
}
return numberOfCandidates > 0;
}

/**
Expand Down
15 changes: 8 additions & 7 deletions contracts/SkaleDKG.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ contract SkaleDKG is Permissions, ISkaleDKG {

mapping(bytes32 => uint256) private _badNodes;

mapping(bytes32 => uint256) public override pendingToBeReplaced;

modifier correctGroup(bytes32 schainHash) {
require(channels[schainHash].active, "Group is not created");
_;
Expand Down Expand Up @@ -320,6 +322,10 @@ contract SkaleDKG is Permissions, ISkaleDKG {
_badNodes[schainHash] = nodeIndex;
}

function resetPendingToBeReplaced(bytes32 schainHash) external override allow("Schains") {
delete pendingToBeReplaced[schainHash];
}

function finalizeSlashing(bytes32 schainHash, uint256 badNode) external override allow("SkaleDKG") {
INodeRotation nodeRotation = INodeRotation(contractManager.getContract("NodeRotation"));
ISchainsInternal schainsInternal = ISchainsInternal(
Expand All @@ -328,7 +334,6 @@ contract SkaleDKG is Permissions, ISkaleDKG {
emit BadGuy(badNode);
emit FailedDKG(schainHash);

schainsInternal.makeSchainNodesInvisible(schainHash);
if (schainsInternal.isAnyFreeNode(schainHash)) {
uint256 newNode = nodeRotation.rotateNode(
badNode,
Expand All @@ -338,14 +343,10 @@ contract SkaleDKG is Permissions, ISkaleDKG {
);
emit NewGuy(newNode);
} else {
pendingToBeReplaced[schainHash] = badNode;
_openChannel(schainHash);
schainsInternal.removeNodeFromSchain(
badNode,
schainHash
);
channels[schainHash].active = false;
}
schainsInternal.makeSchainNodesVisible(schainHash);
IPunisher(contractManager.getPunisher()).slash(
INodes(contractManager.getContract("Nodes")).getValidatorId(badNode),
ISlashingTable(contractManager.getContract("SlashingTable")).getPenalty("FailedDKG")
Expand Down Expand Up @@ -595,7 +596,7 @@ contract SkaleDKG is Permissions, ISkaleDKG {
);
} else if (context.dkgFunction == DkgFunction.Response){
wallets.refundGasBySchain(
schainHash, payable(msg.sender), gasTotal - gasleft() - context.delta, context.isDebt
schainHash, payable(msg.sender), gasTotal - gasleft() + context.delta, context.isDebt
);
} else {
wallets.refundGasBySchain(
Expand Down
1 change: 1 addition & 0 deletions dictionaries/libraries.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mulmod
muln
nbconvert
nbformat
nomicfoundation
nomiclabs
pygments
pyplot
Expand Down
2 changes: 1 addition & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { task, HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-chai-matchers";
import "@nomiclabs/hardhat-etherscan";
import "@nomiclabs/hardhat-waffle";
import "@openzeppelin/hardhat-upgrades";
import '@typechain/hardhat'
import "solidity-coverage";
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@openzeppelin/contracts": "^4.9.3",
"@openzeppelin/contracts-upgradeable": "^4.9.3",
"@openzeppelin/hardhat-upgrades": "^1.14.0",
"@skalenetwork/skale-manager-interfaces": "3.0.0",
"@skalenetwork/skale-manager-interfaces": "3.0.0-develop.1",
"@skalenetwork/upgrade-tools": "^2.0.1",
"@typechain/hardhat": "^7.0.0",
"dotenv": "^16.3.1",
Expand All @@ -53,8 +53,8 @@
"hardhat": "2.11.0 - 2.16.1"
},
"devDependencies": {
"@nomicfoundation/hardhat-chai-matchers": "<2.0.0",
"@nomiclabs/hardhat-etherscan": "^3.1.0",
"@nomiclabs/hardhat-waffle": "^2.0.2",
"@typechain/ethers-v5": "^11.1.1",
"@types/chai": "^4.3.6",
"@types/chai-almost": "^1.0.1",
Expand All @@ -64,6 +64,7 @@
"@types/mocha": "^9.1.1",
"@types/node": "^20.8.7",
"@types/sinon-chai": "^3.2.9",
"@types/underscore": "^1.11.15",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"bignumber.js": "^9.1.2",
Expand Down
7 changes: 0 additions & 7 deletions test/SchainsInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,6 @@ describe("SchainsInternal", () => {
totalResources.should.be.equal(64);
});

it("should change schain lifetime", async () => {
await schainsInternal.changeLifetime(schainNameHash, 7, 8);
const schain = await schainsInternal.schains(schainNameHash);
schain.lifetime.should.be.equal(12);
schain.deposit.should.be.equal(13);
});

describe("on registered schain", () => {
const nodeIndex = 0;
const numberOfNewSchains = 5
Expand Down
32 changes: 20 additions & 12 deletions test/SkaleDKG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1409,7 +1409,7 @@ describe("SkaleDKG", () => {
badEncryptedSecretKeyContributions[indexes[0]]
)
);

const response = await skaleDKG.connect(validators[0].nodeAddress).response(
stringKeccak256(schainName),
0,
Expand Down Expand Up @@ -1498,7 +1498,7 @@ describe("SkaleDKG", () => {
),
"Broadcast 1"
);

await reimbursed(
await skaleDKG.connect(validators[1].nodeAddress).broadcast(
stringKeccak256(schainName),
Expand All @@ -1509,7 +1509,7 @@ describe("SkaleDKG", () => {
),
"Broadcast 2"
);

const complaintBadData = await skaleDKG.connect(validators[1].nodeAddress).complaintBadData(
stringKeccak256(schainName),
1,
Expand All @@ -1533,7 +1533,7 @@ describe("SkaleDKG", () => {
),
"Pre response"
);

const responseTx = await skaleDKG.connect(validators[0].nodeAddress).response(
stringKeccak256(schainName),
0,
Expand Down Expand Up @@ -1928,7 +1928,7 @@ describe("SkaleDKG", () => {
),
"Alright"
);

alrightPoss = await skaleDKG.connect(validators[index].nodeAddress).isAlrightPossible(
stringKeccak256("New16NodeSchain"),
i
Expand Down Expand Up @@ -2381,7 +2381,7 @@ describe("SkaleDKG", () => {
accusedNode
)
);

const complaint = await skaleDKG.connect(validators[0].nodeAddress).complaint(
stringKeccak256("New16NodeSchain"),
8,
Expand Down Expand Up @@ -2454,8 +2454,8 @@ describe("SkaleDKG", () => {
rotation.rotationCounter
);
}
const accusedNode = "15";
const complaintNode = "7";
const accusedNode = 15;
const complaintNode = 7;
await skipTime(1800);
await reimbursed(
await skaleDKG.connect(validators[0].nodeAddress).complaint(
Expand All @@ -2465,7 +2465,8 @@ describe("SkaleDKG", () => {
)
);
const space = await nodes.spaceOfNodes(accusedNode);
space.freeSpace.should.be.equal(128);
// The node is still a part of schain because can't be replaced
space.freeSpace.should.be.equal(0);

const complaint = await skaleDKG.connect(validators[0].nodeAddress).complaint(
stringKeccak256("New16NodeSchain"),
Expand All @@ -2486,11 +2487,18 @@ describe("SkaleDKG", () => {
domainName: "some.domain.name"
}
);
const createdNode = 16;
await schains.restartSchainCreation("New16NodeSchain");


rotation = await nodeRotation.getRotation(stringKeccak256("New16NodeSchain"));

expect(rotation.rotationCounter).to.be.equal(1);
expect(rotation.nodeIndex).to.be.equal(accusedNode);
expect(rotation.newNodeIndex).to.be.equal(createdNode);

for (let i = 0; i < 17; i++) {
if (i.toString() === accusedNode) {
if (i === accusedNode) {
continue;
}
let index = 0;
Expand All @@ -2507,7 +2515,7 @@ describe("SkaleDKG", () => {
}
let comPubKey;
for (let i = 0; i < 17; i++) {
if (i.toString() === accusedNode) {
if (i === accusedNode) {
continue;
}
comPubKey = await keyStorage.getCommonPublicKey(stringKeccak256("New16NodeSchain"));
Expand Down Expand Up @@ -2605,7 +2613,7 @@ describe("SkaleDKG", () => {
accusedNode
)
);

const complaint = await skaleDKG.connect(validators[0].nodeAddress).complaint(
stringKeccak256("New16NodeSchain"),
8,
Expand Down
Loading

0 comments on commit 4e3bc99

Please sign in to comment.