Reader small image

You're reading from  Hands-On Blockchain for Python Developers

Product typeBook
Published inFeb 2019
Reading LevelExpert
PublisherPackt
ISBN-139781788627856
Edition1st Edition
Languages
Concepts
Right arrow
Author (1)
Arjuna Sky Kok
Arjuna Sky Kok
author image
Arjuna Sky Kok

Arjuna Sky Kok has experience more than 10 years in expressing himself as a software engineer. He has developed web applications using Symfony, Laravel, Ruby on Rails, and Django. He also has built mobile applications on top of Android and iOS platforms. Currently, he is researching Ethereum technology. Other than that, he teaches Android and iOS programming to students. He graduated from Bina Nusantara University with majors in Computer Science and Applied Mathematics. He always strives to become a holistic person by enjoying leisure activities, such as dancing Salsa, learning French, and playing StarCraft 2. He lives quietly in the bustling city of Jakarta. In loving memory of my late brother, Hengdra Santoso (1979-2011).
Read more about Arjuna Sky Kok

Right arrow

Implementing a Decentralized Application Using IPFS

In this chapter, we are going to combine a smart contract with the InterPlanetary File System (IPFS) to build a decentralized video-sharing application (similar to YouTube but decentralized). We will use a web application as the frontend for the blockchain and IPFS. As stated previously, IPFS is not a blockchain technology. IPFS is a decentralized technology. However, in a blockchain forum, meetup, or tutorial, you may hear IPFS being mentioned quite often. One of the main reasons for this is that IPFS overcomes the weakness of blockchain, which is that its storage is very expensive.

In this chapter, we will cover the following topics:

  • Architecture of the decentralized video-sharing application
  • Writing the video-sharing smart contract
  • Building the video-sharing web application

Architecture of the decentralized video-sharing application

This is how our application will look after it is finished—first, you go to a website, where you will see a list of videos (just like YouTube). Here, you can play videos in your browser, upload videos to your browser so that people can watch your cute cat video, and like other people's videos.

On the surface, this is like a normal application. You build it with your favorite Python web framework, such as Django, Flask, or Pyramid. Then you use MySQL or PostgreSQL as the database. You could choose NGINX or Apache as the web server in front of the Gunicorn web server. For caching, you can use Varnish for full page caching and Redis for template caching. You will also host the web application and videos on the cloud, such as Amazon Web Service (AWS) or Google Cloud Platform (GCP), Azure. Then you will use a content...

Writing the video-sharing smart contract

Without further ado, let's set up our smart contract development platform:

  1. First things first, we set up our virtual environment as follows:
$ virtualenv -p python3.6 videos-venv
$ source videos-venv/bin/activate
(videos-venv) $
  1. Then we install Web3, Populus, and Vyper:
(videos-venv) $ pip install eth-abi==1.2.2
(videos-venv) $ pip install eth-typing==1.1.0
(videos-venv) $ pip install py-evm==0.2.0a33
(videos-venv) $ pip install web3==4.7.2
(videos-venv) $ pip install -e git+https://github.com/ethereum/populus#egg=populus
(videos-venv) $ pip install vyper
The latest version of Vyper is 0.1.0b6, which breaks Populus. The developer needs some time to fix this. If the bug has not been fixed by the time you are reading this book, you could patch Populus yourself.
  1. Check whether this library has fixed the bug or not using the following command...

Building the video-sharing web application

It's time to build the frontend of our smart contract. Previously, in Chapter 7, Frontend Decentralized Application, and Chapter 9, Cryptocurrency Wallet, we have created a desktop application using Qt for Python or the Pyside2 library. This time we are going to build a web application using the Django library:

  1. Without further ado, let's install Django:
(videos-venv) $ pip install Django
  1. We also need the OpenCV Python library to get the thumbnail of our videos:
(videos-venv) $ pip install opencv-python
  1. Now let's create our Django project directory. This will create a skeleton Django project with its settings files:
(videos-venv) $ django-admin startproject decentralized_videos
  1. Inside this new directory, create a static media directory:
(videos-venv) $ cd decentralized_videos
(videos-venv) $ mkdir static media
  1. Still...

Note

To make this application perform better in the real world, there are a lot of things that need to be done. You need to add testing, you need to test the model, the views, the templates, and, finally, you need to carry out solid integration tests. You also need to put heavy and long operations (such as calling operations on the smart contract, adding and getting files with IPFS) in the background jobs using Celery and RabbitMQ or Redis. In addition to this, you need to add some JavaScript files in order to notice whether the background jobs have finished or not using a pooling mechanism. You could also use the Django channel to do the job.

Instead of accessing methods of the smart contract in the model, perhaps it is better to put all information from the blockchain in the database in the background task using cron. Then the model can access the database to get necessary information...

Summary

In this chapter, we combined IPFS technology and smart contract technology. We built a decentralized video-sharing application. First, we wrote a smart contract to store video information and the video titles. We also built in the crypto economics by making the act of liking videos require coins from the ERC20 token. In addition to this, we learned that even storing video information such as a bytes string of the IPFS path and the title requires more gas than usual. After writing a smart contract, we built a web application using the Django library. We created a project, followed by building an application inside this project. Moving forward, we built views, models, templates, and URLs. In the models, we stored the video file in IPFS and then stored the IPFS path on the blockchain. We made the templates more beautiful using the Bulma CSS framework, and then launched the...

Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Blockchain for Python Developers
Published in: Feb 2019Publisher: PacktISBN-13: 9781788627856
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Arjuna Sky Kok

Arjuna Sky Kok has experience more than 10 years in expressing himself as a software engineer. He has developed web applications using Symfony, Laravel, Ruby on Rails, and Django. He also has built mobile applications on top of Android and iOS platforms. Currently, he is researching Ethereum technology. Other than that, he teaches Android and iOS programming to students. He graduated from Bina Nusantara University with majors in Computer Science and Applied Mathematics. He always strives to become a holistic person by enjoying leisure activities, such as dancing Salsa, learning French, and playing StarCraft 2. He lives quietly in the bustling city of Jakarta. In loving memory of my late brother, Hengdra Santoso (1979-2011).
Read more about Arjuna Sky Kok