forked from Chia-Network/chiapos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_python_bindings.py
More file actions
162 lines (149 loc) · 4.56 KB
/
test_python_bindings.py
File metadata and controls
162 lines (149 loc) · 4.56 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
import unittest
from chiapos import DiskProver, DiskPlotter, Verifier
from hashlib import sha256
from pathlib import Path
from secrets import token_bytes
class TestPythonBindings(unittest.TestCase):
def test_k_21(self):
plot_seed: bytes = bytes(
[
5,
104,
52,
4,
51,
55,
23,
84,
91,
10,
111,
12,
13,
222,
151,
16,
228,
211,
254,
45,
92,
198,
204,
10,
9,
10,
11,
129,
139,
171,
15,
23,
]
)
pl = DiskPlotter()
pl.create_plot_disk(
".",
".",
".",
"myplot.dat",
21,
bytes([1, 2, 3, 4, 5]),
plot_seed,
300,
32,
8192,
8,
False,
)
pl = None
pr = DiskProver(str(Path("myplot.dat")))
total_proofs: int = 0
iterations: int = 5000
v = Verifier()
for i in range(iterations):
if i % 100 == 0:
print(i)
challenge = sha256(i.to_bytes(4, "big")).digest()
for index, quality in enumerate(pr.get_qualities_for_challenge(challenge)):
proof = pr.get_full_proof(challenge, index)
assert len(proof) == 8 * pr.get_size()
computed_quality = v.validate_proof(
plot_seed, pr.get_size(), challenge, proof
)
assert computed_quality == quality
total_proofs += 1
print(
f"total proofs {total_proofs} out of {iterations}\
{total_proofs / iterations}"
)
assert total_proofs > 4000
assert total_proofs < 6000
pr = None
sha256_plot_hash = sha256()
with open("myplot.dat", "rb") as f:
# Read and update hash string value in blocks of 4K
for byte_block in iter(lambda: f.read(4096), b""):
sha256_plot_hash.update(byte_block)
plot_hash = str(sha256_plot_hash.hexdigest())
assert (
plot_hash
== "80e32f560f3a4347760d6baae8d16fbaf484948088bff05c51bdcc24b7bc40d9"
)
print(f"\nPlotfile asserted sha256: {plot_hash}\n")
def test_faulty_plot_doesnt_crash(self):
if Path("myplot.dat").exists():
Path("myplot.dat").unlink()
if Path("myplotbad.dat").exists():
Path("myplotbad.dat").unlink()
plot_id: bytes = bytes([i for i in range(32, 64)])
pl = DiskPlotter()
pl.create_plot_disk(
".",
".",
".",
"myplot.dat",
21,
bytes([1, 2, 3, 4, 5]),
plot_id,
300,
32,
8192,
8,
False,
)
f = open("myplot.dat", "rb")
all_data = bytearray(f.read())
f.close()
assert len(all_data) > 20000000
all_data_bad = all_data[:20000000] + bytearray(token_bytes(10000)) + all_data[20100000:]
f_bad = open("myplotbad.dat", "wb")
f_bad.write(all_data_bad)
f_bad.close()
pr = DiskProver(str(Path("myplotbad.dat")))
iterations: int = 50000
v = Verifier()
successes = 0
failures = 0
for i in range(iterations):
if i % 100 == 0:
print(i)
challenge = sha256(i.to_bytes(4, "big")).digest()
try:
for index, quality in enumerate(pr.get_qualities_for_challenge(challenge)):
proof = pr.get_full_proof(challenge, index)
computed_quality = v.validate_proof(
plot_id, pr.get_size(), challenge, proof
)
if computed_quality == quality:
successes += 1
else:
print("Did not validate")
failures += 1
except Exception as e:
print(f"Exception: {e}")
failures += 1
print(f"Successes: {successes}")
print(f"Failures: {failures}")
if __name__ == "__main__":
unittest.main()