Creating Your First Blockchain with Python

In this tutorial we’ll learn how to create a very basic Blockchain with Python. We will create a Blockchain with just 30 lines of code! The aim is to introduce you to Blockchain programming without getting into inessential details. You should already know fundamentals of Blockchain, if not then you may want to read this article first. You should also know basic Python programming and object oriented concepts.

Prerequisites

  1. Python 3 Installed on Windows, Linux or Mac.
  2. Knowledge of running Python programs on terminal or IDLE.

The Fundamentals

A Blockchain is merely a series of blocks. These blocks are logically connected to each other as each one stores previous block’s hash. We will create a class that represents a block and store its predecessor’s address as one of the variables within it. Other variables in the Block class include:

  1. Data: Actual data to be stored in the block
  2. Hash: Hash of this data
  3. Timestamp: Denotes when the block was added to the blockchain
  4. Nonce: A random number that is used to find target hash

Python Libraries

Firstly we import these three Python libraries. You need not to download them as they are part of standard distribution:

  1. hashlib: To generate SHA-256 of block’s data.
  2. datetime: To get current timestamp.
  3. random: To generate nonces.

#for computing sha256
import hashlib

#for generating timestamps
import datetime

#for generating random numbers
import random

The Block Class

Block class uses a constructor to initialize variables when it is instantiated. Let’s define proof-of-work as finding a hash value that must start with two zeroes (“00”). We define a function getPoWHash() which returns a hash value which satisfies our proof-of-work condition. Basically it appends different random numbers (nonces) to the data until it finds a target hash. When we find the target hash, we also store nonce used to find this hash in the block so that others can verify it.

class Block:
def getPoWHash (self, data):
nonce = str(random.randint(0, 300000))
noncedData = data + nonce
hash = hashlib.sha256 (noncedData.encode(‘utf-8’)).hexdigest()
#hashlib requires data to be encoded before hashed.
if hash.startswith (“00”):
self.nonce = nonce
return hash
else:
return self.getPoWHash (data)

def __init__ (self, data, previousHash):
self.data = data
self.nonce = None
self.previousHash = previousHash
self.timeStamp = datetime.datetime.now()
self.hash = self.getPoWHash (data)

Adding Genesis Block and Other Blocks

Next, we add some blocks to our Blockchain. First block is special, so we don’t have previous block’s hash instead we use a 0 here for the sake of simplicity. We simply instantiate the Block class each time we want to add a block.

genesisBlock = Block(“I am genesis block!”, 0)

secondBlock = Block(“I am second block”, genesisBlock.hash)

thirdBlock = Block(“I am third block”, secondBlock.hash)

Viewing the Blockchain

To view our Blockchain we can simply print the hashes of each of the blocks.

print (“Genesis Block Hash: “, genesisBlock.hash)

print (“Second Block Hash: “, secondBlock.hash)

print (“Third Block Hash: “, thirdBlock.hash)

Output: As you can see all hashes start with two zeroes as that is the condition we’ve set as proof-of-work. In Bitcoin, this requirement is 17 leading zeroes.

Genesis Block Hash: 00ddef133aa388b89d431ffd80874d94c808b363ce1467eb6e38a7c38c7712d1

Second Block Hash: 0029e17e48976fdd651e75ce283770634c33ddaca2ab9d05b2df30a0ab779bfa

Third Block Hash: 0094e2b65446530ff4a046be4a0843799e48e4d8d993fd01669a0328bcf43e79

Congrats! You’ve Made It.

We are all done! Here is the full code for this tutorial:

import hashlib
import datetime
import random

class Block:
def getPoWHash (self, data):
nonce = str(random.randint(0, 300000))
noncedData = data + nonce
hash = hashlib.sha256 (noncedData.encode(‘utf-8’)).hexdigest()
#hashlib requires data to be encoded before hashed.
if hash.startswith (“00”):
self.nonce = nonce
return hash
else:
return self.getPoWHash (data)

def __init__ (self, data, previousHash):
self.data = data
self.nonce = None
self.previousHash = previousHash
self.timeStamp = datetime.datetime.now()
self.hash = self.getPoWHash (data)

