democracyjs
Democracy election for decentralized systems.
App
This application implements consensus algorithm work. You can reimplement classess, objects, methods for your application.
Have four modules, based on app.MODULE:
- Peer
- Data
- PeerManager
- DataManager
- Election
Peer
Define connected node entry
class Peer extends app.MODULE {
constructor(data);
getId();
signVote(vote);
}
PeerManager
Define Mapper of Peer entries. Storing, sorting, searching, etc...
class PeerMapper extends app.MODULE {
getPeersList();
addPeer(peer);
addPeers(peer_list);
removePeerById(peerId);
removePeer(peer);
}
Data
Define election data message (election/vote)
class Data extends app.MODULE {
constructor(data);
static getTypeFieldName()
static getIdFieldName()
static getPublicKeyFieldName()
static getSignFieldName()
static getAnswerFieldName()
static getMethodFieldName()
static getAnswersFieldName()
static getStatusFieldName()
static getUntilFieldName()
static getBalancedFieldName()
getType()
getId()
getPublicKey()
getSign()
getAnswer()
getMethod()
getAnswers()
getStatus()
getUntil()
getBalanced()
isValid()
setStatus(newStatus)
setBalanced(balanced)
}
DataMapper
Define Mapper of Data entries. Storing, sorting, searching, etc...
class DataMapper extends app.MODULE {
getDataList();
_addDataToList(data);
addData(data);
getData(id);
removeData(data);
getElectionVoteByPeer(election_id, peer_id);
getElectionPeers(election_id);
getElectionVotes(election_id);
saveData(data);
}
Election
Describe election algorithm.
class Election extends app.MODULE {
constructor(election_config_field);
getConfig(field, defaultValue);
init()
setKeystore(ks);
setOnEnd(cb);
vote(electionId, answer);
isValidTime(time);
create(request, answers, until, method);
voteProcess(peer, voteData);
balancing(id, data);
getVotersList();
}
App.Extending
Any class in application can be redefined before start, for example we have app methods:
definePeerClass(man);
definePeerManagerClass(man);
setPeerManager(man);
defineDataClass(man);
defineDataManagerClass(man);
setDataManager(man);
defineElectionClass(man);
To redefine class you need create you own class and extends one from default modules:
- app.PEER
- app.DATA
- app.PEERMANAGER
- app.DATAMANAGER
- app.ELECTION
For example we can create new election algorithm with another balancing algorithm and peer list:
module.exports = function(app) {
class NewElection extends app.ELECTION {
constructor(){
super('election_field')
}
getVotersList() {
return [
'0x0',
'0x1',
'0x2',
'0x3',
'0x4',
'0x5',
];
}
balancing(id, data){
let answers = app.dataManager.getElectionVotes(id);
let avg = 0;
for (let i in answers){
avg += answers[i].getAnswer();
}
return avg/answers.length
}
}
return NewElection
}
Put this code to file newcons.js, and require it:
const newelect = require('newelect.js');
const DemocracyJs = require("democracyjs");
let config = {
"election_field":{
"majority": 0.5
}
"node":{
}
};
let app = new DemocracyJs(config);
app.defineElectionClass(newelect(app));
app.start();
Config sections
You can create many config sections:
"section1": {
"param1": "value1",
"param2": "value2"
}
and use it in application: app.config.section1.param1
Any eection have default config section, defined in second param of constructor, for example:
constructor(){
super('newconsensus_config_field')
}
in this example we must have section newconsensus_config_field
in config!
node
Node section is required for application. Its describe keys of current peerm this keys will be used for signing messages
"node": {
"publicKey": '0x...<hex>',
"privateKey": '0x...<hex>'
},
extending sections
Config sections can be extending by existing sections:
"section1": {
"param1":'value1',
"param2":'value2',
},
"section2":{
"extends":"section1",
"param3":"value3"
}
In this example section2 have next params:
"param1":'value1',
"param2":'value2',
"param3":"value3"
Extending can be nested:
"section1": {
"param1":'value1',
"param2":'value2',
},
"section2":{
"extends":"section1",
"param3":"value3"
},
"section3": {
"extends": "section2",
"params5": 1,
}
it will be:
"param1":'value1',
"param2":'value2',
"param3":"value3",
"params5": 1,
Extending config sections is used for default election algorithms, see below.
default config parameters:
Config default parameters, that can be extend and rewrite defined in app.getDefaultConfig()
:
getDefaultConfig() {
return {
'election': {
'majority': 0.75
},
'node': {
'privateKey': '',
'publicKey': ''
}
};
}
}