-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain.py
62 lines (54 loc) · 2.19 KB
/
chain.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
import datetime
from block import *
import hashlib
class MinimalChain():
def __init__(self): # initialize when creating a chain
self.blocks = [self.get_genesis_block()]
def get_genesis_block(self):
return MinimalBlock(0,
datetime.datetime.utcnow(),
'Genesis',
'arbitrary')
def add_block(self, data):
self.blocks.append(MinimalBlock(len(self.blocks),
datetime.datetime.utcnow(),
data,
self.blocks[len(self.blocks)-1].hash))
def get_chain_size(self): # exclude genesis block
return len(self.blocks)-1
def verify_all(self, verbose=True):
flag = True
for i in range(1,len(self.blocks)):
if self.blocks[i].index != i:
flag = False
if verbose:
print(f'Wrong block index at block {i}.')
if self.blocks[i-1].hash != self.blocks[i].previous_hash:
flag = False
if verbose:
print(f'Wrong previous hash at block {i}.')
if self.blocks[i].hash != self.blocks[i].hashing():
flag = False
if verbose:
print(f'Wrong hash at block {i}.')
if self.blocks[i-1].timestamp >= self.blocks[i].timestamp:
flag = False
if verbose:
print(f'Backdating at block {i}.')
return flag
def fork(self, head='latest'):
if head in ['latest', 'whole', 'all']:
return copy.deepcopy(self) # deepcopy since they are mutable
else:
c = copy.deepcopy(self)
c.blocks = c.blocks[0:head+1]
return c
def get_root(self, chain_2):
min_chain_size = min(self.get_chain_size(), chain_2.get_chain_size())
for i in range(1,min_chain_size+1):
if self.blocks[i] != chain_2.blocks[i]:
return self.fork(i-1)
return self.fork(min_chain_size)
def print_all_chains(self):
for blocks in self.blocks:
blocks.get_data()