-
Notifications
You must be signed in to change notification settings - Fork 79
/
block.py
63 lines (53 loc) · 1.6 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
# coding:utf-8
import hashlib
import time
from model import Model
from rpc import BroadCast
class Block(Model):
def __init__(self, index, timestamp, tx, previous_hash):
self.index = index
self.timestamp = timestamp
self.tx = tx
self.previous_block = previous_hash
def header_hash(self):
"""
Refer to bitcoin block header hash
"""
return hashlib.sha256((str(self.index) + str(self.timestamp) + str(self.tx) + str(self.previous_block)).encode('utf-8')).hexdigest()
def pow(self):
"""
Proof of work. Add nouce to block.
"""
nouce = 0
while self.valid(nouce) is False:
nouce += 1
self.nouce = nouce
return nouce
def make(self, nouce):
"""
Block hash generate. Add hash to block.
"""
self.hash = self.ghash(nouce)
def ghash(self, nouce):
"""
Block hash generate.
"""
header_hash = self.header_hash()
token = ''.join((header_hash, str(nouce))).encode('utf-8')
return hashlib.sha256(token).hexdigest()
def valid(self, nouce):
"""
Validates the Proof
"""
return self.ghash(nouce)[:4] == "0000"
def to_dict(self):
return self.__dict__
@classmethod
def from_dict(cls, bdict):
b = cls(bdict['index'], bdict['timestamp'], bdict['tx'], bdict['previous_block'])
b.hash = bdict['hash']
b.nouce = bdict['nouce']
return b
@staticmethod
def spread(block):
BroadCast().new_block(block)