CoolFace
Apppublic

Omnibus-archive/decentral-test-mod

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
block.py34 linesDownload Raw Back to node
1import json2 3from node.utils import calculate_hash, convert_transaction_data_to_bytes4 5 6class Block:7    def __init__(8            self,9            timestamp: float,10            transaction_data: dict,11            previous_block=None,12    ):13        self.transaction_data = transaction_data14        self.timestamp = timestamp15        self.previous_block = previous_block16 17    @property18    def previous_block_cryptographic_hash(self):19        previous_block_cryptographic_hash = ""20        if self.previous_block:21            previous_block_cryptographic_hash = self.previous_block.cryptographic_hash22        return previous_block_cryptographic_hash23 24    @property25    def cryptographic_hash(self) -> str:26        transaction_data_bytes = convert_transaction_data_to_bytes(self.transaction_data)27        block_content = {28            "transaction_data": str(transaction_data_bytes),29            "timestamp": self.timestamp,30            "previous_block_cryptographic_hash": self.previous_block_cryptographic_hash31        }32        block_content_bytes = json.dumps(block_content, indent=2).encode('utf-8')33        return calculate_hash(block_content_bytes)34