genesisBlock = Block(“I am genesis block!”, 0)
secondBlock = Block(“I am second block”, genesisBlock.hash)
thirdBlock = Block(“I am third block”, secondBlock.hash)

print (“Genesis Block Hash: “, genesisBlock.hash)
print (“Second Block Hash: “, secondBlock.hash)
print (“Third Block Hash: “, thirdBlock.hash)

What’s Ahead?

Our Blockchain is stored on a single machine but a Blockchain serves its purpose only when it is distributed. Nevertheless, it is a great start isn’t it?

Proof of Work Explained Simply : Learn Blockchain

Proof of Work (PoW) is a consensus algorithm used in the original Bitcoin implementation. In a Blockchain system new transactions are periodically added by packaging these transactions in a block. This block is then added to the Blockchain. Please read What is Blockchain Technology if you don’t already know.

Background

Users send each other digital tokens (e.g. Bitcoins). All such transactions are stored in a public ledger which implemented on Blockchain. But before transactions are added to the ledger they need to be confirmed. There are special nodes in the network called peers or more popularly miners who are responsible for confirming transactions and adding them to blocks. A block generally contains multiple transactions.

How It Works?

When a new block needs to be added to the Blockchain, every peer in the network tries to solve a puzzle. Whoever solves the puzzle first is given a reward and the block is added to the Blockchain. The puzzle is nothing but finding a SHA-256 hash of given block data in such a manner that the hash value must start with certain number of zeroes. This is known as target hash. A random number (called nonce) is attached to the original data and the peer keeps trying different nonces until it finds target hash. Current target in Bitcoin is 17 zeroes. Setting a higher target value will make it more difficult to find a required hash. This requirement of certain number of leading zeroes in the hash is known as target. Finding target hash requires enormous computing power but verifying it is easy. Suppose peer A finds target hash and sends it to peer B, peer B can easily verify it because nonce value is also known.

Why is Proof of Work Needed?

In the absence of Proof of Work, the Blockchain system can be spammed with illegitimate transactions. It is from Proof of Work that Bitcoin gets its value. A peer who successfully adds a block, gets 25 Bitcoins! There is another popular alternative to Proof of Work known as Proof of Stake. More on it later.

MapReduce Real World Example in Python : Learn Data Science

MapReduce real world example on e-commerce transactions data is described here using Python streaming. The example does not require Hadoop installation. However, if you have Hadoop already installed it will run just fine on it. Python programming language is used because it is easy to read and understand. A real world e-commerce transactions dataset from a UK based retailer is used. The best way to learn with this example is to use an Ubuntu machine with Python 2 or 3 installed on it.

Outline

  1. The dataset consists of real world e-commerece data from UK based retailer
  2. The dataset is provided by Kaggle
  3. It contains 5.42k records (which is not small)
  4. Our goal is to find out country wise total sales
  5. Mapper multiplies quantity and unit price
  6. Mapper emits key-value pair as country, sales
  7. Reducer sums-up all pairs for same country
  8. Final output is country, sales for all countries

The Data

Download:Link to Kaggle DatasetSource: The dataset has real-life transaction data from a UK retailer. Format: CSV Size: 43.4 MB (5,42,000 records) Columns:

  1. InvoiceNo
  2. StockCode
  3. Description
  4. Quantity
  5. InvoiceDate
  6. UnitPrice
  7. CustomerID
  8. Country

The Problem

In this MapReduce real world example, we calculate total sales for each country from given dataset.

The Approach

Firstly, our data doesn’t have a Total column so it is to be computed using Quantity and UnitPrice columns as Total = Quantity * UnitPrice.

What Mapper Does

  1. Read the data
  2. Convert data into proper format
  3. Calculate total
  4. Print output as key-value pair CountryName:Total

What Reducer Does

  1. Read input from mapper
  2. Check for existing country key in the disctionary
  3. Add total to existing total value
  4. Print all key-value pairs

See this article on how to run this code

Python Code for Mapper (MapReduce Real World Example)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python
import sys

# Get input lines from stdin
for line in sys.stdin:
# Remove spaces from beginning and end of the line

line = line.strip()

# Split it into tokens

