Search
  • BBCA

Lets Look At The Code

by Luca Burkhardt

In this week’s Christmas edition, I thought we’d get our hands dirty by looking at some actual code! Yes, it may sound complicated, but as usual, it’s much easier than you might expect.


All in all, a blockchain must be able to do certain things. First, it must contain the format of an individual block. Next, it must be able to add new blocks to the chain as well as add new transactions to the list of transactions within a block.


After defining all of these functions, this is what a blockchain class is meant to look in its most basic form. Note that this code is in Python.

In this weeks article, we will just have a look at the first two elements: An individual block and how transactions are added to the block.


Before I begin, it is import to note what classes, objects, methods and functions are. In object-oriented programming, a class is basically a template for creating objects. Objects are simply the elements within the classes. In this case, the class is called “Blockchain” and the the objects i.e. elements are “new_block” or “new_transcation”. For those more familiar with python, objects are just functions within a class.


Let’s begin with an official block. A block consists of an index, timestamp, a list of transactions, a proof (more on that later), and the hash of the previous block. The timestamp allows the blocks to be order objectively. The proof of work is contains a certain hash value which determines the difficulty of the mining process. Of course, the transaction itself contains the sender, the recipient as well as the amount. Finally, the hash of the previous block ensures immutability and is the core idea behind blockchain. In code, these elements look like this:

So how do I add a new transaction to the block. This method is defined in the “new_transaction” method. This method is essentially divided into two steps. Firstly, the details of the new transaction are defined. In the self.current_transactions.append({ the new transaction is added to the object which contains the list of transactions. The self variable is specific to classes and

After understanding these simple pieces of code, the next articles will include the creation of new blocks and proof of work as well as the several auxiliary methods.

#Blockchain

13 views

©2018 by BBCA.

  • Facebook Social Icon
  • LinkedIn Social Icon
  • BBCA instagram
  • BBCA Medium