Sckathach commited on
Commit
107a1d1
·
1 Parent(s): 11c013a

Added blockchain

Browse files
Files changed (5) hide show
  1. .idea/Zamark.iml +1 -1
  2. .idea/misc.xml +4 -1
  3. app.py +21 -19
  4. blockchain.json +65 -0
  5. blockchain.py +138 -0
.idea/Zamark.iml CHANGED
@@ -2,7 +2,7 @@
2
  <module type="PYTHON_MODULE" version="4">
3
  <component name="NewModuleRootManager">
4
  <content url="file://$MODULE_DIR$" />
5
- <orderEntry type="inheritedJdk" />
6
  <orderEntry type="sourceFolder" forTests="false" />
7
  </component>
8
  </module>
 
2
  <module type="PYTHON_MODULE" version="4">
3
  <component name="NewModuleRootManager">
4
  <content url="file://$MODULE_DIR$" />
5
+ <orderEntry type="jdk" jdkName="Python 3.10 (Zamark)" jdkType="Python SDK" />
6
  <orderEntry type="sourceFolder" forTests="false" />
7
  </component>
8
  </module>
.idea/misc.xml CHANGED
@@ -1,4 +1,7 @@
1
  <?xml version="1.0" encoding="UTF-8"?>
2
  <project version="4">
3
- <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (Team8)" project-jdk-type="Python SDK" />
 
 
 
4
  </project>
 
1
  <?xml version="1.0" encoding="UTF-8"?>
2
  <project version="4">
3
+ <component name="Black">
4
+ <option name="sdkName" value="Python 3.10 (Zamark)" />
5
+ </component>
6
+ <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (Zamark)" project-jdk-type="Python SDK" />
7
  </project>
app.py CHANGED
@@ -6,6 +6,8 @@ import uuid
6
  import time
7
  import json
8
 
 
 
9
  def generate_mock_hash():
10
  return hashlib.sha256(str(time.time()).encode()).hexdigest()
11
 
@@ -216,27 +218,27 @@ if 'block_data' not in st.session_state:
216
 
217
  # Button to update the blockchain
218
  if st.button("Update Blockchain"):
219
- previous_hash = generate_mock_hash()
220
- timestamp = int(time.time() * 1000) # Current timestamp in milliseconds
221
- watermarked_model_hash = generate_mock_hash()
222
- trigger_set_hash = generate_mock_hash()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223
 
224
  # Create the block data structure
225
- st.session_state.block_data = {
226
- "blockNumber": 42,
227
- "previousHash": previous_hash,
228
- "timestamp": timestamp,
229
- "transactions": [
230
- {
231
- "type": "Watermarked Model Hash",
232
- "hash": watermarked_model_hash
233
- },
234
- {
235
- "type": "Trigger Set Hash",
236
- "hash": trigger_set_hash
237
- }
238
- ]
239
- }
240
 
241
  st.success("Blockchain updated successfully!")
242
 
 
6
  import time
7
  import json
8
 
9
+ from blockchain import Blockchain, print_blockchain_details
10
+
11
  def generate_mock_hash():
12
  return hashlib.sha256(str(time.time()).encode()).hexdigest()
13
 
 
218
 
219
  # Button to update the blockchain
220
  if st.button("Update Blockchain"):
221
+ # Charger la blockchain depuis le fichier JSON
222
+ loaded_blockchain, data = Blockchain.load_from_file("blockchain.json")
223
+
224
+ # Vérifier que la blockchain chargée est valide
225
+ print(f"La blockchain chargée est valide : {loaded_blockchain.is_chain_valid()}")
226
+
227
+ # Ajouter un nouveau bloc à la blockchain chargée
228
+ loaded_blockchain.add_block("HF Trigger Set 4", "Client Trigger Set 4", "Encrypted Model 4")
229
+
230
+ print("\nLoaded Blockchain:")
231
+ print_blockchain_details(loaded_blockchain)
232
+
233
+ # Sauvegarder la blockchain mise à jour
234
+ loaded_blockchain.save_to_file("blockchain.json")
235
+
236
+
237
+ # watermarked_model_hash = generate_mock_hash()
238
+ # trigger_set_hash = generate_mock_hash()
239
 
240
  # Create the block data structure
241
+ st.session_state.block_data = data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
 
243
  st.success("Blockchain updated successfully!")
244
 
