Skip to content

Commit

Permalink
Merge pull request #117 from skalenetwork/bugfix/Fix-deleteMonitor
Browse files Browse the repository at this point in the history
Bugfix/fix delete monitor
  • Loading branch information
payvint authored Mar 11, 2020
2 parents 7f9eefa + 3fdc993 commit abb4a48
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
4 changes: 4 additions & 0 deletions contracts/MonitorsData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ contract MonitorsData is GroupsData {
return checkedNodes[monitorIndex];
}

function getCheckedArrayLength(bytes32 monitorIndex) external view returns (uint) {
return checkedNodes[monitorIndex].length;
}

function getLengthOfMetrics(bytes32 monitorIndex) external view returns (uint) {
return verdicts[monitorIndex].length;
}
Expand Down
5 changes: 4 additions & 1 deletion contracts/MonitorsFunctionality.sol
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ contract MonitorsFunctionality is GroupsFunctionality {
for (uint i = 0; i < nodesInGroup.length; i++) {
monitorIndex = keccak256(abi.encodePacked(nodesInGroup[i]));
(index, ) = find(monitorIndex, nodeIndex);
data.removeCheckedNode(monitorIndex, index);
if (index < data.getCheckedArrayLength(monitorIndex)) {
data.removeCheckedNode(monitorIndex, index);
}
}
deleteGroup(groupIndex);
}
Expand Down Expand Up @@ -266,6 +268,7 @@ contract MonitorsFunctionality is GroupsFunctionality {
bytes32[] memory checkedNodes = data.getCheckedArray(monitorIndex);
uint possibleIndex;
uint32 possibleTime;
index = checkedNodes.length;
for (uint i = 0; i < checkedNodes.length; i++) {
(possibleIndex, possibleTime) = getDataFromBytes(checkedNodes[i]);
if (possibleIndex == nodeIndex && (time == 0 || possibleTime < time)) {
Expand Down
36 changes: 36 additions & 0 deletions test/MonitorsFunctionality.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,42 @@ contract("MonitorsFunctionality", ([owner, validator]) => {
await monitorsData.getCheckedArray(node4Hash).should.be.eventually.empty;
});

it("should delete nodes from checked list", async () => {
await monitorsFunctionality.addMonitor(0);
await monitorsFunctionality.addMonitor(1);

const node0Hash = web3.utils.soliditySha3(0);
const node1Hash = web3.utils.soliditySha3(1);
const node2Hash = web3.utils.soliditySha3(2);
const node3Hash = web3.utils.soliditySha3(3);
const node4Hash = web3.utils.soliditySha3(4);

(await monitorsData.getCheckedArray(node0Hash)).length.should.be.equal(1);
(await monitorsData.getCheckedArray(node1Hash)).length.should.be.equal(1);
(await monitorsData.getCheckedArray(node2Hash)).length.should.be.equal(2);
(await monitorsData.getCheckedArray(node3Hash)).length.should.be.equal(2);
(await monitorsData.getCheckedArray(node4Hash)).length.should.be.equal(2);

await monitorsFunctionality.deleteMonitor(0);
console.log("Finished delete 0 monitor");

await monitorsData.getCheckedArray(node0Hash).should.be.eventually.empty;
await monitorsData.getCheckedArray(node1Hash).should.be.eventually.empty;
(await monitorsData.getCheckedArray(node2Hash)).length.should.be.equal(1);
(await monitorsData.getCheckedArray(node3Hash)).length.should.be.equal(1);
(await monitorsData.getCheckedArray(node4Hash)).length.should.be.equal(1);

console.log("Started delete 1 monitor");
await monitorsFunctionality.deleteMonitor(1);
console.log("Finished delete 1 monitor");

// await monitorsData.getCheckedArray(node0Hash).should.be.eventually.empty;
// await monitorsData.getCheckedArray(node1Hash).should.be.eventually.empty;
// await monitorsData.getCheckedArray(node2Hash).should.be.eventually.empty;
// await monitorsData.getCheckedArray(node3Hash).should.be.eventually.empty;
// await monitorsData.getCheckedArray(node4Hash).should.be.eventually.empty;
});

const nodesCount = 50;
const activeNodesCount = 30;
describe("when " + nodesCount + " nodes in network", async () => {
Expand Down

0 comments on commit abb4a48

Please sign in to comment.