Skip to content

Commit

Permalink
Small changes in Transaction and Block class
Browse files Browse the repository at this point in the history
  • Loading branch information
HXM4Tech committed Jul 8, 2022
1 parent 605682d commit f506218
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import json

import ecc
import logger

################################################################
# USEFUL FUNCTIONS
Expand All @@ -25,7 +24,7 @@ def address_generator(pubkey: ecc.S256Point) -> str:
return "0x" + (checksum + mainpart).hex()

def get_difficulty(height: int) -> int:
start_difficulty = 5
start_difficulty = 6

return start_difficulty + (height // 100000) # difficulty of mining increases every 100000 blocks

Expand Down Expand Up @@ -162,10 +161,11 @@ def parse(cls, serialization: str):
################################################################

class Transaction:
def __init__(self, sender: PublicKey, recipient: str, amount: int, signature: Signature = None):
def __init__(self, sender: PublicKey, recipient: str, amount: int, prev_hash: bytes, signature: Signature = None):
self.sender = sender
self.recipient = recipient
self.amount = amount # 1 means 1/10000000000 of FUZC
self.amount = amount # 1 means 1/000000000 of FUZC
self.prev_hash = prev_hash

if signature is not None:
if not self.verify():
Expand All @@ -184,7 +184,7 @@ def sign(self, privkey: PrivateKey):
m_recipient = self.recipient.encode("ascii") # 50 bytes
m_amount = self.amount.to_bytes((self.amount.bit_length() + 7) // 8, "big") # variable size

m = m_sender + m_recipient + m_amount
m = self.prev_hash + m_sender + m_recipient + m_amount

self.signature = privkey.sign(m)

Expand All @@ -193,7 +193,7 @@ def verify(self) -> bool:
m_recipient = self.recipient.encode("ascii") # 50 bytes
m_amount = self.amount.to_bytes((self.amount.bit_length() + 7) // 8, "big") # variable size

m = m_sender + m_recipient + m_amount
m = self.prev_hash + m_sender + m_recipient + m_amount

return self.sender.verify(m, self.signature)

Expand All @@ -202,16 +202,17 @@ def hash(self) -> bytes:
m_recipient = self.recipient.encode("ascii") # 50 bytes
m_amount = self.amount.to_bytes((self.amount.bit_length() + 7) // 8, "big") # variable size

m = m_sender + m_recipient + m_amount
m = self.prev_hash + m_sender + m_recipient + m_amount

return hash256(m)

def serialize(self) -> str:
return json.dumps({
"sender": self.sender.serialize(),
"recipient": self.recipient,
"amount": self.amount,
"amount": self.amount / 1000000000,
"signature": self.signature.serialize(),
"prev_hash": self.prev_hash.hex(),
"hash": self.hash().hex()
})

Expand All @@ -226,17 +227,22 @@ def parse(cls, serialization: str):
raise ValueError("Cannot parse JSON serialization")

try:
return cls(
out = cls(
sender = PublicKey.parse(data["sender"]),
recipient = data["recipient"],
amount = data["amount"],
amount = int(data["amount"] * 1000000000),
prev_hash = bytes.fromhex(data["prev_hash"]),
signature = Signature.parse(data["signature"])
)

if out.hash().hex() != data["hash"]:
raise ValueError("Invalid hash in JSON serialization")

except KeyError:
raise ValueError("This is not valid Transaction serialization")

class Block:
def __init__(self, height: int, transactions: List[Transaction], prev_hash: str = None, nonce: int = None):
def __init__(self, height: int, transactions: list[Transaction], prev_hash: str = None, nonce: int = None):
self.height = height
self.transactions = transactions
self.prev_hash = prev_hash
Expand Down Expand Up @@ -268,15 +274,13 @@ def serialize(self) -> str:
def mine(self):
difficulty = get_difficulty(self.height)

while self.hash()[0:difficulty] != bytes(difficulty):
while self.hash().hex()[0:difficulty] != "0" * difficulty:
self.nonce += 1

logger.Logger("MINER").info(f"Block mined:\n height: {self.height}\n hash: {self.hash().hex()}\n nonce: {self.nonce}")

def validate(self) -> bool:
difficulty = get_difficulty(self.height)

if self.hash()[0:difficulty] != bytes(difficulty):
if self.hash().hex()[0:difficulty] != "0" * difficulty:
return False

for tx in self.transactions:
Expand All @@ -286,7 +290,7 @@ def validate(self) -> bool:
return True

def __repr__(self) -> str:
return f"Block:\n height: {self.height}\n hash: {self.hash().hex()}\n nonce: {self.nonce}\n transactions: {len(self.transactions)}"
return f"block:\n height: {self.height}\n hash: {self.hash().hex()}\n nonce: {self.nonce}\n transactions: {len(self.transactions)}"

@classmethod
def parse(cls, serialization: str):
Expand Down

0 comments on commit f506218

Please sign in to comment.