This tutorial aims to introduce you to the foundational concepts of blockchain technology, its operations, and its role in the Web3 ecosystem.
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.
A basic understanding of computer programming and data structures would be beneficial, but it's not strictly necessary.
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.
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.
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:
Block
class that has properties like index
, previous_hash
, timestamp
, data
, and hash
.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.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.
create_new_block
function to include a proof of work.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:
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).