Creating a simple cryptocurrency: part 8
The repository now includes consensus determination.
Consensus determination
At the beginning of the first beta period, the beta server packs the balance database into a file, computes the hash of the file, then broadcasts the hash along with the transaction buffer to all other servers.
The other servers use the transactions received from the beta server to update their balance databases, compute the hash, then broadcast their version of the hash to all other servers. After this step, all servers know all versions of the current hash.
At the beginning of the second beta period, each server compares its own hash to the other hashes and determines whether it is part of a majority. If not, it terminates itself. Eventually, servers will have the ability to restart themselves by rejoining the consensus with a corrected database or, if there is no consensus, regressing to the most recent consensus state.
The message format between servers is like so:
head | head | payload | payload |
---|---|---|---|
public key | signature of (hash of payload) | hash of packed balance database file | transaction buffer (beta only) |
Testing
- As before, modify server.js to include three of your own server keys and ports
- Start the servers at about the same time
- Observer them exchange messages
- Stop one server and observe the other two continue to achieve consensus
- Restart the stopped server and observe it rejoin consensus (since balances haven't changed)
- Stop two servers and observe the third one self-terminate because consensus is not possible (at least two must agree)
- Restart the three servers and send the beta server some transactions
- Observe that consensus persists
- Stop one server, send transactions to the others, then restart the stopped server
- Observe that the restarted server cannot rejoin consensus because its database is out of sync
- Copy the database of one of the consensus servers to the one that is out of sync, then restart it
- Observe that the server rejoin consensus