Skip to content

gruyaume/my-blockchain

Repository files navigation

Create your own blockchain using Python

The goal of this project is to provide code to support a tutorial on blockchains. You can find the complete tutorial on medium.

Table of contents

  1. Introduction
  2. The basics
  3. Merkle tree
  4. Transactions and security
  5. Double-entry bookkeeping and UTXO’s
  6. Transaction scripts
  7. The network
  8. New Block Creation and Proof-of-Work
  9. Incentives and Transaction fees
  10. A Distributed Network
  11. Publishing and testing

Requirements

Python version

Python 3.8

Libraries

Install libraries with pip: pip3 install -r requirements.txt

Hardware

  • Processor: 2 vCPU
  • Memory: 4GB
  • Storage: 50GB

Installation Guide

A complete installation guide is available on Medium (pt. 10). You should be following this guide if you want your block to be part of the network and be accessible from the outside world. Here we will simply deploy the node on a virtual machine without any other infrastructure consideration. We will also assume you have access to a virtual machine running Ubuntu 20.04. We will install Kubernetes on this VM and deploy our application on it.

Setting up Microk8s

We will be using Microk8s, which is a free, fast and easy to deploy Kubernetes release. Once you're inside the virtual machine, install microk8s using snap:

ubuntu@ip-10-0-0-54:~$ sudo apt-get update && sudo apt-get upgrade
ubuntu@ip-10-0-0-54:~$ sudo snap install microk8s --classic

Update your user's permission to be added to the microk8s group:

ubuntu@ip-10-0-0-54:~$ sudo usermod -a -G microk8s ubuntu
ubuntu@ip-10-0-0-54:~$ sudo chown -f -R ubuntu ~/.kube

After changing those permissions, you'll have to create a new shell for them to take effect, so you can exit and re-ssh to the machine. Once you're in again, enable some add-ons to your microk8s cluster:

ubuntu@ip-10-0-0-54:~$ microk8s enable dns ingress storage

We will be using MetalLB as our load balancer for Kubernetes. It can be enabled the same way as the other add-ons:

ubuntu@ip-10-0-0-54:~$ microk8s enable metallb

You will be asked for a range of IP's to provide, answer with the range of private addresses you want, here I'll use : 10.0.1.1–10.0.1.254.

Deploying our node using kubectl

MicroK8s uses a namespaced kubectl command to prevent conflicts with any existing installs of kubectl. In our case, we don't have an existing install so we will add an alias like this:

ubuntu@ip-10-0-0-54:~$ alias kubectl='microk8s kubectl'

Note that this alias won't survive exiting your shell session so you'll have to re-run the command every time you log back in. Now clone the code from the github and head to the deploy directory:

ubuntu@ip-10-0-0-54:~$ git clone https://github.com/gruyaume/my-blockchain.git
ubuntu@ip-10-0-0-54:~$ cd my-blockchain/deploy/

Deploy the blockchain using kubectl apply:

ubuntu@ip-10-0-0-54:~/my-blockchain/deploy$ kubectl apply -f kubernetes/

Voilà, you now have a node running.

Validating the Kubernetes deployment

You can validate that our service is correctly created:

ubuntu@ip-10-0-0-54:~$ kubectl get svc
NAME            TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP      10.152.183.1     <none>        443/TCP        22m
my-blockchain   LoadBalancer   10.152.183.158   10.0.1.1      80:32301/TCP   20m

And that our deployment is also up:

ubuntu@ip-10-0-0-54:~$ kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
my-blockchain-b9d844446-9hnzg   1/1     Running   0          21m

Validating Networking

Still from inside the virtual machine, validate that our service returns something when we call it. Here the IP is the EXTERNAL-IP associated to the my-blockchain service. Make sure you use the correct one:

ubuntu@ip-10-0-0-54:~$ curl 10.0.1.1/block

You should get in return the blockchain in a list format.

New blockchain user

In the current implementation there are 4 users of the blockchain: albert, bertrand, camille and the miner. To create a new user, you will have to generate a new public/private key pair. To do so, you can simply run the "new_user_creation.py" script inside of the common directory:

export PYTHONPATH=src
python src/common/new_user_creation.py 

The output will simply print you new public/private keys that you will be able to use.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published