SlideShare a Scribd company logo
1 of 60
Build a private blockchain
Pad Kankipati
Blockchain Conference
Presenters
Pad Kankipati
VP of Architecture, Loyal Health
Pratik Patel
IBM - Lead Developer Advocate
Pad Kankipati
Prerequisites
• Download Python 3.6+, Flash and requests library and pip..
Checklist:
• Install Python 3.6+ https://www.python.org/downloads/
• Install pip (if not installed already by Python)
• Install Flask and Requests libraries.
pip install Flask==0.12.2 requests==2.18.4
• What IDE to use?
Install PyCharm or any other IDE.. (Community edition)
https://www.jetbrains.com/pycharm/download/
HTTP client?
Finally a HTTP client like Postman or Curl?
Postman: https://www.getpostman.com/
PLEASE REGISTER SLACK: tech404.github.io Channel: #blockchainconf
WIFI: SID: EVENTS
Username: blockchain
Password: blockchain2018
What is blockchain?
A blockchain is a decentralized, distributed and
incorruptible digital ledger that is used to record
transactions across many computers
Blockchain Simple terms
• Distributed network of computers (nodes)
• where each node contains a chain-of-blocks
• where each block contains a ledger with a list of transactions
• where each transaction
is incorruptible (i.e. cryptographically secure)
• & is linked to the previous transactions for the resource it is
representing.
• Blockchain is an immutable, sequential chain of records called
Blocks.
• Blocks contain transactions, files or any data you like
• Blocks are chained together using hashes
Incorruptible (“cyrpto” of cryptocurrency)
• Achieves this by using 3 concepts:
• 1. Hash Functions (H(x)): A function which is
used to convert a random set of input to an
output of a fixed size.
• For any input x, H(x) is the value of the hash
function.
Incorruptible (contd).
• 2. Digital Signatures: Digital signatures are like
our normal signatures in digital form. They need
to have this basic property:
• Only you can sign BUT anyone can verify.
• Uses Private Key to sign and Public key to verify
• Bitcoin uses (Elliptical Curve Digital Signature
Algorithm) ECDSA for digital signatures.
Elliptical curve cryptography (ECC)
• Public key encryption technique to create faster, smaller and
efficient cryptographic keys.
• Used for encrypting public keys
• Relatively easy to verify, very difficult to traverse back to
obtain critical private data
• Bitcoin, Ethereum etc use, secp256k1 (y2 = x3 + 7)
Incorruptible (contd).
• 3. Hash Pointers
• Hash pointer is another good data structure
that is leveraged in the blockchain
Incorruptible (contd).
•
Incorruptible (contd).
• Block 1 (from the left) contains the data and a
hash-pointer to the previous block. Block 2
contains a pointer and the hash of the first
block and also its data.
• Key point is that hash is the hash of data and
the hash of previous block combined.
H(this-block) = H(H(previous-block) + data-in-
this-block)
Incorruptible (contd).
• Effectively, altering any unit of information on the
blockchain would mean using a huge amount of
computing power to override the entire network.
And any block can validate whether this
transaction is valid or not by following all the
previous blocks in the chain.
Merkle Tree
• Better data structure to avoid traversal of all
blocks
• The one used by Bitcoin and Ethereum is
known as Merkle Tree.
Merkle Tree
• The idea here is to quickly validate a transaction by
going from root to leaf rather than traversing all the
blocks linearly
Decentralized & Distributed Ledger
• A basic Peer to Peer network
• Everybody is responsible for keeping the entire
set of data and keeping checks on each other
• Consensus among nodes is used to keep the
distributed ledger in-tact, using incentives and
Proof of work
Incentives & Proof of Work
• Addition of each block (ie transaction(s)) and creation
of coin is managed by computational activity called
“Proof of Work”
• Whenever a new block is added by an honest
computer, it gets reward or coin, incentivizing the
miner
• It is similar to mining gold etc., except CPU time and
electricity is expended
• A computer only gets paid if the block ends up in
the longest-chain
• There is additional Transactional fee to whoever
creates a block and puts the transaction
Nonce
• The goal of PoW is to discover a number which
solves a problem
• The number must be difficult to find but easy
to verify—computationally speaking—by
anyone on the network. This is the core idea
behind Proof of Work.
• We vary the string by adding an integer value
to the end called a nonce and incrementing it
each time until the hash (SHA-256) starts with
“0000”
Nonce• For eg: with “Hello world” we keep adding the number and hash it until it starts with “0000”, as we
can see difficulty keeps going up until attempt 4251.
• Attempt1 => "Hello, world!0" =>
1312af178c253f84028d480a6adc1e25e81caa44c749ec81976192e2ec934c64
• Attempt2 => "Hello, world!1" =>
e9afc424b79e4f6ab42d99c81156d3a17228d6e1eef4139be78e948a9332a7d8
• Attempt3 => "Hello, world!2" =>
ae37343a357a8297591625e7134cbea22f5928be8ca2a32aa475cf05fd4266b7
...
...
...
...
...
• Attempt4249 => "Hello, world!4248" =>
6e110d98b388e77e9c6f042ac6b497cec46660deef75a55ebc7cfdf65cc0b965
• Attempt4250 => "Hello, world!4249" =>
c004190b822f1669cac8dc37e761cb73652e7832fb814565702245cf26ebb9e6
• Attempt4251 => "Hello, world!4250" =>
0000c3af42fc31103f1fdc0151fa747ff87349a4714df7cc52ea464e12dcd4e9
Transactions
• In blockchain, every coin (or entity) is defined
as chain of digital signatures
• Each owner digitally signs the hash of previous
transaction and the public key.
• A simple transaction would look like
Transactions
• This process continues to verify the chain of
ownership (end-to-end)
Double spend and longest chain
• Verify that the owner did not spend the same
coin at two different entities, ie double-spend
• Nodes always consider the longest-chain to be
the correct one and will keep working on
extending it
Conclusion
• New transactions are broadcast to all nodes.
• Each node collects new transactions into a block.
• Each node works on finding a “difficult” proof-of-work
for its block.
• When a node finds a proof-of-work, it broadcasts the
block to all nodes (and gets paid)
• Nodes accept the block only if all transactions in it are
valid and not already spent.
• Nodes express their acceptance of the block by
working on creating the next block in the chain, using
the hash of the accepted block as the previous hash.
Let’s dig in
Prerequisites
• Download Python 3.6+, Flash and requests library and pip..
Checklist:
• Install Python 3.6+ https://www.python.org/downloads/
• Install pip (if not installed already by Python)
• Install Flask and Requests libraries.
pip install Flask==0.12.2 requests==2.18.4
• What IDE to use?
Install PyCharm or any other IDE.. (Community edition)
https://www.jetbrains.com/pycharm/download/
HTTP client?
Finally a HTTP client like Postman or Curl?
Postman: https://www.getpostman.com/
Steps to create and run a blockchain
• Step 1: Building a Blockchain
• Step 2: Blockchain as an API
• Step 3: Interacting with our blockchain
• Step 4: Consensus
• Step 5: Test the chain
That’s it.
Step 1: Building a Blockchain
• In Pycharm create a new file blockchain.py
• It’ll store transactions and have some helper
methods for adding new blocks to the chain.
class Blockchain(object):
def __init__(self):
self.chain = []
self.currentTransactions = []
def new_block(self):
# Creates a new Block and adds it to the chain
pass
def new_transaction(self):
# Adds a new transaction to the list of transactions
pass
@staticmethod
def hash(block):
# Hashes a Block
pass
@property
def last_block(self):
# Returns the last Block in the chain
pass
What does a block has?
• A Block has
• Index
• Timestamp
• A list of transactions
• A Proof
• And a Hash to the previous Block.
block = {
'index': 1,
'timestamp': 1506057125.900785,
'transactions': [
{
'sender': "8527147fe1f5426f9dd545de4b27ee00",
'recipient': "a77f5cdfa2934df3954a5c7c7da5df1f",
'amount': 5,
}
],
'proof': 324984774000,
'previous_hash': "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
}
Adding Transactions to a Block
• Add the code to new transaction method:
def new_transaction(self, sender, recipient, amount):
"""
Creates a new transaction to go into the next mined Block
:param sender: <str> Address of the Sender
:param recipient: <str> Address of the Recipient
:param amount: <int> Amount
:return: <int> The index of the Block that will hold this transaction
"""
self.currentTransactions.append({
'sender': sender,
'recipient': recipient,
'amount': amount,
})
return self.last_block['index'] + 1
Creating new Blocks
• When new block is created we need to seed
with “Genesis” block – a starting block.
• We also need to add Proof to our genesis
block, result of mining (proof of work)
• We will update methods
new_block(), new_transaction() and hash():
import hashlib
import json
from time import time
class Blockchain(object):
def __init__(self):
self.chain = []
self.currentTransactions = []
self.nodes = set() # Set of nodes - only unique values are stored - idempotent.
# create genesis block.
self.new_block(previous_hash=1, proof=100)
def new_block(self, proof, previous_hash):
"""
Create a new block in the blockchain.
:param proof: <int> The proof given by the proof of the work algorithm
:param previous_hash: (Optional) <str> Hash of the previous block.
:return: new block
"""
block = {
'index': len(self.chain) + 1,
'timestamp': time(),
'transactions': self.currentTransactions,
'proof': proof,
'previous_hash': previous_hash or self.hash(self.chain[-1]),
}
# Reset the current list of transactions
self.currentTransactions = []
self.chain.append(block)
def new_transaction(self, sender, recipient, amount):
"""
Creates a new transaction to go into the next mined block.
:param sender: <str> Address of the sender
:param recipient: <str> Address of the recipient
:param amount: <int> amount
:return: <int> index of the block that holds the transaction
"""
self.currentTransactions.append({
'sender': sender,
'recipient' : recipient,
'amount' : amount,
})
return self.last_block['index'] + 1
@property
def last_block(self):
# Returns the last block in the chain.
return self.chain[-1]
@staticmethod
def hash(block):
"""
Creates a SHA-256 hash of a block.
:param block: <dict> block
:return: <str> hash of the block.
"""
# Make sure the dictionary is Ordered, otherwise we will have inconsistent hashes
block_string = json.dumps(block, sort_keys=True).encode()
return hashlib.sha256(block_string).hexdigest()
Proof of Work
• A Proof of Work is how new Blocks are created or
mined on the blockchain.
• The goal of PoW is to discover a number which
solves a problem.
• The number must be difficult to find but easy to
verify – by anyone in the network.
• For eg: hashing the previous and current hash and
incrementing the number until the hash starts with
4 ‘0’s like ‘0000c3af42fc31103f1fdc0151fa747ff87349a4714df7cc52ea464e12dcd4e9’
Implementing Proof of Work
• Implement the algorithm for our blockchain.
• Find the number ‘p’ that when hashed with
the previous block’s solution a hash with 4
leading 0’s is produced
import hashlib
import json
from time import time
from uuid import uuid4
class Blockchain(object):
...
def proof_of_work(self, last_block):
"""
Simple proof of work algorithm
- find a number 'p' such that hash(pp') contains last 4 leading zeroes, where p is the previous p'
- p is the previous proof, and p' is the new proof
:param last_block: last block
:return: <int>
"""
last_proof = last_block['proof']
last_hash = self.hash(last_block)
proof = 0
while self.valid_proof(last_proof, proof, last_hash) is False:
proof += 1
return proof
@staticmethod
def valid_proof(last_proof, proof, last_hash):
"""
Validates the Proof: Does hash(last_proof, proof) contains 4 leading zeroes?
:param last_proof: <int> Previous proof
:param proof: <int> current proof
:param last_hash: <str> The hash of the previous block
:return:<bool> True if correct, false if not
"""
guess = f'{last_proof}{proof}{last_hash}'.encode()
guess_hash = hashlib.sha256(guess).hexdigest()
return guess_hash[:4] == "0000"
Step 2: Blockchain as an API
We will use Python Flask, create 3 methods
/transactions/new: to create a new transaction to
a block
/mine: to tell our server to mine a new block
/chain: to return the full blockchain
Runs as a server on port 5000
import hashlib
import json
from textwrap import dedent
from time import time
from uuid import uuid4
from flask import Flask
class Blockchain(object):
...
# Instantiate our Node
app = Flask(__name__)
# Generate a globally unique address for this node
node_identifier = str(uuid4()).replace('-', '')
# Instantiate the Blockchain
blockchain = Blockchain()
@app.route('/mine', methods=['GET'])
def mine():
return "We'll mine a new Block"
@app.route('/transactions/new', methods=['POST'])
def new_transaction():
return "We'll add a new transaction"
@app.route('/chain', methods=['GET'])
def full_chain():
response = {
'chain': blockchain.chain,
'length': len(blockchain.chain),
}
return jsonify(response), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Transactions endpoint
• This is what a user will send to the server
{
"sender": "my address",
"recipient": "someone else's address",
"amount": 5
}
• Update the new_transactions() method to implement
the logic to add transactions to the block.
import hashlib
import json
from textwrap import dedent
from time import time
from uuid import uuid4
from flask import Flask, jsonify, request
...
@app.route('/transactions/new', methods = ['POST'])
def new_transactions():
values = request.get_json()
# Check that the required fields are in the POSTed data
required = ['sender', 'recipient', 'amount']
if not all (k in values for k in required):
return 'Missing values: {k}', 400
# Create a new transaction
index = blockchain.new_transaction(values['sender'], values['recipient'],
values['amount'])
response = {'message': f'Transaction will be added to Block {index}'}
return jsonify(response), 200
Mining endpoint
1. Calculate Proof of work
2. Reward the miner by adding a transaction
granting 1 coin
3. Forge the new block by adding to the chain.
We will update the mine() method to add the
logic.
import hashlib
import json
from time import time
from uuid import uuid4
from flask import Flask, jsonify, request
...
@app.route('/mine', methods=['GET'])
def mine():
# We will run the proof of work algorithm to get the next proof..
last_block = blockchain.last_block
proof = blockchain.proof_of_work(last_block)
# We must receive a reward for finding the proof.
# The sender is "0" to signify that this node has mined a new coin.
blockchain.new_transaction(
sender = "0",
recipient=node_identifier,
amount=1,
)
# Forge the new block by adding it to the chain.
previous_hash = blockchain.hash(last_block)
block = blockchain.new_block(proof, previous_hash)
response = {
'message': 'New block forged',
'index': block['index'],
'transactions': block['transactions'],
'proof': block['proof'],
'previous_hash': block['previous_hash'],
}
return jsonify(response), 200
Step 3: Interacting with our blockchain
• Fire up the server
python blockchain.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
• Use Postman to mine a block (GET) :
http://localhost:5000/mine
Create a new transaction
• Postman create a POST request
http://localhost:5000/transactions/new
Headers:
Body (JSON) RAW:
{
"sender": "3243c66fa5ce46658d57c3624f306e32",
"recipient": "some-other-user",
"amount": 8
}
More mining
• Mine multiple times by using (GET)
http://localhost:5000/mine
And inspect the chain by calling in a Browser or
Postman
http://localhost:5000/chain
{
"chain": [
{
"index": 1,
"previous_hash": 1,
"proof": 100,
"timestamp": 1506280650.770839,
"transactions": []
},
{
"index": 2,
"previous_hash": "c099bc...bfb7",
"proof": 35293,
"timestamp": 1506280664.717925,
"transactions": [
{
"amount": 1,
"recipient": "8bbcb347e0634905b0cac7955bae152b",
"sender": "0"
}
]
},
{
"index": 3,
"previous_hash": "eff91a...10f2",
"proof": 35089,
"timestamp": 1506280666.1086972,
"transactions": [
{
"amount": 1,
"recipient": "8bbcb347e0634905b0cac7955bae152b",
"sender": "0"
}
]
}
],
"length": 3
}
Yay!! We got basic blockchain.
More…
Step 4: Consensus
• Consensus allows the blockchains to be
decentralized.
• For consensus to work, all nodes should be
aware of its’ neighbors by registering and
resolving differences
/nodes/register: to accept a new list of new nodes
/nodes/resolve: implement consensus algorithm, resolves any
conflicts
• Add these methods to the blockchain code
...
from urllib.parse import urlparse
...
class Blockchain(object):
def __init__(self):
...
self.nodes = set() # Set of nodes - only unique values are stored - idempotent.
...
def register_node(self, address):
"""
Add a new node to the list of nodes.
:param address: <str> address of the node eg: http://192.168.0.10:5005
:return: None
"""
parsed_url = urlparse(address)
if parsed_url.netloc:
self.nodes.add(parsed_url.netloc)
elif parsed_url.path:
# Accepts an URL with path '192.168.0.0:5000'.
self.nodes.add(parsed_url.path)
else:
raise ValueError('Invalid URL')
Implement Consensus algorithm
• A conflict is when one node has a different chain
than another node
• To resolve, our rule is the ‘longest valid chain is
authoritative’
• Ie, the longest chain on the network is the de-facto
one.
• Let’s implement the valid_chain method and
resolve_conflicts method on blockchain.py
• valid_chain() – loops through blocks and verifies both hash and proof.
• resolve_conflicts() – loops through neighbors, downloads their chains, and
verifies. If a valid chain is found, whose length is greater than ours, we
replace ours.
...
import requests
class Blockchain(object)
...
def valid_chain(self, chain):
"""
Determine if a given blockchain is valid. Consensus - longest valid chain
is authoritative.
:param chain: <list> a blockchain
:return: <bool> True if valid, False if not.
"""
last_block = chain[0]
current_index = 1
while current_index < len(chain):
block = chain[current_index]
print(f'lastBlock: {last_block}')
print(f'currentBlock: {block}')
print("nn--------------------n")
# Check that the hash of the block is correct
last_block_hash = self.hash(last_block)
if block['previous_hash'] != last_block_hash:
return False
# Check if the proof of work is valid.
if not self.valid_proof(last_block['proof'], block['proof'],
last_block_hash):
return False
last_block = block
current_index += 1
return True
def resolve_conflicts(self):
"""
This is our consensus algorithm, it resolves conflicts by replacing our
chain with the longest chain
in the network
:return: <bool> True if the chain is replaced, False if not.
"""
neighbors = self.nodes
new_chain = None
# We're looking for chains longer than ours
max_length = len(self.chain)
# Grab and verify the chains from all nodes on the network
for node in neighbors:
print(f'requesting for node: {node}')
response = requests.get(f'http://{node}/chain')
print(f'node {node} - chain: {response}')
if response.status_code == 200:
length = response.json()['length']
chain = response.json()['chain']
# Check if the length is longer and is valid.
if length > max_length and self.valid_chain(chain):
max_length = length
new_chain = chain
# Replace our chain if the length is longer and valid.
if new_chain:
self.chain = new_chain
return True
return False
Add endpoints for consensus
• Add 2 more methods to register and resolve
/nodes/register (POST)
/nodes/resolve (GET)
@app.route('/nodes/register', methods=['POST'])
def register_nodes():
values = request.get_json()
nodes = values.get('nodes')
if nodes is None:
return "Error, please supply a valid list of nodes", 400
for node in nodes:
blockchain.register_node(node)
response = {
'message': 'New nodes have been added',
'total_nodes': list(blockchain.nodes),
}
return jsonify(response), 201
@app.route('/nodes/resolve', methods = ['GET'])
def consensus():
replaced = blockchain.resolve_conflicts()
if replaced:
response = {
'message': 'Our chain got replaced',
'new_chain': blockchain.chain
}
else:
response = {
'message': 'Our chain is authoritative',
'chain': blockchain.chain
}
return jsonify(response), 200
Step 5: Test the chain
• Fire up multiple nodes on different ports (use
flaskrun to configure multiple ports)
python blockchain.py –p 5000
python blockchain.py –p 5001
…
• Use Postman to register nodes
http://localhost:5000/register
Body:
{
“nodes”:[“http://127.0.0.1:5001”]
}
More testing..
• Mine more blocks on nodes
– Mine 3 blocks on node 2 to ensure chain was longer
• And resolve to see consensus in work on node 1.
See how chain gets replaced with longest chain
– GET /nodes/resolve
Note: You can do a GET /chain on nodes to see the
chain before calling resolve
Yay!! We are done (for now)…
Time for Q&A
Contact: Pad Kankipati
pkankipati@gmail.com
Meetup – Blockchain Hands-on Coding

More Related Content

What's hot

BlueHat v18 || Memory resident implants - code injection is alive and well
BlueHat v18 || Memory resident implants - code injection is alive and wellBlueHat v18 || Memory resident implants - code injection is alive and well
BlueHat v18 || Memory resident implants - code injection is alive and wellBlueHat Security Conference
 
Flaying the Blockchain Ledger for Fun, Profit, and Hip Hop
Flaying the Blockchain Ledger for Fun, Profit, and Hip HopFlaying the Blockchain Ledger for Fun, Profit, and Hip Hop
Flaying the Blockchain Ledger for Fun, Profit, and Hip HopAndrew Morris
 
[CB16] DeathNote of Microsoft Windows Kernel by Peter Hlavaty & Jin Long
[CB16] DeathNote of Microsoft Windows Kernel by Peter Hlavaty & Jin Long[CB16] DeathNote of Microsoft Windows Kernel by Peter Hlavaty & Jin Long
[CB16] DeathNote of Microsoft Windows Kernel by Peter Hlavaty & Jin LongCODE BLUE
 
Mobile Programming - Network Universitas Budi Luhur
Mobile Programming - Network Universitas Budi LuhurMobile Programming - Network Universitas Budi Luhur
Mobile Programming - Network Universitas Budi LuhurRiza Fahmi
 
OSMC 2018 | Thruk 2½ – Current state of development by Sven Nierlein
OSMC 2018 | Thruk 2½ – Current state of development by Sven NierleinOSMC 2018 | Thruk 2½ – Current state of development by Sven Nierlein
OSMC 2018 | Thruk 2½ – Current state of development by Sven NierleinNETWAYS
 
Alexey Golub - Dependency absolution (application as a pipeline) | Svitla Sma...
Alexey Golub - Dependency absolution (application as a pipeline) | Svitla Sma...Alexey Golub - Dependency absolution (application as a pipeline) | Svitla Sma...
Alexey Golub - Dependency absolution (application as a pipeline) | Svitla Sma...Oleksii Holub
 
"Into the Fog The Return of ICEFOG APT" - Chi en (Ashley) Shen
"Into the Fog The Return of ICEFOG APT" - Chi en (Ashley) Shen"Into the Fog The Return of ICEFOG APT" - Chi en (Ashley) Shen
"Into the Fog The Return of ICEFOG APT" - Chi en (Ashley) ShenPROIDEA
 
J.burke HackMiami6
J.burke HackMiami6J.burke HackMiami6
J.burke HackMiami6Jesse Burke
 
Scaling Ethereum using Zero-Knowledge Proofs
Scaling Ethereum using Zero-Knowledge ProofsScaling Ethereum using Zero-Knowledge Proofs
Scaling Ethereum using Zero-Knowledge ProofsHyojun Kim
 
Azure Unchained (Azure boot camp Sofia 2017)
Azure Unchained (Azure boot camp Sofia 2017)Azure Unchained (Azure boot camp Sofia 2017)
Azure Unchained (Azure boot camp Sofia 2017)Valio Bonev
 
Developing Business Blockchain Applications on Hyperledger
Developing Business  Blockchain Applications on Hyperledger Developing Business  Blockchain Applications on Hyperledger
Developing Business Blockchain Applications on Hyperledger IMC Institute
 
What's New in Go Crypto - Gotham Go
What's New in Go Crypto - Gotham GoWhat's New in Go Crypto - Gotham Go
What's New in Go Crypto - Gotham GoNick Sullivan
 
Journée DevOps : Des dashboards pour tous avec ElasticSearch, Logstash et Kibana
Journée DevOps : Des dashboards pour tous avec ElasticSearch, Logstash et KibanaJournée DevOps : Des dashboards pour tous avec ElasticSearch, Logstash et Kibana
Journée DevOps : Des dashboards pour tous avec ElasticSearch, Logstash et KibanaPublicis Sapient Engineering
 
Jose Selvi - Side-Channels Uncovered [rootedvlc2018]
Jose Selvi - Side-Channels Uncovered [rootedvlc2018]Jose Selvi - Side-Channels Uncovered [rootedvlc2018]
Jose Selvi - Side-Channels Uncovered [rootedvlc2018]RootedCON
 
Netty from the trenches
Netty from the trenchesNetty from the trenches
Netty from the trenchesJordi Gerona
 
Redis - for duplicate detection on real time stream
Redis - for duplicate detection on real time streamRedis - for duplicate detection on real time stream
Redis - for duplicate detection on real time streamCodemotion
 
OSDC 2016 - Unifying Logs and Metrics Data with Elastic Beats by Monica Sarbu
OSDC 2016 - Unifying Logs and Metrics Data with Elastic Beats by Monica SarbuOSDC 2016 - Unifying Logs and Metrics Data with Elastic Beats by Monica Sarbu
OSDC 2016 - Unifying Logs and Metrics Data with Elastic Beats by Monica SarbuNETWAYS
 
Cracking JWT tokens: a tale of magic, Node.js and parallel computing - Code E...
Cracking JWT tokens: a tale of magic, Node.js and parallel computing - Code E...Cracking JWT tokens: a tale of magic, Node.js and parallel computing - Code E...
Cracking JWT tokens: a tale of magic, Node.js and parallel computing - Code E...Luciano Mammino
 
Bringing Elliptic Curve Cryptography into the Mainstream
Bringing Elliptic Curve Cryptography into the MainstreamBringing Elliptic Curve Cryptography into the Mainstream
Bringing Elliptic Curve Cryptography into the MainstreamNick Sullivan
 

What's hot (20)

BlueHat v18 || Memory resident implants - code injection is alive and well
BlueHat v18 || Memory resident implants - code injection is alive and wellBlueHat v18 || Memory resident implants - code injection is alive and well
BlueHat v18 || Memory resident implants - code injection is alive and well
 
Flaying the Blockchain Ledger for Fun, Profit, and Hip Hop
Flaying the Blockchain Ledger for Fun, Profit, and Hip HopFlaying the Blockchain Ledger for Fun, Profit, and Hip Hop
Flaying the Blockchain Ledger for Fun, Profit, and Hip Hop
 
[CB16] DeathNote of Microsoft Windows Kernel by Peter Hlavaty & Jin Long
[CB16] DeathNote of Microsoft Windows Kernel by Peter Hlavaty & Jin Long[CB16] DeathNote of Microsoft Windows Kernel by Peter Hlavaty & Jin Long
[CB16] DeathNote of Microsoft Windows Kernel by Peter Hlavaty & Jin Long
 
Mobile Programming - Network Universitas Budi Luhur
Mobile Programming - Network Universitas Budi LuhurMobile Programming - Network Universitas Budi Luhur
Mobile Programming - Network Universitas Budi Luhur
 
OSMC 2018 | Thruk 2½ – Current state of development by Sven Nierlein
OSMC 2018 | Thruk 2½ – Current state of development by Sven NierleinOSMC 2018 | Thruk 2½ – Current state of development by Sven Nierlein
OSMC 2018 | Thruk 2½ – Current state of development by Sven Nierlein
 
Alexey Golub - Dependency absolution (application as a pipeline) | Svitla Sma...
Alexey Golub - Dependency absolution (application as a pipeline) | Svitla Sma...Alexey Golub - Dependency absolution (application as a pipeline) | Svitla Sma...
Alexey Golub - Dependency absolution (application as a pipeline) | Svitla Sma...
 
Reactive server with netty
Reactive server with nettyReactive server with netty
Reactive server with netty
 
"Into the Fog The Return of ICEFOG APT" - Chi en (Ashley) Shen
"Into the Fog The Return of ICEFOG APT" - Chi en (Ashley) Shen"Into the Fog The Return of ICEFOG APT" - Chi en (Ashley) Shen
"Into the Fog The Return of ICEFOG APT" - Chi en (Ashley) Shen
 
J.burke HackMiami6
J.burke HackMiami6J.burke HackMiami6
J.burke HackMiami6
 
Scaling Ethereum using Zero-Knowledge Proofs
Scaling Ethereum using Zero-Knowledge ProofsScaling Ethereum using Zero-Knowledge Proofs
Scaling Ethereum using Zero-Knowledge Proofs
 
Azure Unchained (Azure boot camp Sofia 2017)
Azure Unchained (Azure boot camp Sofia 2017)Azure Unchained (Azure boot camp Sofia 2017)
Azure Unchained (Azure boot camp Sofia 2017)
 
Developing Business Blockchain Applications on Hyperledger
Developing Business  Blockchain Applications on Hyperledger Developing Business  Blockchain Applications on Hyperledger
Developing Business Blockchain Applications on Hyperledger
 
What's New in Go Crypto - Gotham Go
What's New in Go Crypto - Gotham GoWhat's New in Go Crypto - Gotham Go
What's New in Go Crypto - Gotham Go
 
Journée DevOps : Des dashboards pour tous avec ElasticSearch, Logstash et Kibana
Journée DevOps : Des dashboards pour tous avec ElasticSearch, Logstash et KibanaJournée DevOps : Des dashboards pour tous avec ElasticSearch, Logstash et Kibana
Journée DevOps : Des dashboards pour tous avec ElasticSearch, Logstash et Kibana
 
Jose Selvi - Side-Channels Uncovered [rootedvlc2018]
Jose Selvi - Side-Channels Uncovered [rootedvlc2018]Jose Selvi - Side-Channels Uncovered [rootedvlc2018]
Jose Selvi - Side-Channels Uncovered [rootedvlc2018]
 
Netty from the trenches
Netty from the trenchesNetty from the trenches
Netty from the trenches
 
Redis - for duplicate detection on real time stream
Redis - for duplicate detection on real time streamRedis - for duplicate detection on real time stream
Redis - for duplicate detection on real time stream
 
OSDC 2016 - Unifying Logs and Metrics Data with Elastic Beats by Monica Sarbu
OSDC 2016 - Unifying Logs and Metrics Data with Elastic Beats by Monica SarbuOSDC 2016 - Unifying Logs and Metrics Data with Elastic Beats by Monica Sarbu
OSDC 2016 - Unifying Logs and Metrics Data with Elastic Beats by Monica Sarbu
 
Cracking JWT tokens: a tale of magic, Node.js and parallel computing - Code E...
Cracking JWT tokens: a tale of magic, Node.js and parallel computing - Code E...Cracking JWT tokens: a tale of magic, Node.js and parallel computing - Code E...
Cracking JWT tokens: a tale of magic, Node.js and parallel computing - Code E...
 
Bringing Elliptic Curve Cryptography into the Mainstream
Bringing Elliptic Curve Cryptography into the MainstreamBringing Elliptic Curve Cryptography into the Mainstream
Bringing Elliptic Curve Cryptography into the Mainstream
 

Similar to BlockchainConf.tech - Build a private blockchain workshop

Node.js Blockchain Implementation
Node.js Blockchain ImplementationNode.js Blockchain Implementation
Node.js Blockchain ImplementationGlobalLogic Ukraine
 
A Quick Start To Blockchain by Seval Capraz
A Quick Start To Blockchain by Seval CaprazA Quick Start To Blockchain by Seval Capraz
A Quick Start To Blockchain by Seval CaprazSeval Çapraz
 
How to Build Your Own Blockchain
How to Build Your Own BlockchainHow to Build Your Own Blockchain
How to Build Your Own BlockchainLeonid Beder
 
Blockchain Demystified
Blockchain DemystifiedBlockchain Demystified
Blockchain DemystifiedMahesh M Reddy
 
20190606 blockchain101
20190606 blockchain10120190606 blockchain101
20190606 blockchain101Hu Kenneth
 
Understanding Blockchain
Understanding BlockchainUnderstanding Blockchain
Understanding BlockchainTony Willenberg
 
Blockchain and bitcoin fundamentals (usages and applications)
Blockchain and bitcoin fundamentals (usages and applications)Blockchain and bitcoin fundamentals (usages and applications)
Blockchain and bitcoin fundamentals (usages and applications)Amir Rafati
 
Introduction to Blockchain with an Ethereuem Hands-on
Introduction to Blockchain with an Ethereuem Hands-onIntroduction to Blockchain with an Ethereuem Hands-on
Introduction to Blockchain with an Ethereuem Hands-onJohann Romefort
 
Introduction to Ethereum
Introduction to EthereumIntroduction to Ethereum
Introduction to EthereumArnold Pham
 
Architecture and operations.pptx
Architecture and operations.pptxArchitecture and operations.pptx
Architecture and operations.pptxharshitmittal737363
 
The Ethereum Geth Client
The Ethereum Geth ClientThe Ethereum Geth Client
The Ethereum Geth ClientArnold Pham
 
Blockchain, cryptography and tokens — NYC Bar presentation
Blockchain, cryptography and tokens — NYC Bar presentationBlockchain, cryptography and tokens — NYC Bar presentation
Blockchain, cryptography and tokens — NYC Bar presentationPaperchain
 
Bitcoin, Banking and the Blockchain
Bitcoin, Banking and the BlockchainBitcoin, Banking and the Blockchain
Bitcoin, Banking and the Blockchainseancarmody
 
blockchain class 3.pdf
blockchain class 3.pdfblockchain class 3.pdf
blockchain class 3.pdfGopalSB
 
14 Jan17- Nullmeets -Blockchain concept decoded by Ninad Sarang
14 Jan17- Nullmeets -Blockchain concept decoded by Ninad Sarang14 Jan17- Nullmeets -Blockchain concept decoded by Ninad Sarang
14 Jan17- Nullmeets -Blockchain concept decoded by Ninad SarangNinad Sarang
 

Similar to BlockchainConf.tech - Build a private blockchain workshop (20)

Blockchain Fundamentals
Blockchain FundamentalsBlockchain Fundamentals
Blockchain Fundamentals
 
Node.js Blockchain Implementation
Node.js Blockchain ImplementationNode.js Blockchain Implementation
Node.js Blockchain Implementation
 
A Quick Start To Blockchain by Seval Capraz
A Quick Start To Blockchain by Seval CaprazA Quick Start To Blockchain by Seval Capraz
A Quick Start To Blockchain by Seval Capraz
 
Basics of Block Chain
Basics of Block ChainBasics of Block Chain
Basics of Block Chain
 
Blockchain - a simple implementation
Blockchain - a simple implementationBlockchain - a simple implementation
Blockchain - a simple implementation
 
How to Build Your Own Blockchain
How to Build Your Own BlockchainHow to Build Your Own Blockchain
How to Build Your Own Blockchain
 
Blockchain Demystified
Blockchain DemystifiedBlockchain Demystified
Blockchain Demystified
 
20190606 blockchain101
20190606 blockchain10120190606 blockchain101
20190606 blockchain101
 
Understanding Blockchain
Understanding BlockchainUnderstanding Blockchain
Understanding Blockchain
 
Blockchain and bitcoin fundamentals (usages and applications)
Blockchain and bitcoin fundamentals (usages and applications)Blockchain and bitcoin fundamentals (usages and applications)
Blockchain and bitcoin fundamentals (usages and applications)
 
Introduction to Blockchain with an Ethereuem Hands-on
Introduction to Blockchain with an Ethereuem Hands-onIntroduction to Blockchain with an Ethereuem Hands-on
Introduction to Blockchain with an Ethereuem Hands-on
 
Day 1.pptx
Day 1.pptxDay 1.pptx
Day 1.pptx
 
Introduction to Ethereum
Introduction to EthereumIntroduction to Ethereum
Introduction to Ethereum
 
Blockchain
BlockchainBlockchain
Blockchain
 
Architecture and operations.pptx
Architecture and operations.pptxArchitecture and operations.pptx
Architecture and operations.pptx
 
The Ethereum Geth Client
The Ethereum Geth ClientThe Ethereum Geth Client
The Ethereum Geth Client
 
Blockchain, cryptography and tokens — NYC Bar presentation
Blockchain, cryptography and tokens — NYC Bar presentationBlockchain, cryptography and tokens — NYC Bar presentation
Blockchain, cryptography and tokens — NYC Bar presentation
 
Bitcoin, Banking and the Blockchain
Bitcoin, Banking and the BlockchainBitcoin, Banking and the Blockchain
Bitcoin, Banking and the Blockchain
 
blockchain class 3.pdf
blockchain class 3.pdfblockchain class 3.pdf
blockchain class 3.pdf
 
14 Jan17- Nullmeets -Blockchain concept decoded by Ninad Sarang
14 Jan17- Nullmeets -Blockchain concept decoded by Ninad Sarang14 Jan17- Nullmeets -Blockchain concept decoded by Ninad Sarang
14 Jan17- Nullmeets -Blockchain concept decoded by Ninad Sarang
 

Recently uploaded

Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 

Recently uploaded (20)

Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 

BlockchainConf.tech - Build a private blockchain workshop

  • 1. Build a private blockchain Pad Kankipati Blockchain Conference
  • 2. Presenters Pad Kankipati VP of Architecture, Loyal Health Pratik Patel IBM - Lead Developer Advocate Pad Kankipati
  • 3. Prerequisites • Download Python 3.6+, Flash and requests library and pip.. Checklist: • Install Python 3.6+ https://www.python.org/downloads/ • Install pip (if not installed already by Python) • Install Flask and Requests libraries. pip install Flask==0.12.2 requests==2.18.4 • What IDE to use? Install PyCharm or any other IDE.. (Community edition) https://www.jetbrains.com/pycharm/download/ HTTP client? Finally a HTTP client like Postman or Curl? Postman: https://www.getpostman.com/ PLEASE REGISTER SLACK: tech404.github.io Channel: #blockchainconf WIFI: SID: EVENTS Username: blockchain Password: blockchain2018
  • 4. What is blockchain? A blockchain is a decentralized, distributed and incorruptible digital ledger that is used to record transactions across many computers
  • 5. Blockchain Simple terms • Distributed network of computers (nodes) • where each node contains a chain-of-blocks • where each block contains a ledger with a list of transactions • where each transaction is incorruptible (i.e. cryptographically secure) • & is linked to the previous transactions for the resource it is representing. • Blockchain is an immutable, sequential chain of records called Blocks. • Blocks contain transactions, files or any data you like • Blocks are chained together using hashes
  • 6. Incorruptible (“cyrpto” of cryptocurrency) • Achieves this by using 3 concepts: • 1. Hash Functions (H(x)): A function which is used to convert a random set of input to an output of a fixed size. • For any input x, H(x) is the value of the hash function.
  • 7. Incorruptible (contd). • 2. Digital Signatures: Digital signatures are like our normal signatures in digital form. They need to have this basic property: • Only you can sign BUT anyone can verify. • Uses Private Key to sign and Public key to verify • Bitcoin uses (Elliptical Curve Digital Signature Algorithm) ECDSA for digital signatures.
  • 8. Elliptical curve cryptography (ECC) • Public key encryption technique to create faster, smaller and efficient cryptographic keys. • Used for encrypting public keys • Relatively easy to verify, very difficult to traverse back to obtain critical private data • Bitcoin, Ethereum etc use, secp256k1 (y2 = x3 + 7)
  • 9. Incorruptible (contd). • 3. Hash Pointers • Hash pointer is another good data structure that is leveraged in the blockchain
  • 11. Incorruptible (contd). • Block 1 (from the left) contains the data and a hash-pointer to the previous block. Block 2 contains a pointer and the hash of the first block and also its data. • Key point is that hash is the hash of data and the hash of previous block combined. H(this-block) = H(H(previous-block) + data-in- this-block)
  • 12. Incorruptible (contd). • Effectively, altering any unit of information on the blockchain would mean using a huge amount of computing power to override the entire network. And any block can validate whether this transaction is valid or not by following all the previous blocks in the chain.
  • 13. Merkle Tree • Better data structure to avoid traversal of all blocks • The one used by Bitcoin and Ethereum is known as Merkle Tree.
  • 14. Merkle Tree • The idea here is to quickly validate a transaction by going from root to leaf rather than traversing all the blocks linearly
  • 15. Decentralized & Distributed Ledger • A basic Peer to Peer network • Everybody is responsible for keeping the entire set of data and keeping checks on each other • Consensus among nodes is used to keep the distributed ledger in-tact, using incentives and Proof of work
  • 16. Incentives & Proof of Work • Addition of each block (ie transaction(s)) and creation of coin is managed by computational activity called “Proof of Work” • Whenever a new block is added by an honest computer, it gets reward or coin, incentivizing the miner • It is similar to mining gold etc., except CPU time and electricity is expended • A computer only gets paid if the block ends up in the longest-chain • There is additional Transactional fee to whoever creates a block and puts the transaction
  • 17. Nonce • The goal of PoW is to discover a number which solves a problem • The number must be difficult to find but easy to verify—computationally speaking—by anyone on the network. This is the core idea behind Proof of Work. • We vary the string by adding an integer value to the end called a nonce and incrementing it each time until the hash (SHA-256) starts with “0000”
  • 18. Nonce• For eg: with “Hello world” we keep adding the number and hash it until it starts with “0000”, as we can see difficulty keeps going up until attempt 4251. • Attempt1 => "Hello, world!0" => 1312af178c253f84028d480a6adc1e25e81caa44c749ec81976192e2ec934c64 • Attempt2 => "Hello, world!1" => e9afc424b79e4f6ab42d99c81156d3a17228d6e1eef4139be78e948a9332a7d8 • Attempt3 => "Hello, world!2" => ae37343a357a8297591625e7134cbea22f5928be8ca2a32aa475cf05fd4266b7 ... ... ... ... ... • Attempt4249 => "Hello, world!4248" => 6e110d98b388e77e9c6f042ac6b497cec46660deef75a55ebc7cfdf65cc0b965 • Attempt4250 => "Hello, world!4249" => c004190b822f1669cac8dc37e761cb73652e7832fb814565702245cf26ebb9e6 • Attempt4251 => "Hello, world!4250" => 0000c3af42fc31103f1fdc0151fa747ff87349a4714df7cc52ea464e12dcd4e9
  • 19. Transactions • In blockchain, every coin (or entity) is defined as chain of digital signatures • Each owner digitally signs the hash of previous transaction and the public key. • A simple transaction would look like
  • 20. Transactions • This process continues to verify the chain of ownership (end-to-end)
  • 21. Double spend and longest chain • Verify that the owner did not spend the same coin at two different entities, ie double-spend • Nodes always consider the longest-chain to be the correct one and will keep working on extending it
  • 22. Conclusion • New transactions are broadcast to all nodes. • Each node collects new transactions into a block. • Each node works on finding a “difficult” proof-of-work for its block. • When a node finds a proof-of-work, it broadcasts the block to all nodes (and gets paid) • Nodes accept the block only if all transactions in it are valid and not already spent. • Nodes express their acceptance of the block by working on creating the next block in the chain, using the hash of the accepted block as the previous hash.
  • 24. Prerequisites • Download Python 3.6+, Flash and requests library and pip.. Checklist: • Install Python 3.6+ https://www.python.org/downloads/ • Install pip (if not installed already by Python) • Install Flask and Requests libraries. pip install Flask==0.12.2 requests==2.18.4 • What IDE to use? Install PyCharm or any other IDE.. (Community edition) https://www.jetbrains.com/pycharm/download/ HTTP client? Finally a HTTP client like Postman or Curl? Postman: https://www.getpostman.com/
  • 25. Steps to create and run a blockchain • Step 1: Building a Blockchain • Step 2: Blockchain as an API • Step 3: Interacting with our blockchain • Step 4: Consensus • Step 5: Test the chain That’s it.
  • 26. Step 1: Building a Blockchain • In Pycharm create a new file blockchain.py • It’ll store transactions and have some helper methods for adding new blocks to the chain. class Blockchain(object): def __init__(self): self.chain = [] self.currentTransactions = [] def new_block(self): # Creates a new Block and adds it to the chain pass def new_transaction(self): # Adds a new transaction to the list of transactions pass @staticmethod def hash(block): # Hashes a Block pass @property def last_block(self): # Returns the last Block in the chain pass
  • 27. What does a block has? • A Block has • Index • Timestamp • A list of transactions • A Proof • And a Hash to the previous Block. block = { 'index': 1, 'timestamp': 1506057125.900785, 'transactions': [ { 'sender': "8527147fe1f5426f9dd545de4b27ee00", 'recipient': "a77f5cdfa2934df3954a5c7c7da5df1f", 'amount': 5, } ], 'proof': 324984774000, 'previous_hash': "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824" }
  • 28. Adding Transactions to a Block • Add the code to new transaction method: def new_transaction(self, sender, recipient, amount): """ Creates a new transaction to go into the next mined Block :param sender: <str> Address of the Sender :param recipient: <str> Address of the Recipient :param amount: <int> Amount :return: <int> The index of the Block that will hold this transaction """ self.currentTransactions.append({ 'sender': sender, 'recipient': recipient, 'amount': amount, }) return self.last_block['index'] + 1
  • 29. Creating new Blocks • When new block is created we need to seed with “Genesis” block – a starting block. • We also need to add Proof to our genesis block, result of mining (proof of work) • We will update methods new_block(), new_transaction() and hash():
  • 30. import hashlib import json from time import time class Blockchain(object): def __init__(self): self.chain = [] self.currentTransactions = [] self.nodes = set() # Set of nodes - only unique values are stored - idempotent. # create genesis block. self.new_block(previous_hash=1, proof=100) def new_block(self, proof, previous_hash): """ Create a new block in the blockchain. :param proof: <int> The proof given by the proof of the work algorithm :param previous_hash: (Optional) <str> Hash of the previous block. :return: new block """ block = { 'index': len(self.chain) + 1, 'timestamp': time(), 'transactions': self.currentTransactions, 'proof': proof, 'previous_hash': previous_hash or self.hash(self.chain[-1]), } # Reset the current list of transactions self.currentTransactions = [] self.chain.append(block)
  • 31. def new_transaction(self, sender, recipient, amount): """ Creates a new transaction to go into the next mined block. :param sender: <str> Address of the sender :param recipient: <str> Address of the recipient :param amount: <int> amount :return: <int> index of the block that holds the transaction """ self.currentTransactions.append({ 'sender': sender, 'recipient' : recipient, 'amount' : amount, }) return self.last_block['index'] + 1 @property def last_block(self): # Returns the last block in the chain. return self.chain[-1] @staticmethod def hash(block): """ Creates a SHA-256 hash of a block. :param block: <dict> block :return: <str> hash of the block. """ # Make sure the dictionary is Ordered, otherwise we will have inconsistent hashes block_string = json.dumps(block, sort_keys=True).encode() return hashlib.sha256(block_string).hexdigest()
  • 32. Proof of Work • A Proof of Work is how new Blocks are created or mined on the blockchain. • The goal of PoW is to discover a number which solves a problem. • The number must be difficult to find but easy to verify – by anyone in the network. • For eg: hashing the previous and current hash and incrementing the number until the hash starts with 4 ‘0’s like ‘0000c3af42fc31103f1fdc0151fa747ff87349a4714df7cc52ea464e12dcd4e9’
  • 33. Implementing Proof of Work • Implement the algorithm for our blockchain. • Find the number ‘p’ that when hashed with the previous block’s solution a hash with 4 leading 0’s is produced
  • 34. import hashlib import json from time import time from uuid import uuid4 class Blockchain(object): ... def proof_of_work(self, last_block): """ Simple proof of work algorithm - find a number 'p' such that hash(pp') contains last 4 leading zeroes, where p is the previous p' - p is the previous proof, and p' is the new proof :param last_block: last block :return: <int> """ last_proof = last_block['proof'] last_hash = self.hash(last_block) proof = 0 while self.valid_proof(last_proof, proof, last_hash) is False: proof += 1 return proof @staticmethod def valid_proof(last_proof, proof, last_hash): """ Validates the Proof: Does hash(last_proof, proof) contains 4 leading zeroes? :param last_proof: <int> Previous proof :param proof: <int> current proof :param last_hash: <str> The hash of the previous block :return:<bool> True if correct, false if not """ guess = f'{last_proof}{proof}{last_hash}'.encode() guess_hash = hashlib.sha256(guess).hexdigest() return guess_hash[:4] == "0000"
  • 35. Step 2: Blockchain as an API We will use Python Flask, create 3 methods /transactions/new: to create a new transaction to a block /mine: to tell our server to mine a new block /chain: to return the full blockchain Runs as a server on port 5000
  • 36. import hashlib import json from textwrap import dedent from time import time from uuid import uuid4 from flask import Flask class Blockchain(object): ... # Instantiate our Node app = Flask(__name__) # Generate a globally unique address for this node node_identifier = str(uuid4()).replace('-', '') # Instantiate the Blockchain blockchain = Blockchain() @app.route('/mine', methods=['GET']) def mine(): return "We'll mine a new Block" @app.route('/transactions/new', methods=['POST']) def new_transaction(): return "We'll add a new transaction" @app.route('/chain', methods=['GET']) def full_chain(): response = { 'chain': blockchain.chain, 'length': len(blockchain.chain), } return jsonify(response), 200 if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)
  • 37. Transactions endpoint • This is what a user will send to the server { "sender": "my address", "recipient": "someone else's address", "amount": 5 } • Update the new_transactions() method to implement the logic to add transactions to the block.
  • 38. import hashlib import json from textwrap import dedent from time import time from uuid import uuid4 from flask import Flask, jsonify, request ... @app.route('/transactions/new', methods = ['POST']) def new_transactions(): values = request.get_json() # Check that the required fields are in the POSTed data required = ['sender', 'recipient', 'amount'] if not all (k in values for k in required): return 'Missing values: {k}', 400 # Create a new transaction index = blockchain.new_transaction(values['sender'], values['recipient'], values['amount']) response = {'message': f'Transaction will be added to Block {index}'} return jsonify(response), 200
  • 39. Mining endpoint 1. Calculate Proof of work 2. Reward the miner by adding a transaction granting 1 coin 3. Forge the new block by adding to the chain. We will update the mine() method to add the logic.
  • 40. import hashlib import json from time import time from uuid import uuid4 from flask import Flask, jsonify, request ... @app.route('/mine', methods=['GET']) def mine(): # We will run the proof of work algorithm to get the next proof.. last_block = blockchain.last_block proof = blockchain.proof_of_work(last_block) # We must receive a reward for finding the proof. # The sender is "0" to signify that this node has mined a new coin. blockchain.new_transaction( sender = "0", recipient=node_identifier, amount=1, ) # Forge the new block by adding it to the chain. previous_hash = blockchain.hash(last_block) block = blockchain.new_block(proof, previous_hash) response = { 'message': 'New block forged', 'index': block['index'], 'transactions': block['transactions'], 'proof': block['proof'], 'previous_hash': block['previous_hash'], } return jsonify(response), 200
  • 41. Step 3: Interacting with our blockchain • Fire up the server python blockchain.py * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) • Use Postman to mine a block (GET) : http://localhost:5000/mine
  • 42.
  • 43. Create a new transaction • Postman create a POST request http://localhost:5000/transactions/new Headers: Body (JSON) RAW: { "sender": "3243c66fa5ce46658d57c3624f306e32", "recipient": "some-other-user", "amount": 8 }
  • 44.
  • 45. More mining • Mine multiple times by using (GET) http://localhost:5000/mine And inspect the chain by calling in a Browser or Postman http://localhost:5000/chain
  • 46. { "chain": [ { "index": 1, "previous_hash": 1, "proof": 100, "timestamp": 1506280650.770839, "transactions": [] }, { "index": 2, "previous_hash": "c099bc...bfb7", "proof": 35293, "timestamp": 1506280664.717925, "transactions": [ { "amount": 1, "recipient": "8bbcb347e0634905b0cac7955bae152b", "sender": "0" } ] }, { "index": 3, "previous_hash": "eff91a...10f2", "proof": 35089, "timestamp": 1506280666.1086972, "transactions": [ { "amount": 1, "recipient": "8bbcb347e0634905b0cac7955bae152b", "sender": "0" } ] } ], "length": 3 }
  • 47. Yay!! We got basic blockchain. More…
  • 48. Step 4: Consensus • Consensus allows the blockchains to be decentralized. • For consensus to work, all nodes should be aware of its’ neighbors by registering and resolving differences /nodes/register: to accept a new list of new nodes /nodes/resolve: implement consensus algorithm, resolves any conflicts • Add these methods to the blockchain code
  • 49. ... from urllib.parse import urlparse ... class Blockchain(object): def __init__(self): ... self.nodes = set() # Set of nodes - only unique values are stored - idempotent. ... def register_node(self, address): """ Add a new node to the list of nodes. :param address: <str> address of the node eg: http://192.168.0.10:5005 :return: None """ parsed_url = urlparse(address) if parsed_url.netloc: self.nodes.add(parsed_url.netloc) elif parsed_url.path: # Accepts an URL with path '192.168.0.0:5000'. self.nodes.add(parsed_url.path) else: raise ValueError('Invalid URL')
  • 50. Implement Consensus algorithm • A conflict is when one node has a different chain than another node • To resolve, our rule is the ‘longest valid chain is authoritative’ • Ie, the longest chain on the network is the de-facto one. • Let’s implement the valid_chain method and resolve_conflicts method on blockchain.py • valid_chain() – loops through blocks and verifies both hash and proof. • resolve_conflicts() – loops through neighbors, downloads their chains, and verifies. If a valid chain is found, whose length is greater than ours, we replace ours.
  • 51. ... import requests class Blockchain(object) ... def valid_chain(self, chain): """ Determine if a given blockchain is valid. Consensus - longest valid chain is authoritative. :param chain: <list> a blockchain :return: <bool> True if valid, False if not. """ last_block = chain[0] current_index = 1 while current_index < len(chain): block = chain[current_index] print(f'lastBlock: {last_block}') print(f'currentBlock: {block}') print("nn--------------------n") # Check that the hash of the block is correct last_block_hash = self.hash(last_block) if block['previous_hash'] != last_block_hash: return False # Check if the proof of work is valid. if not self.valid_proof(last_block['proof'], block['proof'], last_block_hash): return False last_block = block current_index += 1 return True
  • 52. def resolve_conflicts(self): """ This is our consensus algorithm, it resolves conflicts by replacing our chain with the longest chain in the network :return: <bool> True if the chain is replaced, False if not. """ neighbors = self.nodes new_chain = None # We're looking for chains longer than ours max_length = len(self.chain) # Grab and verify the chains from all nodes on the network for node in neighbors: print(f'requesting for node: {node}') response = requests.get(f'http://{node}/chain') print(f'node {node} - chain: {response}') if response.status_code == 200: length = response.json()['length'] chain = response.json()['chain'] # Check if the length is longer and is valid. if length > max_length and self.valid_chain(chain): max_length = length new_chain = chain # Replace our chain if the length is longer and valid. if new_chain: self.chain = new_chain return True return False
  • 53. Add endpoints for consensus • Add 2 more methods to register and resolve /nodes/register (POST) /nodes/resolve (GET)
  • 54. @app.route('/nodes/register', methods=['POST']) def register_nodes(): values = request.get_json() nodes = values.get('nodes') if nodes is None: return "Error, please supply a valid list of nodes", 400 for node in nodes: blockchain.register_node(node) response = { 'message': 'New nodes have been added', 'total_nodes': list(blockchain.nodes), } return jsonify(response), 201 @app.route('/nodes/resolve', methods = ['GET']) def consensus(): replaced = blockchain.resolve_conflicts() if replaced: response = { 'message': 'Our chain got replaced', 'new_chain': blockchain.chain } else: response = { 'message': 'Our chain is authoritative', 'chain': blockchain.chain } return jsonify(response), 200
  • 55. Step 5: Test the chain • Fire up multiple nodes on different ports (use flaskrun to configure multiple ports) python blockchain.py –p 5000 python blockchain.py –p 5001 … • Use Postman to register nodes http://localhost:5000/register Body: { “nodes”:[“http://127.0.0.1:5001”] }
  • 56.
  • 57. More testing.. • Mine more blocks on nodes – Mine 3 blocks on node 2 to ensure chain was longer • And resolve to see consensus in work on node 1. See how chain gets replaced with longest chain – GET /nodes/resolve Note: You can do a GET /chain on nodes to see the chain before calling resolve
  • 58.
  • 59. Yay!! We are done (for now)… Time for Q&A
  • 60. Contact: Pad Kankipati pkankipati@gmail.com Meetup – Blockchain Hands-on Coding