Skip to content

Commit

Permalink
Removed buggy codes and reimplemented the serialization of a transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
obamwonyi committed May 8, 2024
1 parent fe79eaa commit 2b48717
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
guidelines.md
3 changes: 0 additions & 3 deletions guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,3 @@ In this assignment, you should treat the transaction validation and mining proce
- 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.
13 changes: 6 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
async def main():
# Step 1: Read and deserialize transactions from the mempool directory
transactions = []
# errors = 0
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:
loaded_data = json.loads(json_data)
# transaction = transaction_schema.load(loaded_data)
for tx_input in loaded_data.get("vin", []):
txid = tx_input.get("txid")
transaction = transaction_schema.load(loaded_data)

for tx_input in transaction.vin:
txid = tx_input["txid"]
if txid:
TRANSACTION_BY_ID[txid] = loaded_data

Expand All @@ -39,12 +39,11 @@ async def main():
# print(f"Total failed transactions:{errors}")

# Step 2: Validate transactions asynchronously
validate_transaction = ValidateTransaction(TRANSACTION_BY_ID)
valid_transactions = await validate_transaction.validate_transactions(transactions)
# valid_transactions = await validate_transaction.validate_transactions(transactions)

# implement an initial transaction validation process.

print(valid_transactions)
# print(valid_transactions)

# print(valid_transactions)
# Step 3: Mine the block
Expand Down
Binary file modified src/__pycache__/validation.cpython-310.pyc
Binary file not shown.
36 changes: 1 addition & 35 deletions src/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,8 @@ async def validate_transaction(self, tx) -> (bool, str):
:param tx:
:return: validated transaction
"""
if not self.validate_transaction_version(tx):
return False, f"Transaction has invalid version \n"
return True

total_input_value = 0

for tx_input in tx.get("vin"):
prev_tx, prev_outputs = await self.get_prev_tx_output(tx_input["txid"], tx_input["vout"])
if not prev_outputs:
return False, "Previous output not found"

for prev_output in prev_outputs:
total_input_value += prev_output["value"]

vout = tx_input["vout"]

if not self.verify_input_script(prev_tx, vout, prev_output["scriptpubkey"]):
return False, "Failed to verify input script"
total_input_value += prev_output.get("value")

# Validate outputs
total_output_value = sum(output.get("value") for output in tx.get("vout"))
if total_output_value > total_input_value:
return False

for output in tx.get("vout"):
if not self.is_valid_output_script(output.get("scriptpubkey")):
return False

if not self.validate_locktime_and_sequence(tx):
return False

if self.is_double_spend(tx):
return False
print("Everything is valid")
return True
# print("Transaction has valid version") tested

async def validate_transactions(self, transactions):
"""
Expand Down

0 comments on commit 2b48717

Please sign in to comment.