Blockchain Basics
15 Aug 2017A blockchain is a linked list of record items that are chained together with hashes. To make it a little more concrete, each subsequent block in a chain contains its predecessors hash as a piece of the information made up to make its own hash.
This forms a strong chain of records that is very difficult to change without re-processing all of the ancestor records.
Each record in the chain typically stores:
- A timestamp
- The actual data for the block
- A reference to the predecessor block
In today’s post, I’ll try to continue this explanation using an implementation written in C++.
A simple implementation
It’ll be a pretty easy build. We’ll need a block
class, which really does all of the work for us. We’ll need a way to hash
a block in a way that gives us a re-usable string. Finally, we’ll put the whole implementation using a vector
.
The block
We need a timestamp, the actual data and the hash of the predecessor.
In this class, _ts
assumes the role of the timestamp; _data
holds an arbitrary string of our data and _prev_hash
will be the hex string of the hash from the previous record.
The block needs a way of hashing all of its details to produce a new hash. We’ll do this by concatenating all of the data within the block, and running it through a SHA256 hasher. I found a really simple implementation here.
_ts
, _data
and _prev_hash
get concatenated and hashed.
Now we need a way to seed a chain, as well as build subsequent blocks. Seeding a list is nothing more than just generating a single block that contains no previous reference:
Really simple. Empty string can be swapped out for nullptr
should we want to add some more branches to the hasher and change the internal type of _prev_hash
. This will do for our purposes though.
The next blocks need to be generated from another block; in this case b
. We use its hash to populate the _prev_hash
field of the new block.
This is the key part of the design though. With the previous block making in to being a part of the concatenated string that gets hashed into this new block, we form a strong dependency on it. This dependency is what chains the records together and makes it very difficult to change.
Finally, we can test out our implementation. I’ve created a function called make_data
which just generates a JSON string, ready for the _data
field to manage. It simply holds 3 random numbers; but you could imagine that this might be imperative data for your business process.
Running this code, we can see that the chains are printed to screen:
Note that index isn’t a member of the class; it just counts while we’re iterating over the vector. The real membership here is established through the _prev_hash
; as discussed above.
Where to?
Now that the storage mechanism is understood, we can apply proof-of-work paradigms to attribute a sense of value to our records. More information on how this has been applied can be read up in the following:
The full source code for this article can be found here.