Skip to content

Commit

Permalink
Merge pull request #109 from skalenetwork/enhancement/SKALE-2243-impr…
Browse files Browse the repository at this point in the history
…ove-node-rotation

Enhancement/skale 2243 improve node rotation
  • Loading branch information
payvint authored Mar 3, 2020
2 parents f71a345 + bd8d55d commit e8e88e3
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 34 deletions.
12 changes: 6 additions & 6 deletions contracts/SchainsData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,9 @@ contract SchainsData is GroupsData {
}

function getActiveSchain(uint nodeIndex) external view returns (bytes32) {
for (uint i = 0; i < schainsForNodes[nodeIndex].length; i++) {
if (schainsForNodes[nodeIndex][i] != bytes32(0)) {
return schainsForNodes[nodeIndex][i];
for (uint i = schainsForNodes[nodeIndex].length; i > 0; i--) {
if (schainsForNodes[nodeIndex][i - 1] != bytes32(0)) {
return schainsForNodes[nodeIndex][i - 1];
}
}
return bytes32(0);
Expand All @@ -379,9 +379,9 @@ contract SchainsData is GroupsData {

uint cursor = 0;
activeSchains = new bytes32[](activeAmount);
for (uint i = 0; i < schainsForNodes[nodeIndex].length; i++) {
if (schainsForNodes[nodeIndex][i] != bytes32(0)) {
activeSchains[cursor++] = schainsForNodes[nodeIndex][i];
for (uint i = schainsForNodes[nodeIndex].length; i > 0; i--) {
if (schainsForNodes[nodeIndex][i - 1] != bytes32(0)) {
activeSchains[cursor++] = schainsForNodes[nodeIndex][i - 1];
}
}
}
Expand Down
33 changes: 27 additions & 6 deletions contracts/SkaleDKG.sol
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ contract SkaleDKG is Permissions {
channels[groupIndex].completed = new bool[](IGroupsData(channels[groupIndex].dataAddress).getRecommendedNumberOfNodes(groupIndex));
channels[groupIndex].publicKeyy.x = 1;
channels[groupIndex].nodeToComplaint = uint(-1);
channels[groupIndex].startedBlockTimestamp = block.timestamp;
emit ChannelOpened(groupIndex);
}

Expand All @@ -134,6 +135,25 @@ contract SkaleDKG is Permissions {
delete channels[groupIndex];
}

function reopenChannel(bytes32 groupIndex) external allow("SkaleDKG") {
require(channels[groupIndex].active, "Channel is not created");
delete channels[groupIndex].broadcasted;
delete channels[groupIndex].completed;
channels[groupIndex].broadcasted = new bool[](IGroupsData(channels[groupIndex].dataAddress).getRecommendedNumberOfNodes(groupIndex));
channels[groupIndex].completed = new bool[](IGroupsData(channels[groupIndex].dataAddress).getRecommendedNumberOfNodes(groupIndex));
delete channels[groupIndex].publicKeyx.x;
delete channels[groupIndex].publicKeyx.y;
channels[groupIndex].publicKeyy.x = 1;
delete channels[groupIndex].publicKeyy.y;
delete channels[groupIndex].fromNodeToComplaint;
channels[groupIndex].nodeToComplaint = uint(-1);
delete channels[groupIndex].numberOfBroadcasted;
delete channels[groupIndex].numberOfCompleted;
delete channels[groupIndex].startComplaintBlockTimestamp;
channels[groupIndex].startedBlockTimestamp = block.timestamp;
emit ChannelOpened(groupIndex);
}

function broadcast(
bytes32 groupIndex,
uint nodeIndex,
Expand Down Expand Up @@ -183,19 +203,20 @@ contract SkaleDKG is Permissions {
correctNode(groupIndex, toNodeIndex)
{
require(isNodeByMessageSender(fromNodeIndex, msg.sender), "Node does not exist for message sender");
if (isBroadcasted(groupIndex, toNodeIndex) && channels[groupIndex].nodeToComplaint == uint(-1)) {
bool broadcasted = isBroadcasted(groupIndex, toNodeIndex);
if (broadcasted && channels[groupIndex].nodeToComplaint == uint(-1)) {
// need to wait a response from toNodeIndex
channels[groupIndex].nodeToComplaint = toNodeIndex;
channels[groupIndex].fromNodeToComplaint = fromNodeIndex;
channels[groupIndex].startComplaintBlockTimestamp = block.timestamp;
emit ComplaintSent(groupIndex, fromNodeIndex, toNodeIndex);
} else if (isBroadcasted(groupIndex, toNodeIndex) && channels[groupIndex].nodeToComplaint != toNodeIndex) {
} else if (broadcasted && channels[groupIndex].nodeToComplaint != toNodeIndex) {
revert("One complaint has already sent");
} else if (isBroadcasted(groupIndex, toNodeIndex) && channels[groupIndex].nodeToComplaint == toNodeIndex) {
} else if (broadcasted && channels[groupIndex].nodeToComplaint == toNodeIndex) {
require(channels[groupIndex].startComplaintBlockTimestamp.add(1800) <= block.timestamp, "One more complaint rejected");
// need to penalty Node - toNodeIndex
finalizeSlashing(groupIndex, channels[groupIndex].nodeToComplaint);
} else if (!isBroadcasted(groupIndex, toNodeIndex)) {
} else if (!broadcasted) {
// if node have not broadcasted params
require(channels[groupIndex].startedBlockTimestamp.add(1800) <= block.timestamp, "Complaint rejected");
// need to penalty Node - toNodeIndex
Expand Down Expand Up @@ -316,20 +337,20 @@ contract SkaleDKG is Permissions {
emit FailedDKG(groupIndex);

address dataAddress = channels[groupIndex].dataAddress;
delete channels[groupIndex];
if (schainsFunctionalityInternal.isAnyFreeNode(groupIndex)) {
uint newNode = schainsFunctionality.rotateNode(
badNode,
groupIndex
);
emit NewGuy(newNode);
this.openChannel(groupIndex);
this.reopenChannel(groupIndex);
} else {
schainsFunctionalityInternal.removeNodeFromSchain(
badNode,
groupIndex
);
IGroupsData(dataAddress).setGroupFailedDKG(groupIndex);
delete channels[groupIndex];
}

DelegationService delegationService = DelegationService(contractManager.getContract("DelegationService"));
Expand Down
20 changes: 11 additions & 9 deletions legacy_tests/Init.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
require('dotenv').config();
const networkName = process.env.NETWORK;
const privateKey = process.env.ETH_PRIVATE_KEY;
const privateKey = process.env.PRIVATE_KEY;

let networks = require("../truffle-config.js");
let currentNetwork = networks['networks'][networkName];

const LINE = '======================================';

const Web3 = require('web3');
const PrivateKeyProvider = require("truffle-hdwallet-provider");
//const provider = new PrivateKeyProvider(privateKey, `http://${currentNetwork['host']}:${currentNetwork['port']}`);
//const web3 = new Web3(provider);
// const PrivateKeyProvider = require("@truffle/hdwallet-provider");
// const provider = new PrivateKeyProvider(privateKey, `http://${currentNetwork['host']}:${currentNetwork['port']}`);
// const web3 = new Web3(provider);
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
//////
// const mainAccount = web3['_provider']['address'];
const mainAccount = "0x0000000000000000000000000000000000000000";

const jsonData = require(`../data/${networkName}.json`);
// const updatedData = require("../data/updated.json");

const SkaleManager = new web3.eth.Contract(jsonData['skale_manager_abi'], jsonData['skale_manager_address']);
module.exports.SkaleManager = SkaleManager;//new web3.eth.Contract(jsonData['skale_manager_abi'], jsonData['skale_manager_address']);
const SkaleToken = new web3.eth.Contract(jsonData['skale_token_abi'], jsonData['skale_token_address']);
module.exports.SkaleToken = SkaleToken;//new web3.eth.Contract(jsonData['skale_token_abi'], jsonData['skale_token_address']);
const NodesFunctionality = new web3.eth.Contract(jsonData['nodes_functionality_abi'], jsonData['nodes_functionality_address']);
module.exports.NodesFunctionality = NodesFunctionality;//new web3.eth.Contract(jsonData['nodes_functionality_abi'], jsonData['nodes_functionality_address']);
const Constants = new web3.eth.Contract(jsonData['constants_abi'], jsonData['constants_address']);
const Constants = new web3.eth.Contract(jsonData['constants_holder_abi'], jsonData['constants_holder_address']);
module.exports.Constants = Constants;//new web3.eth.Contract(jsonData['constants_abi'], jsonData['constants_address']);
const ManagerData = new web3.eth.Contract(jsonData['manager_data_abi'], jsonData['manager_data_address']);
module.exports.ManagerData = ManagerData;//new web3.eth.Contract(jsonData['manager_data_abi'], jsonData['manager_data_address']);
module.exports.NodesData = new web3.eth.Contract(jsonData['nodes_data_abi'], jsonData['nodes_data_address']);
module.exports.SchainsFunctionality = new web3.eth.Contract(jsonData['schains_functionality_abi'], jsonData['schains_functionality_address']);
module.exports.ValidatorsFunctionality = new web3.eth.Contract(jsonData['validators_functionality_abi'], jsonData['validators_functionality_address']);
module.exports.ValidatorsFunctionality = new web3.eth.Contract(jsonData['monitors_functionality_abi'], jsonData['monitors_functionality_address']);
module.exports.SchainsData = new web3.eth.Contract(jsonData['schains_data_abi'], jsonData['schains_data_address']);
module.exports.ValidatorsData = new web3.eth.Contract(jsonData['validators_data_abi'], jsonData['validators_data_address']);
module.exports.ValidatorsData = new web3.eth.Contract(jsonData['monitors_data_abi'], jsonData['monitors_data_address']);
module.exports.ContractManager = new web3.eth.Contract(jsonData['contract_manager_abi'], jsonData['contract_manager_address']);
module.exports.DelegationService = new web3.eth.Contract(jsonData['delegation_service_abi'], jsonData['delegation_service_address']);
module.exports.ValidatorService = new web3.eth.Contract(jsonData['validator_service_abi'], jsonData['validator_service_address']);
module.exports.TokenState = new web3.eth.Contract(jsonData['token_state_abi'], jsonData['token_state_address']);
module.exports.web3 = web3;
module.exports.mainAccount = mainAccount;
module.exports.jsonData = jsonData;
Expand Down
4 changes: 2 additions & 2 deletions legacy_tests/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async function rotationNode(secondRandomNumber) {
let nodeRotated = new Array();
for (let i = 0; i < schainIds.length; i++) {
// await schainsFunctionality.rotateNode(schainIds[i]);
let tx_hash = await init.SchainsFunctionality.methods.rotateNode(schainIds[i]).send({from: init.mainAccount, gas: 8000000});
let tx_hash = await init.SchainsFunctionality.methods.rotateNode(schainIds[i]).send({from: init.mainAccount, gas: 6900000});
let blockNumber = tx_hash.blockNumber;
await init.SchainsFunctionality.getPastEvents('NodeRotated', {fromBlock: blockNumber, toBlock: blockNumber}).then(
function(events) {
Expand Down Expand Up @@ -106,7 +106,7 @@ async function rotationValidator(nodeIndex) {
console.log(res.ids);
for (index of res.ids) {
let groupIndex = init.web3.utils.soliditySha3(index);
let {logs} = await init.ValidatorsFunctionality.rotateNode(groupIndex).send({from: init.mainAccount, gas: 8000000});
let {logs} = await init.ValidatorsFunctionality.rotateNode(groupIndex).send({from: init.mainAccount, gas: 6900000});
console.log(logs);
}
}
Expand Down
36 changes: 28 additions & 8 deletions legacy_tests/Nodes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const init = require("./Init.js");
const GenerateBytesData = require("./GenerateBytesData.js");
//console.log(SkaleToken);

async function generateRandomIP() {
let ip1 = Math.floor(Math.random() * 255);
Expand Down Expand Up @@ -40,9 +39,25 @@ async function createNode() {
console.log("Account Deposit", accountDeposit);
//console.log(SkaleToken);
//console.log(jsonData['skale_token_address']);
let res = await init.SkaleToken.methods.transfer(init.jsonData['skale_manager_address'], init.web3.utils.toBN(100000000000000000000).toString(), data).send({from: init.mainAccount, gas: 8000000});
//let registerValidator = await init.DelegationService.methods.registerValidator("ValidatorName", "Really good validator", 500, 100).send({from: init.mainAccount, gas: 6900000});
//console.log(registerValidator);
console.log("Validator Registered!");
//let enableValidator = await init.ValidatorService.methods.enableValidator(1).send({from: init.mainAccount, gas: 6900000});
//console.log(enableValidator);
console.log("Validator Enabled!");
//await init.Constants.methods.setMSR(100).send({from: init.mainAccount, gas: 6900000});
//let delegated = await init.DelegationService.methods.delegate(1, 100, 3, "Nice").send({from: init.mainAccount, gas: 6900000});
//console.log(delegated);
console.log("Delegated!");
//let accept = await init.DelegationService.methods.acceptPendingDelegation(0).send({from: init.mainAccount, gas: 6900000});
//console.log(accept);
console.log("Accepted!");
let skipped = await init.TokenState.methods.skipTransitionDelay(0).send({from: init.mainAccount, gas: 6900000});
console.log(skipped);
console.log("Skipped!");
let res = await init.SkaleManager.methods.createNode(data).send({from: init.mainAccount, gas: 6900000});
console.log(res);
let blockNumber = res.blockNumber;
/*let blockNumber = res.blockNumber;
let nodeIndex;
await init.NodesFunctionality.getPastEvents('NodeCreated', {fromBlock: blockNumber, toBlock: blockNumber}).then(
function(events) {
Expand All @@ -59,12 +74,12 @@ async function createNode() {
console.log(events[i].returnValues);
}
});
console.log("Node", nodeIndex, "created with", res.gasUsed, "gas consumption");
return nodeIndex;
console.log("Node", nodeIndex, "created with", res.gasUsed, "gas consumption");*/
//return nodeIndex;
}

async function deleteNode(nodeIndex) {
let res = await init.SkaleManager.methods.deleteNode(nodeIndex).send({from: init.mainAccount, gas: 8000000});
let res = await init.SkaleManager.methods.deleteNode(nodeIndex).send({from: init.mainAccount, gas: 6900000});
console.log("Node:", nodeIndex, "deleted with", res.gasUsed, "gas consumption");
}

Expand All @@ -76,7 +91,11 @@ async function getNode(nodeIndex) {
}

async function getNodeNextRewardDate(nodeIndex) {
let res = await init.NodesData.methods.getNodeNextRewardDate(nodeIndex).call();
let res = await init.NodesData.methods.nodes(nodeIndex).call();
console.log(res);
let res1 = await init.NodesData.methods.getNodeNextRewardDate(nodeIndex).call();
console.log(res1);
console.log("Did everything!");
return res;
}

Expand All @@ -88,7 +107,8 @@ async function createNodes(n) {
return nodeIndexes;
}

//createNodes(15);
//createNode();
getNodeNextRewardDate(0)

module.exports.createNode = createNode;
module.exports.createNodes = createNodes;
Expand Down
2 changes: 1 addition & 1 deletion legacy_tests/Schains.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function createSchain(typeOfSchain, lifetime) {
console.log("Number of nodes: ", numberOfNodes);
console.log("Number of full nodes: ", numberOfFullNodes);
console.log("NUmber of fractional nodes: ", numberOfFractionalNodes);
res = await init.SkaleToken.methods.transfer(init.jsonData['skale_manager_address'], deposit, data).send({from: account, gas: 8000000});
res = await init.SkaleToken.methods.transfer(init.jsonData['skale_manager_address'], deposit, data).send({from: account, gas: 6900000});
let blockNumber = res.blockNumber;
//init.SchainsFunctionality.getPastEvents("GroupGenerated", {fromBlock: blockNumber, toBlock:blockNumber}).then(function(events) {console.log(events)});
//init.SchainsFunctionality.getPastEvents("SchainCreated", {fromBlock: blockNumber, toBlock:blockNumber}).then(function(events) {console.log(events)});
Expand Down
2 changes: 1 addition & 1 deletion legacy_tests/Validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ async function sendVerdict(fromNodeIndex, toNodeIndex, downtime, latency) {
}

async function getBounty(nodeIndex) {
let res = await init.SkaleManager.methods.getBounty(nodeIndex).send({from: init.mainAccount, gas: 8000000});
let res = await init.SkaleManager.methods.getBounty(nodeIndex).send({from: init.mainAccount, gas: 6900000});
let amount;
await init.SkaleManager.getPastEvents('BountyGot', {fromBlock: res.blockNumber, toBlock: res.blockNumber}).then(
function(events) {
Expand Down
2 changes: 1 addition & 1 deletion truffle-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = {
test: {
host: "127.0.0.1",
port: 8545,
gas: 8000000,
gas: 6900000,
network_id: "*"
}
},
Expand Down

0 comments on commit e8e88e3

Please sign in to comment.