From a76f89d4e8188d5146a7eb714ee6da69ce2aa714 Mon Sep 17 00:00:00 2001 From: Adarshsidnal <01fe21bcs288@kletech.ac.in> Date: Mon, 2 Oct 2023 18:53:37 +0530 Subject: [PATCH 1/4] Added blockchain basics in the blockchain folder --- .../1.Representing-a-transaction/README.md | 43 +++++++++++ .../transaction.py | 34 +++++++++ .../2.Creating-Block/README.md | 67 +++++++++++++++++ .../2.Creating-Block/block.py | 26 +++++++ .../Proof-of-work/block.py | 40 ++++++++++ .../Proof-of-work/proofOfWork.py | 54 +++++++++++++ .../Consensus-algorithms/README.md | 60 +++++++++++++++ blockchain/Blockchain-basics/README.md | 75 +++++++++++++++++++ 8 files changed, 399 insertions(+) create mode 100644 blockchain/Blockchain-basics/1.Representing-a-transaction/README.md create mode 100644 blockchain/Blockchain-basics/1.Representing-a-transaction/transaction.py create mode 100644 blockchain/Blockchain-basics/2.Creating-Block/README.md create mode 100644 blockchain/Blockchain-basics/2.Creating-Block/block.py create mode 100644 blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/block.py create mode 100644 blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/proofOfWork.py create mode 100644 blockchain/Blockchain-basics/Consensus-algorithms/README.md create mode 100644 blockchain/Blockchain-basics/README.md diff --git a/blockchain/Blockchain-basics/1.Representing-a-transaction/README.md b/blockchain/Blockchain-basics/1.Representing-a-transaction/README.md new file mode 100644 index 000000000000..9b67974de52c --- /dev/null +++ b/blockchain/Blockchain-basics/1.Representing-a-transaction/README.md @@ -0,0 +1,43 @@ +# Building a Transaction Representation + +Welcome to this hands-on lesson where you'll embark on the journey of constructing your own blockchain in Python! This tutorial assumes a prior understanding of Python syntax, functions, loops, library imports, and class construction. However, we've included some hints along the way to assist you. If you're new to Python, you may find it beneficial to [acquire some foundational Python knowledge through geeksforgeeks](https://www.codecademy.com/learn/learn-python). + +## The Role of Transactions + +Blockchain technology introduces a novel approach to securely store and transmit data. Most notably, it excels at managing transactions, which involve exchanges of information between two parties. Before we dive into the creation of our blockchain, let's consider an effective way to represent a transaction like the one illustrated below: + +**Transaction Representation** + +Consider the following transaction details: + +- **Amount:** 20 +- **Sender:** Add +- **Receiver:** White + +In this scenario, Add aims to transfer 20 units of a particular currency to White. To represent this transaction effectively in Python, we opt for a data type that suits its structure. + +The ideal choice for representing such transactions is by utilizing a Python dictionary. This dictionary can hold keys corresponding to the essential fields and values that contain transaction-specific information. + +These transactions are commonly stored within a component known as the "mempool." The mempool serves as a reservoir of pending transactions, which miners consult when determining the set of transactions they intend to validate. + + +### Instructions + +#### Step 1: Creating a Transaction + +To begin, let's craft a transaction and incorporate it into the `mempool`. We'll name this new transaction `my_transaction` and assign key-value pairs for `amount`, `sender`, and `receiver`. + +**Hint:** In Python, you can create a dictionary using the following syntax: `dictionary = {}` + +#### Step 2: Joining the Mempool + +Next, let's include `my_transaction` within the `mempool` list. + +**Hint:** You can add an item to a Python list using the following syntax: `list.append(element)` + +#### Step 3: Compiling Block Transactions + +Now, create a fresh list named `block_transactions` and populate it with three transactions sourced from the `mempool`. This step will prepare the transactions for future integration into our evolving Block structure. + +By following these steps, you'll gain hands-on experience in managing transactions within the blockchain and laying the groundwork for their incorporation into Blocks. + diff --git a/blockchain/Blockchain-basics/1.Representing-a-transaction/transaction.py b/blockchain/Blockchain-basics/1.Representing-a-transaction/transaction.py new file mode 100644 index 000000000000..0529e755eb8c --- /dev/null +++ b/blockchain/Blockchain-basics/1.Representing-a-transaction/transaction.py @@ -0,0 +1,34 @@ +transaction1 = { + 'amount': '20', + 'sender': 'Add', + 'receiver': 'White'} +transaction2 = { + 'amount': '200', + 'sender': 'White', + 'receiver': 'Add'} +transaction3 = { + 'amount': '300', + 'sender': 'Alice', + 'receiver': 'Timothy' } +transaction4 = { + 'amount': '300', + 'sender': 'Rodrigo', + 'receiver': 'Thomas' } +transaction5 = { + 'amount': '200', + 'sender': 'Timothy', + 'receiver': 'Thomas' } + + +mempool = [transaction1, transaction2, transaction3, transaction4, transaction5] + +# add your code below +my_transaction = { + 'amount': '500', + 'sender': 'name_1', + 'receiver': 'name_2' +} + +mempool.append(my_transaction) + +block_transactions = [transaction1, transaction3, my_transaction] \ No newline at end of file diff --git a/blockchain/Blockchain-basics/2.Creating-Block/README.md b/blockchain/Blockchain-basics/2.Creating-Block/README.md new file mode 100644 index 000000000000..ad1905b35b1b --- /dev/null +++ b/blockchain/Blockchain-basics/2.Creating-Block/README.md @@ -0,0 +1,67 @@ +# Building Blocks + +Now, let's consider an effective approach to represent a block in Python. Instead of using a large dictionary to store our data, we can leverage object-oriented programming principles and create a Block Class. This Block Class will facilitate the straightforward creation of new blocks. + +Remember that a Block encompasses the following fundamental properties: + +Timestamp +Transaction +Hash +Previous Hash +Nonce +In this exercise, we'll be crafting the default constructor for the Block class within our Mini-Blockchain. + +# Building Blocks and Managing Timestamps + +In this guide, you will learn how to incorporate timestamps and initialize a Block class for your Mini-Blockchain project. + +## Introduction + +A blockchain relies on the structured arrangement of blocks, each containing vital information. We'll work on integrating timestamps into your blockchain and create a Block class to store essential blockchain data. + +### Step 1: Importing the `datetime` Module + +Every `Block` in the blockchain is associated with a timestamp, representing its creation time. To generate this timestamp dynamically, we need to import the `datetime` module from the `datetime` library. + +**Hint:** To make a module accessible in your code, you must import it from the respective library. Use the following format to import a specific module: + +```python +from datetime import datetime + +#### Step: 2 +Inside the [datetime module](https://docs.python.org/2/library/datetime.html) there is a `.now()` method that returns the current date and time. + +Call the `datetime` module’s `.now()` method to print out the current date and time. + +###### Hint: +The appropriate way to call this method is `datetime.now()` enclosed in a `print()` statement. + +#### Step: 3 +Now let’s work on creating our `Block`. We will be passing `transactions` and `previous_hash` to the default constructor each time a `Block` is created. + +Complete the `__init__()` method inside the Block class by initializing the following instance variables: + +- `transactions` +- `previous_hash` +- `nonce` (with a default value of `0`). + +###### Hint: +The header for the function should look as follows: + +`def __init__(self, transactions, previous_hash, nonce = 0):` + +Be sure to initialize all the variables as well: + +```def __init__(self, value): + # Initialization: + self.value = value``` + +#### Step: 4 +Inside the `__init__()` method, create a `timestamp` instance variable that stores the current date and time. + +###### Hint: +Call the `.now()` method from the `datetime` module and store the result in `timestamp`. + + + + diff --git a/blockchain/Blockchain-basics/2.Creating-Block/block.py b/blockchain/Blockchain-basics/2.Creating-Block/block.py new file mode 100644 index 000000000000..216d2e270fd7 --- /dev/null +++ b/blockchain/Blockchain-basics/2.Creating-Block/block.py @@ -0,0 +1,26 @@ +# Import the datetime module from the datetime library +from datetime import datetime + +# Print the current date and time +print(datetime.now()) + +# Create a Block class +class Block: + # Initialize a timestamp variable with a default value of 0 + timestamp = 0 + + # Default constructor for the Block class + def __init__(self, transactions, previous_hash, nonce=0): + # Initialize instance variables + self.transactions = transactions # Store transaction data + self.previous_hash = previous_hash # Store the hash of the previous block + self.nonce = nonce # Initialize the nonce with a default value of 0 + + # Set the timestamp to the current date and time + self.timestamp = datetime.now() + +# This is the end of the Block class definition + +# There's an indentation error on the "add comments explaining everystep" line, so I've removed it for clarity. + +# The code defines a Block class with a constructor that initializes instance variables such as transactions, previous_hash, nonce, and timestamp. The timestamp is set to the current date and time using the datetime module. diff --git a/blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/block.py b/blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/block.py new file mode 100644 index 000000000000..ce09d32d1580 --- /dev/null +++ b/blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/block.py @@ -0,0 +1,40 @@ +# Import the datetime module for timestamp and hashlib module for SHA-256 hashing +import datetime +from hashlib import sha256 + +# Create a Block class +class Block: + # Initialize the Block object with transactions and the previous block's hash + def __init__(self, transactions, previous_hash): + # Set the timestamp to the current date and time + self.time_stamp = datetime.datetime.now() + self.transactions = transactions # Store transaction data + self.previous_hash = previous_hash # Store the hash of the previous block + self.nonce = 0 # Initialize the nonce (number used once) to 0 + self.hash = self.generate_hash() # Compute and store the block's hash + + # Generate a hash for the block + def generate_hash(self): + # Create a block header by combining timestamp, transactions, previous hash, and nonce + block_header = str(self.time_stamp) + str(self.transactions) + str(self.previous_hash) + str(self.nonce) + # Calculate the SHA-256 hash of the block header + block_hash = sha256(block_header.encode()) + return block_hash.hexdigest() # Return the hexadecimal representation of the hash + + # Print the contents of the block + def print_contents(self): + print("timestamp:", self.time_stamp) + print("transactions:", self.transactions) + print("current hash:", self.generate_hash()) + print("previous hash:", self.previous_hash) + +# This is the end of the Block class definition + +# Example usage: +# Create a new block with some sample transactions and a previous block's hash +transactions_data = "Sample transactions go here" +previous_block_hash = "Previous block's hash goes here" +block = Block(transactions_data, previous_block_hash) + +# Print the contents of the block +block.print_contents() diff --git a/blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/proofOfWork.py b/blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/proofOfWork.py new file mode 100644 index 000000000000..c5a7d6c5ac5e --- /dev/null +++ b/blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/proofOfWork.py @@ -0,0 +1,54 @@ +# Import the Block class from block.py +from block import Block + +# Create a Blockchain class +class Blockchain: + # Constructor to initialize the blockchain + def __init__(self): + self.chain = [] # List to hold blocks in the blockchain + self.all_transactions = [] # List to store all transactions across blocks + self.genesis_block() # Create the genesis block to start the blockchain + + # Create the genesis block (the first block in the blockchain) + def genesis_block(self): + transactions = {} # Transactions are empty for the genesis block + genesis_block = Block(transactions, "0") # Create a new Block object for the genesis block + self.chain.append(genesis_block) # Add the genesis block to the blockchain + return self.chain + + # Function to print the contents of all blocks in the blockchain + def print_blocks(self): + for i in range(len(self.chain)): + current_block = self.chain[i] + print("Block {} {}".format(i, current_block)) + current_block.print_contents() # Call the print_contents() method of the Block class to print block details + + # Function to add a new block to the blockchain + def add_block(self, transactions): + previous_block_hash = self.chain[len(self.chain) - 1].hash # Get the hash of the previous block + new_block = Block(transactions, previous_block_hash) # Create a new Block with the provided transactions + self.chain.append(new_block) # Add the new block to the blockchain + + # Function to validate the integrity of the blockchain + def validate_chain(self): + for i in range(1, len(self.chain)): + current = self.chain[i] + previous = self.chain[i - 1] + if (current.hash != current.generate_hash()): + print("The current hash of the block does not equal the generated hash of the block.") + return False + if (current.previous_hash != previous.generate_hash()): + print("The previous block's hash does not equal the previous hash value stored in the current block.") + return False + return True + + # Function to perform proof of work (mining) + def proof_of_work(self, block, difficulty=2): + self.proof = block.generate_hash() # Calculate the hash of the block + while self.proof[:difficulty] != '0' * difficulty: + block.nonce += 1 # Increment the nonce value + self.proof = block.generate_hash() # Recalculate the hash + block.nonce = 0 # Reset the nonce value + return self.proof + +# This is the end of the Blockchain class definition diff --git a/blockchain/Blockchain-basics/Consensus-algorithms/README.md b/blockchain/Blockchain-basics/Consensus-algorithms/README.md new file mode 100644 index 000000000000..5df71f5d4253 --- /dev/null +++ b/blockchain/Blockchain-basics/Consensus-algorithms/README.md @@ -0,0 +1,60 @@ +# Understanding Consensus Algorithms + +Consensus algorithms are at the heart of blockchain technology. They are the mechanisms that enable multiple nodes (computers) in a decentralized network to agree on the state of a shared ledger, ensuring data integrity and security. In this README, we will explore the concept of consensus algorithms and their importance in blockchain networks. + +## Table of Contents + +1. [What Is Consensus?](#what-is-consensus) +2. [Why Do We Need Consensus Algorithms?](#why-do-we-need-consensus-algorithms) +3. [Popular Consensus Algorithms](#popular-consensus-algorithms) + - [Proof of Work (PoW)](#proof-of-work-pow) + - [Proof of Stake (PoS)](#proof-of-stake-pos) +4. [How Consensus Algorithms Work](#how-consensus-algorithms-work) +5. [Implementing Consensus Algorithms](#implementing-consensus-algorithms) +6. [Conclusion](#conclusion) + +## What Is Consensus? + +Consensus, in the context of blockchain, refers to the agreement among participants (nodes) in a network about the validity of transactions and the order in which they are added to the blockchain. Achieving consensus ensures that all nodes have a consistent view of the ledger, even in a decentralized and trustless environment. + +## Why Do We Need Consensus Algorithms? + +Consensus algorithms serve several critical purposes in blockchain networks: + +- **Security:** They prevent malicious actors from tampering with the blockchain, making it highly secure and resistant to attacks. +- **Data Integrity:** Consensus ensures that the data stored in the blockchain is accurate and consistent. +- **Decentralization:** They enable decentralized control and decision-making, eliminating the need for a central authority. + +## Popular Consensus Algorithms + +### Proof of Work (PoW) + +- PoW is used by Bitcoin and many other cryptocurrencies. +- Miners compete to solve complex mathematical puzzles to add new blocks to the blockchain. +- The first miner to solve the puzzle gets the right to add the block and is rewarded with cryptocurrency. +- Energy-intensive but highly secure. + +### Proof of Stake (PoS) + +- PoS is used in Ethereum 2.0 and other cryptocurrencies. +- Validators are chosen to create new blocks based on the amount of cryptocurrency they "stake" or hold. +- More energy-efficient compared to PoW. +- Promotes decentralization and economic incentives for validators. + +## How Consensus Algorithms Work + +Consensus algorithms work by defining a set of rules and protocols that all participants in the network must follow: + +1. **Transaction Propagation:** Nodes broadcast transactions to their peers on the network. +2. **Validation:** Transactions are validated by nodes to ensure they meet specific criteria. +3. **Block Creation:** Valid transactions are grouped into blocks. +4. **Consensus Process:** Nodes participate in the consensus process to agree on the next block. +5. **Block Addition:** Once consensus is reached, the new block is added to the blockchain. + +## Implementing Consensus Algorithms + +Implementing a consensus algorithm from scratch is a complex task. It involves designing protocols, handling network communication, and managing node behavior. Various blockchain development libraries and platforms, such as Ethereum and Hyperledger Fabric, provide tools to simplify the implementation of consensus algorithms. + +## Conclusion + +Consensus algorithms are the foundation of blockchain technology, ensuring that decentralized networks maintain trust and security. Understanding the nuances of different consensus mechanisms is essential for blockchain developers and enthusiasts alike, as it impacts the scalability, security, and energy efficiency of blockchain networks. diff --git a/blockchain/Blockchain-basics/README.md b/blockchain/Blockchain-basics/README.md new file mode 100644 index 000000000000..5402707000e2 --- /dev/null +++ b/blockchain/Blockchain-basics/README.md @@ -0,0 +1,75 @@ +# Blockchain Overview + +## Table of Contents + +- [Introduction](#introduction) +- [What is a Blockchain?](#what-is-a-blockchain) +- [How Does a Blockchain Work?](#how-does-a-blockchain-work) +- [Key Concepts](#key-concepts) +- [Applications of Blockchain](#applications-of-blockchain) +- [Getting Started](#getting-started) + +## Introduction + +Welcome to the Blockchain section of our repository! This README provides an overview of blockchain technology, its key concepts, and its potential applications. + +## What is a Blockchain? + +A blockchain is a distributed and immutable ledger technology that underlies cryptocurrencies like Bitcoin and Ethereum. It consists of a chain of blocks, where each block contains a list of transactions. Here are some important characteristics of blockchains: + +- **Decentralization:** Blockchains are typically maintained by a decentralized network of nodes (computers) rather than a central authority. + +- **Immutability:** Once data is recorded on a blockchain, it becomes extremely difficult to alter or delete. + +- **Security:** Transactions on a blockchain are secured using cryptographic techniques, making it highly resistant to fraud and tampering. + +- **Transparency:** Blockchain transactions are visible to all participants, ensuring transparency. + +## How Does a Blockchain Work? + +1. **Transactions:** Participants create transactions, which include sender, recipient, and amount. + +2. **Validation:** Transactions are verified by network nodes through a consensus mechanism (e.g., Proof of Work or Proof of Stake). + +3. **Block Formation:** Validated transactions are grouped into blocks, and each block contains a reference to the previous block, creating a chain. + +4. **Mining:** Miners compete to solve a complex mathematical puzzle to add a new block to the blockchain. + +5. **Consensus:** Once a block is added, all nodes agree on its validity, achieving consensus. + +6. **Immutability:** Once a block is added to the blockchain, it cannot be altered without consensus. + + +## Key Concepts + +- **Cryptographic Hashing:** Each block contains a cryptographic hash of the previous block, creating a secure link between blocks. + +- **Public and Private Keys:** Users have public and private keys for secure transactions and access to their digital assets. + +- **Smart Contracts:** Self-executing contracts with predefined rules that automatically execute when conditions are met. + +- **Nodes:** Participants in the network that validate transactions and maintain the blockchain. + +## Applications of Blockchain + +Blockchain technology extends beyond cryptocurrencies. It has applications in various industries, including: + +- **Finance:** For secure and transparent transactions, remittances, and asset management. + +- **Supply Chain:** To track the origin and movement of products. + +- **Healthcare:** For managing patient records and drug traceability. + +- **Voting Systems:** To ensure secure and transparent elections. + +## Getting Started + +If you want to explore blockchain development or interact with blockchain networks, you can follow these steps: + +1. Install the necessary dependencies, such as [Python](https://www.python.org/downloads/) and [Web3.py](https://web3py.readthedocs.io/en/stable/). + +2. Clone this repository and navigate to the Blockchain section. + +3. Explore the provided scripts and documentation to learn how to interact with blockchain networks. + +4. Experiment with blockchain development and explore the possibilities! From e41e32d355fcc7d2d6d235897e759e6c3809a99e Mon Sep 17 00:00:00 2001 From: Adarshsidnal <01fe21bcs288@kletech.ac.in> Date: Mon, 2 Oct 2023 19:04:15 +0530 Subject: [PATCH 2/4] Added blockchain basics in the blockchain folder --- .../Blockchain-basics/1.Representing-a-transaction/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockchain/Blockchain-basics/1.Representing-a-transaction/README.md b/blockchain/Blockchain-basics/1.Representing-a-transaction/README.md index 9b67974de52c..fd803e9b2446 100644 --- a/blockchain/Blockchain-basics/1.Representing-a-transaction/README.md +++ b/blockchain/Blockchain-basics/1.Representing-a-transaction/README.md @@ -1,6 +1,6 @@ # Building a Transaction Representation -Welcome to this hands-on lesson where you'll embark on the journey of constructing your own blockchain in Python! This tutorial assumes a prior understanding of Python syntax, functions, loops, library imports, and class construction. However, we've included some hints along the way to assist you. If you're new to Python, you may find it beneficial to [acquire some foundational Python knowledge through geeksforgeeks](https://www.codecademy.com/learn/learn-python). +Welcome to this hands-on lesson where you'll embark on the journey of constructing your own blockchain in Python! This tutorial assumes a prior understanding of Python syntax, functions, loops, library imports, and class construction. However, we've included some hints along the way to assist you. If you're new to Python, you may find it beneficial to [acquire some foundational Python knowledge through geeksforgeeks](https://www.geeksforgeeks.org/python-programming-language/) ## The Role of Transactions From b3c81a197d62cfa51891af4a57ab43388ab4d5e5 Mon Sep 17 00:00:00 2001 From: Adarshsidnal <01fe21bcs288@kletech.ac.in> Date: Mon, 2 Oct 2023 19:13:16 +0530 Subject: [PATCH 3/4] Added blockchain basics in the blockchain folder --- .../Proof-of-work/README.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/README.md diff --git a/blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/README.md b/blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/README.md new file mode 100644 index 000000000000..feeeca480c51 --- /dev/null +++ b/blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/README.md @@ -0,0 +1,25 @@ +# Blockchain Summary +Congratulations! You have completed all the steps required to build a basic blockchain! In this exercise, we will bring the key parts together to review what we have built so far. + +*Note: * The blockchain we have built only exists on a local machine. It is important to know that actual blockchain applications operate on multiple computers in a decentralized manner. + +### Instructions + + +#### Step: 1 +Create a Blockchain object named `local_blockchain`. Verify that this automatically creates a Genesis Block by printing out the contents of `local_blockchain`. + +###### Hint: +Use the `.print_blocks()` method to print out the contents of the blockchain. + +#### Step: 2 +Individually add `block_one_transactions`, `block_two_transactions`, and `block_three_transactions` respectively into `local_blockchain`. Print out the contents of `local_blockchain` to see what the block holds. + +###### Hint: +Use the method `.add_block(transactions)` to add blocks that contain the required transactions. + +#### Step: 3 +Modify the second block you added in `local_blockchain` by changing the block’s transactions to `fake_transactions`. Check to see if the blockchain is still valid using the correct method. + +###### Hint: +You can access the block by indexing into `local_blockchain.chain`. You can validate the block using the `.validate_chain()` method. \ No newline at end of file From 2fe480195638cc851820f79726b1e80a36d83baa Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 13:56:29 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../transaction.py | 33 ++++--------------- .../2.Creating-Block/block.py | 2 ++ .../Proof-of-work/block.py | 13 ++++++-- .../Proof-of-work/proofOfWork.py | 28 +++++++++++----- 4 files changed, 40 insertions(+), 36 deletions(-) diff --git a/blockchain/Blockchain-basics/1.Representing-a-transaction/transaction.py b/blockchain/Blockchain-basics/1.Representing-a-transaction/transaction.py index 0529e755eb8c..6abd7a0d334e 100644 --- a/blockchain/Blockchain-basics/1.Representing-a-transaction/transaction.py +++ b/blockchain/Blockchain-basics/1.Representing-a-transaction/transaction.py @@ -1,34 +1,15 @@ -transaction1 = { - 'amount': '20', - 'sender': 'Add', - 'receiver': 'White'} -transaction2 = { - 'amount': '200', - 'sender': 'White', - 'receiver': 'Add'} -transaction3 = { - 'amount': '300', - 'sender': 'Alice', - 'receiver': 'Timothy' } -transaction4 = { - 'amount': '300', - 'sender': 'Rodrigo', - 'receiver': 'Thomas' } -transaction5 = { - 'amount': '200', - 'sender': 'Timothy', - 'receiver': 'Thomas' } +transaction1 = {"amount": "20", "sender": "Add", "receiver": "White"} +transaction2 = {"amount": "200", "sender": "White", "receiver": "Add"} +transaction3 = {"amount": "300", "sender": "Alice", "receiver": "Timothy"} +transaction4 = {"amount": "300", "sender": "Rodrigo", "receiver": "Thomas"} +transaction5 = {"amount": "200", "sender": "Timothy", "receiver": "Thomas"} mempool = [transaction1, transaction2, transaction3, transaction4, transaction5] # add your code below -my_transaction = { - 'amount': '500', - 'sender': 'name_1', - 'receiver': 'name_2' -} +my_transaction = {"amount": "500", "sender": "name_1", "receiver": "name_2"} mempool.append(my_transaction) -block_transactions = [transaction1, transaction3, my_transaction] \ No newline at end of file +block_transactions = [transaction1, transaction3, my_transaction] diff --git a/blockchain/Blockchain-basics/2.Creating-Block/block.py b/blockchain/Blockchain-basics/2.Creating-Block/block.py index 216d2e270fd7..1090bc4c3fc6 100644 --- a/blockchain/Blockchain-basics/2.Creating-Block/block.py +++ b/blockchain/Blockchain-basics/2.Creating-Block/block.py @@ -4,6 +4,7 @@ # Print the current date and time print(datetime.now()) + # Create a Block class class Block: # Initialize a timestamp variable with a default value of 0 @@ -19,6 +20,7 @@ def __init__(self, transactions, previous_hash, nonce=0): # Set the timestamp to the current date and time self.timestamp = datetime.now() + # This is the end of the Block class definition # There's an indentation error on the "add comments explaining everystep" line, so I've removed it for clarity. diff --git a/blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/block.py b/blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/block.py index ce09d32d1580..fe9fea3dc599 100644 --- a/blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/block.py +++ b/blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/block.py @@ -2,6 +2,7 @@ import datetime from hashlib import sha256 + # Create a Block class class Block: # Initialize the Block object with transactions and the previous block's hash @@ -16,10 +17,17 @@ def __init__(self, transactions, previous_hash): # Generate a hash for the block def generate_hash(self): # Create a block header by combining timestamp, transactions, previous hash, and nonce - block_header = str(self.time_stamp) + str(self.transactions) + str(self.previous_hash) + str(self.nonce) + block_header = ( + str(self.time_stamp) + + str(self.transactions) + + str(self.previous_hash) + + str(self.nonce) + ) # Calculate the SHA-256 hash of the block header block_hash = sha256(block_header.encode()) - return block_hash.hexdigest() # Return the hexadecimal representation of the hash + return ( + block_hash.hexdigest() + ) # Return the hexadecimal representation of the hash # Print the contents of the block def print_contents(self): @@ -28,6 +36,7 @@ def print_contents(self): print("current hash:", self.generate_hash()) print("previous hash:", self.previous_hash) + # This is the end of the Block class definition # Example usage: diff --git a/blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/proofOfWork.py b/blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/proofOfWork.py index c5a7d6c5ac5e..134ab62a6b7a 100644 --- a/blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/proofOfWork.py +++ b/blockchain/Blockchain-basics/Consensus-algorithms/Proof-of-work/proofOfWork.py @@ -1,6 +1,7 @@ # Import the Block class from block.py from block import Block + # Create a Blockchain class class Blockchain: # Constructor to initialize the blockchain @@ -12,7 +13,9 @@ def __init__(self): # Create the genesis block (the first block in the blockchain) def genesis_block(self): transactions = {} # Transactions are empty for the genesis block - genesis_block = Block(transactions, "0") # Create a new Block object for the genesis block + genesis_block = Block( + transactions, "0" + ) # Create a new Block object for the genesis block self.chain.append(genesis_block) # Add the genesis block to the blockchain return self.chain @@ -25,8 +28,12 @@ def print_blocks(self): # Function to add a new block to the blockchain def add_block(self, transactions): - previous_block_hash = self.chain[len(self.chain) - 1].hash # Get the hash of the previous block - new_block = Block(transactions, previous_block_hash) # Create a new Block with the provided transactions + previous_block_hash = self.chain[ + len(self.chain) - 1 + ].hash # Get the hash of the previous block + new_block = Block( + transactions, previous_block_hash + ) # Create a new Block with the provided transactions self.chain.append(new_block) # Add the new block to the blockchain # Function to validate the integrity of the blockchain @@ -34,21 +41,26 @@ def validate_chain(self): for i in range(1, len(self.chain)): current = self.chain[i] previous = self.chain[i - 1] - if (current.hash != current.generate_hash()): - print("The current hash of the block does not equal the generated hash of the block.") + if current.hash != current.generate_hash(): + print( + "The current hash of the block does not equal the generated hash of the block." + ) return False - if (current.previous_hash != previous.generate_hash()): - print("The previous block's hash does not equal the previous hash value stored in the current block.") + if current.previous_hash != previous.generate_hash(): + print( + "The previous block's hash does not equal the previous hash value stored in the current block." + ) return False return True # Function to perform proof of work (mining) def proof_of_work(self, block, difficulty=2): self.proof = block.generate_hash() # Calculate the hash of the block - while self.proof[:difficulty] != '0' * difficulty: + while self.proof[:difficulty] != "0" * difficulty: block.nonce += 1 # Increment the nonce value self.proof = block.generate_hash() # Recalculate the hash block.nonce = 0 # Reset the nonce value return self.proof + # This is the end of the Blockchain class definition