About two weeks ago I realized why I had such an animosity towards bitcoin: I didn’t own any and I didn’t understand it. I decided to start learning about bitcoin through researching the technology behind it, aka block chain. I learned through creating a python script that builds a block chain, so I thought I would share it with others who would like to learn more about block chain. To clarify, while I was inspired by bitcoin, this post is focused on block chain.
Contents of a Block
In general, a block contains transaction data, a timestamp, and a link to a previous block. In my implementation of a block, I created a Block class with the following attributes:
- Index: keeps track of location of block within the chain
- Timestamp: the date / time the block was created
- Data: the actual data that is stored in the block (like who bought how much)
- Previous Hash: the hash of the previous block in the chain
- Hash: the hash of the block; if you are not familiar with hashing, it essentially maps data of an arbitrary length to data of fixed size; the hash is essentially a string that represents the block
I calculate the hash of a block with the SHA-256 algorithm, which is a cryptographic hash algorithm. I input into the hash algorithm the attributes that represent the block, so the index, timestamp, data and previous hash. The “calculateHash” function then returns a 256-bit string based on that input.
Initializing a Chain
To start, the first block in a block chain is called the genesis block. This block is just the original block that starts off the chain. In my implementation, when a block chain is created, the genesis block is automatically created.
Adding a block
In order to add a new block to the chain, you need to get the hash of the previous block and then calculate the hash of the new block. Because the previous block’s hash is an input to the hash function, calculating the hash of the new block has to be done after the previous hash is retrieved. The “getLatestBlock” method is just used to get the last block on the chain, so when you are adding a new block, that is the previous block.
Securing the chain
Block chains are resistant to modifications by design. Part of the security of a block chain is ensuring that the links between the blocks have not been tampered with, so the previous hash in block #3 is actually equivalent to the hash in block #2. This is something I check for in my “isChainValid” method. The other part of securing a block chain is ensuring that the data in the block has not been altered since the block was created. For example, if someone tried to change who completed a transaction inside a block, this must be prevented to ensure security. If the data in the block has been tampered with, then the hash of the block would also change, since the data is an input to the hash function. The change in hash value is also accounted for in the “isChainValid” method in order to check if the data has changed since the block was created.
Testing the Block Chain with Anna Coin
Below is the code that creates a BlockChain object called annaCoin and adds blocks to it. I prove the security of annaCoin through checking the validity of the block chain before any tampering as well as after I attempt to tamper a block.
The output of this function is this:
Note: I did make some print functions in my Block Chain and Block classes in order to make the block chain more readable when testing. I did not include the print functions in the earlier code snippets for simplifications.
The full script can be found here
Originally published on blog.goodaudience.com