Creating a simple cryptocurrency: part 8

in #cryptocurrency7 years ago (edited)

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:

headheadpayloadpayload
public keysignature of (hash of payload)hash of packed balance database filetransaction buffer (beta only)

Testing

  1. As before, modify server.js to include three of your own server keys and ports
  2. Start the servers at about the same time
  3. Observer them exchange messages
  4. Stop one server and observe the other two continue to achieve consensus
  5. Restart the stopped server and observe it rejoin consensus (since balances haven't changed)
  6. Stop two servers and observe the third one self-terminate because consensus is not possible (at least two must agree)
  7. Restart the three servers and send the beta server some transactions
  8. Observe that consensus persists
  9. Stop one server, send transactions to the others, then restart the stopped server
  10. Observe that the restarted server cannot rejoin consensus because its database is out of sync
  11. Copy the database of one of the consensus servers to the one that is out of sync, then restart it
  12. Observe that the server rejoin consensus

< part 7 | part 9 >

Coin Marketplace

STEEM 0.16
TRX 0.16
JST 0.030
BTC 58478.56
ETH 2523.24
USDT 1.00
SBD 2.36