Mokka Consensus Algorithm implementation in Node.js.
Concept description (PDF)
$ npm install mokka --save
$ npm run build
Client example can be found under:
The package.json already includes the script for running demo client. By default, the cluster has 4 members. So, you have to open 4 terminals and type in each terminal the appropriate command:
npm run serve_client1
npm run serve_client2
npm run serve_client3
npm run serve_client4
In order to generate new random logs count (N), type:
To get RSM state, type:
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
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 tweetnacl key pair generator
push new log and replicate it over the cluster.
Returns the current state of node (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.
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 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).
Copyright (c) 2018-2019 Egor Zuev