-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathblock.py
72 lines (62 loc) · 2.19 KB
/
block.py
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
import hashlib
import threading
import jsonpickle
import numpy as np
import enum
stakes = [10, 10, 10, 10, 10]
majority_vote = sum(stakes) / len(stakes)
class Block():
def __init__(self, index, timestamp, data, previous_hash):
self.is_timed_out = False
self.index = index
self.timestamp = timestamp
self.data = data
self.previous_hash = previous_hash
if index == 0:
self.pos = False
else:
self.pos = self.proof_of_stake(stakes)
self.hash = self.hashing()
def __iter__(self):
return self
def mytimer(self, time_limit=10.0):
self.is_timed_out = True
def hashing(self):
key = hashlib.sha256()
key.update(str(self.index).encode("utf-8"))
key.update(str(self.timestamp).encode("utf-8"))
key.update(str(self.data).encode("utf-8"))
key.update(str(self.previous_hash).encode("utf-8"))
key.update(str(self.pos).encode("utf-8"))
return key.hexdigest()
# * whether the bot thinks it should update
def voting(self):
np.random.shuffle(stakes)
consesus = 0
for i in range(0, len(stakes) / 2):
consesus = consesus + stakes[i]
if consesus >= majority_vote:
return True
else:
return False
def proof_of_work(self, puzzle_bits=4, time_limit=10.0):
my_timer = threading.Timer(time_limit, self.mytimer)
my_timer.start()
for i in range(pow(2, 256)):
if not self.is_timed_out:
m = hashlib.sha256()
m.update(str(self.index).encode("utf-8"))
m.update(str(self.timestamp).encode("utf-8"))
m.update(str(self.data).encode("utf-8"))
m.update(str(self.previous_hash).encode("utf-8"))
m.update(str(i).encode("utf-8"))
string = m.hexdigest()
if string[0:1] == "0":
print("hit hit hit for", i)
my_timer.cancel()
return i
else:
print("timed out. -1 returned")
return -1
def toJSON(self):
return jsonpickle.encode(self)