How I built BlueCoin blockchain using Python

How I built BlueCoin blockchain using Python

Welcome,

In this article, I'm going to be explaining my concept of building a blockchain around a group of friends using python.

But first we need to know a few things.

Starting with the basics...

What is Blockchain?

A blockchain is a type of ledger (book, record book, account book, register) shared amongst participants in a network. It is a secure means of transacting assets either physical or non-physical and once data is entered it cannot be altered, that is It is Immutable. The accuracy of a blockchain is dependent on how fast it is received, the faster it is received the more accurate it becomes.

It is used for delivering information on the ledger which can only be accessed by authorized/licensed members of the network and since all members in the network have access to the same ledger, a transparent view of every transaction is available. This boosts trust and confidence, as well as possibilities for efficiency.

Common Misconceptions around Blockchain

  1. Blockchain is just for cryptocurrencies: Blockchain was originally built for Bitcoin but it has many potential uses asides crypto. For example, it can be used for voting systems, securing records etc.

  2. Blockchains are completely secure: Blockchains are indeed secure by design but it doesn't mean they're not susceptible to breaches and attacks. Therefore it is important to have proper security measures to ensure the security of a blockchain.

  3. It eliminates the need for intermediaries: The fact that it can reduce the need for intermediaries doesn't mean it is a complete replacement for intermediaries.

  4. Not just tech experts can use blockchain: Although having some technical knowledge is necessary to fully utilize blockchain technology, there are now various user-friendly applications and platforms that allow people with no technical background to benefit from and use blockchain easily.

It is important to have a clear understanding of blockchain, its capabilities and its limitations for an accurate assess of its benefits.

What is Python?

Python is an interpreted, high-level programming language. It is also a multi-purpose and multi-paradigm type of programming language useful for a range of applications that incorporates web development, data analysis, scientific computing etc. and supports more than one programming style for tasks.

Programming Language issues around building a blockchain.

When building a blockchain, there can be several programming language related issues, including:

  1. Lack of familiarity with the language: Building a blockchain requires knowledge of advanced programming concepts, such as cryptography and consensus algorithms, so it's important to choose a language that you are comfortable working with.

  2. Performance limitations: Some programming languages may not be well-suited for blockchain development due to performance limitations. For example, languages with a high level of abstraction may not be able to handle the complex and resource-intensive calculations required for blockchain consensus.

  3. Scalability issues: The language chosen for blockchain development must be scalable to accommodate the growing demands of the network. For example, the language must be able to handle the increasing number of transactions and blocks.

  4. Lack of development resources: Some programming languages may have limited support or a small community of developers, making it harder to find help when you need it.

  5. Lack of integration with other technologies: Blockchains often require integration with other technologies, such as databases, and it's important to choose a language that can easily integrate with these technologies.

Why I used Python for this project?

Honestly, asides the simplicity of its syntax, I am personally comfortable with the ease of use and versatility of the language. Additionally, it has an active community of developers contributing to its development.

Let's get to it...

About BlueCoin

This blockchain project aims to secure and make transparent the transactions of a virtual coin called BlueCoin among a group of friends. The transactions are stored in blocks to ensure their safety. This project addresses the issue of unreliable and opaque transactions within a network.

Why I built it?

This project intends to show you how valued assets are been transferred through blocks. The term "blocks" refers to digital information that is linked together through a cryptographic hash of the preceding block, creating a chain.

How did I build it?

To begin, we need to create a Python file and give it a desired name.

Mine is Blockchain.py

Next, you need to import the hash library in your editor.

import hashlib

Then, create a class with a desired name (for example, BlueCoin).

class BlueCoin:

Then instantiate the class like this:

def __init__(self, prev_block_hash, transaction_list):
self.prev_block_hash = prev_block_hash
self.transaction_list = transaction_list

Now you should have this:

import hashlib

class BlueCoin:
    def __init__(self, prev_block_hash, transaction_list):
        self.prev_block_hash = prev_block_hash
        self.transaction_list = transaction_list

Now let's construct a data string:

self.blockData = " - " .join(transaction_list) + " - " + prev_block_hash
self.blockHash = hashlib.sha256(self.blockData.encode()).hexdigest()

Note: this " - " is used as a separator. Also sha256 stands for Secure Hash Algorithm 256-bit and it's used for cryptographic security.

Let's write some transactions:

t1 = "Tom sends 5 BC to Dan"
t2 = "Dan sends 2 BC to David"
t3 = "David sends 3.5 BC to Bruce"
t4 = "Bruce sends 0.3 BC to Mary"
t5 = "Mary sends 1.5 BC to Tom"
t6 = "Anna send 3 BC to Dan"

BC represents BlueCoin, t1 stands for transaction 1, t2 stands for transaction 2 and so on.

Now, we will create the initial block and include a list of transactions.

initial_block = BlueCoin("Initial String", [t1, t2])
OR
initial_block = BlueCoin("", [t1, t2])

In the code above, either an initializing message can be passed or an empty string.

Let's see an output:

print(initial_block.blockData)
print(initial_block.blockHash)

Regardless of the transaction input you choose, the result should be in this format:

Tom sends 5 BC to Dan - Dan sends 2 BC to Anna - Initial String 2ee4f69ec6271fdbe6ff45f02ce1e378e432813c477dc95f12fc59fd8c72666f

Please note that any changes made to a transaction will result in a completely different hash. For instance, if Tom sends 6 GC to Dan, the hash will be different as demonstrated below.

Tom sends 6 BC to Dan - Dan sends 2 BC to Anna - Initial String 720a696c4dcfa3432e775f327b0d7837ff16e21ca050f1dbb345f41bd1609d11

Let's Continue.

Next, we need to create additional blocks for the remaining transactions.

second_block = BlueCoin(initial_block.blockHash, [t3, t4])
print(second_block.blockData)
print(second_block.blockHash)

third_block = BlueCoin(second_block.blockHash, [t5, t6])
print(third_block.blockData)
print(third_block.blockHash)

Run the file and see your results.

Summarily, the integrity of a blockchain transaction cannot be altered because the blockchain relies on the unalterable sequence of the transaction string.

Conclusion

This is a basic overview of a blockchain and it helps to guide you on creating one using Python. However, other programming languages can also be used for blockchain and there is more to learn about the technology. You can choose the language that fits you best.