diff --git a/contracts/SchainsData.sol b/contracts/SchainsData.sol index 1d5aebd5a..1efaa850a 100644 --- a/contracts/SchainsData.sol +++ b/contracts/SchainsData.sol @@ -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); @@ -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]; } } } diff --git a/contracts/SkaleDKG.sol b/contracts/SkaleDKG.sol index a8254ce4e..78c823a57 100644 --- a/contracts/SkaleDKG.sol +++ b/contracts/SkaleDKG.sol @@ -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); } @@ -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, @@ -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 @@ -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")); diff --git a/legacy_tests/Init.js b/legacy_tests/Init.js index 1a310735b..4ccedf8ec 100644 --- a/legacy_tests/Init.js +++ b/legacy_tests/Init.js @@ -1,6 +1,6 @@ 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]; @@ -8,32 +8,34 @@ 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; diff --git a/legacy_tests/Main.js b/legacy_tests/Main.js index b7ca51a37..3550d1e3b 100644 --- a/legacy_tests/Main.js +++ b/legacy_tests/Main.js @@ -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) { @@ -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); } } diff --git a/legacy_tests/Nodes.js b/legacy_tests/Nodes.js index a44e9e0d4..0862d5a0f 100644 --- a/legacy_tests/Nodes.js +++ b/legacy_tests/Nodes.js @@ -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); @@ -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) { @@ -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"); } @@ -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; } @@ -88,7 +107,8 @@ async function createNodes(n) { return nodeIndexes; } -//createNodes(15); +//createNode(); +getNodeNextRewardDate(0) module.exports.createNode = createNode; module.exports.createNodes = createNodes; diff --git a/legacy_tests/Schains.js b/legacy_tests/Schains.js index b37a5381d..35b993245 100644 --- a/legacy_tests/Schains.js +++ b/legacy_tests/Schains.js @@ -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)}); diff --git a/legacy_tests/Validators.js b/legacy_tests/Validators.js index 2daf725a4..eaac977c9 100644 --- a/legacy_tests/Validators.js +++ b/legacy_tests/Validators.js @@ -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) { diff --git a/truffle-config.js b/truffle-config.js index 061a46c5a..c27690fc7 100644 --- a/truffle-config.js +++ b/truffle-config.js @@ -40,7 +40,7 @@ module.exports = { test: { host: "127.0.0.1", port: 8545, - gas: 8000000, + gas: 6900000, network_id: "*" } },