Skip to content

Commit

Permalink
Merge pull request #15 from Alethio/validators
Browse files Browse the repository at this point in the history
Validators
  • Loading branch information
baxy authored Jun 6, 2019
2 parents 667dfca + ca57b9a commit 6d45ce0
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 103 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog
All notable changes to this project will be documented in this file.

## [2.5.3] - 2019-06-06
- Add support for sending to the server validators/signers on "clique" POA networks

## [2.5.2] - 2019-05-23
- Fixed output on the stats variables sent to the server

Expand Down
14 changes: 14 additions & 0 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ export default class Server {
case 'getBlocksResponse':
this.resolveResponse(message.topic, message.payload);
break;
case 'validatorsResponse':
this.resolveResponse(message.topic, message.payload);
break;
case 'getConfigResponse':
this.resolveGetConfigResponse(message.payload);
break;
default:
this.log.info(`Undefined topic: ${message.topic}`);
break;
Expand Down Expand Up @@ -335,6 +341,7 @@ export default class Server {
this.log.echo('Successfully logged in');
this.log.echo(`${this.pkg.description} v${this.pkg.version} started and running...`);
this.send('connection', {isConnected: true});
this.send('getConfig', {configName: 'NETWORK_ALGO'});

let configStoreServer = this.config.configStore.get('server');
if (configStoreServer && configStoreServer.net !== undefined) {
Expand Down Expand Up @@ -426,4 +433,11 @@ export default class Server {
this.log.warning(`Server response on topic: "${topic}" warnings: ${JSON.stringify(response.warnings)}`);
}
}

resolveGetConfigResponse(response) {
this.resolveResponse('getConfig', response);
if (response.success) {
this.lodash.merge(this.config, response.data.shift());
}
}
}
4 changes: 3 additions & 1 deletion lib/client/Usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ export default class Usage {
}

setNodeProcessName(node) {
this.nodeProcessName = node.split('/')[0].toLowerCase();
if (node) {
this.nodeProcessName = node.split('/')[0].toLowerCase();
}
}

async getStats() {
Expand Down
15 changes: 14 additions & 1 deletion lib/client/protocol/Abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export default class Abstract {
if (lastBlockNumber === null || (currentBlockNumber - lastBlockNumber) === 1) {
this.lastBlock = block;
this.server.send('block', block);

if (this.config.NETWORK_ALGO === 'clique') {
this.getValidators(block);
}
}

if (lastBlockNumber > 0 && (currentBlockNumber - lastBlockNumber) > 1 && (currentBlockNumber - lastBlockNumber) <= 1000) {
Expand All @@ -65,7 +69,16 @@ export default class Abstract {
}

if (currentBlockNumber === lastBlockNumber) {
this.log.info(`Block "${currentBlockNumber}" has already been sent`);
if (this.lastBlock.hash === block.hash) {
this.log.info(`Block "${currentBlockNumber}" has already been sent`);
} else {
this.log.info(`Reorg for block "${currentBlockNumber}"`);
this.server.send('block', block);

if (this.config.NETWORK_ALGO === 'clique') {
this.getValidators(block);
}
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions lib/client/protocol/Http.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,21 @@ export default class Http extends Abstract {
this.server.send('getBlocksData', results);
});
}

getValidators(block) {
let result = {
blockNumber: block.number,
blockHash: block.hash,
validators: []
};

this.web3._requestManager.sendAsync({method: 'clique_getSignersAtHash', params: [block.hash]}, (error, validators) => {
if (error) {
this.log.error(`Could not get validators for block ${block.number}::${block.hash} => ${error.message}`);
} else {
result.validators = validators;
this.server.send('validators', result);
}
});
}
}
17 changes: 17 additions & 0 deletions lib/client/protocol/WebSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,21 @@ export default class WebSocket extends Abstract {
this.server.send('getBlocksData', []);
});
}

getValidators(block) {
let result = {
blockNumber: block.number,
blockHash: block.hash,
validators: []
};

this.web3._requestManager.send({method: 'clique_getSignersAtHash', params: [block.hash]}, (error, validators) => {
if (error) {
this.log.error(`Could not get validators for block ${block.number}::${block.hash} => ${error.message}`);
} else {
result.validators = validators;
this.server.send('validators', result);
}
});
}
}
Loading

0 comments on commit 6d45ce0

Please sign in to comment.