9
9
import rlp
10
10
from rlp .utils import encode_hex
11
11
from ethereum .exceptions import InvalidNonce , InsufficientStartGas , UnsignedTransaction , \
12
- BlockGasLimitReached , InsufficientBalance
12
+ BlockGasLimitReached , InsufficientBalance , InvalidTransaction , VerificationFailed
13
13
from ethereum .slogging import get_logger
14
14
from ethereum .config import Env
15
15
from ethereum .state import State , dict_to_prev_header
20
20
import json
21
21
log = get_logger ('eth.chain' )
22
22
23
+ from ethereum .slogging import LogRecorder , configure_logging , set_level
24
+ config_string = ':info,eth.chain:debug'
25
+ #config_string = ':info,eth.vm.log:trace,eth.vm.op:trace,eth.vm.stack:trace,eth.vm.exit:trace,eth.pb.msg:trace,eth.pb.tx:debug'
26
+ configure_logging (config_string = config_string )
27
+
23
28
24
29
class Chain (object ):
25
30
26
31
def __init__ (self , genesis = None , env = None , coinbase = b'\x00 ' * 20 , \
27
- new_head_cb = None , ** kwargs ):
32
+ new_head_cb = None , localtime = None , ** kwargs ):
28
33
self .env = env or Env ()
29
34
# Initialize the state
30
35
if 'head_hash' in self .db :
@@ -77,6 +82,7 @@ def __init__(self, genesis=None, env=None, coinbase=b'\x00' * 20, \
77
82
self .extra_data = 'moo ha ha says the laughing cow.'
78
83
self .time_queue = []
79
84
self .parent_queue = {}
85
+ self .localtime = time .time () if localtime is None else localtime
80
86
81
87
@property
82
88
def head (self ):
@@ -121,7 +127,7 @@ def mk_poststate_of_blockhash(self, blockhash):
121
127
for h in jsondata ["prev_headers" ][:header_depth - i ]:
122
128
state .prev_headers .append (dict_to_prev_header (h ))
123
129
for blknum , uncles in jsondata ["recent_uncles" ].items ():
124
- if blknum >= state .block_number - state .config ['MAX_UNCLE_DEPTH' ]:
130
+ if int ( blknum ) >= state .block_number - int ( state .config ['MAX_UNCLE_DEPTH' ]) :
125
131
state .recent_uncles [blknum ] = [parse_as_bin (u ) for u in uncles ]
126
132
else :
127
133
raise Exception ("Dangling prevhash" )
@@ -201,10 +207,10 @@ def get_score(self, block):
201
207
# process blocks that were received but laid aside because
202
208
# either the parent was missing or they were received
203
209
# too early
204
- def process_time_queue (self ):
205
- now = self .time ()
210
+ def process_time_queue (self , new_time = None ):
211
+ self . localtime = time .time () if new_time is None else new_time
206
212
i = 0
207
- while i < len (self .time_queue ) and self .time_queue [i ].timestamp <= now :
213
+ while i < len (self .time_queue ) and self .time_queue [i ].timestamp <= new_time :
208
214
log .info ('Adding scheduled block' )
209
215
pre_len = len (self .time_queue )
210
216
self .add_block (self .time_queue .pop (i ))
@@ -218,12 +224,9 @@ def process_parent_queue(self):
218
224
self .add_block (block )
219
225
del self .parent_queue [parent_hash ]
220
226
221
- def time (self ):
222
- return int (time .time ())
223
-
224
227
# Call upon receiving a block
225
228
def add_block (self , block ):
226
- now = self .time ()
229
+ now = self .localtime
227
230
if block .header .timestamp > now :
228
231
i = 0
229
232
while i < len (self .time_queue ) and block .timestamp > self .time_queue [i ].timestamp :
@@ -236,7 +239,7 @@ def add_block(self, block):
236
239
log .info ('Adding to head' , head = encode_hex (block .header .prevhash ))
237
240
try :
238
241
apply_block (self .state , block )
239
- except (KeyError , ValueError ) as e : # FIXME add relevant exceptions here
242
+ except (AssertionError , KeyError , ValueError , InvalidTransaction , VerificationFailed ) as e : # FIXME add relevant exceptions here
240
243
log .info ('Block %s with parent %s invalid, reason: %s' % (encode_hex (block .header .hash ), encode_hex (block .header .prevhash ), e ))
241
244
return False
242
245
self .db .put ('block:' + str (block .header .number ), block .header .hash )
@@ -250,7 +253,7 @@ def add_block(self, block):
250
253
temp_state = self .mk_poststate_of_blockhash (block .header .prevhash )
251
254
try :
252
255
apply_block (temp_state , block )
253
- except (KeyError , ValueError ) as e : # FIXME add relevant exceptions here
256
+ except (AssertionError , KeyError , ValueError , InvalidTransaction , VerificationFailed ) as e : # FIXME add relevant exceptions here
254
257
log .info ('Block %s with parent %s invalid, reason: %s' % (encode_hex (block .header .hash ), encode_hex (block .header .prevhash ), e ))
255
258
return False
256
259
self .db .put (b'state:' + block .header .hash , temp_state .trie .root_hash )
0 commit comments