Skip to content

Commit

Permalink
added the documentation for the implementation so far
Browse files Browse the repository at this point in the history
  • Loading branch information
obamwonyi committed May 6, 2024
1 parent a8ad18c commit 1626dfc
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 15 deletions.
110 changes: 110 additions & 0 deletions SOLUTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
Sure, I can provide a documentation for the given code. Here's a breakdown of the different components:
> ## main.py
>
**Entry Point**
---
The `main.py` file serves as the entry point of the project. It imports necessary modules and defines the main function that orchestrates the entire workflow.

**Imports**
- `os`: This module provides a way to interact with the operating system, including file operations.
- `asyncio`: This module is used for writing concurrent code using the async/await syntax.
- `json`: This module is used for serializing and deserializing JSON data (note the TODO comment suggesting replacement with a different serializer).
- `Transaction` and `TransactionSchema`: These are imported from the `src.transaction` module and are used for handling transaction data and its schema.
- `ValidateTransaction`: This is imported from the `src.validation` module and is used for validating transactions.
- `ValidationError`: This is imported from the `marshmallow` library and is used for handling validation errors.
- `mine_block`: This is imported from the `src.mine` module and is used for mining a block.
- `defaultdict`: This is imported from the `collections` module and is used for creating a default dictionary to store transactions by their IDs.

**Global Variable**
- `TRANSACTION_BY_ID`: This is a `defaultdict` that stores transactions by their IDs.

**Main Function**
The `main` function is an asynchronous function (defined with `async def`) and is the main entry point of the program. Here's what it does:

1. **Read and Deserialize Transactions**: It reads transaction data from files in the `mempool` directory, deserializes them using the `TransactionSchema`, and stores them in the `transactions` list. It also populates the `TRANSACTION_BY_ID` dictionary with transactions indexed by their IDs.

2. **Validate Transactions Asynchronously**: It creates an instance of the `ValidateTransaction` class, passing the `TRANSACTION_BY_ID` dictionary, and calls the `validate_transactions` method asynchronously to validate the transactions. The valid transactions are stored in the `valid_transactions` list.

3. **Mine the Block**: It calls the `mine_block` function, passing the `valid_transactions` list, to mine a new block.

4. **Write Block Data**: It writes the mined block data to the `output.txt` file.

**Entry Point Check**
The `if __name__ == "__main__":` block ensures that the `main` function is executed only when the script is run directly (not imported as a module).

<br>
<br>

> ## transaction.py
>
**Recieving and Filtering Transactions**
---
**Imports**
- `marshmallow`: This is a library used for data serialization and deserialization. The following classes and functions are imported from it:
- `Schema`: This is the base class for creating data serialization/deserialization schemas.
- `fields`: This module contains various field types used to define the structure of the data.
- `post_load`: This is a decorator used to define a method that will be called after the data is deserialized.
- `ValidationError`: This exception is raised when data fails to validate against the defined schema.
- `typing`: This module provides support for type hints. In this case, it's used to specify the type of an optional variable.

**Schemas**
The code defines several schemas using the `marshmallow` library:

1. `PrevOutSchema`: This schema defines the structure for the `prevout` field in a transaction input (`vin`). It includes fields like `scriptpubkey`, `scriptpubkey_asm`, `scriptpubkey_type`, `scriptpubkey_address`, and `value`.

2. `VinSchema`: This schema defines the structure for a transaction input (`vin`). It includes fields like `txid`, `vout`, `prevout` (nested `PrevOutSchema`), `scriptsig`, `scriptsig_asm`, `witness`, `is_coinbase`, `sequence`, `inner_redeemscript_asm`, and `inner_witnessscript_asm`.

3. `VoutSchema`: This schema defines the structure for a transaction output (`vout`). It includes fields like `scriptpubkey`, `scriptpubkey_asm`, `scriptpubkey_type`, `scriptpubkey_address`, and `value`.

4. `TransactionSchema`: This schema defines the overall structure of a transaction. It includes fields like `version`, `locktime`, `vin` (a list of nested `VinSchema`), and `vout` (a list of nested `VoutSchema`).

**Transaction Class**
The `Transaction` class is defined to hold methods for managing transactions. It has the following components:

- `__init__` method: This is the constructor for the `Transaction` class. It takes four parameters: `version`, `locktime`, `vin`, and `vout`, and initializes the corresponding attributes of the class.

- `make_transaction` method: This method is a `post_load` hook for the `TransactionSchema`. It is called after the data is deserialized, and it returns an instance of the `Transaction` class with the deserialized data.

The `Transaction` class is designed to hold the structure of a transaction, which includes the version, locktime, input transactions (`vin`), and output transactions (`vout`). The input and output transactions are represented using nested schemas (`VinSchema` and `VoutSchema`, respectively) to define their structure.

Overall, this code defines the data structure and schema for representing transactions in a blockchain system, using the `marshmallow` library for serialization and deserialization.

<br>
<br>


> ## mine.py
**Block Mining Functions**
---


### `mine_block(valid_transactions) -> str`

This function is responsible for mining a block by including the valid transactions and finding a valid block hash.

#### Parameters:
- `valid_transactions`: List of valid transactions to include in the block.

#### Returns:
- A string representing the block data in the required format.

### `calculate_merkle_root(transactions) -> hashlib object`

This function calculates the Merkle root for the given transactions.

#### Parameters:
- `transactions`: List of transactions to include in the Merkle tree.

#### Returns:
- Hashlib object representing the Merkle root.

### `calculate_coinbase_transaction(transactions) -> bytes`

This function calculates the coinbase transaction for the given block.

#### Parameters:
- `transactions`: List of transactions to include in the block.

#### Returns:
- Bytes representing the coinbase transaction.
8 changes: 0 additions & 8 deletions src/block.py

This file was deleted.

7 changes: 0 additions & 7 deletions src/utils.py

This file was deleted.

0 comments on commit 1626dfc

Please sign in to comment.