Tuesday, June 05, 2018

It's All Connected - Adventures in Blockchain Implementation

Programming Praxis followed up its hashing implementation exercise with a challenge that uses this newly crafted code. The idea is to implement a blockchain. The only thing I know about crypto currencies like Bitcoin is that they're all about blockchains. I figured I'd tackle this exercise and then use my new found knowledge to quit my day job and become a crypto currency day trader.

After a few minutes with this exercise, I realized my plans of striking it digitally rich would have to wait. Implementing blockchains would be: (a) simple to do, and (b) not put me any closer to understanding Bitcoin.

A blockchain is a cleverly crafted list of items, where each item contains a hashed value that matches the previous item. If any of the items on the list are tampered with, the hashs won't match and the blockchain will be trivially noted as compromised.

As a contrived example, consider this list of readings from the Potomac River water Gauge:

(define water-guage-levels 
  '("USGS    2018-06-04 00:00    EST    70200    P    7.78    P"
    "USGS    2018-06-04 00:15    EST    70600    P    7.80    P"
    "USGS    2018-06-04 00:30    EST    70900    P    7.81    P"
    "USGS    2018-06-04 00:45    EST    71500    P    7.84    P"
    "USGS    2018-06-04 01:00    EST    72300    P    7.88    P"
    "USGS    2018-06-04 01:15    EST    72500    P    7.89    P"
    "USGS    2018-06-04 01:30    EST    73300    P    7.93    P"
    "USGS    2018-06-04 01:45    EST    74200    P    7.97    P"
    "USGS    2018-06-04 02:00    EST    75000    P    8.01    P"
    "USGS    2018-06-04 02:15    EST    76100    P    8.06    P"
    "USGS    2018-06-04 02:30    EST    76500    P    8.08    P"
    "USGS    2018-06-04 02:45    EST    77400    P    8.12    P"
    "USGS    2018-06-04 03:00    EST    79100    P    8.20    P"
    "USGS    2018-06-04 03:15    EST    79800    P    8.23    P"
    "USGS    2018-06-04 03:30    EST    81100    P    8.29    P"
    "USGS    2018-06-04 03:45    EST    82500    P    8.35    P"))

If I published this list, and someone came along with a similar yet tweaked list, who's would you believe?

Now suppose I publish this list as a blockchain:

> (define water-guage-levels-bc
  (fold (lambda (entry bc)
          (bc-adjoin bc entry))
        (bc-init)
        water-guage-levels))

> (bc-show water-guage-levels-bc)
(16 "USGS    2018-06-04 03:45    EST    82500    P    8.35    P" "8671b82a1a8d1c9ff668c11b133c4c67" "1b3df4a71fe0c8fbd3ed6ddf9f39193a")
(15 "USGS    2018-06-04 03:30    EST    81100    P    8.29    P" "a294e5e636810aa425c1c5e1f497893" "8671b82a1a8d1c9ff668c11b133c4c67")
(14 "USGS    2018-06-04 03:15    EST    79800    P    8.23    P" "fe60902aa25d4cbe978169a3bbc8cb6" "a294e5e636810aa425c1c5e1f497893")
(13 "USGS    2018-06-04 03:00    EST    79100    P    8.20    P" "c3fcd4c796b0a923aa857d1e9e80792" "fe60902aa25d4cbe978169a3bbc8cb6")
(12 "USGS    2018-06-04 02:45    EST    77400    P    8.12    P" "45423288514717ecc5829ad011e657dd" "c3fcd4c796b0a923aa857d1e9e80792")
(11 "USGS    2018-06-04 02:30    EST    76500    P    8.08    P" "c125bd9184f97dc149363a8b05766ec" "45423288514717ecc5829ad011e657dd")
(10 "USGS    2018-06-04 02:15    EST    76100    P    8.06    P" "105fb72524937269e0a75e2d3512ab49" "c125bd9184f97dc149363a8b05766ec")
(9 "USGS    2018-06-04 02:00    EST    75000    P    8.01    P" "52ab9918616a5742262e928cf4fdc4" "105fb72524937269e0a75e2d3512ab49")
(8 "USGS    2018-06-04 01:45    EST    74200    P    7.97    P" "b67c5dd2c33111f4fc48d7312a1812f" "52ab9918616a5742262e928cf4fdc4")
(7 "USGS    2018-06-04 01:30    EST    73300    P    7.93    P" "dbca959d4ec7d930d3537d4516df4198" "b67c5dd2c33111f4fc48d7312a1812f")
(6 "USGS    2018-06-04 01:15    EST    72500    P    7.89    P" "f03e16843cca2258a127d7f495ab2d8" "dbca959d4ec7d930d3537d4516df4198")
(5 "USGS    2018-06-04 01:00    EST    72300    P    7.88    P" "4263d01116c6c42d12c3b17197ffadd5" "f03e16843cca2258a127d7f495ab2d8")
(4 "USGS    2018-06-04 00:45    EST    71500    P    7.84    P" "629f057e62534baaa0b14e6f4fd8a" "4263d01116c6c42d12c3b17197ffadd5")
(3 "USGS    2018-06-04 00:30    EST    70900    P    7.81    P" "beaf7199fac22cfc8f565120133a342d" "629f057e62534baaa0b14e6f4fd8a")
(2 "USGS    2018-06-04 00:15    EST    70600    P    7.80    P" "dcba2a9715e2e5ce4e27a19e1c69f3d" "beaf7199fac22cfc8f565120133a342d")
(1 "USGS    2018-06-04 00:00    EST    70200    P    7.78    P" "7f4e55deaa11a9a796adedebbb50b0" "dcba2a9715e2e5ce4e27a19e1c69f3d")
(0 "Genesis Block" "0" "7f4e55deaa11a9a796adedebbb50b0")

It's the same data, but now hashes link each item in the chain. If an attacker attempted to publish a hacked version (say they change 7.97 to 7.92), then the hashes would no longer match and the blockchain would be outed as an obvious fraud. Another useful property of blockchains is that I can continue to append new readings without weakening the integrity of previous entries.

As to how this powers Bitcoin, well your guess is as good as mine. Implementing a crypto-currency will have to be another exercise for another day. In the mean time, check out the source code of my solution and enjoy this video that details What Bitcoin is and How It Works.

No comments:

Post a Comment