File size: 5,087 Bytes
107a1d1 3d1ef0a 107a1d1 3d1ef0a |
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
import hashlib
import time
import json
class Block:
def __init__(self, previous_hash, trigger_set_huggingface_hash, trigger_set_client_hash, encrypted_watermarked_model_hash, counter,
timestamp=None):
self.timestamp = timestamp if timestamp else time.time()
self.previous_hash = previous_hash
self.counter = counter
self.trigger_set_huggingface = trigger_set_huggingface_hash
self.trigger_set_client = trigger_set_client_hash
self.encrypted_watermarked_model = encrypted_watermarked_model_hash
self.hash = self.calculate_hash()
def calculate_hash(self):
hash_string = (
f"{self.timestamp:.6f}" +
str(self.previous_hash) +
str(self.counter) +
str(self.trigger_set_huggingface) +
str(self.trigger_set_client) +
str(self.encrypted_watermarked_model)
)
return hashlib.sha256(hash_string.encode()).hexdigest()
@staticmethod
def hash_data(data):
return hashlib.sha256(str(data).encode()).hexdigest()
def to_dict(self):
return {
"timestamp": self.timestamp,
"previous_hash": self.previous_hash,
"counter": self.counter,
"trigger_set_huggingface": self.trigger_set_huggingface,
"trigger_set_client": self.trigger_set_client,
"encrypted_watermarked_model": self.encrypted_watermarked_model,
"hash": self.hash
}
class Blockchain:
def __init__(self):
self.chain = {}
self.add_block("Genesis HuggingFace", "Genesis Client", "Genesis Model")
def add_block(self, trigger_set_huggingface, trigger_set_client, encrypted_watermarked_model):
counter = len(self.chain)
previous_hash = self.chain[counter - 1].hash if counter > 0 else "0"
new_block = Block(previous_hash, trigger_set_huggingface, trigger_set_client, encrypted_watermarked_model,
counter)
self.chain[counter] = new_block
return new_block
def is_chain_valid(self):
for i in range(1, len(self.chain)):
current_block = self.chain[i]
previous_block = self.chain[i - 1]
if current_block.hash != current_block.calculate_hash():
print(f"Invalid hash for block {i}")
return False
if current_block.previous_hash != previous_block.hash:
print(f"Invalid previous hash for block {i}")
return False
return True
def to_dict(self):
return {str(counter): block.to_dict() for counter, block in self.chain.items()}
def save_to_file(self, filename):
with open(filename, 'w') as file:
json.dump(self.to_dict(), file, indent=4)
print(f"Blockchain saved to {filename}")
@classmethod
def load_from_file(cls, filename):
with open(filename, 'r') as file:
data = json.load(file)
blockchain = cls()
blockchain.chain.clear() # Clear the genesis block
for counter, block_data in data.items():
block = Block(
block_data["previous_hash"],
block_data["trigger_set_huggingface"],
block_data["trigger_set_client"],
block_data["encrypted_watermarked_model"],
int(counter),
block_data["timestamp"]
)
blockchain.chain[int(counter)] = block
print(f"Blockchain loaded from {filename}")
return blockchain, data
def print_blockchain_details(blockchain):
for counter, block in blockchain.chain.items():
print(f"Block {counter}:")
print(f" Timestamp: {block.timestamp:.6f}")
print(f" Previous Hash: {block.previous_hash}")
print(f" Hash: {block.hash}")
print(f" Calculated Hash: {block.calculate_hash()}")
print()
# # Exemple d'utilisation
# blockchain = Blockchain()
#
# # Ajouter quelques blocs
# blockchain.add_block("HF Trigger Set 1", "Client Trigger Set 1", "Encrypted Model 1")
# blockchain.add_block("HF Trigger Set 2", "Client Trigger Set 2", "Encrypted Model 2")
# blockchain.add_block("HF Trigger Set 3", "Client Trigger Set 3", "Encrypted Model 3")
#
# print("Original Blockchain:")
# print_blockchain_details(blockchain)
#
# # Sauvegarder la blockchain dans un fichier JSON
# blockchain.save_to_file("blockchain.json")
#
# # Charger la blockchain depuis le fichier JSON
# loaded_blockchain, _ = Blockchain.load_from_file("blockchain.json")
#
# print("\nLoaded Blockchain:")
# print_blockchain_details(loaded_blockchain)
#
# # Vérifier que la blockchain chargée est valide
# print(f"La blockchain chargée est valide : {loaded_blockchain.is_chain_valid()}")
#
# # Ajouter un nouveau bloc à la blockchain chargée
# loaded_blockchain.add_block("HF Trigger Set 4", "Client Trigger Set 4", "Encrypted Model 4")
#
# # Sauvegarder la blockchain mise à jour
# loaded_blockchain.save_to_file("blockchain.json") |