MatverseHub's picture
Upload folder using huggingface_hub
a757600 verified
Raw
History Blame Contribute Delete
1.66 kB
import hashlib
import json
import time
from typing import List, Dict, Any
class Ledger:
"""Cadeia de registros imutável (Append-only Hash-chain)"""
def __init__(self):
self.chain: List[Dict[str, Any]] = []
self._initialize_genesis()
def _initialize_genesis(self):
genesis_entry = {
"index": 0,
"timestamp": time.time(),
"data": "Genesis Block - MatVerse Truth Engine",
"previous_hash": "0" * 64
}
genesis_entry["hash"] = self._calculate_hash(genesis_entry)
self.chain.append(genesis_entry)
def _calculate_hash(self, entry: Dict[str, Any]) -> str:
content = f"{entry['index']}{entry['timestamp']}{json.dumps(entry['data'])}{entry['previous_hash']}"
return hashlib.sha256(content.encode()).hexdigest()
def append(self, data: Any) -> str:
previous_entry = self.chain[-1]
new_entry = {
"index": len(self.chain),
"timestamp": time.time(),
"data": data,
"previous_hash": previous_entry["hash"]
}
new_entry["hash"] = self._calculate_hash(new_entry)
self.chain.append(new_entry)
return new_entry["hash"]
def verify(self) -> bool:
for i in range(1, len(self.chain)):
current = self.chain[i]
previous = self.chain[i-1]
if current["previous_hash"] != previous["hash"]:
return False
if current["hash"] != self._calculate_hash(current):
return False
return True
def get_last_hash(self) -> str:
return self.chain[-1]["hash"]