tokens = line.split(',')

#Get country, price and quantity values
try:
country = tokens\[7\]
price = float(tokens\[5\])
qty = int(tokens\[3\])
print '%s\\t%s' % (country, (price\*qty))
except ValueError:
pass

Python Code for Reducer (MapReduce Real World Example)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python
import sys

# Create a dictionary to map countries to totals
countrySales = {}

# Get input from stdin
for line in sys.stdin:
#Remove spaces from beginning and end of the line
line = line.strip()

# parse the input from mapper.py
country, total = line.split('\\t', 1)

# convert total (currently a string) to float
try:
total = float(total)
except ValueError:
pass

#update dictionary
try:
countrySales\[country\] = countrySales\[country\] + total
except:
countrySales\[country\] = total

# Write the tuples to stdout
for country in countrySales.keys():
print '%s\\t%s'% (country, countrySales\[country\])

Output

Country Score
Canada 3599.68
Brazil 1143.6
Italy 16506.03
Czech Republic 707.72
USA 1730.92
Lithuania 1661.06
Unspecified 4746.65
France 197194.15
Norway 34908.13
Bahrain 548.4
Israel 7867.42
Australia 135330.19
Singapore 9054.69
Iceland 4299.8
Channel Islands 19950.54
Germany 220791.78
Belgium 40752.83
European Community 1291.75
Hong Kong 10037.84
Spain 54632.86
EIRE 262112.48
Netherlands 283440.66
Denmark 18665.18
Poland 7193.34
Finland 22226.69
Saudi Arabia 131.17
Sweden 36374.15
Malta 2503.19
Switzerland 56199.23
Portugal 29272.34
United Arab Emirates 1877.08
Lebanon 1693.88
RSA 1002.31
United Kingdom 8148025.164
Austria 10149.28
Greece 4644.82
Japan 34616.06
Cyprus 12791.31

Conclusions

  • Mapper picks-up a record and emits country and total for that record
  • Mapper repeats this process for all 5.42k records
  • Now, we have 5.42k key value pairs
  • Reducer’s role is to combine these pairs until all keys are unique!

If you have questions, please feel free to comment below.

Arduino Serial Communication Basics : Learn Arduino

Arduino serial communication is one of the many ways Arduino talks to other devices. Arduino uses asynchronous serial communication to send-receive data to and from other devices. Arduino Uno supports serial communication via on-board UART port and Tx/Rx pins. Generally this transmission happens at 9600 bits per second which is termed as baud rate. [caption id=”attachment_840” align=”aligncenter” width=”424”] Arduino Uno Shown with UART port and Tx,Rx Pins[/caption] Arduino may communicate with many devices including:

  • Another Arduino
  • A computer
  • LCD display
  • Modem
  • Or any device that supports serial communication

How to do it? In this example we’ll connect Arduino and a computer using UART port. For this we are going to use Arduino IDE’s Serial Monitor. Serial Monitor will display data we sent from Arduino Uno. Code:

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println(“Hello World…”);
delay(1000);
}

Upload this code and select Tools > Serial Monitor (Shift+Ctrl+M). You should see this output. [caption id=”attachment_841” align=”alignnone” width=”579”] Serial Monitor output on the computer[/caption] Further Reading:

  1. Wikipedia: Serial communication
  2. Arduino Reference: Serial

Arduino Pushbutton Example : Learn Arduino

Arduino pushbutton example shows how to read a pushbutton with Arduino Uno. Pushbuttons (also spelled push-buttons) are widely used in calculators, phones and appliances. It closes the circuit when pushed and keeps it close until it is pressed. As soon as you release the button the circuit is open again. Here, we are going to use a push-button to flash built-in LED on Arduino Uno. The circuit is pretty simple! [caption id=”attachment_832” align=”alignright” width=”300”]Circuit diagram Circuit Diagram Showing Connection of Arduino Uno and a Push-button[/caption] Parts needed:

  1. Arduino Uno
  2. A push-button

Approximate Cost: ₹ 30 ($ 0.50)

         

Code:

// Declare the pins for the Button and the LED
int buttonPin = 12;
int LED = 13;

