Return block number

This commit is contained in:
Andrea Maria Piana 2019-04-10 15:07:37 +02:00
parent de5fa5f6ec
commit 1b1ea38a78
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
2 changed files with 31 additions and 10 deletions

View File

@ -9,11 +9,11 @@ contract NodesV2
// Nodes that have not passed any check or failed previous check // Nodes that have not passed any check or failed previous check
Enode[] public inactiveNodes; Enode[] public inactiveNodes;
// For testing purposes
bool overrideBlockPeriod;
uint quorum; uint quorum;
// How many blocks is a period
uint blockPeriod; uint blockPeriod;
uint currentBlock; uint currentBlockStart;
uint public currentBlock;
struct Enode { struct Enode {
bytes publicKey; bytes publicKey;
@ -39,10 +39,19 @@ contract NodesV2
public public
{ {
owner = msg.sender; owner = msg.sender;
currentBlock = block.number; currentBlock = 0;
currentBlockStart = block.number;
blockPeriod = _blockPeriod; blockPeriod = _blockPeriod;
} }
function getCurrentBlock() public view returns (uint) {
if (newBlockPeriod()) {
return currentBlock + 1;
} else {
return currentBlock;
}
}
function getNode(uint index) public view returns (bytes memory, uint32, uint16) { function getNode(uint index) public view returns (bytes memory, uint32, uint16) {
Enode memory enode = activeNodes[index]; Enode memory enode = activeNodes[index];
@ -90,7 +99,9 @@ contract NodesV2
// Reset quorum for next vote // Reset quorum for next vote
quorum = calculateQuorum(activeNodes.length); quorum = calculateQuorum(activeNodes.length);
// Set current block // Set current block
currentBlock = block.number; currentBlock++;
// Set start
currentBlockStart = block.number;
} }
function vote( function vote(
@ -153,7 +164,7 @@ contract NodesV2
} }
function newBlockPeriod() private view returns (bool) { function newBlockPeriod() private view returns (bool) {
return block.number >= currentBlock + blockPeriod; return block.number >= currentBlockStart + blockPeriod;
} }
function publicKeyToAddress (bytes memory publicKey) public pure returns (address) { function publicKeyToAddress (bytes memory publicKey) public pure returns (address) {
@ -190,10 +201,6 @@ contract NodesV2
return a / 2 + 1; return a / 2 + 1;
} }
function setOverrideBlockPeriod(bool val) public onlyOwner {
overrideBlockPeriod = val;
}
function _deleteInactiveNode(uint index) internal { function _deleteInactiveNode(uint index) internal {
require(index < inactiveNodes.length); require(index < inactiveNodes.length);
// Remove from index // Remove from index

View File

@ -156,6 +156,13 @@ contract('Nodes', async (accounts) => {
await instance.vote([node4.address, node5.address], [node2.address], { from: accounts[2] }); await instance.vote([node4.address, node5.address], [node2.address], { from: accounts[2] });
}); });
describe('currentBlock', async () => {
it('returns 1', async () => {
const actualCurrentBlock = await instance.getCurrentBlock();
assert.equal(1, actualCurrentBlock);
});
});
describe('double voting', async () => { describe('double voting', async () => {
it('throws an exception', async () => { it('throws an exception', async () => {
try { try {
@ -196,6 +203,13 @@ contract('Nodes', async (accounts) => {
await instance.vote([], [node2.address]); await instance.vote([], [node2.address]);
}); });
describe('currentBlock', async () => {
it('returns 2', async () => {
const actualCurrentBlock = await instance.getCurrentBlock();
assert.equal(2, actualCurrentBlock);
});
});
it('promotes the inactive nodes that have passed the checks', async () => { it('promotes the inactive nodes that have passed the checks', async () => {
const actualNodeCount = await instance.inactiveNodeCount(); const actualNodeCount = await instance.inactiveNodeCount();
assert.equal(1, actualNodeCount); assert.equal(1, actualNodeCount);