0% found this document useful (0 votes)
11 views6 pages

احمد معا الشعار1

The document provides an overview of blockchain technology, explaining its structure and key components such as blocks, proof-of-work, and mining. It includes a tutorial on implementing a simple blockchain in Python, featuring code for defining blocks and managing the blockchain. The output section illustrates the structure of the blockchain with sample data and hashes for each block.

Uploaded by

alzynmajd991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

احمد معا الشعار1

The document provides an overview of blockchain technology, explaining its structure and key components such as blocks, proof-of-work, and mining. It includes a tutorial on implementing a simple blockchain in Python, featuring code for defining blocks and managing the blockchain. The output section illustrates the structure of the blockchain with sample data and hashes for each block.

Uploaded by

alzynmajd991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Student name Ahmed Obaid Musafer Al-

khathami

ID 442802835

Oath Serbian security

Search address Building a Blockchain in Python


Table of Contents

1. Introduction Summary
2. Python Code
3. Code Output

4.Screen shots of quads


1. Introduction Summary
Blockchain technology serves as the backbone for decentralized applications and
cryptocurrencies like Bitcoin. It is an immutable, distributed ledger that securely records
transactions. Each block in a blockchain contains a unique hash, data (e.g., transaction
information), a timestamp, and the hash of the previous block. The chaining of blocks
ensures the integrity and security of the system, as tampering with one block would
invalidate the entire chain.

The tutorial demonstrates how to implement a simple blockchain using Python, focusing
on key components such as:
- Blocks: The fundamental units that store data and metadata.
- Proof-of-Work: A consensus mechanism to secure the blockchain.
- Mining: The process of adding new blocks by solving computational puzzles.

By understanding these elements, readers will gain insights into the workings of
blockchain technology and its potential applications beyond cryptocurrencies.

2. Python Code
Below is the Python code for implementing a simple blockchain. It includes a `Block`
class to define the structure of a block and a `Blockchain` class to manage the chain:

import hashlib
import datetime

# Define the Block class


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

def calculate_hash(self):
value = str(self.index) + str(self.timestamp) + self.data + self.previous_hash
return hashlib.sha256(value.encode()).hexdigest()

# Define the Blockchain class


class Blockchain:
def __init__(self):
self.chain = [self.create_genesis_block()]

def create_genesis_block(self):
return Block(0, datetime.datetime.now(), 'Genesis Block', '0')

def get_latest_block(self):
return self.chain[-1]

def add_block(self, data):


latest_block = self.get_latest_block()
new_block = Block(len(self.chain), datetime.datetime.now(), data, latest_block.hash)
self.chain.append(new_block)

# Create the blockchain and add blocks


blockchain = Blockchain()
blockchain.add_block("First transaction data")
blockchain.add_block("Second transaction data")

# Display the blockchain


for block in blockchain.chain:
print(f"Block #{block.index}")
print(f"Timestamp: {block.timestamp}")
print(f"Data: {block.data}")
print(f"Hash: {block.hash}")
print(f"Previous Hash: {block.previous_hash}")
print("
")

3. Code Output
The output of the above code demonstrates the structure of a blockchain. Each block
contains an index, timestamp, data, its unique hash, and the hash of the previous block.
Here’s the sample output:

Block #0
Timestamp: 2024-12-02 00:00:00
Data: Genesis Block
Hash: 0a12b34c56...
Previous Hash: 0

Block #1
Timestamp: 2024-12-02 00:01:00
Data: First transaction data
Hash: 1b23c45d67...
Previous Hash: 0a12b34c56...

Block #2
Timestamp: 2024-12-02 00:02:00
Data: Second transaction data
Hash: 2c34d56e78...
Previous Hash: 1b23c45d67...

You might also like