-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented the code entry(main.py) point from which other functional…
…ity would follow.
- Loading branch information
Showing
1,423 changed files
with
257,222 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Guidelines and Restrictions | ||
|
||
## Guidelines | ||
|
||
1. Implement the transaction validation logic manually without using Bitcoin-specific libraries or frameworks. | ||
2. Implement the mining algorithm yourself, including creating the block header, calculating the hash, and finding a valid nonce that meets the difficulty target. | ||
3. Use standard cryptographic libraries (e.g., secp256k1 for elliptic curve cryptography, SHA-256 for hashing) as needed. | ||
4. Document your work in the `SOLUTION.md` file, explaining your design approach, implementation details, results, and conclusions. | ||
5. Publish your solution in the designated private repository, and do not share it publicly or with peers. | ||
|
||
## Restrictions | ||
|
||
1. Do not use any Bitcoin-specific libraries or frameworks that automate transaction validation processes. | ||
2. Do not use existing implementations of the mining algorithm or block construction logic. | ||
3. Do not plagiarize or copy solutions from others (solutions will be checked for plagiarism). | ||
4. While you can use AI tools like ChatGPT to gather information and explore alternative approaches, do not rely solely on AI for complete solutions. Verify and validate any insights obtained, and maintain a balance between AI assistance and independent problem-solving. | ||
|
||
The main objective of these guidelines and restrictions is to deepen your understanding of Bitcoin fundamentals by implementing the transaction validation and block mining processes from scratch, without relying on existing libraries or implementations. This exercise is designed to test your problem-solving skills and your ability to implement complex algorithms independently. | ||
|
||
|
||
># Asynchronous Transaction Validation and Mining | ||
In this assignment, you should treat the transaction validation and mining process as an asynchronous operation. Here's how you can approach it: | ||
## 1. Fetch transactions from mempool asynchronously | ||
|
||
- Create a function or method that reads all the JSON files in the `mempool` directory asynchronously. | ||
- Use Python's built-in `asyncio` module or a third-party library like `aiofiles` to read the files concurrently. | ||
- Deserialize each JSON transaction into your `Transaction` data structure. | ||
|
||
## 2. Validate transactions asynchronously | ||
|
||
- Create a function or method that takes a list of `Transaction` objects and validates them asynchronously. | ||
- Use Python's `asyncio` module and its `gather` function to run the validation tasks concurrently. | ||
- For each transaction, call your transaction validation functions or methods (implemented in `validation.py`) to check if it's valid or not. | ||
|
||
## 3. Store valid transactions in a dictionary | ||
|
||
- Create an empty dictionary (or any suitable data structure) to store the valid transactions. | ||
- After the asynchronous validation is complete, iterate through the list of validated transactions and add the valid ones to the dictionary. | ||
- Use the transaction ID (`txid`) as the key and the `Transaction` object as the value in the dictionary. | ||
|
||
## 4. Mine the block | ||
|
||
- In your mining logic (implemented in `mining.py`), iterate through the dictionary of valid transactions and construct the block accordingly. | ||
- Create the coinbase transaction and include it in the block. | ||
- Serialize the block header and coinbase transaction, and write them to the `output.txt` file as per the specified format. | ||
- Write the transaction IDs (`txid`) of the valid transactions to the `output.txt` file, following the coinbase transaction. | ||
|
||
## 5. Implement concurrency control | ||
|
||
- Since multiple tasks are running concurrently, you may need to implement proper synchronization mechanisms to avoid race conditions or data corruption. | ||
- Use Python's `asyncio` locks, semaphores, or other synchronization primitives to control access to shared resources, such as the dictionary of valid transactions. | ||
|
||
By following this asynchronous approach, you can take advantage of concurrent execution, potentially improving the overall performance of your solution. However, keep in mind that asynchronous programming can introduce additional complexity, and you'll need to handle potential exceptions, errors, and edge cases appropriately. | ||
|
||
Remember to document your approach, implementation details, and any challenges you faced in the `SOLUTION.md` file, as per the assignment requirements. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import os | ||
import asyncio | ||
from src.transaction import Transaction | ||
from src.schema import TransactionSchema | ||
from src.validation import validate_transactions | ||
from src.mine import Mine | ||
|
||
async def main() -> None: | ||
""" | ||
This will be responsible for kick-starting the mining process | ||
:return: None | ||
""" | ||
# Read and deserialize transactions from mempool directory | ||
transactions = [] | ||
for filename in os.listdir("mempool"): | ||
filepath = os.path.join("mempool", filename) | ||
with open(filepath, "r") as file: | ||
json_data = file.read() | ||
transaction_schema = TransactionSchema() | ||
try: | ||
transaction = transaction_schema.load(json_data) | ||
transactions.append(transaction) | ||
except Exception as e: | ||
print(f"Error deserializing transaction {filename}: {e}") | ||
|
||
# Validate transactions asynchronously | ||
valid_transactions = await validate_transactions(transactions) | ||
|
||
# Mine the block | ||
block_data = mine_block(valid_transactions) | ||
|
||
with open("output.txt", "w") as output_file: | ||
output_file.write(block_data) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,13 @@ | ||
# Update this file to run your own code | ||
#!/bin/bash | ||
|
||
# Create a virtual environment | ||
python3 -m venv venv | ||
|
||
# Activate the virtual environment | ||
source venv/bin/activate | ||
|
||
# Install project dependencies | ||
pip install -r requirements.txt | ||
|
||
# Run the mine_block.py script | ||
python3 src/mine_block.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
# for creating and managing blocks | ||
|
||
|
||
class Block: | ||
""" | ||
This class is responsible for creating blocks | ||
""" | ||
pass | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
|
||
class Mine: | ||
""" | ||
This class holds method for mining a block | ||
""" | ||
def mine_block(valid_transactions) -> str: | ||
""" | ||
Mine all blocks from all the valid transactions | ||
:param valid_transactions: all the validated transactions | ||
:return: str | ||
""" | ||
block_data = "Block header\nCoinbase transaction\n" | ||
for transaction in valid_transactions: | ||
block_data += f"{transaction.txid}\n" | ||
return block_data |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from marshmallow import Schema, fields, post_load | ||
|
||
class TransactionSchema(Schema): | ||
version = fields.Int(required=True) | ||
locktime = fields.Int(required=True) | ||
vin = fields.List(fields.Dict(), required=True) | ||
vout = fields.List(fields.Dict(), required=True) | ||
|
||
@post_load | ||
def make_transaction(self, data, **kwargs): | ||
return Transaction(**data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
# for managing transactions | ||
|
||
|
||
class Transaction: | ||
""" | ||
This class holds method for managing transactions | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
# for utilities | ||
|
||
|
||
class Utils: | ||
""" | ||
This would be a utility class | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,31 @@ | ||
# for handling validations | ||
import asyncio | ||
|
||
async def validate_transaction(transaction) -> bool: | ||
""" | ||
This function is responsible for validating a | ||
transaction | ||
:param transaction: a transaction to be validated | ||
:return: bool | ||
""" | ||
pass | ||
|
||
async def validate_transactions(transactions) -> list: | ||
""" | ||
gather and return all valid transactions | ||
:param transactions: all transactions, both valid and invalid | ||
:return: list | ||
""" | ||
async_tasks = [] | ||
for tx in transactions: | ||
async_task = validate_transaction(tx) | ||
async_tasks.append(async_task) | ||
|
||
validation_results = await asyncio.gather(*async_tasks) | ||
|
||
valid_transactions = [] | ||
# Check and store valid transactions | ||
for tx, is_valid in zip(transactions, validation_results): | ||
if is_valid: | ||
valid_transactions.append(tx) | ||
|
||
return valid_transactions |
Oops, something went wrong.