{"id":14143,"date":"2020-01-26T11:15:02","date_gmt":"2020-01-26T15:15:02","guid":{"rendered":"https:\/\/101blockchains.com\/?p=14143"},"modified":"2023-07-14T03:14:54","modified_gmt":"2023-07-14T07:14:54","slug":"build-a-blockchain-in-python","status":"publish","type":"post","link":"https:\/\/101blockchains.com\/build-a-blockchain-in-python\/","title":{"rendered":"How to Build a Blockchain in Python?"},"content":{"rendered":"<p style=\"text-align: justify;\">If you want to learn how to build a <a href=\"https:\/\/101blockchains.com\/blockchain-technology-explained\/\" target=\"_blank\" rel=\"noopener noreferrer\">blockchain<\/a>, then you have come to the right place. Let&#8217;s dive deep to learn how you can build a blockchain in python.<\/p>\n<p style=\"text-align: justify;\">There are tons of articles around blockchain, but not all of them talk about building a blockchain from scratch. As a developer, theories are important, but you also need to create a blockchain to understand the concept behind it completely.<\/p>\n<p style=\"text-align: justify;\">The core idea behind blockchains is its decentralized nature. You will be fascinated by the fact of how it all works inside. Blockchain might sound simple, but inside there are a lot of protocols and algorithms that make it happen. We are going to use Python to create a blockchain from scratch.<\/p>\n<p style=\"text-align: justify;\">You can also save the article and bookmark it as, &#8220;How to build our blockchain.&#8221; Believe me; the article will help you in the long run.<\/p>\n<blockquote><p>Build your identity as a certified blockchain expert with 101 Blockchains\u2019\u00a0<a href=\"https:\/\/101blockchains.com\/certifications\/\" target=\"_blank\" rel=\"noopener\" data-wpel-link=\"internal\">Blockchain Certifications<\/a>\u00a0designed to provide enhanced career prospects.<\/p><\/blockquote>\n<h2>How To Build A Blockchain<\/h2>\n<p style=\"text-align: justify;\">The best way to learn anything is to learn by doing. To make sure you are on the same page as me, let\u2019s check out the requirements first. We will also briefly describe blockchain so that we both are on the same page.<\/p>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/How-to-Build-A-Blockchain-In-Python.png\"><picture><source srcset=\"https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/How-to-Build-A-Blockchain-In-Python-1024x1024.png.webp 1024w,https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/How-to-Build-A-Blockchain-In-Python-300x300.png.webp 300w,https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/How-to-Build-A-Blockchain-In-Python-150x150.png.webp 150w,https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/How-to-Build-A-Blockchain-In-Python-768x768.png.webp 768w,https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/How-to-Build-A-Blockchain-In-Python-1536x1536.png.webp 1536w,https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/How-to-Build-A-Blockchain-In-Python-600x600.png.webp 600w,https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/How-to-Build-A-Blockchain-In-Python-100x100.png.webp 100w,https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/How-to-Build-A-Blockchain-In-Python.png.webp 2000w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" type=\"image\/webp\"><img src=\"https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/How-to-Build-A-Blockchain-In-Python-1024x1024.png\" height=\"1200\" width=\"1200\" srcset=\"https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/How-to-Build-A-Blockchain-In-Python-1024x1024.png 1024w, https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/How-to-Build-A-Blockchain-In-Python-300x300.png 300w, https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/How-to-Build-A-Blockchain-In-Python-150x150.png 150w, https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/How-to-Build-A-Blockchain-In-Python-768x768.png 768w, https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/How-to-Build-A-Blockchain-In-Python-1536x1536.png 1536w, https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/How-to-Build-A-Blockchain-In-Python-600x600.png 600w, https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/How-to-Build-A-Blockchain-In-Python-100x100.png 100w, https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/How-to-Build-A-Blockchain-In-Python.png 2000w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" class=\"alignnone wp-image-14250 sp-no-webp\" alt=\"How to Build A Blockchain In Python\" loading=\"lazy\" decoding=\"async\"  > <\/picture><\/a><\/p>\n<pre>Please include attribution to 101blockchains.com with this graphic. &lt;a href='https:\/\/101blockchains.com\/blockchain-infographics\/'&gt; &lt;img src='https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/How-to-Build-A-Blockchain-In-Python.png' alt='How to Build A Blockchain In Python='0' \/&gt; &lt;\/a&gt;\r\n<\/pre>\n<h3>Requirement<\/h3>\n<p style=\"text-align: justify;\">The basic requirement to follow the guide is to know how to code. If you have never coded in your life, then this guide is not for you. I recommend checking out the various beginner-level courses that are available online. I recommend EdX, Udacity, and Coursera for the best learning experience.<\/p>\n<p style=\"text-align: justify;\">You already a coder? Then, you should know about basic Python. Also, it will help you to follow the \u201cHow To Build A Blockchain\u201d tutorial.<\/p>\n<p style=\"text-align: justify;\">We created the tutorial so anyone can follow. So, if you do not know and want to learn how to build a blockchain from scratch, you are welcome to do so!<\/p>\n<p style=\"text-align: justify;\">\u00a0If you do not know Python, do not worry as it is easy to pick up. Check out the <a href=\"https:\/\/docs.python.org\/3\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python documentation<\/a> to get started.<\/p>\n<p style=\"text-align: justify;\">We are going to use Python 3.6+ for the tutorial. If you decide to use other versions of Python, then you might have to do more research for the code to work. For tutorial purposes, I recommend installing the version that I am using.<\/p>\n<p style=\"text-align: justify;\">Lastly, you should also know about HTTP &#8212; the protocol that is used to communicate between two parties on the internet.<\/p>\n<p style=\"text-align: justify;\">Installing Python using pip<\/p>\n<p style=\"text-align: justify;\">You need to install Python using pip. You also need to install wonderful Requests and Flask library. To do so, simply run the command below.<\/p>\n<pre>$pip install Flask==0.12.2 requests==2.18.4<\/pre>\n<p style=\"text-align: justify;\">If you are using the Windows environment, you can directly go to python.org to download the latest version of Python.<\/p>\n<p style=\"text-align: justify;\">As for me, I downloaded the Python 3.8.1 &#8212; the latest build at the time of writing the tutorial.<\/p>\n<p style=\"text-align: justify;\">For the IDE, I prefer using PyCharm. I am using the Python Community edition for the tutorial. You are free to use any IDE of your choice. Another great alternative is Visual Studio Code &#8212; a free, open-source integrated development environment.<\/p>\n<p style=\"text-align: justify;\">The last tool that you are going to need is Postman &#8212; an HTTP client. Alternatively, you can also use cURL.<\/p>\n<p style=\"text-align: justify;\">I also recommend installing a virtual environment to create a completely different environment for your development. You can follow <a href=\"https:\/\/timmyreilly.azurewebsites.net\/python-flask-windows-development-environment-setup\/\" target=\"_blank\" rel=\"noopener noreferrer\">this guide<\/a> to set it up properly.<\/p>\n<p><a href=\"https:\/\/101blockchains.com\/certification\/blockchain-expert-certification\/\" target=\"_blank\" rel=\"noopener\"><picture><source srcset=\"https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/05\/Certified-Enterprise-Blockchain-Professional-1.webp 1200w,https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/05\/Certified-Enterprise-Blockchain-Professional-1-300x23.webp 300w,https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/05\/Certified-Enterprise-Blockchain-Professional-1-1024x77.webp 1024w, https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/05\/Certified-Enterprise-Blockchain-Professional-1-768x58.png 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" type=\"image\/webp\"><img src=\"https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/05\/Certified-Enterprise-Blockchain-Professional-1.png\" height=\"90\" width=\"1200\" srcset=\"https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/05\/Certified-Enterprise-Blockchain-Professional-1.png 1200w, https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/05\/Certified-Enterprise-Blockchain-Professional-1-300x23.png 300w, https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/05\/Certified-Enterprise-Blockchain-Professional-1-1024x77.png 1024w, https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/05\/Certified-Enterprise-Blockchain-Professional-1-768x58.png 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" class=\"aligncenter wp-image-43445 size-full sp-no-webp\" alt=\"certified enterprise blockchain professional\" loading=\"lazy\" decoding=\"async\"  > <\/picture><\/a><\/p>\n<h3>Blockchain Revisited<\/h3>\n<p style=\"text-align: justify;\">Before we go and build the blockchain, we need to refresh our idea of blockchain. This will help us to stay on the same page.<\/p>\n<p style=\"text-align: justify;\">Blockchain is a <a href=\"https:\/\/101blockchains.com\/distributed-ledger-technology\/\" target=\"_blank\" rel=\"noopener noreferrer\">distributed ledger technology<\/a> where peers can participate, interact, and execute transactions without the need for a centralized entity. Each peer has a copy of the ledger, which makes it impossible for hackers to do fraud in the system. The key <a href=\"https:\/\/101blockchains.com\/introduction-to-blockchain-features\/\" target=\"_blank\" rel=\"noopener\">features of blockchain<\/a> include transparency, immutability, and security. It utilizes advanced <a href=\"https:\/\/101blockchains.com\/what-is-a-cryptocurrency\/\" target=\"_blank\" rel=\"noopener\">cryptocurrency<\/a> algorithms to ensure data security. Also, to reach consensus, a blockchain network will utilize <a href=\"https:\/\/101blockchains.com\/consensus-algorithms-blockchain\/\" target=\"_blank\" rel=\"noopener\">consensus algorithms<\/a> such as <a href=\"https:\/\/101blockchains.com\/blockchain-proof-of-work\/\" target=\"_blank\" rel=\"noopener\">Proof-of-Work<\/a>, <a href=\"https:\/\/101blockchains.com\/proof-of-stake\/\" target=\"_blank\" rel=\"noopener\">Proof-of-Stake<\/a>, and so on.<\/p>\n<blockquote><p>Start your blockchain journey Now with the\u00a0<a href=\"https:\/\/101blockchains.com\/free-blockchain-course\/\" target=\"_blank\" rel=\"noopener\" data-wpel-link=\"internal\">Enterprise Blockchains Fundamentals<\/a><\/p><\/blockquote>\n<h3>Getting Started With Building a Blockchain<\/h3>\n<p style=\"text-align: justify;\">With all the prerequisite installations done, we are now ready to get started with our tutorial, \u201cHow to Build A Blockchain.\u201d<\/p>\n<p style=\"text-align: justify;\">To get started, you need to create a blockchain.py file.<\/p>\n<h4>1. Blockchain Creation<\/h4>\n<p style=\"text-align: justify;\">The first step is to make a functional blockchain solution. At first, we need to create a Blockchain class. There we will create a constructor that will initiate the chain and transaction list. The chain list will store our blockchain, whereas the transactions will be stored in the current_transacations array.<\/p>\n<pre>def __init__(self):\r\n\r\n\u00a0\u00a0\u00a0self.chain = []\r\n\r\n\u00a0\u00a0\u00a0self.current_transactions = []<\/pre>\n<p style=\"text-align: justify;\">Next, we need to create a new_block function which will be used to create new blocks and then add them to the existing chain. The code for the new_block function is as below:<\/p>\n<pre>def new_block(self):\r\n\r\n\u00a0\u00a0\u00a0#This function creates new blocks and then adds to the existing chain\r\n\r\n\u00a0\u00a0\u00a0pass<\/pre>\n<p style=\"text-align: justify;\">The new_transcaction method will create a new transaction and then add the transaction to the already existing transaction list.<\/p>\n<pre>def new_transaction(self):\r\n\r\n\u00a0\u00a0\u00a0#This function adds a new transaction to already existing transactions\r\n\r\n\u00a0\u00a0\u00a0pass<\/pre>\n<pre style=\"text-align: justify;\">We also have a hash function that is used to create the hash for a block.<\/pre>\n<pre>\u00a0@staticmethod\r\n\r\ndef hash(block):\r\n\r\n\u00a0\u00a0\u00a0#Used for hashing a block<\/pre>\n<p style=\"text-align: justify;\">The last function that we are going to create last_block. It is used to call the last block in the chain.<\/p>\n<p style=\"text-align: justify;\">We can also create three more functions here.<\/p>\n<ul style=\"text-align: justify;\">\n<li style=\"font-weight: 400;\">register_node() \u2192 To register a new node and add it to the network<\/li>\n<li style=\"font-weight: 400;\">valid_proof() \u2192 Will ensure wheather a submitted block to the chain solves the problem<\/li>\n<li style=\"font-weight: 400;\">valid_chain() \u2192 This will check if the subsequent blocks in the chain are valid or not.<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">The blueprint for the blockchain.py looks as below.<\/p>\n<pre>class Blockchain(object):\r\n\r\n\u00a0\u00a0\u00a0def __init__(self):\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0self.chain = []\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0self.current_transactions = []\r\n\r\n\r\n\u00a0\u00a0\u00a0def new_block(self):\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#This function creates new blocks and then adds to the existing chain\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0pass\r\n\r\n\r\n\u00a0\u00a0\u00a0def new_transaction(self):\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#This function adds a new transaction to already existing transactions\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0pass\r\n\r\n\r\n\u00a0\u00a0\u00a0@staticmethod\r\n\r\n\u00a0\u00a0\u00a0def hash(block):\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#Used for hashing a block\r\n\r\n\r\n\u00a0\u00a0\u00a0@property\r\n\r\n\u00a0\u00a0\u00a0def last_block(self):\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0# Calls and returns the last block of the chain\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0pass<\/pre>\n<p style=\"text-align: justify;\">The Blockchain class manages the whole chain. The definitions defined under the class manages the different actions within the blockchain.<\/p>\n<p style=\"text-align: justify;\">Let\u2019s go through each one of the methods below.<\/p>\n<p><strong>Structure of a block<\/strong><\/p>\n<p style=\"text-align: justify;\">A block contains vital information about transactions and chains. Any block on the chain will contain the following information<\/p>\n<ul style=\"text-align: justify;\">\n<li style=\"font-weight: 400;\">index<\/li>\n<li style=\"font-weight: 400;\">timestamp in Unix time<\/li>\n<li style=\"font-weight: 400;\">transactions list<\/li>\n<li style=\"font-weight: 400;\">proof<\/li>\n<li style=\"font-weight: 400;\">Previous block hash<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">Most of the information within the block are self-explanatory. Here, the two crucial variables are the previous_hash, which contains the hash value of the previous block. This is important as it makes sure that the blockchain is immutable, and no malicious actor can update, delete or add any data to the chain.<\/p>\n<p style=\"text-align: justify;\"><strong>Adding Transaction To A Block<\/strong><\/p>\n<p style=\"text-align: justify;\">With the structure of the block understood, let\u2019s now learn how to add a transaction to the block. The code for it is as below.<\/p>\n<pre>def new_transaction(self):\r\n\r\n\u00a0\u00a0\u00a0#This function adds a new transaction to already existing transactions\r\n\r\n\u00a0\u00a0\u00a0\"\"\"This will create a new transaction which will be sent to the next block. It will contain\r\n\r\n\u00a0\u00a0\u00a0three variables including sender, recipient and amount\r\n\r\n\u00a0\u00a0\u00a0\"\"\"\r\n\r\n\u00a0\u00a0\u00a0self.current_transactions.append(\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'sender': sender,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'recipient': recipient\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'amount': amount,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0)\r\n\r\n\u00a0\u00a0\u00a0return self.last_block['index'] + 1<\/pre>\n<pre style=\"text-align: justify;\">As you can see, it simply appends the current_transaction list with an object that contains three variables \u2192 sender, recipient, amount.<\/pre>\n<p style=\"text-align: justify;\">Once the addition is done, the method then sends the block index to the chain. It is the next block that is going to be mined.<\/p>\n<blockquote><p>Start learning Blockchain with World\u2019s first\u00a0<a href=\"https:\/\/101blockchains.com\/skill\/\" target=\"_blank\" rel=\"noopener\" data-wpel-link=\"internal\">Blockchain Skill Paths<\/a>\u00a0with quality resources tailored by industry experts Now!<\/p><\/blockquote>\n<p style=\"text-align: justify;\"><strong>Creating new Blocks<\/strong><\/p>\n<p style=\"text-align: justify;\">Before we start creating new blocks, we need to create the genesis block. To do so, we need to use a simple line of code as below.<\/p>\n<pre>self.new_block(previous_hash=1, proof=100)<\/pre>\n<p style=\"text-align: justify;\">This line should be added to your Blockchain class.<\/p>\n<p style=\"text-align: justify;\">For the new_block() method, you need to send two variables as its parameters. It includes proof and previous_hash.<\/p>\n<p style=\"text-align: justify;\">Now, let\u2019s take a look at the new_block() method below.<\/p>\n<pre>def new_block(self, proof, previous_hash=None):\r\n\r\n\u00a0\u00a0\u00a0#This function creates new blocks and then adds to the existing chain\r\n\r\n\u00a0\u00a0\u00a0\"\"\"This method will contain two parameters proof, previous hash\"\"\"\r\n\r\n\u00a0\u00a0\u00a0block = {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'index': len(self.chain) + 1,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'timestamp' : time(),\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'proof': proof,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0previous_hash: previous_hash or self.hash(self.chain[-1]),\r\n\r\n\u00a0\u00a0\u00a0}\r\n\r\n<\/pre>\n<p># Set the current transaction list to empty.<\/p>\n<pre>\u00a0\u00a0\u00a0self.current_transactions=[]\r\n\r\n\u00a0\u00a0\u00a0self.chain.append(block)\r\n\r\n\u00a0\u00a0\u00a0return block<\/pre>\n<p style=\"text-align: justify;\">Let\u2019s explain the code of the new_block. We create a block list which contains the three important parameters. Once they are defined, we then also write the code to reset the current_transaction list and then append the block to the chain. The new_block method, when called, returns the block.<\/p>\n<p style=\"text-align: justify;\">Let\u2019s also define the last_block method below.<\/p>\n<p>@property<\/p>\n<p>def last_block(self):<\/p>\n<p># Calls and returns the last block of the chain<\/p>\n<p>return self.chain[-1]<\/p>\n<p>Also, the code for the hash() method is as below:<\/p>\n<pre>def hash(block):\r\n\r\n\u00a0\u00a0\u00a0#Used for hashing a block\r\n\r\n\u00a0\u00a0\u00a0\"\"\"The follow code will create a SHA-256 block hash and also ensure that the dictionary is ordered\"\"\"\r\n\r\n\u00a0\u00a0\u00a0block_string = json.dumps(block, sort_keys=True).encode()\r\n\r\n\u00a0\u00a0\u00a0return hashlib.sha256(block_string).hexdigest()<\/pre>\n<p style=\"text-align: justify;\">If you combine all the code until now, we will have the following blockchain.py. Check it out below.<\/p>\n<pre>class Blockchain(object):\r\n\r\n\u00a0\u00a0\u00a0def __init__(self):\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0self.chain = []\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0self.current_transactions = []\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0self.new_block(previous_hash=1, proof=100)\r\n\r\n\u00a0\u00a0\u00a0def new_block(self, proof, previous_hash=None):\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#This function creates new blocks and then adds to the existing chain\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"\"\"This method will contain two parameters proof, previous hash\"\"\"\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0block = {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'index': len(self.chain) + 1,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'timestamp' : time(),\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'proof': proof,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0previous_hash: previous_hash or self.hash(self.chain[-1]),\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0# Set the current transaction list to empty.\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0self.current_transactions=[]\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0self.chain.append(block)\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return block\r\n\r\n\u00a0\u00a0\u00a0def new_transaction(self):\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#This function adds a new transaction to already existing transactions\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"\"\"This will create a new transaction which will be sent to the next block. It will contain\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0three variables including sender, recipient and amount\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"\"\"\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0self.current_transactions.append(\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'sender': sender,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'recipient': recipient,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'amount': amount,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0)\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return self.last_block['index'] + 1\r\n\r\n\u00a0\u00a0\u00a0@staticmethod\r\n\r\n\u00a0\u00a0\u00a0def hash(block):\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#Used for hashing a block\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"\"\"The follow code will create a SHA-256 block hash and also ensure that the dictionary is ordered\"\"\"\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0block_string = json.dumps(block, sort_keys=True).encode()\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return hashlib.sha256(block_string).hexdigest()\r\n\r\n\u00a0\u00a0\u00a0@property\r\n\r\n\u00a0\u00a0\u00a0def last_block(self):\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0# Calls and returns the last block of the chain\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return self.chain[-1]<\/pre>\n<blockquote><p>Not sure how to build your career in enterprise blockchains? Enroll Now in\u00a0<a href=\"https:\/\/101blockchains.com\/course\/career-in-blockchain\/\" target=\"_blank\" rel=\"noopener\" data-wpel-link=\"internal\">How to Build Your Career in Enterprise Blockchains<\/a> Course<\/p><\/blockquote>\n<p style=\"text-align: justify;\"><strong>Proof of Work Implementation<\/strong><\/p>\n<p style=\"text-align: justify;\">Our next step in building blockchain.py is the Proof-of-Work implementation.<\/p>\n<p style=\"text-align: justify;\">So, what is <a href=\"https:\/\/101blockchains.com\/blockchain-proof-of-work\/\" target=\"_blank\" rel=\"noopener\">Proof of Work<\/a>? It is a consensus method that is utilized to add new blocks into the chain. It poses a challenge to the miner, and once the miner solves the problem, the block is then verified. The miner, in return, gets a reward based on the problem complexity.<\/p>\n<p style=\"text-align: justify;\">Bitcoin\u2019s Proof of Work is known as Hashcash. It ensures that the bitcoin network consensus work as intended.<\/p>\n<p style=\"text-align: justify;\">Let\u2019s define the proof_of_work() method. The method will be part of the Blockchain class.<\/p>\n<pre>def proof_of_work(self, last_proof):\r\n\r\n\u00a0\u00a0\u00a0\"\"\"This method is where you the consensus algorithm is implemented.\r\n\r\n\u00a0\u00a0\u00a0It takes two parameters including self and last_proof\"\"\"\r\n\r\n\u00a0\u00a0\u00a0proof = 0\r\n\r\n\u00a0\u00a0\u00a0while self.valid_proof(last_proof, proof) is False:\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0proof +=1\r\n\r\n\u00a0\u00a0\u00a0return proof\r\n\r\n@staticmethod\r\n\r\ndef valid_proof(last_proof, proof):\r\n\r\n\u00a0\u00a0\u00a0\"\"\"This method validates the block\"\"\"\r\n\r\n\u00a0\u00a0\u00a0guess = f'{last_proof}{proof}'.encode()\r\n\r\n\u00a0\u00a0\u00a0guess_hash = hashlib.sha256(guess).hexigest()\r\n\r\n\u00a0\u00a0\u00a0return guess_hash[:4] == \"0000\"<\/pre>\n<p style=\"text-align: justify;\">The first proof_of_work method is simple; it simply set proof to 0 and then run a while loop to run the valid_proof method. If it is False, then add one to the proof.<\/p>\n<p style=\"text-align: justify;\">You can increase the difficulty of the hash by adding more zeroes.<\/p>\n<p style=\"text-align: justify;\">This leads us to the end of our Blockchain.py. Our code stands as below.<\/p>\n<pre>class Blockchain(object):\r\n\r\n\u00a0\u00a0\u00a0def __init__(self):\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0self.chain = []\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0self.current_transactions = []\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0self.new_block(previous_hash=1, proof=100)\r\n\r\n\r\n<\/pre>\n<pre>\u00a0\u00a0\u00a0def proof_of_work(self, last_proof):\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"\"\"This method is where you the consensus algorithm is implemented.\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0It takes two parameters including self and last_proof\"\"\"\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0proof = 0\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0while self.valid_proof(last_proof, proof) is False:\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0proof +=1\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return proof\r\n\r\n\u00a0\u00a0\u00a0@staticmethod\r\n\r\n\u00a0\u00a0\u00a0def valid_proof(last_proof, proof):\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"\"\"This method validates the block\"\"\"\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0guess = f'{last_proof}{proof}'.encode()\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0guess_hash = hashlib.sha256(guess).hexigest()\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return guess_hash[:4] == \"0000\"\r\n\r\n\u00a0\u00a0\u00a0def new_block(self, proof, previous_hash=None):\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#This function creates new blocks and then adds to the existing chain\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"\"\"This method will contain two parameters proof, previous hash\"\"\"\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0block = {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'index': len(self.chain) + 1,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'timestamp' : time(),\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'proof': proof,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0previous_hash: previous_hash or self.hash(self.chain[-1]),\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0# Set the current transaction list to empty.\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0self.current_transactions=[]\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0self.chain.append(block)\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return block\r\n\r\n\u00a0\u00a0\u00a0def new_transaction(self):\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#This function adds a new transaction to already existing transactions\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"\"\"This will create a new transaction which will be sent to the next block. It will contain\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0three variables including sender, recipient and amount\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"\"\"\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0self.current_transactions.append(\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'sender': sender,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'recipient': recipient,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'amount': amount,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0)<\/pre>\n<p>&nbsp;<\/p>\n<p>return self.last_block[&#8216;index&#8217;] + 1<\/p>\n<p>@staticmethod<\/p>\n<p>def hash(block):<\/p>\n<p>#Used for hashing a block<\/p>\n<p>&#8220;&#8221;&#8221;The follow code will create a SHA-256 block hash and also ensure that the dictionary is ordered&#8221;&#8221;&#8221;<\/p>\n<p>block_string = json.dumps(block, sort_keys=True).encode()<\/p>\n<p>return hashlib.sha256(block_string).hexdigest()<\/p>\n<p>@property<\/p>\n<p>def last_block(self):<\/p>\n<p># Calls and returns the last block of the chain<\/p>\n<p>return self.chain[-1]<\/p>\n<blockquote><p>Curious to learn about blockchain implementation and strategy for managing your blockchain projects? Enroll Now in\u00a0<a href=\"https:\/\/101blockchains.com\/course\/blockchain-implementation-and-strategy\/\" target=\"_blank\" rel=\"noopener\" data-wpel-link=\"internal\">Blockchain Technology \u2013 Implementation And Strategy<\/a> Course!<\/p><\/blockquote>\n<h4 style=\"text-align: justify;\">2. API Integration<\/h4>\n<p style=\"text-align: justify;\">Great, we create a blockchain that works! Now, to use it effectively, we need to create an API(Application Programming Interface).<\/p>\n<p style=\"text-align: justify;\">To do so, we are going to use the popular Python Framework: Flask.<\/p>\n<p style=\"text-align: justify;\">Flask is a micro-framework, which means that it is lightweight, and you can add the libraries that you need. It also enables you to create Python function endpoint easily.<\/p>\n<p style=\"text-align: justify;\">To create APIs, we first need to ensure our blockchain can accept requests over HTTP. To do so, we have to create more methods.<\/p>\n<p style=\"text-align: justify;\">Now, we will modify the blockchain.py.<\/p>\n<pre># Creating the app node\r\n\r\napp = Flask(__name__)\r\n\r\nnode_identifier = str(uuid4()).replace('-','')\r\n\r\n# Initializing blockchain\r\n\r\nblockchain = Blockchain()\r\n\r\n@app.route('\/mine', methods=['GET'])\r\n\r\ndef mine():\r\n\r\n\u00a0\u00a0\u00a0return \"Mining a new Block\"\r\n\r\n\r\n\r\n\r\n@app.route('\/transactions\/new', methods=['POST'])\r\n\r\ndef new_transaction():\r\n\r\n\u00a0\u00a0\u00a0return \"Adding a new transaction\"\r\n\r\n\r\n\r\n\r\n@app.router('\/chain', methods=['GET'])\r\n\r\n\r\n\r\n\r\ndef full_chain():\r\n\r\n\u00a0\u00a0\u00a0response = {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'chain' : blockchain.chain,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'length' : len(blockchain.chain)\r\n\r\n\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0return jsonify(response), 200\r\n\r\n\r\n\r\n\r\nif __name__ == '__main__':\r\n\r\n\u00a0\u00a0\u00a0app.run(host=\"0.0.0.0\", port=5000)<\/pre>\n<p style=\"text-align: justify;\">The code is self-explanatory for the most part. We first do a node initiation. Next, we create a random node. Once done, we then initialize our Blockchain class. Once done, we need to create three endpoints as below.<\/p>\n<ul style=\"text-align: justify;\">\n<li style=\"font-weight: 400;\">\/mine endpoint<\/li>\n<li style=\"font-weight: 400;\">\/transactions\/new endpoint<\/li>\n<li style=\"font-weight: 400;\">\/chain endpoint<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">Finally, we run the server on 5000 port.<\/p>\n<p style=\"text-align: justify;\">The app is now almost created. All we need to do is create the transactions class method.<\/p>\n<pre>@app.route('\/transactions\/new', methods=['POST'])\r\n\r\n\u00a0\u00a0\u00a0def new_transaction():\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0values = request.get_json()\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0# Checking if the required data is there or not\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0required = ['sender','recipient','amount']\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if not all(k in values for k in required):\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return 'Missing values', 400\r\n\r\n\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0# creating a new transaction\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0index = blockchain.new_transaction(values['sender'], values['recipient', values['amount']])\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0response = {'message': f\u2019Transaction is scheduled to be added to Block No. {index}'}\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return jsonify(response), 201<\/pre>\n<p><strong>Mining Endpoint<\/strong><\/p>\n<p style=\"text-align: justify;\">The last endpoint that we are going to create is the mining endpoint. To do so, we have to make sure that it does the following three things.<\/p>\n<ul style=\"text-align: justify;\">\n<li style=\"font-weight: 400;\">Proof of Work calculation<\/li>\n<li style=\"font-weight: 400;\">Forge new blockchain and then add it to the chain<\/li>\n<li style=\"font-weight: 400;\">Reward the miner for his work<\/li>\n<\/ul>\n<pre>@app.route('\/mine', methods=['GET'])\r\n\r\ndef mine():\r\n\r\n\u00a0\u00a0\u00a0\"\"\"Here we make the proof of work algorithm work\"\"\"\r\n\r\n\u00a0\u00a0\u00a0last_block = blockchain.last_block\r\n\r\n\u00a0\u00a0\u00a0last_proof = last_block['proof']\r\n\r\n\u00a0\u00a0\u00a0proof = blockchain.proof_of_work(last_proof)\r\n\r\n\r\n\r\n\r\n\u00a0\u00a0\u00a0# rewarding the miner for his contribution. 0 specifies new coin has been mined\r\n\r\n\u00a0\u00a0\u00a0blockchain.new_transaction(\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0sender=\"0\",\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0recipient = node_identifier,\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0amount = 1,\r\n\r\n\u00a0\u00a0\u00a0)\r\n\r\n\r\n\r\n\r\n\u00a0\u00a0\u00a0# now create the new block and add it to the chain\r\n\r\n\u00a0\u00a0\u00a0previous_hash = blockchain.hash(last_block)\r\n\r\n\u00a0\u00a0\u00a0block = blockchain.new_block(proof, previous_hash)\r\n\r\n\r\n\r\n\r\n\u00a0\u00a0\u00a0response = {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'message': 'The new block has been forged',\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'index': block['index'],\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'transactions': block['transactions'],\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'proof': block['proof'],\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'previous_hash' : block['previous_hash']\r\n\r\n\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0return jsonify(response), 200<\/pre>\n<h4>3. Blockchain Interaction<\/h4>\n<p style=\"text-align: justify;\">The last step is to interact with the blockchain. We are going to use Postman to interact with the blockchain network using the API we just created.<\/p>\n<p style=\"text-align: justify;\">Run the code below on the Python Console<\/p>\n<pre>python blockchain.py<\/pre>\n<blockquote><p>Aspiring to make a lucrative career as a blockchain developer but not sure how? Check the detailed guide Now on\u00a0<a href=\"https:\/\/101blockchains.com\/how-to-become-blockchain-developer\/\" target=\"_blank\" rel=\"noopener\" data-wpel-link=\"internal\">How To Become Blockchain Developer?<\/a><\/p><\/blockquote>\n<h3>Conclusion<\/h3>\n<p style=\"text-align: justify;\">This leads us to the end of our How to build a <a href=\"https:\/\/101blockchains.com\/ultimate-blockchain-technology-guide\/\" target=\"_blank\" rel=\"noopener\">blockchain<\/a>. Hope the steps and codes above will help you build a Blockchain in Python. So, if you are a novice and aspiring to build a career in Blockchain, we suggest you start your blockchain career with our <a href=\"https:\/\/academy.101blockchains.com\/courses\/enterprise-blockchains-fundamentals\/\" target=\"_blank\" rel=\"noopener noreferrer\">Free Blockchain Fundamentals Course<\/a>. Choose us as your study partner and we&#8217;ll have you until you don&#8217;t get your dream job.<\/p>\n<p><a href=\"https:\/\/101blockchains.com\/pricing\/\" target=\"_blank\" rel=\"noopener\"><picture><source srcset=\"https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/12\/Advance-your-Career-with-Blockchain-Web3-Skills.webp 1200w,https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/12\/Advance-your-Career-with-Blockchain-Web3-Skills-300x25.webp 300w,https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/12\/Advance-your-Career-with-Blockchain-Web3-Skills-1024x85.webp 1024w, https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/12\/Advance-your-Career-with-Blockchain-Web3-Skills-768x64.png 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" type=\"image\/webp\"><img src=\"https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/12\/Advance-your-Career-with-Blockchain-Web3-Skills.png\" height=\"100\" width=\"1200\" srcset=\"https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/12\/Advance-your-Career-with-Blockchain-Web3-Skills.png 1200w, https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/12\/Advance-your-Career-with-Blockchain-Web3-Skills-300x25.png 300w, https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/12\/Advance-your-Career-with-Blockchain-Web3-Skills-1024x85.png 1024w, https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/12\/Advance-your-Career-with-Blockchain-Web3-Skills-768x64.png 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" class=\"aligncenter wp-image-43619 size-full sp-no-webp\" alt=\"Advance your Career with Blockchain &amp; Web3 Skills\" loading=\"lazy\" decoding=\"async\"  > <\/picture><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to learn how to build a blockchain, then you have come to the right place. Let&#8217;s dive deep to learn how you can build a blockchain in python. There are tons of articles around blockchain, but not all of them talk about building a blockchain from scratch. As a developer, theories are<\/p>\n<div class=\"read-more\"><a href=\"https:\/\/101blockchains.com\/build-a-blockchain-in-python\/\" title=\"Read More\">Read More<\/a><\/div>\n","protected":false},"author":57,"featured_media":17825,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":{"0":"post-14143","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-opinions"},"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.5 (Yoast SEO v27.5) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Build a Blockchain in Python? - 101 Blockchains<\/title>\n<meta name=\"description\" content=\"Check out the complete tutorial on how to build a blockchain. Here you can build your own blockchain from scratch! Come and start learning now!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/101blockchains.com\/build-a-blockchain-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Build a Blockchain in Python?\" \/>\n<meta property=\"og:description\" content=\"If you want to learn how to build a blockchain, then you have come to the right place. Let&#039;s dive deep to learn how you can build a blockchain in python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/101blockchains.com\/build-a-blockchain-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"101 Blockchains\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/101Blockchains\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-26T15:15:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-14T07:14:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/Building-a-Blockchain-in-Python.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"580\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"101 Blockchains\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@101Blockchains\" \/>\n<meta name=\"twitter:site\" content=\"@101Blockchains\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"101 Blockchains\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/101blockchains.com\\\/build-a-blockchain-in-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/101blockchains.com\\\/build-a-blockchain-in-python\\\/\"},\"author\":{\"name\":\"101 Blockchains\",\"@id\":\"https:\\\/\\\/101blockchains.com\\\/#\\\/schema\\\/person\\\/a358796f59a63df2e672a4907be2d80a\"},\"headline\":\"How to Build a Blockchain in Python?\",\"datePublished\":\"2020-01-26T15:15:02+00:00\",\"dateModified\":\"2023-07-14T07:14:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/101blockchains.com\\\/build-a-blockchain-in-python\\\/\"},\"wordCount\":1923,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/101blockchains.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/101blockchains.com\\\/build-a-blockchain-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/101blockchains.com\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/Building-a-Blockchain-in-Python.png\",\"articleSection\":[\"Opinions\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/101blockchains.com\\\/build-a-blockchain-in-python\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/101blockchains.com\\\/build-a-blockchain-in-python\\\/\",\"url\":\"https:\\\/\\\/101blockchains.com\\\/build-a-blockchain-in-python\\\/\",\"name\":\"How to Build a Blockchain in Python? - 101 Blockchains\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/101blockchains.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/101blockchains.com\\\/build-a-blockchain-in-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/101blockchains.com\\\/build-a-blockchain-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/101blockchains.com\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/Building-a-Blockchain-in-Python.png\",\"datePublished\":\"2020-01-26T15:15:02+00:00\",\"dateModified\":\"2023-07-14T07:14:54+00:00\",\"description\":\"Check out the complete tutorial on how to build a blockchain. Here you can build your own blockchain from scratch! Come and start learning now!\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/101blockchains.com\\\/build-a-blockchain-in-python\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/101blockchains.com\\\/build-a-blockchain-in-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/101blockchains.com\\\/build-a-blockchain-in-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/101blockchains.com\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/Building-a-Blockchain-in-Python.png\",\"contentUrl\":\"https:\\\/\\\/101blockchains.com\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/Building-a-Blockchain-in-Python.png\",\"width\":1200,\"height\":580,\"caption\":\"Building a Blockchain in Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/101blockchains.com\\\/build-a-blockchain-in-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/101blockchains.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Build a Blockchain in Python?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/101blockchains.com\\\/#website\",\"url\":\"https:\\\/\\\/101blockchains.com\\\/\",\"name\":\"101 Blockchains\",\"description\":\"Your guide to the Blockchain World\",\"publisher\":{\"@id\":\"https:\\\/\\\/101blockchains.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/101blockchains.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/101blockchains.com\\\/#organization\",\"name\":\"101 Blockchains\",\"url\":\"https:\\\/\\\/101blockchains.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/101blockchains.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/101blockchains.com\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/image_2021_02_25T10_04_45_211Z.png\",\"contentUrl\":\"https:\\\/\\\/101blockchains.com\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/image_2021_02_25T10_04_45_211Z.png\",\"width\":183,\"height\":175,\"caption\":\"101 Blockchains\"},\"image\":{\"@id\":\"https:\\\/\\\/101blockchains.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/101Blockchains\",\"https:\\\/\\\/x.com\\\/101Blockchains\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/101blockchains\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/101blockchains.com\\\/#\\\/schema\\\/person\\\/a358796f59a63df2e672a4907be2d80a\",\"name\":\"101 Blockchains\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/efbdb7dd09f99737887d8f8b030566510ec270efeb0c70fc60d6df8f52906615?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/efbdb7dd09f99737887d8f8b030566510ec270efeb0c70fc60d6df8f52906615?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/efbdb7dd09f99737887d8f8b030566510ec270efeb0c70fc60d6df8f52906615?s=96&d=mm&r=g\",\"caption\":\"101 Blockchains\"},\"description\":\"101 Blockchains is the world\u2019s leading online independent research-based network for Blockchain and Web3 Practitioners. We are a professional and trusted provider of accredited certifications, and online training.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/company\\\/101blockchains\\\/\"],\"url\":\"https:\\\/\\\/101blockchains.com\\\/author\\\/101blockchains\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Build a Blockchain in Python? - 101 Blockchains","description":"Check out the complete tutorial on how to build a blockchain. Here you can build your own blockchain from scratch! Come and start learning now!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/101blockchains.com\/build-a-blockchain-in-python\/","og_locale":"en_US","og_type":"article","og_title":"How to Build a Blockchain in Python?","og_description":"If you want to learn how to build a blockchain, then you have come to the right place. Let's dive deep to learn how you can build a blockchain in python.","og_url":"https:\/\/101blockchains.com\/build-a-blockchain-in-python\/","og_site_name":"101 Blockchains","article_publisher":"https:\/\/www.facebook.com\/101Blockchains","article_published_time":"2020-01-26T15:15:02+00:00","article_modified_time":"2023-07-14T07:14:54+00:00","og_image":[{"width":1200,"height":580,"url":"https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/Building-a-Blockchain-in-Python.png","type":"image\/png"}],"author":"101 Blockchains","twitter_card":"summary_large_image","twitter_creator":"@101Blockchains","twitter_site":"@101Blockchains","twitter_misc":{"Written by":"101 Blockchains","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/101blockchains.com\/build-a-blockchain-in-python\/#article","isPartOf":{"@id":"https:\/\/101blockchains.com\/build-a-blockchain-in-python\/"},"author":{"name":"101 Blockchains","@id":"https:\/\/101blockchains.com\/#\/schema\/person\/a358796f59a63df2e672a4907be2d80a"},"headline":"How to Build a Blockchain in Python?","datePublished":"2020-01-26T15:15:02+00:00","dateModified":"2023-07-14T07:14:54+00:00","mainEntityOfPage":{"@id":"https:\/\/101blockchains.com\/build-a-blockchain-in-python\/"},"wordCount":1923,"commentCount":1,"publisher":{"@id":"https:\/\/101blockchains.com\/#organization"},"image":{"@id":"https:\/\/101blockchains.com\/build-a-blockchain-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/Building-a-Blockchain-in-Python.png","articleSection":["Opinions"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/101blockchains.com\/build-a-blockchain-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/101blockchains.com\/build-a-blockchain-in-python\/","url":"https:\/\/101blockchains.com\/build-a-blockchain-in-python\/","name":"How to Build a Blockchain in Python? - 101 Blockchains","isPartOf":{"@id":"https:\/\/101blockchains.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/101blockchains.com\/build-a-blockchain-in-python\/#primaryimage"},"image":{"@id":"https:\/\/101blockchains.com\/build-a-blockchain-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/Building-a-Blockchain-in-Python.png","datePublished":"2020-01-26T15:15:02+00:00","dateModified":"2023-07-14T07:14:54+00:00","description":"Check out the complete tutorial on how to build a blockchain. Here you can build your own blockchain from scratch! Come and start learning now!","breadcrumb":{"@id":"https:\/\/101blockchains.com\/build-a-blockchain-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/101blockchains.com\/build-a-blockchain-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/101blockchains.com\/build-a-blockchain-in-python\/#primaryimage","url":"https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/Building-a-Blockchain-in-Python.png","contentUrl":"https:\/\/101blockchains.com\/wp-content\/uploads\/2020\/01\/Building-a-Blockchain-in-Python.png","width":1200,"height":580,"caption":"Building a Blockchain in Python"},{"@type":"BreadcrumbList","@id":"https:\/\/101blockchains.com\/build-a-blockchain-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/101blockchains.com\/"},{"@type":"ListItem","position":2,"name":"How to Build a Blockchain in Python?"}]},{"@type":"WebSite","@id":"https:\/\/101blockchains.com\/#website","url":"https:\/\/101blockchains.com\/","name":"101 Blockchains","description":"Your guide to the Blockchain World","publisher":{"@id":"https:\/\/101blockchains.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/101blockchains.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/101blockchains.com\/#organization","name":"101 Blockchains","url":"https:\/\/101blockchains.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/101blockchains.com\/#\/schema\/logo\/image\/","url":"https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/03\/image_2021_02_25T10_04_45_211Z.png","contentUrl":"https:\/\/101blockchains.com\/wp-content\/uploads\/2021\/03\/image_2021_02_25T10_04_45_211Z.png","width":183,"height":175,"caption":"101 Blockchains"},"image":{"@id":"https:\/\/101blockchains.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/101Blockchains","https:\/\/x.com\/101Blockchains","https:\/\/www.linkedin.com\/company\/101blockchains\/"]},{"@type":"Person","@id":"https:\/\/101blockchains.com\/#\/schema\/person\/a358796f59a63df2e672a4907be2d80a","name":"101 Blockchains","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/efbdb7dd09f99737887d8f8b030566510ec270efeb0c70fc60d6df8f52906615?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/efbdb7dd09f99737887d8f8b030566510ec270efeb0c70fc60d6df8f52906615?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/efbdb7dd09f99737887d8f8b030566510ec270efeb0c70fc60d6df8f52906615?s=96&d=mm&r=g","caption":"101 Blockchains"},"description":"101 Blockchains is the world\u2019s leading online independent research-based network for Blockchain and Web3 Practitioners. We are a professional and trusted provider of accredited certifications, and online training.","sameAs":["https:\/\/www.linkedin.com\/company\/101blockchains\/"],"url":"https:\/\/101blockchains.com\/author\/101blockchains\/"}]}},"_links":{"self":[{"href":"https:\/\/101blockchains.com\/wp-json\/wp\/v2\/posts\/14143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/101blockchains.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/101blockchains.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/101blockchains.com\/wp-json\/wp\/v2\/users\/57"}],"replies":[{"embeddable":true,"href":"https:\/\/101blockchains.com\/wp-json\/wp\/v2\/comments?post=14143"}],"version-history":[{"count":8,"href":"https:\/\/101blockchains.com\/wp-json\/wp\/v2\/posts\/14143\/revisions"}],"predecessor-version":[{"id":45664,"href":"https:\/\/101blockchains.com\/wp-json\/wp\/v2\/posts\/14143\/revisions\/45664"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/101blockchains.com\/wp-json\/wp\/v2\/media\/17825"}],"wp:attachment":[{"href":"https:\/\/101blockchains.com\/wp-json\/wp\/v2\/media?parent=14143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/101blockchains.com\/wp-json\/wp\/v2\/categories?post=14143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/101blockchains.com\/wp-json\/wp\/v2\/tags?post=14143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}