blockchain.json ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "0": {
3
+ "timestamp": 1727520928.1020324,
4
+ "previous_hash": "0",
5
+ "counter": 0,
6
+ "trigger_set_huggingface": "Genesis HuggingFace",
7
+ "trigger_set_client": "Genesis Client",
8
+ "encrypted_watermarked_model": "Genesis Model",
9
+ "hash": "0fe4deddadab0f2d5884b955fe6761fc39a448d73efc96cdebb2673a14ed45dd"
10
+ },
11
+ "1": {
12
+ "timestamp": 1727520928.1021242,
13
+ "previous_hash": "0fe4deddadab0f2d5884b955fe6761fc39a448d73efc96cdebb2673a14ed45dd",
14
+ "counter": 1,
15
+ "trigger_set_huggingface": "HF Trigger Set 1",
16
+ "trigger_set_client": "Client Trigger Set 1",
17
+ "encrypted_watermarked_model": "Encrypted Model 1",
18
+ "hash": "4e95a394e97baa18ea44e17a32266c4c39ffd5da28bc45d79811d50bcc4c8e28"
19
+ },
20
+ "2": {
21
+ "timestamp": 1727520928.1021426,
22
+ "previous_hash": "4e95a394e97baa18ea44e17a32266c4c39ffd5da28bc45d79811d50bcc4c8e28",
23
+ "counter": 2,
24
+ "trigger_set_huggingface": "HF Trigger Set 2",
25
+ "trigger_set_client": "Client Trigger Set 2",
26
+ "encrypted_watermarked_model": "Encrypted Model 2",
27
+ "hash": "195c1701b1b019219182df1b7c7a524a9a9a3dcaaab3fa899ae29bdab7e8e44f"
28
+ },
29
+ "3": {
30
+ "timestamp": 1727520928.1021535,
31
+ "previous_hash": "195c1701b1b019219182df1b7c7a524a9a9a3dcaaab3fa899ae29bdab7e8e44f",
32
+ "counter": 3,
33
+ "trigger_set_huggingface": "HF Trigger Set 3",
34
+ "trigger_set_client": "Client Trigger Set 3",
35
+ "encrypted_watermarked_model": "Encrypted Model 3",
36
+ "hash": "e30ccc1af1af5241babc635ebf2f23ebcf7d97690eefb3d49f52f84e3f5ab64a"
37
+ },
38
+ "4": {
39
+ "timestamp": 1727520928.10693,
40
+ "previous_hash": "e30ccc1af1af5241babc635ebf2f23ebcf7d97690eefb3d49f52f84e3f5ab64a",
41
+ "counter": 4,
42
+ "trigger_set_huggingface": "HF Trigger Set 4",
43
+ "trigger_set_client": "Client Trigger Set 4",
44
+ "encrypted_watermarked_model": "Encrypted Model 4",
45
+ "hash": "6bcd4768b2b5d2a6d51ae12add4c6da6cb43a32ada36a247ea4734fa9a60e647"
46
+ },
47
+ "5": {
48
+ "timestamp": 1727520938.4695382,
49
+ "previous_hash": "6bcd4768b2b5d2a6d51ae12add4c6da6cb43a32ada36a247ea4734fa9a60e647",
50
+ "counter": 5,
51
+ "trigger_set_huggingface": "HF Trigger Set 4",
52
+ "trigger_set_client": "Client Trigger Set 4",
53
+ "encrypted_watermarked_model": "Encrypted Model 4",
54
+ "hash": "83d84d3bfc3f707b1987227187468aeb4cfca44c4bdf9e746c9334f6cb80d2a4"
55
+ },
56
+ "6": {
57
+ "timestamp": 1727520943.492908,
58
+ "previous_hash": "83d84d3bfc3f707b1987227187468aeb4cfca44c4bdf9e746c9334f6cb80d2a4",
59
+ "counter": 6,
60
+ "trigger_set_huggingface": "HF Trigger Set 4",
61
+ "trigger_set_client": "Client Trigger Set 4",
62
+ "encrypted_watermarked_model": "Encrypted Model 4",
63
+ "hash": "aaccd1f43b43d924619665155cfcb292cf2cf2597d9c4c32197e2155696fde47"
64
+ }
65
+ }
blockchain.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import hashlib
2
+ import time
3
+ import json
4
+
5
+
6
+ class Block:
7
+ def __init__(self, previous_hash, trigger_set_huggingface_hash, trigger_set_client_hash, encrypted_watermarked_model_hash, counter,
8
+ timestamp=None):
9
+ self.timestamp = timestamp if timestamp else time.time()
10
+ self.previous_hash = previous_hash
11
+ self.counter = counter
12
+ self.trigger_set_huggingface = trigger_set_huggingface_hash
13
+ self.trigger_set_client = trigger_set_client_hash
14
+ self.encrypted_watermarked_model = encrypted_watermarked_model_hash
15
+ self.hash = self.calculate_hash()
16
+
17
+ def calculate_hash(self):
18
+ hash_string = (
19
+ f"{self.timestamp:.6f}" +
20
+ str(self.previous_hash) +
21
+ str(self.counter) +
22
+ str(self.trigger_set_huggingface) +
23
+ str(self.trigger_set_client) +
24
+ str(self.encrypted_watermarked_model)
25
+ )
26
+ return hashlib.sha256(hash_string.encode()).hexdigest()
27
+
28
+ @staticmethod
29
+ def hash_data(data):
30
+ return hashlib.sha256(str(data).encode()).hexdigest()
31
+
32
+ def to_dict(self):
33
+ return {
34
+ "timestamp": self.timestamp,
35
+ "previous_hash": self.previous_hash,
36
+ "counter": self.counter,
37
+ "trigger_set_huggingface": self.trigger_set_huggingface,
38
+ "trigger_set_client": self.trigger_set_client,
39
+ "encrypted_watermarked_model": self.encrypted_watermarked_model,
40
+ "hash": self.hash
41
+ }
42
+
43
+
44
+ class Blockchain:
45
+ def __init__(self):
46
+ self.chain = {}
47
+ self.add_block("Genesis HuggingFace", "Genesis Client", "Genesis Model")
48
+
49
+ def add_block(self, trigger_set_huggingface, trigger_set_client, encrypted_watermarked_model):
50
+ counter = len(self.chain)
51
+ previous_hash = self.chain[counter - 1].hash if counter > 0 else "0"
52
+ new_block = Block(previous_hash, trigger_set_huggingface, trigger_set_client, encrypted_watermarked_model,
53
+ counter)
54
+ self.chain[counter] = new_block
55
+
56
+ def is_chain_valid(self):
57
+ for i in range(1, len(self.chain)):
58
+ current_block = self.chain[i]
59
+ previous_block = self.chain[i - 1]
60
+
61
+ if current_block.hash != current_block.calculate_hash():
62
+ print(f"Invalid hash for block {i}")
63
+ return False
64
+
65
+ if current_block.previous_hash != previous_block.hash:
66
+ print(f"Invalid previous hash for block {i}")
67
+ return False
68
+
69
+ return True
70
+
71
+ def to_dict(self):
72
+ return {str(counter): block.to_dict() for counter, block in self.chain.items()}
73
+
74
+ def save_to_file(self, filename):
75
+ with open(filename, 'w') as file:
76
+ json.dump(self.to_dict(), file, indent=4)
77
+ print(f"Blockchain saved to {filename}")
78
+
79
+ @classmethod
80
+ def load_from_file(cls, filename):
81
+ with open(filename, 'r') as file:
82
+ data = json.load(file)
83
+
84
+ blockchain = cls()
85
+ blockchain.chain.clear() # Clear the genesis block
86
+ for counter, block_data in data.items():
87
+ block = Block(
88
+ block_data["previous_hash"],
89
+ block_data["trigger_set_huggingface"],
90
+ block_data["trigger_set_client"],
91
+ block_data["encrypted_watermarked_model"],
92
+ int(counter),
93
+ block_data["timestamp"]
94
+ )
95
+ blockchain.chain[int(counter)] = block
96
+
97
+ print(f"Blockchain loaded from {filename}")
98
+ return blockchain, data
99
+
100
+
101
+ def print_blockchain_details(blockchain):
102
+ for counter, block in blockchain.chain.items():
103
+ print(f"Block {counter}:")
104
+ print(f" Timestamp: {block.timestamp:.6f}")
105
+ print(f" Previous Hash: {block.previous_hash}")
106
+ print(f" Hash: {block.hash}")
107
+ print(f" Calculated Hash: {block.calculate_hash()}")
108
+ print()
109
+
110
+
111
+ # Exemple d'utilisation
112
+ blockchain = Blockchain()
113
+
114
+ # Ajouter quelques blocs
115
+ blockchain.add_block("HF Trigger Set 1", "Client Trigger Set 1", "Encrypted Model 1")
116
+ blockchain.add_block("HF Trigger Set 2", "Client Trigger Set 2", "Encrypted Model 2")
117
+ blockchain.add_block("HF Trigger Set 3", "Client Trigger Set 3", "Encrypted Model 3")
118
+
119
+ print("Original Blockchain:")
120
+ print_blockchain_details(blockchain)
121
+
122
+ # Sauvegarder la blockchain dans un fichier JSON
123
+ blockchain.save_to_file("blockchain.json")
124
+
125
+ # Charger la blockchain depuis le fichier JSON
126
+ loaded_blockchain, _ = Blockchain.load_from_file("blockchain.json")
127
+
128
+ print("\nLoaded Blockchain:")
129
+ print_blockchain_details(loaded_blockchain)
130
+
131
+ # Vérifier que la blockchain chargée est valide
132
+ print(f"La blockchain chargée est valide : {loaded_blockchain.is_chain_valid()}")
133
+
134
+ # Ajouter un nouveau bloc à la blockchain chargée
135
+ loaded_blockchain.add_block("HF Trigger Set 4", "Client Trigger Set 4", "Encrypted Model 4")
136
+
137
+ # Sauvegarder la blockchain mise à jour
138
+ loaded_blockchain.save_to_file("blockchain.json")