-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchained.py
More file actions
280 lines (229 loc) · 7.49 KB
/
chained.py
File metadata and controls
280 lines (229 loc) · 7.49 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import argparse
import json
import os
import sys
import jsonpatch
import hashlib
from datetime import datetime
from typing import Callable, Optional
from random import randint
from pathlib import Path
try:
import ENDFtk as tk
except Exception:
print(
"You need to install ENDFtk to use this :(, please compile and install it locally"
)
sys.exit(1)
def load_json(filename: str | Path) -> dict:
"""Load data from the specified file."""
if os.path.exists(filename):
with open(filename, "r") as file:
return json.load(file)
return {}
def save_json(filename: str | Path, data: dict) -> None:
"""Save data to the specified file."""
with open(filename, "w") as file:
json.dump(data, file, indent=4)
def _now() -> datetime:
return datetime.now()
def _hashfunc(x: str) -> str:
return hashlib.sha256(x.encode()).hexdigest()
def load_tape(filename: str | Path) -> tuple[bool, tk.tree.Tape]:
tape = tk.tree.Tape.from_file(str(filename))
try:
for material in tape.materials:
material.parse()
except Exception:
return (False, None)
return (True, tape)
def create_data_block_from_tape(tape: tk.tree.Tape) -> dict:
# assumes one material per tape for now
assert len(tape.materials) == 1
material = tape.materials.front()
parsed_material = material.parse()
mf1 = parsed_material.file(1)
details = mf1.sections.front()
zai = details.ZA * 10 + details.LISO
# use mf3, mt1 for test purposes
energies = xs = []
if parsed_material.has_section(3, 1):
mf3_mt1 = parsed_material.section(3, 1)
energies = mf3_mt1.energies.to_list()
xs = mf3_mt1.cross_sections.to_list()
return {
"zai": zai,
"mf3": {
"mt1": {
"energies": energies,
"xs": xs,
}
},
}
class Block:
def __init__(
self,
index: int,
patch: dict,
previous_hash: str,
timestamp: Optional[None | datetime] = None,
difficulty: Optional[int] = 1,
):
self.index = index
self.patch = patch
self.timestamp = _now() if timestamp is None else timestamp
self.previous_hash = previous_hash
self.difficulty = difficulty
self.hashresult = None
self.nonce = None
def do_hash(self, workvalue: int) -> str:
props = (
(
self.index,
lambda x: str(x),
),
(
self.timestamp,
lambda x: str(x),
),
(
self.patch,
lambda x: json.dumps(x, sort_keys=True),
),
(
self.previous_hash,
lambda x: str(x),
),
(
workvalue,
lambda x: str(x),
),
)
dstr = "".join([fn(p) for p, fn in props])
return _hashfunc(dstr)
def to_dict(self) -> dict:
return {
"index": self.index,
"timestamp": self.timestamp.isoformat(),
"previous_hash": self.previous_hash,
"block_hash": self.hashresult,
"patch": self.patch,
"workvalue": self.nonce,
"difficulty": self.difficulty,
}
@staticmethod
def from_dict(data: dict) -> "Block":
block = Block(
data["index"],
data["patch"],
data["previous_hash"],
timestamp=datetime.fromisoformat(data["timestamp"]),
difficulty=data["difficulty"],
)
block.hashresult = data["block_hash"]
block.nonce = data["workvalue"]
return block
def check(self, hashresult: str) -> bool:
return int(hashresult, 16) < 2 ** (256 - self.difficulty)
def do_work(self) -> tuple[int, str]:
"""
In the case of nuclear data, the nonce doesn't make much sense.
"""
compare_value = 2 ** (256 - self.difficulty)
def _check_hash_quick(hashresult: str) -> bool:
return int(hashresult, 16) < compare_value
nonce = randint(0, 2**32)
hashresult = self.do_hash(nonce)
while _check_hash_quick(hashresult) is False:
nonce += 1
hashresult = self.do_hash(nonce)
self.hashresult = hashresult
self.nonce = nonce
return (
nonce,
hashresult,
)
def verify(
self,
) -> bool:
"""workvalue is the cnonce"""
if self.nonce is None:
return False
hashresult = self.do_hash(self.nonce)
return self.check(hashresult)
_BLOCKCHAIN_FILE = "blockchain.json"
class Blockchain:
def __init__(self):
if Path(_BLOCKCHAIN_FILE).is_file():
raw = load_json(_BLOCKCHAIN_FILE)
self.from_dict(raw)
else:
self._chain = [self._create_genesis_block()]
self._apply_patches()
def _apply_patches(self) -> "Blockchain":
self._patched_data = {}
for block in self._chain:
patch = jsonpatch.JsonPatch(block.patch)
self._patched_data = patch.apply(self._patched_data)
# the serailization will be really slow for large chains
# this is far from optimal but for a POC it should be fine
def from_dict(self, chain: dict) -> "Blockchain":
self._chain = []
for data in chain:
self._chain.append(Block.from_dict(data))
return self
# the serailization will be really slow for large chains
# this is far from optimal but for a POC it should be fine
def to_dict(self) -> dict:
chain_data = []
for block in self._chain:
assert isinstance(block, Block)
chain_data.append(block.to_dict())
return chain_data
def _create_genesis_block(self) -> Block:
block = Block(0, {}, '', difficulty=16)
block.do_work()
return block
def append(self, data: dict, difficulty: int) -> "Blockchain":
previous_block = self._chain[-1]
index = previous_block.index + 1
previous_hash = previous_block.hashresult
# Create a JSON patch to track changes
patch = jsonpatch.make_patch(self._patched_data, data)
new_block = Block(index, patch.patch, previous_hash, difficulty=difficulty)
_ = new_block.do_work()
self._chain.append(new_block)
save_json(_BLOCKCHAIN_FILE, self.to_dict())
return self
def is_valid(self) -> bool:
for block in self._chain:
if not block.verify():
return False
return True
def iterate(self, func: Callable[[Block], None]) -> None:
for block in self._chain:
func(block)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("endf_file")
args = parser.parse_args()
endf_file = args.endf_file
is_valid, tape = load_tape(endf_file)
if not is_valid:
raise RuntimeError("Not a valid ENDF file.")
tape_data = create_data_block_from_tape(tape)
chain = Blockchain()
print("Processing chain....")
is_valid = chain.is_valid()
print(f"Chain is {'' if is_valid else 'not'} valid")
if not is_valid:
sys.exit(1)
print("Adding patch...")
chain.append(tape_data, difficulty=8)
print("Checking chain....")
is_valid = chain.is_valid()
print(f"Chain is {'' if is_valid else 'not'} valid")
if not is_valid:
sys.exit(1)
if __name__ == "__main__":
main()