void setup() {
// Define pin #12 as input and activate the internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
// Define pin #13 as output, for the LED
pinMode(LED, OUTPUT);
}

void loop(){
// Read the value of the input. It can either be 1 or 0
int buttonValue = digitalRead(buttonPin);
if (buttonValue == LOW){
// If button pushed, turn LED on
digitalWrite(LED,HIGH);
} else {
// Otherwise, turn the LED off
digitalWrite(LED, LOW);
}
}

References:

  1. What is internal pull-up resistor?
  2. Wikipedia: Push-button
  3. Same project with an external pull-up resistor

Displaying Hello World on LCD : Learn Arduino

In this article we’ll learn displaying hello world on LCD; Interfacing 16x2 LCD with Arduino Uno and display some text on it. Writing Hello World pleases the gods of any new programming language that you want to learn. But in case of Arduino it is the LED blinking program that is generally written first. Any ways, Hello World can certainly be second if not the first so let’s roll!

Parts needed:

  1. Arduino Uno
  2. 16 x 2 LCD
  3. Some jumper wires

Approximate Cost: ₹ 125 ($ 2)

The Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include   

//Initialize LCD with interface pins
//Pin Description: REGISTER SELECT PIN,ENABLE PIN,D4 PIN,D5 PIN, D6 PIN, D7 PIN
LiquidCrystal lcd(0, 1, 8, 9, 10, 11);

void setup() {
//Initialize LCD's number of columns and rows (16,2)
lcd.begin(16, 2);
}

void loop() {
//Set the cursor to column 0, line 0
lcd.setCursor(0, 0);

lcd.print("Hello World to Circuitry!");

//Delay of 0.75 sec
delay(750);

//Shift data to the left
lcd.scrollDisplayLeft();
}

Finally check and upload the code. Read this article to know how to upload and run the code.

References:

  1. 16 x 2 LCD Datasheet
  2. My simulation of this project on Tinkercad
  3. Arduino Howto

LED Blinking on Arduino : Learn Arduino

LED Blinking on Arduino Uno should be your first Arduino project. Arduino Uno has an on-board or built-in LED, in this project we will see how to blink it.

Parts you will need:

  1. An Arduino Uno

Steps:

  1. Connect Arduino with your computer.
  2. Download, install and open Arduino IDE. (See how)
  3. Within IDE chooose File Menu > Examples > 01. Basics > Blink
  4. Now click Verify button and wait for it finish the verification
  5. Finally click on Upload button, and you’re done!
  6. See the LED blinking!

Explanation:

  • Built-in LED is referenced by pin number 13.
  • In setup(), we declare this pin as output pin as we want to use it as so.
  • Next, in loop(), we turn it on, then wait for some time (delay) and than turn it off.
  • This process repeats infinitely.

Remember that this is your first Arduino project, do not worry much if you don’t understand every piece of it. If you are facing difficulties and need help, use comments section below.

An Introduction to Arduino Platform : Learn Arduino

Arduino is an open-source hardware prototyping platform. It is widely used today in electronics projects because it is easy to learn, simple in design, well documented and cheaper. We call it platform because it is both hardware circuit as well as piece of software, the IDE. It also has its own programming language. All these open-source components collectively makes the Arduino platform.

Bit of History: It all started at IDII, Ivrea, Italy in 2003 when some students at the institute started working on creating a low cost prototyping tool for digital projects. Arduino’s sturdy design tells us that it is created by design people and not hardcore electronics guys. You know how bad they are :P! The project witnessed steady rise in its popularity and today it has become the most popular prototyping tool.

Arduino Circuits: As of writing this there are 58 different types of Arduino circuits (simply referred to as Arduinos). You can choose from this variety of circuits the one which fits your application needs. The most popular of them is Arduino Uno.

Arduino IDE: This piece of software helps you write application logic with a fancy editor, compile it and burn it on the circuit board. It can be installed on Windows, Linux and Mac OS platforms. There is also a web based IDE (if you just want to try).

Arduino Programming Language: Arduino programs are called sketches. A sketch is divided into two parts setup() and loop(). Numerous built-in functions are available to make the job easy. The language is largely inspired by C, try to see the code displayed in IDE!

