8
8
from ethereum import bloom
9
9
from ethereum import vm as vm
10
10
from ethereum .exceptions import InvalidNonce , InsufficientStartGas , UnsignedTransaction , \
11
- BlockGasLimitReached , InsufficientBalance
12
- from ethereum .utils import safe_ord , mk_contract_address
11
+ BlockGasLimitReached , InsufficientBalance , VerificationFailed
12
+ from ethereum .utils import safe_ord , normalize_address , mk_contract_address , \
13
+ mk_metropolis_contract_address , big_endian_to_int
13
14
from ethereum import transactions
14
15
import ethereum .config as config
15
16
@@ -37,7 +38,7 @@ def verify(block, parent):
37
38
env = parent .env , parent = parent )
38
39
assert block == block2
39
40
return True
40
- except blocks . VerificationFailed :
41
+ except VerificationFailed :
41
42
return False
42
43
43
44
@@ -81,7 +82,10 @@ def rp(what, actual, target):
81
82
82
83
# (1) The transaction signature is valid;
83
84
if not tx .sender : # sender is set and validated on Transaction initialization
84
- raise UnsignedTransaction (tx )
85
+ if block .number >= config .default_config ["METROPOLIS_FORK_BLKNUM" ]:
86
+ tx ._sender = normalize_address (config .default_config ["METROPOLIS_ENTRY_POINT" ])
87
+ else :
88
+ raise UnsignedTransaction (tx )
85
89
if block .number >= config .default_config ["HOMESTEAD_FORK_BLKNUM" ]:
86
90
tx .check_low_s ()
87
91
@@ -222,6 +226,8 @@ def __init__(self, block, tx):
222
226
self .get_code = block .get_code
223
227
self .get_balance = block .get_balance
224
228
self .set_balance = block .set_balance
229
+ self .get_nonce = block .get_nonce
230
+ self .set_nonce = block .set_nonce
225
231
self .set_storage_data = block .set_storage_data
226
232
self .get_storage_data = block .get_storage_data
227
233
self .log_storage = lambda x : block .account_to_dict (x )['storage' ]
@@ -299,10 +305,21 @@ def create_contract(ext, msg):
299
305
log_msg .debug ('CONTRACT CREATION' )
300
306
#print('CREATING WITH GAS', msg.gas)
301
307
sender = decode_hex (msg .sender ) if len (msg .sender ) == 40 else msg .sender
302
- if ext .tx_origin != msg .sender :
303
- ext ._block .increment_nonce (msg .sender )
304
- nonce = utils .encode_int (ext ._block .get_nonce (msg .sender ) - 1 )
305
- msg .to = mk_contract_address (sender , nonce )
308
+ code = msg .data .extract_all ()
309
+ if ext ._block .number >= ext ._block .config ['METROPOLIS_FORK_BLKNUM' ]:
310
+ msg .to = mk_metropolis_contract_address (msg .sender , code )
311
+ if ext .get_code (msg .to ):
312
+ if ext .get_nonce (msg .to ) >= 2 ** 40 :
313
+ ext .set_nonce (msg .to , (ext .get_nonce (msg .to ) + 1 ) % 2 ** 160 )
314
+ msg .to = normalize_address ((ext .get_nonce (msg .to ) - 1 ) % 2 ** 160 )
315
+ else :
316
+ ext .set_nonce (msg .to , (big_endian_to_int (msg .to ) + 2 ) % 2 ** 160 )
317
+ msg .to = normalize_address ((ext .get_nonce (msg .to ) - 1 ) % 2 ** 160 )
318
+ else :
319
+ if ext .tx_origin != msg .sender :
320
+ ext ._block .increment_nonce (msg .sender )
321
+ nonce = utils .encode_int (ext ._block .get_nonce (msg .sender ) - 1 )
322
+ msg .to = mk_contract_address (sender , nonce )
306
323
b = ext .get_balance (msg .to )
307
324
if b > 0 :
308
325
ext .set_balance (msg .to , b )
@@ -311,7 +328,6 @@ def create_contract(ext, msg):
311
328
ext ._block .reset_storage (msg .to )
312
329
msg .is_create = True
313
330
# assert not ext.get_code(msg.to)
314
- code = msg .data .extract_all ()
315
331
msg .data = vm .CallData ([], 0 , 0 )
316
332
snapshot = ext ._block .snapshot ()
317
333
res , gas , dat = _apply_msg (ext , msg , code )
0 commit comments