Introduction to Blockchain

Tutorial 4 of 5

Introduction to Blockchain

1. Introduction

Goals of this tutorial

This tutorial aims to introduce you to the foundational concepts of blockchain technology, its operations, and its role in the Web3 ecosystem.

Learning outcomes

By the end of this tutorial, you will have a basic understanding of what blockchain is, how it works, and how it's used in the context of Web3.

Prerequisites

A basic understanding of computer programming and data structures would be beneficial, but it's not strictly necessary.

2. Step-by-Step Guide

What is a Blockchain?

A blockchain is a type of distributed ledger that stores list of records, called blocks, which are linked using cryptography. Each block contains a cryptographic hash of the previous block, a timestamp, and transaction data.

How does it work?

The blockchain operates by validating and recording transactions into blocks. Once a block is filled with transactions, it's added to the blockchain in a linear, chronological order. This makes it extremely difficult to alter past transactions because changing any information would require altering all subsequent blocks.

3. Code Examples

Creating a basic block

import hashlib
import time

class Block:
    def __init__(self, index, previous_hash, timestamp, data, hash):
        self.index = index
        self.previous_hash = previous_hash
        self.timestamp = timestamp
        self.data = data
        self.hash = hash

def calculate_hash(index, previous_hash, timestamp, data):
    value = str(index) + str(previous_hash) + str(timestamp) + str(data)
    return hashlib.sha256(value.encode('utf-8')).hexdigest()

def create_genesis_block():
    return Block(0, "0", int(time.time()), "Genesis Block", calculate_hash(0, "0", int(time.time()), "Genesis Block"))

def create_new_block(previous_block, data):
    index = previous_block.index + 1
    timestamp = int(time.time())
    hash = calculate_hash(index, previous_block.hash, timestamp, data)
    return Block(index, previous_block.hash, timestamp, data, hash)

In the above code:

  • We first create a Block class that has properties like index, previous_hash, timestamp, data, and hash.
  • The calculate_hash function computes the hash of a block.
  • create_genesis_block creates the first block of the blockchain, often referred to as the Genesis Block.
  • create_new_block creates a new block using the previous block's hash and new data.

4. Summary

We've covered the basics of what a blockchain is and how it operates. We've also seen how to create a basic blockchain using Python.

5. Practice Exercises

  1. Exercise 1: Write a function to validate the blockchain. It should return true if the blockchain is valid, and false otherwise.
  2. Exercise 2: Modify the create_new_block function to include a proof of work.

Solutions

  1. Solution to Exercise 1:

    python def is_chain_valid(chain): for i in range(1, len(chain)): current_block = chain[i] previous_block = chain[i - 1] if current_block.hash != calculate_hash(current_block.index, current_block.previous_hash, current_block.timestamp, current_block.data): return False if current_block.previous_hash != previous_block.hash: return False return True

    Here, we're checking two conditions for all blocks in the chain:

    • The current block's hash must be valid.
    • The current block's previous hash must match the hash of the previous block.
  2. Solution to Exercise 2:

    ```python
    def proof_of_work(last_proof):
    proof = 0
    while not is_valid_proof(last_proof, proof):
    proof += 1
    return proof

    def is_valid_proof(last_proof, proof):
    guess = f'{last_proof}{proof}'.encode()
    guess_hash = hashlib.sha256(guess).hexdigest()
    return guess_hash[:4] == "0000"

    def create_new_block(previous_block, data):
    index = previous_block.index + 1
    timestamp = int(time.time())
    proof = proof_of_work(previous_block.hash)
    hash = calculate_hash(index, previous_block.hash, timestamp, data, proof)
    return Block(index, previous_block.hash, timestamp, data, hash, proof)
    ```

    In this solution, we've added a proof_of_work function that finds a number that when hashed with the previous proof, the hash starts with four zeroes. This is a simple Proof of Work (PoW) algorithm.

Keep practicing and exploring more features of blockchain technology, like smart contracts and decentralized applications (DApps).