This program simulates the core functionalities of a blockchain system. It allows you to create and manage transactions, mine blocks with a specified difficulty, and view the current state of the blockchain, transactions and wallets. The blockchain is represented using a Merkle tree to ensure data integrity and security.
- Transaction Management: Add transactions between different wallets.
- Block Mining: Mine new blocks with a customizable difficulty level.
- Blockchain Display: Print the current state of the blockchain, including details of each block.
- Wallet Management: View the balances and transaction history of each wallet.
- Transactions Managment: View transaction history in the Blockchain.
- Python 3.x
pandas
librarycolorama
library
You can install the required libraries using:
pip install pandas colorama
To run the program, follow these steps:
- Open the Colab Notebook: Click on the link to open the notebook in Google Colab.
- Execute Cells: Run each cell in the notebook sequentially. This will set up the blockchain environment and make the program ready for interaction.
- Interact with the Program: Once the setup is complete, you can start using the program by following the menu options provided.
After starting the program, you will be presented with a menu with the following options:
-
Add Transaction:
- Description: Add a new transaction to the blockchain.
- Inputs Required: Sender's name, receiver's name, and the amount to be transferred.
-
Mine Block:
- Description: Mine a new block with the pending transactions.
- Inputs Required: Mining difficulty, which determines the number of leading zeros required in the block hash.
-
Print Blockchain:
- Description: Display the details of each block in the blockchain.
- Details Shown: Nonce, root hash, previous hash, and block hash.
-
Print Wallets:
- Description: Show the current state of each wallet.
- Details Shown: Wallet name, balance, and last transaction ID.
-
Print Transactions:
- Description: Show transactions that have been added to the Blockchain, along with the corresponding block in which each transaction is stored.
- Details Shown: id, from, to, refundTo, amount, block ID.
-
Exit:
- Description: Exit the program.
- Hash Calculation:
get_hash(dato)
calculates the SHA-256 hash of the input data. - Merkle Tree Construction:
build_merkle_tree(info)
constructs a Merkle tree to summarize the transactions in a block. - Block Mining:
mintBlock(data, min)
mines a new block by finding a nonce that meets the difficulty requirement. - Transaction Checking:
checkTransaction(data)
verifies and processes a transaction, updating the relevant wallets. - Printing Information:
print_network()
,print_wallets()
andprint_transactions()
display the blockchain, wallet and transactions information respectively.
-
Adding a Transaction:
- Input: Sender: Juan, Receiver: Esteban, Amount: 10
- Output: Transaction completed successfully.
-
Mining a Block:
- Input: Difficulty: 2
- Output: Block successfully mined.
-
Printing transactions
- Output: Displays all transactions that have been added to the Blockchain so far.
-
Printing Blockchain:
- Output: Displays the details of each block.
-
Printing Wallets:
- Output: Displays the current state of all wallets.