Home Blockchain Build a blockchain application from scratch in Python: Understanding Blockchain | by Ngoc Bui | DataDrivenInvestor

Build a blockchain application from scratch in Python: Understanding Blockchain | by Ngoc Bui | DataDrivenInvestor

by Assessor
Published: Last Updated on

Rate this post

I’m not a blockchain professional. I solely studied blockchain for a semester. However I do know the best solution to study one thing is to construct it from scratch. I’ll have probabilities to cope with the problems throughout constructing it. I’m blissful to share my view on the method of constructing a totally useful utility: A Easy Blockchain-based Voting System (video demo). Open supply is offered right here.

This text consists of two components:Half 1: Understanding blockchain. Half 2: A Easy Blockchain-based Voting System (not prepared now).

Blockchain formation. The principle chain (black) consists of the longest collection of blocks from the genesis block (inexperienced) to the present block. Orphan blocks (purple) exist outdoors of the primary chain. ( supply: wiki )

We are able to merely perceive blockchain is a sort of decentralized database used to retailer knowledge, any kind of information. The important thing distinction is that knowledge is saved within the type of blocks, a series of blocks utilizing a hash operate. For Bitcoin, the info may be the transactions (logs of transfers of Bitcoin from one account to a different). The chain of blocks is linked by hashes the place the hash of the present block included the hash of the earlier block.

A Easy Blockchain Structure

Blockchain sustains the next traits:

  • Decentralization: It means any data saved in blockchain acts as a unit of the entire community. There isn’t a centralized authority; as an alternative, all the knowledge is shared amongst nodes.
  • Immutability: The info of blockchain is append-only. No delete. You need to add new annotated knowledge to repair one thing prior to now. It makes knowledge on the blockchain traceable.
  • Distributed ledgers: Blockchain may be public or non-public. But it surely at all times belongs to a bunch, to not anybody. It’s sometimes managed by a peer-to-peer community collectively adhering to a protocol for inter-node communication and validating new blocks.

The Core Concept Behind Blockchain

A bit little bit of historical past, in 2008, to implement a system the place doc timestamps couldn’t be tampered with out requiring a trusted social gathering, Nakamoto improved the design of Merkle timber by utilizing a Hashcash-like methodology to timestamp blocks. Hashcash is a proof-of-work system that was first used to restrict electronic mail spam and denial-of-service assaults. Hashcash algorithm requires the sender so as to add an encoding of hashcash stamp to the header of the e-mail. The header requires the recipient’s electronic mail tackle, the date of the message, the counter and the necessary situation, the primary 20 bits of the 160-bit SHA-1 hash of the header are all zeros. The sender has to search out the suitable counter to fulfill the above situation. The one recognized manner to do this is brute power. The recipient can simply verify the situation of the e-mail. If it satisfies, the recipient is aware of that the sender has misplaced a specific amount of computations to ship an electronic mail, to allow them to consider that the sender can’t ship this mail to everybody.

The Hashcash-like methodology in blockchain works with the identical mechanism. The info is append-only and arranged as blocks. A block consists of the info, earlier hash, and nonce quantity. The nonce quantity performs a job because the counter and the present hash because the hashcash within the electronic mail instance.

current_hash = hash(previous_hash + knowledge + nonce)

The variety of first zeros within the present hash is known as the issue parameter. The better the issue, the decrease the chance of discovering the suitable nonce. With the intention to append some knowledge, it’s important to create a brand new block and discover a nonce that makes the present hash illustration has the variety of first zeros sufficient. It means it’s important to commerce a specific amount of computational energy so as to add a brand new block. This algorithm is known as proof of labor and this course of is known as mining.

Because the hash of a block shall be within the header of the subsequent block, it’s important to recalculate the next blocks if you wish to change knowledge prior to now. It additionally signifies that it’s potential to have many various branches and many various knowledge variations. In that case, blockchain maintains a protocol guaranteeing that the longest chain shall be handled as the primary chain. This protocol is known as consensus.