Arduino Culture and Community: Arduino has a strong community helping each other on forums and sharing project ideas. The official website itself is a great resource for learning basics. Arduino day is a world-wide event celebrated in April each year that brings together developers, enthusiasts, students, teachers and novices.

Suggested Reading:

  1. The Untold History of Arduino by Hernando Barragán
  2. Arduino - Products
  3. Arduino Language Reference
  4. Arduino Web Editor
  5. Arduino Forums
  6. Arduino Day at Department of Computer Science, KSKVKU
  7. Download this Article as PDF

Reclaiming Your Privacy on Android

A major threat to your privacy emanates from your smart phone. These devices have become central medium of social interaction for everyone. Android is the only open source platform among the popular smart phone operating systems. But even with Android your privacy is under attack as large corporations have figured out that data is the new oil. Android privacy threats, for the scope of this article, are of three kinds: emanating from the device itself, emanating from third-party apps, emanating from your carrier. In this article I try to explain how to counter these three categories of threats to an extent where you can still use your phone, you don’t need any technical knowledge and you don’t pay anything. Privacy threats from the device itself: A new device may contain pre-loaded apps and services from carrier or from the manufacturer. Such apps are referred to as bloatware. These preloaded apps have a potential to share your data with your carrier or manufacturer. Solution: You should simply disable all such apps. Privacy threats from third-party apps: Majority of third-party apps are not open source which means you don’t know what these apps actually do with your phone. YouTube, Instagram, PayTM are all examples of such apps. In fact majority of apps sold on Google Play Store are proprietary in nature. Solution: Use only open source apps from F-Droid or other providers. Stop using Play Store, only first few days would be painful, you will slowly discover the beauty of being free from proprietary software. Using F-Droid for example you replace YouTube with NewPipe which is way better than official YouTube app in terms of functionality. Now, there are apps which you simply cannot do without e.g. Facebook, Instagram etc. Such apps can be replaced with their web version using WebApps. Privacy threats from carrier: Everything you do on Internet using your phone is recorded by your carrier. All data traffic from your phone has to go through carrier’s servers. Carriers maintain logs of all websites you visit, every video you play and sometimes even your passwords can be stored. There are definite laws in India as to what can and what cannot be logged by carriers! In short, they have got you! Solution: Use an anonymous VPN which doesn’t keep a log. With VPN in place, carrier cannot see your data or requests, i.e. now carrier doesn’t know which website you visit or how long do you stay there. Free VPNs such as can be used. If you have questions, please feel free to comment below.

Merge Sort : Why Sorting Lists in Halves is Interesting?

Searching and sorting are two basic problems which occur in various more complex computing problems. Time complexity of sorting algorithms cannot get better than O (nlogn). Many popular algorithms have polynomial time complexity O (n2). Undoubtedly sorted lists are interesting because we can solve many problems using it: from binary search to Kruskal’s algorithm. This post tries to point out why sorting divisions of lists (say, two halves) is interesting using examples of merge sort and selection algorithms. If we have a list of unsorted numbers:
A= [9, 5, 8, 3, 6, 7, 4, 0, 2, 5]

sorting it using any O(n2) sorting algorithm requires 102 = 100 units of time. If we divide the list into two lists of 5 elements each

A1 = [9, 5, 8, 3, 6]

and
A2 = [7, 4, 0, 2, 5]

each list will require 52 = 25 units of time which is only half of original 100. But the problem is that if we again concatenate the sorted lists the resultant list still remains unsorted :

A' = [3, 5, 6, 8, 9, 0, 2, 4, 5, 7]

Even if we could not sort the list as whole reducing time complexity is certainly interesting! This property can be used in recursively sorting the array more efficiently. Merge-sort is one such algorithm with O (n log n) time complexity. Merge sort does not concatenate divided list but as the name suggests it merges them. Merging two sorted lists is easy. Another example is selection algorithms where the problem is to find kth smallest/largest elements in a list. Selection also requires sorting. Median of medians is a selection algorithm which uses this property to look for kth smallest/largest element by reducing search space into half at each iteration. Both these algorithms are examples of divide and conquer strategy.