Mokka Consensus Algorithm implementation in Node.js.
Concept description (PDF)
$ npm install mokka --save
$ npm run build
Returns a new mokka instance. As mokka is agnostic to protocol implementation,
you have to create your own.
Please check the
Custom transport layer section.
address(string): an address in custom format. The only rule is that address should include the public key in the end (example:
electionMin(integer): minimum time required for voting
electionMax(integer): max time required for voting
heartbeat(integer): leader heartbeat timeout
gossipHeartbeat(integer): gossip heartbeat timeout
proofExpiration(integer): when the leader's proof token should expire.
storage: levelDb compatible instance (can be leveldown, memdown and so on). Also be sure, that your instance satisfy the interface
logger(ILoggerInterface): logger instance. If omitted, then console.log will be used
privateKey: the 64 length private key. Please take a look at example key pair generator
push new log and replicate it over the cluster.
Returns the current state of node, stored in db (last log index, last committed log index, merkle root)
Returns the current known state of follower. This request only works on leader node.
Returns the current state of node, stored in memory (should be equal to the db's one) (last log index, last committed log index, merkle root)
Returns entry (i.e. structure with log), by provided index.
Returns N (specified in limit) logs after specified index.
Compacts logs by their key (keep most recent version).
A Mokka instance emits the following events (available at
join: once we add new peer
leave: once we remove peer
error: once error happens (for instance, bad voting)
heartbeat_timeout: once we can't receive the heartbeat from leader in certain time (specified in config)
state: once the state of node changed (i.e. leader, candidate, follower)
log: once node received new log
log_ack: once node acked the log
Also gossip expose events. To use them, you have to listen events from gossip instance:
peer_new: once gossip connects to new peer
peer_update: once gossip update information about certain peer
peer_alive: once gossip checked that certain peer alive
peer_failed: once gossip can't receive any answer from certain peer
In case, the certain instance has been dropped, the leader will reappend all logs. However, you should keep in mind, that follower is passive. Which means, that communication comes from leader to the follower. The leader only updates the its local state (info about every follower) during the voting process. So, the dropped node will be resynced once new voting happen (by expiration timeout or condition)
In order to communicate between nodes, you have to implement the interface by yourself. As an example you can take a look at TCP implementation:
In order to write your own implementation you have to implement 2 methods:
async initialize() function, which fires on mokka start. This method is useful, when you want to open the connection, for instance, tcp one, or connect to certain message broker like rabbitMQ.
async write(address: string, packet: Buffer) function, which fires each time mokka wants to broadcast message to other peer (address param).
Also, keep in mind, that mokka doesn't handle the disconnected / dead peers, which means that mokka will try to make requests to all presented members in cluster, even if they are not available. So, you need to handle it on your own.
|running cluster||running cluster|
|running private blockchain||-|
Copyright (c) 2018-2019 Egor Zuev