These days, there are numerous kinds of blockchain with totally different consensus algorithms. We won’t talk about them right here.

Why are “hashcash”, “chain”, and “longest-chain”?

With the intention to comprehend, we contemplate a state of affairs referred to as a 51% assault, which is posed when Bitcoin was born. Let think about that you simply be a part of Bitcoin and use Bitcoin to purchase a automobile. No trusted social gathering in Bitcoin. It has to validate your transaction by itself ( checking if there may be sufficient cash in your account, this course of shall be validated by different folks within the Bitcoin community ). Suppose you’re a hacker and also you need to cheat that transaction. Bitcoin doesn’t retailer your stability (Bitcoin hint all your transactions prior to now to get your stability). The one factor you are able to do is to make the earlier transaction disappear from the Bitcoin database. How?

You may clone blockchain to your machine, take away your transaction, and proceed sharing within the community and validating different transactions. You’re going to get your personal department. On this department, you bought a automobile with out cash.

The issue is different folks within the blockchain community solely settle for the longest chain. You need to make different folks consider that your chain is the true one. Thus, it’s important to make your chain longer than others. However if you mine your block, others are additionally mining in the primary chain. As a consequence, you will need to have a pc with better computing energy than all others mixed. In different phrases, your computational energy should be a minimum of 51% of the computing energy of the complete community to meet up with the primary chain. With a public community like Bitcoin, it’s potential to contemplate it as not possible.

On this part, we implement a easy blockchain from scratch by Python. This piece is a summarization of the incredible article. This publish goals that will help you simpler to observe supply code, you must run it for your self and print all issues that you simply confuse. When you have already recognized what it’s, verify half 2 to construct a totally useful Blockchain-based Voting System.

First, you want a category to symbolize a block:

and a category to symbolize a blockchain:

transactions are the info which are saved in JSON format. For instance, in Bitcoin, it’ll seem like:

{ “index”: “an distinctive id”, “sender”: “non-public key of sender”, “receiver”: “public key of receiver”, “quantity”: 100, “timestamp”: “The time at which the transaction was created”}

unconfimed_transactions is a pool that shops unvalidated transactions. Miner will decide transactions from the pool to validate.

The primary block within the blockchain is at all times constructed with previous_hash is 0. With the intention to add a brand new block to the blockchain, you will need to implement the proof of labor algorithm which can discover acceptable nonce by brute-force and mine operate which can get unconfirmed_transactions and attempt to validate these transactions and run proof_of_work and add a brand new block to the chain.

add_block ought to be simply appending a brand new block. However the issue happens when there are numerous folks mining on the identical time. They could get a brand new block earlier than us, then the chain we use is now not the longest chain. So we should verify the validity of our chain earlier than including.

We virtually had a blockchain that may run on a single laptop. The following factor we should always do is create an utility utilizing this blockchain. We leverage the Flask framework to construct a RESTful utility that interacts with our blockchain database.

Lastly, to hitch a blockchain community, it’s important to adhere to the protocol for inter-node communication, which is the consensus algorithm.

Up so far, we now have understood what blockchain is, and find out how to implement a easy one. Within the subsequent publish, I need to share with you find out how to implement a totally useful blockchain-based utility with totally different structure. I additionally need to introduce a brand new idea in blockchain, smart-contract, and find out how to deal with it.

  1. https://developer.ibm.com/applied sciences/blockchain/tutorials/develop-a-blockchain-application-from-scratch-in-python/
  2. https://en.wikipedia.org/wiki/Blockchain
  3. https://en.wikipedia.org/wiki/Hashcash
  4. https://www.coindesk.com/moscow-blockchain-voting-system-completely-insecure-says-researcher
  5. https://steemit.com/blockchain/@abdulganiy/understanding-decentralised-and-distributed-databases
  6. https://weblog.goodaudience.com/what-is-a-51-attack-or-double-spend-attack-aa108db63474

Related Posts