-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
82 lines (69 loc) · 2.63 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import hashlib
import json
import os
import time
class Transaction:
def __init__(self, data):
self.data = data
def is_valid(self):
required_fields = ["version", "locktime", "vin", "vout"]
if not all(field in self.data for field in required_fields):
return False
# Check if each 'vin' entry has a 'txid' field
for vin in self.data.get("vin", []):
if "txid" not in vin:
return False
return True
class Block:
def __init__(self, transactions):
self.transactions = transactions
self.previous_hash = (
"0000000000000000000000000000000000000000000000000000000000000000"
)
self.nonce = 0
self.timestamp = int(time.time())
self.difficulty_target = (
"0000ffff0000000000000000000000000000000000000000000000000000000"
)
def calculate_hash(self):
header = f"{self.previous_hash}{self.timestamp}{self.nonce}"
assert len(header) <= 80, "Block header exceeds 80 bytes"
return hashlib.sha256(header.encode()).hexdigest()
def mine_block(self, difficulty):
target = "0" * difficulty
while self.calculate_hash()[:difficulty] != target:
self.nonce += 1
def main():
transactions = []
try:
for filename in os.listdir("mempool"):
with open(f"mempool/{filename}", "r") as file:
data = json.load(file)
tx = Transaction(data)
if tx.is_valid():
transactions.append(tx)
except FileNotFoundError:
print("Error: The 'mempool' directory does not exist.")
return
except json.JSONDecodeError:
print("Error: A file in the 'mempool' directory could not be decoded as JSON.")
return
block = Block(transactions)
try:
block.mine_block(4) # Assuming a difficulty of 4 for simplicity
except Exception as e:
print(f"Error: An error occurred while mining the block: {e}")
return
try:
with open("output.txt", "w") as file:
file.write(f"Block Header: {block.calculate_hash()}\n")
file.write("Coinbase Transaction: Simplified for demonstration\n")
for tx in block.transactions:
if tx.is_valid():
# Assuming the first 'vin' entry's 'txid' is the one we're interested in
txid = tx.data["vin"][0]["txid"] if tx.data["vin"] else "N/A"
file.write(f"Transaction ID: {txid}\n")
except IOError:
print("Error: Unable to write to 'output.txt'.")
if __name__ == "__main__":
main()