-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchain.py
382 lines (333 loc) · 14.2 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import json
import time
import itertools
from utils import big_endian_to_int
import rlp
from rlp.utils import encode_hex
from config import Env
from state import State, dict_to_prev_header
from block import Block, BlockHeader, FakeHeader, UnsignedBlock
from genesis_helpers import state_from_genesis_declaration, initialize, initialize_genesis_keys
from apply import apply_block, update_block_env_variables, validate_block, validate_transaction, verify_block_signature
from patricia_state import PatriciaState
import logging
databaseLog = logging.getLogger('Database')
class Chain(object):
def __init__(self, genesis=None, env=None,
new_head_cb=None, reset_genesis=False, localtime=None, max_history=1000, **kwargs):
self.env = env or Env()
self.patricia = PatriciaState()
self.patricia.from_db() # TODO: test
# Initialize the state
if 'head_hash' in self.db: # new head tag
self.state = self.mk_poststate_of_blockhash(self.db.get('head_hash'))
self.state.executing_on_head = True
databaseLog.info('Initializing chain from saved head, #%d (%s)',self.state.prev_headers[0].number, encode_hex(self.state.prev_headers[0].hash))
elif genesis is None:
raise Exception("Need genesis decl!")
elif isinstance(genesis, State):
assert env is None
self.state = genesis
self.env = self.state.env
databaseLog.info('Initializing chain from provided state')
elif isinstance(genesis, dict):
databaseLog.info('Initializing chain from new state based on alloc')
diction = {}
self.state = state_from_genesis_declaration(
genesis, self.env, executing_on_head=True, pytricia=diction)
for key in diction:
self.patricia.set_value(str(key), str(diction[key]))
self.patricia.to_db()
reset_genesis = True
assert self.env.db == self.state.db
initialize(self.state)
self.new_head_cb = new_head_cb
if self.state.block_number == 0:
assert self.state.block_number == self.state.prev_headers[0].number
else:
assert self.state.block_number == self.state.prev_headers[0].number
if reset_genesis:
if isinstance(self.state.prev_headers[0], FakeHeader):
header = self.state.prev_headers[0].to_block_header()
else:
header = self.state.prev_headers[0]
self.genesis = Block(header)
self.state.prev_headers[0] = header
initialize_genesis_keys(self.state, self.genesis)
else:
self.genesis = self.get_block_by_number(0)
self.head_hash = self.state.prev_headers[0].hash
self.time_queue = []
self.parent_queue = {}
self.localtime = time.time() if localtime is None else localtime
self.max_history = max_history
# Head (tip) of the chain
@property
def head(self):
try:
block_rlp = self.db.get(self.head_hash)
if block_rlp == 'GENESIS':
return self.genesis
else:
return rlp.decode(block_rlp, Block)
except Exception:
return None
# Returns the post-state of the block
def mk_poststate_of_blockhash(self, blockhash):
if blockhash not in self.db:
raise Exception("Block hash %s not found" % encode_hex(blockhash))
block_rlp = self.db.get(blockhash)
if block_rlp in ('GENESIS', b'GENESIS'):
return State.from_snapshot(json.loads(
self.db.get('GENESIS_STATE')), self.env)
block = rlp.decode(block_rlp, Block)
state = State(env=self.env)
state.trie.root_hash = block.header.state_root
update_block_env_variables(state, block)
state.txindex = len(block.transactions)
state.prev_headers = []
b = block
header_depth = state.config['PREV_HEADER_DEPTH']
for i in range(header_depth + 1):
state.prev_headers.append(b.header)
try:
b = rlp.decode(state.db.get(b.header.prevhash), Block)
except Exception:
break
if i < header_depth:
if state.db.get(b.header.prevhash) == 'GENESIS':
jsondata = json.loads(state.db.get('GENESIS_STATE'))
for h in jsondata["prev_headers"][:header_depth - i]:
state.prev_headers.append(dict_to_prev_header(h))
else:
raise Exception("Dangling prevhash")
assert len(state.journal) == 0, state.journal
return state
# Gets the parent block of a given block
def get_parent(self, block):
if block.header.number == int(self.db.get('GENESIS_NUMBER')):
return None
return self.get_block(block.header.prevhash)
# Gets the block with a given blockhash
def get_block(self, blockhash):
#try:
block_rlp = self.db.get(blockhash)
if block_rlp == 'GENESIS':
if not hasattr(self, 'genesis'):
self.genesis = rlp.decode(self.db.get('GENESIS_RLP'), sedes=Block.exclude(['v', 'r', 's']))
return self.genesis
else:
return rlp.decode(block_rlp, Block)
def get_head_block(self):
try:
block_rlp = self.db.get(self.head_hash)
if block_rlp == 'GENESIS':
return self.genesis
else:
return rlp.decode(block_rlp, Block)
except Exception:
return None
# Add a record allowing you to later look up the provided block's
# parent hash and see that it is one of its children
def add_child(self, child):
try:
existing = self.db.get(b'child:' + child.header.prevhash)
except Exception:
existing = b''
existing_hashes = []
for i in range(0, len(existing), 32):
existing_hashes.append(existing[i: i + 32])
if child.header.hash not in existing_hashes:
self.db.put(
b'child:' + child.header.prevhash,
existing + child.header.hash)
# Gets the hash of the block with the given block number
def get_blockhash_by_number(self, number):
try:
return self.db.get(b'block:%d' % number)
except Exception:
return None
# Gets the block with the given block number
def get_block_by_number(self, number):
hash = self.get_blockhash_by_number(number)
block = self.get_block(hash)
return block
# Get the hashes of all known children of a given block
def get_child_hashes(self, blockhash):
o = []
try:
data = self.db.get(b'child:' + blockhash)
for i in range(0, len(data), 32):
o.append(data[i:i + 32])
return o
except Exception:
return []
# Get the children of a block
def get_children(self, block):
if isinstance(block, Block):
block = block.header.hash
if isinstance(block, BlockHeader):
block = block.hash
return [self.get_block(h) for h in self.get_child_hashes(block)]
# This function should be called periodically so as to
# process blocks that were received but laid aside because
# they were received too early
def process_time_queue(self, new_time=None):
self.localtime = int(time.time()) if new_time is None else new_time
i = 0
while i < len(
self.time_queue) and self.time_queue[i].timestamp <= new_time:
pre_len = len(self.time_queue)
self.add_block(self.time_queue.pop(i))
if len(self.time_queue) == pre_len:
i += 1
def validate_transaction(self, tx):
return validate_transaction(self.state,tx)
def validate_block(self,block):
return validate_block(self.state,block)
def verify_block_signature(self,block,ip):
return verify_block_signature(self.state,block,ip)
# Call upon receiving a block
def add_block(self, block):
# Are we receiving the block too early?
try:
databaseLog.debug('Validating block: number %d hash %s', block.header.number, block.header.hash.encode('HEX'))
validate_block(self.state,block)
databaseLog.debug('Block validated: number %d hash %s', block.header.number, block.header.hash.encode('HEX'))
except (Exception):
databaseLog.info("Exception found while validating block %s. Discarding...", block.hash.encode("HEX"))
return False
now = self.localtime
#if block.header.timestamp > (now+30):
if block.header.timestamp > int(time.time()):
i = 0
while i < len(
self.time_queue) and block.timestamp > self.time_queue[i].timestamp:
i += 1
self.time_queue.insert(i, block)
databaseLog.debug("Block timestamp greater than now: Block Timestamp: %s - Now: %s", str(block.header.timestamp), str(now))
return False
# Is the block being added to the head?
if block.header.prevhash == self.head_hash:
self.state.deletes = []
self.state.changed = {}
apply_block(self.state, block, self.patricia)
self.patricia.to_db()
self.db.put(b'block:%d' % block.header.number, block.header.hash)
self.head_hash = block.header.hash
for i, tx in enumerate(block.transactions):
self.db.put(b'txindex:' +
tx.hash, rlp.encode([block.number, i]))
assert self.get_blockhash_by_number(
block.header.number) == block.header.hash
changed = self.state.changed
# Or is the block being added to a chain that is not currently the
# head?
elif block.header.prevhash in self.env.db:
temp_state = self.mk_poststate_of_blockhash(block.header.prevhash)
try:
databaseLog.info("Block being added to a chain that is not currently the head")
apply_block(temp_state, block)
except (Exception):
databaseLog.error("Exception found while applying block. Returning False")
return False
changed = temp_state.changed
# Block has no parent yet
else:
if block.header.prevhash not in self.parent_queue:
self.parent_queue[block.header.prevhash] = []
self.parent_queue[block.header.prevhash].append(block)
databaseLog.debug("Previous hash not found in parent queue, returning False")
return False
self.add_child(block)
self.db.put('head_hash', self.head_hash)
self.db.put(block.hash, rlp.encode(block))
self.db.put(b'changed:' + block.hash,
b''.join([k.encode() if not isinstance(k, bytes) else k for k in list(changed.keys())]))
databaseLog.info('Adding block: number %d hash %s block timestamp %s number of new transactions %s current machine time %s', \
block.header.number, block.header.hash.encode('HEX'), \
block.header.timestamp, block.transaction_count, int(time.time()))
databaseLog.debug('Saved %d address change logs', len(changed.keys()))
self.db.commit()
if self.new_head_cb and block.header.number != 0:
self.new_head_cb(block)
self.state.block_number = block.header.number
if block.header.hash in self.parent_queue:
for _blk in self.parent_queue[block.header.hash]:
self.add_block(_blk)
del self.parent_queue[block.header.hash]
return True
def __contains__(self, blk):
if isinstance(blk, (str, bytes)):
try:
blk = rlp.decode(self.db.get(blk), Block)
except Exception:
return False
try:
o = self.get_block(self.get_blockhash_by_number(blk.number)).hash
assert o == blk.hash
return True
except Exception:
return False
def has_block(self, block):
return block in self
def has_blockhash(self, blockhash):
return blockhash in self.db
def get_chain(self, frm=None, to=2**63 - 1):
if frm is None:
frm = int(self.db.get('GENESIS_NUMBER')) + 1
chain = []
for i in itertools.islice(itertools.count(), frm, to):
h = self.get_blockhash_by_number(i)
if not h:
return chain
chain.append(self.get_block(h))
# Get block number and transaction index
def get_tx_position(self, tx):
if isinstance(tx,str):
tx = tx.decode("HEX")
elif not isinstance(tx, bytes):
tx = tx.hash
if b'txindex:' + tx in self.db:
data = rlp.decode(self.db.get(b'txindex:' + tx))
return big_endian_to_int(data[0]), big_endian_to_int(data[1])
else:
return None
def get_transaction(self, tx):
blknum, index = self.get_tx_position(tx)
blk = self.get_block_by_number(blknum)
return blk.transactions[index], blk, index
# Get descendants of a block
def get_descendants(self, block):
output = []
blocks = [block]
while len(blocks):
b = blocks.pop()
blocks.extend(self.get_children(b))
output.append(b)
return output
@property
def db(self):
return self.env.db
# Get blockhashes starting from a hash and going backwards
def get_blockhashes_from_hash(self, hash, max):
block = self.get_block(hash)
if block is None:
return []
header = block.header
hashes = []
for i in xrange(max):
hash = header.prevhash
block = self.get_block(hash)
if block is None:
break
header = block.header
hashes.append(header.hash)
if header.number == 0:
break
return hashes
@property
def config(self):
return self.env.config
def get_all_current_addresses(self):
return self.state.list_all_addresses()