-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase.py
213 lines (179 loc) · 6.53 KB
/
base.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
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
from .utils import *
from time import time
from hashlib import sha256
from functools import reduce
import operator
class UTXO(object):
__slots__ = ['sender', 'receiver', 'value', 'timestamp', 'id', 'transaction']
def __init__(self, sender_address, receiver_address, amount):
self.sender = sender_address
self.receiver = receiver_address
self.value = amount
self.timestamp = time()
self.id = sha256((self.sender + self.receiver + str(self.timestamp)).encode('utf-8')).hexdigest()
self.transaction = None
def __repr__(self):
try:
return "<%s> * %s *" %(self.block, self.id[:10])
except:
return "%s" %(self.id[:10])
class Transaction(object):
__slots__ = ['utxos', 'block', 'sender', 'signature', 'inputs']
def __init__(self,utxos):
self.inputs = []
for utxo in utxos:
assert(isinstance(utxo, UTXO))
self.utxos = utxos
if self.utxos != []:
self.sender = utxos[0].sender
try:
assert(reduce(lambda x, y: x.sender == self.sender and y, self.utxos))
except AssertionError:
raise ValidityError("UTXOs of a given transaction must have same sender.")
def __str__(self):
return str(self.sender) +': ' + '|'.join(map(lambda u: str(u.receiver), self.utxos))
def __repr__(self):
return self.sender + ': ' + ' '.join([utxo.id[:10] for utxo in self.utxos])
class BlockHeader(object):
__slots__ = ['previous_hash', 'timestamp', 'nonce', 'threshold', 'size', 'block', 'height']
def __init__(self, prev_block, threshold, block):
try:
self.height = prev_block.header.height + 1
self.previous_hash = prev_block.hash
except AttributeError:
self.previous_hash = None
self.height = 0
self.timestamp = time()
self.threshold = threshold
self.block = block
self.nonce = None
self.size = 0
def save(self, nonce, size):
self.nonce = nonce
self.size = size
def __repr__(self):
return "<%s> Header of Block %s" %(self.timestamp, self.block.header.height)
def __str__(self):
return '|'.join([str(getattr(self, attr)) for attr in self.__slots__ if attr not in ['block']])
class Block(object):
__slots__ = ['header', 'transactions', 'flags', 'chain', 'threshold', 'hash', 'signature', 'miner', 'node']
hash_variables = ['header', 'transactions', 'flags']
depth = 0
def __init__(self, chain, threshold = 2):
prev_block = chain.getHead()
if prev_block:
self.header = BlockHeader(prev_block,threshold, self)
else:
raise ValidityError("The chain has no Genesis")
self.transactions = []
self.flags = 0x00
self.chain = chain
self.signature = None
self.threshold = threshold
self.hash = None
def save(self, nonce):
size = len(str(self))
self.header.save(nonce, size)
size = len(str(self))
self.header.save(nonce, size)
self.hash = sha256(str(self).encode('utf-8')).hexdigest()
def addTransaction(self, transaction):
self.transactions.append(transaction)
if self.flags == 0x00:
self.flags = 0x11
def __repr__(self):
try:
return "indieChain[%s] %s" %(self.header.height, self.hash[:10])
except:
return "indieChain[%s]" % self.header.height
def __str__(self):
return '_*_'.join([str(getattr(self, attr)) for attr in self.hash_variables])
class GenesisBlock(object):
def __init__(self):
self.header = BlockHeader(None, 0, self)
self.hash = '0' * 64
self.size = len(self.hash)
self.depth = 0
def __repr__(self):
try:
return "indieChain[%s] %s" %(self.header.height, self.hash[:10])
except:
return "indieChain[%s]" % self.header.height
class SummaryBlock(object):
__slots__ = ['depth', 'changes', 'header', 'blocks', 'hash', 'height']
def __init__(self, blocks, depth, prev_block):
self.header = BlockHeader(prev_block, 4, self)
self.height = blocks[0].header.height
self.depth = depth
self.changes = {}
self.createSummary(blocks)
if all(isinstance(block, Block) for block in blocks):
self.blocks = [block.header.height for block in blocks]
elif all(isinstance(block, SummaryBlock) for block in blocks):
self.blocks = [block.height for block in blocks]
self.hash = blocks[-1].hash
def createSummary(self, blocks):
if all(isinstance(block, Block) for block in blocks):
utxos = []
for blk in blocks:
for transaction in blk.transactions:
utxos += transaction.utxos
outgoing = [(utxo.sender, utxo.value) for utxo in utxos]
incoming = [(utxo.receiver, utxo.value) for utxo in utxos]
senders = set([utxo.sender for utxo in utxos])
receivers = set([utxo.receiver for utxo in utxos])
for wallet in list(senders) + list(receivers):
self.changes[wallet] = 0
for sender in senders:
self.changes[sender] = -sum(map(lambda v: v[1], filter(lambda u: u[0] == sender, outgoing)))
for receiver in receivers:
self.changes[receiver] += sum(map(lambda v: v[1], filter(lambda u: u[0] == receiver, incoming)))
elif all(isinstance(block, SummaryBlock) for block in blocks):
all_keys = reduce(operator.add,[list(block.changes.keys()) for block in blocks])
for key in all_keys:
self.changes[key] = 0
for block in blocks:
for key, value in block.changes.items():
self.changes[key] += value
else:
raise TypeError('Invalid typing of blocks')
def __repr__(self):
return 'Summary['+ str(self.depth) +'] [' + '|'.join(map(str, self.blocks)) +']'
class indieChain(object):
__slots__ = ['transactions', 'blocks', 'freelen', 'base_pointers', 'end_pointers', 'summary_width']
def __init__(self, freelen=5, width=5):
self.blocks = [GenesisBlock()]
self.transactions = []
#base_pointer[0] points to first height of normal blocks, base_pointer[1] points to depth1 summary blocks.
#base_pointer[2] points to first dept2 summary blocks
self.base_pointers = [1]
self.end_pointers =[1]
self.freelen = freelen
self.summary_width = width
def getHead(self):
if self.blocks == []:
return None
else:
return self.blocks[-1]
def push(self, block):
def validateBlock(block):
head = self.getHead()
assert(isinstance(block, Block))
return (head.hash == block.header.previous_hash)
if validateBlock(block):
block.header.height = 1 + self.getHead().header.height
self.end_pointers[0] += 1
self.blocks.append(block)
self.transactions += block.transactions
def getGenesis(self):
return self.blocks[0]
def getIndexByHeight(self, h):
for index, block in enumerate(self.blocks):
if block.header.height == h:
return index
def __repr__(self):
return 'indieChain: ' + ' '.join([str(block.hash)[:10] for block in self.blocks])
def getHeaders(self):
return [block.header for block in self.blocks]
def getBlock(self, b_hash):
return next(block for block in self.blocks if block.hash == b_hash)