The article guides readers through enhancing a basic Python blockchain by integrating Flask to create an API, allowing dynamic interactions such as mining new blocks and handling transactions.
Abstract
The provided content is a tutorial from a series on building a blockchain in Python. It builds upon a previously created simple blockchain by transforming it into an interactive API using the Flask framework. The author outlines the necessary endpoints for a functional blockchain API, including those for mining new blocks and adding transactions. The article also touches on the concept of nodes within the blockchain network, the importance of a unique node identifier, and the process of validating transactions. Practical examples are provided, demonstrating how to use HTTP requests to interact with the blockchain, such as mining a new block and creating transactions. The tutorial emphasizes the educational value of hands-on practice and concludes by teasing the upcoming topic of consensus algorithms in the series.
Opinions
The author believes that the best way to learn about blockchains is through practical implementation, emphasizing the value of building one to understand its workings.
Flask is presented as a preferable framework for this project over Django due to its simplicity and suitability for the task at hand.
The importance of nodes in maintaining a decentralized and valid blockchain is highlighted, suggesting that anyone can contribute to the network by becoming a node.
The tutorial encourages readers to interact with their blockchain using tools like Postman or curl, suggesting a hands-on approach to learning.
The author expresses that the blockchain created is not yet complete, hinting at future content covering consensus algorithms to further enhance the blockchain's functionality.
The article concludes with a call to action for readers to follow the author for more content and to consider subscribing to Medium for full access to the author's stories.
Learn Blockchains by Building One in Python — 2. Turning our Blockchain into an API
In the last story, we’ve built a simple blockchain we can run from our computer. For now, no one can interact with it as what we do with our blockchain is hard-coded. The purpose is now to turn our blockchain into an API, so it allows us and other users to interact with it dynamically.
Endpoints
Before starting, it’s good to think about the endpoints we’ll need. First, we need an endpoint to mine, else we can’t create blocks. We also need an endpoint to add a transaction, else our blockchain is useless. And we finally need an endpoint to retrieve our blockchain, because a blockchain is transparent and anyone can access it and see the transactions and the blocks.
Flask
To turn our blockchain into an API, we need to build a web application. The most known Python frameworks for doing this are Django and Flask. Django is a bit over-complicated for what we want to do, so we’ll use Flask.
Flask allows you to build a web app in a few lines of code:
We create a Flask app.
We add the “/ route (the root).
We run our app.
We can now create the routes we’ll need in a file called app.py:
Nodes
In the blockchain, people validating the transactions and creating new blocks are known as “miners”. But they can also be called “nodes”. They ensure the blockchain is valid.
Nodes are what allow the blockchain to be so decentralized, as anyone can become a node from his computer and contribute.
When a node has found a valid proof, it creates a new block that is then verified by the entire network. If the block is valid, the node receives a coin.
Each node has an address. Let’s create our own. We can put the following code at the top of app.py .
fromuuidimport uuid4
node_identifier = str(uuid4()).replace('-', '')
Transactions Endpoint
A transaction is composed of a sender, a receiver, and an amount. If one of these values is missing, we can’t continue and should return an error. Else, we just create a new transaction in the blockchain, and we return a successful response.
We must return responses as text or JSON, so we use jsonify . The first returned value is the response’s content, and the second returned value is the status code (400 means error and 201 success).
Mine Endpoint
This endpoint is used to add new blocks to the blockchain. It has to calculate the Proof of Work, reward the miner, and add the new block to the chain.
Interacting with the Blockchain
Now, your app.py file should look like this:
We can run this file to run the app on our computer.
Now, open your favorite HTTP requests tool. I like Postman, but you can also use the Command Prompt and curl if you want.
Let’s try to mine a new block. If you want to use your Command Prompt, open it, and type:
As you can see, we’ve successfully mined a new block. If you want to experience this like if you were mining on a real blockchain, you can increase the Proof of Work difficulty, and the block won’t be mined so quickly.
We can also create transactions. You can do it with curl:
Now, you can play with your blockchain and create transactions as it is interactive. But it’s not finished yet, our blockchain still lacks a big feature: the Consensus algorithm. More about that in the next story!
If you don’t want to miss it, be sure to follow me!
You can find the other stories of this series here: