Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 53209e7

Browse files
vubvub
authored andcommitted
Initial py3 compatibility fixes (NOT comprehensive!)
1 parent 3f87f0e commit 53209e7

21 files changed

+104
-95
lines changed

ethereum/block.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from ethereum import trie
77
from ethereum.trie import Trie
88
from ethereum.securetrie import SecureTrie
9-
from config import default_config
9+
from ethereum.config import default_config
1010
from ethereum.transactions import Transaction
11-
from db import BaseDB
11+
from ethereum.db import BaseDB
1212
import sys
1313
if sys.version_info.major == 2:
1414
from repoze.lru import lru_cache

ethereum/block_creation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def add_transactions(state, block, txqueue, min_gasprice=0):
2626
apply_transaction(state, tx)
2727
block.transactions.append(tx)
2828
except (InsufficientBalance, BlockGasLimitReached, InsufficientStartGas,
29-
InvalidNonce, UnsignedTransaction), e:
29+
InvalidNonce, UnsignedTransaction) as e:
3030
pass
3131
log.info('Added %d transactions' % (len(block.transactions) - pre_txs))
3232

ethereum/chain.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ def __init__(self, genesis=None, env=None, coinbase=b'\x00' * 20, \
2727
# Initialize the state
2828
if 'head_hash' in self.db:
2929
self.state = self.mk_poststate_of_blockhash(self.db.get('head_hash'))
30-
print 'Initializing chain from saved head, #%d (%s)' % \
31-
(self.state.prev_headers[0].number, encode_hex(self.state.prev_headers[0].hash))
30+
print('Initializing chain from saved head, #%d (%s)' % \
31+
(self.state.prev_headers[0].number, encode_hex(self.state.prev_headers[0].hash)))
3232
elif genesis is None:
3333
raise Exception("Need genesis decl!")
3434
elif isinstance(genesis, State):
3535
self.state = genesis
36-
print 'Initializing chain from provided state'
36+
print('Initializing chain from provided state')
3737
elif "extraData" in genesis:
3838
self.state = parse_genesis_declaration.state_from_genesis_declaration(
3939
genesis, self.env)
40-
print 'Initializing chain from provided genesis declaration'
40+
print('Initializing chain from provided genesis declaration')
4141
elif "prev_headers" in genesis:
4242
self.state = State.from_snapshot(genesis, self.env)
43-
print 'Initializing chain from provided state snapshot, %d (%s)' % \
44-
(self.state.block_number, encode_hex(self.state.prev_headers[0].hash[:8]))
43+
print('Initializing chain from provided state snapshot, %d (%s)' % \
44+
(self.state.block_number, encode_hex(self.state.prev_headers[0].hash[:8])))
4545
else:
46-
print 'Initializing chain from new state based on alloc'
46+
print('Initializing chain from new state based on alloc')
4747
self.state = parse_genesis_declaration.mk_basic_state(genesis, {
4848
"number": kwargs.get('number', 0),
4949
"gas_limit": kwargs.get('gas_limit', 4712388),
@@ -230,7 +230,7 @@ def add_block(self, block):
230230
log.info('Adding to head', head=encode_hex(block.header.prevhash))
231231
try:
232232
apply_block(self.state, block)
233-
except (KeyError, ValueError), e: # FIXME add relevant exceptions here
233+
except (KeyError, ValueError) as e: # FIXME add relevant exceptions here
234234
log.info('Block %s with parent %s invalid, reason: %s' % (encode_hex(block.header.hash), encode_hex(block.header.prevhash), e))
235235
return False
236236
self.db.put('block:' + str(block.header.number), block.header.hash)

ethereum/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
DAO_WITHDRAWER = utils.normalize_address('0xbf4ed7b27f1d666546e30d74d50d173d20bca754'),
6464
# Anti-DoS fork
6565
ANTI_DOS_FORK_BLKNUM = 2463000,
66-
CLEARING_FORK_BLKNUM = 2**98,
66+
CLEARING_FORK_BLKNUM = 3500000,
6767
# Default consensus strategy: ethash, poa, casper, pbft
6868
CONSENSUS_STRATEGY = 'ethash',
6969
# Serenity fork

ethereum/opcodes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
0x07: ['SMOD', 2, 1, 5],
1212
0x08: ['ADDMOD', 3, 1, 8],
1313
0x09: ['MULMOD', 3, 1, 8],
14-
0x0a: ['EXP', 2, 1, 10], # soon 80
14+
0x0a: ['EXP', 2, 1, 10],
1515
0x0b: ['SIGNEXTEND', 2, 1, 5],
1616
0x10: ['LT', 2, 1, 3],
1717
0x11: ['GT', 2, 1, 3],
@@ -132,4 +132,4 @@
132132
CALL_CHILD_LIMIT_NUM = 63
133133
CALL_CHILD_LIMIT_DENOM = 64
134134
SUICIDE_SUPPLEMENTAL_GAS = 5000
135-
EXP_SUPPLEMENTAL_GAS = 70
135+
EXP_SUPPLEMENTAL_GAS = 40

ethereum/processblock.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from ethereum import bloom
99
from ethereum import vm as vm
1010
from ethereum.utils import safe_ord, normalize_address, mk_contract_address, \
11-
mk_metropolis_contract_address, int_to_addr, big_endian_to_int
11+
mk_metropolis_contract_address, int_to_addr, big_endian_to_int, encode_hex
1212
from ethereum.exceptions import InvalidNonce, InsufficientStartGas, UnsignedTransaction, \
1313
BlockGasLimitReached, InsufficientBalance
1414
from ethereum import transactions
@@ -142,6 +142,7 @@ def create_contract(ext, msg):
142142
# assert not ext.get_code(msg.to)
143143
msg.data = vm.CallData([], 0, 0)
144144
snapshot = ext.snapshot()
145+
ext.set_nonce(msg.to, 1)
145146
res, gas, dat = _apply_msg(ext, msg, code)
146147
assert utils.is_numeric(gas)
147148
log_msg.debug('CONTRACT CREATION FINISHED', res=res, gas=gas, dat=dat)
@@ -159,7 +160,8 @@ def create_contract(ext, msg):
159160
ext.revert(snapshot)
160161
return 0, 0, b''
161162
ext.set_code(msg.to, b''.join(map(ascii_chr, dat)))
162-
log_msg.debug('SETTING CODE', addr=msg.to.encode('hex'))
163+
log_msg.debug('SETTING CODE', addr=encode_hex(msg.to))
163164
return 1, gas, msg.to
164165
else:
166+
ext.revert(snapshot)
165167
return 0, gas, b''

ethereum/state.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
from ethereum.utils import normalize_address, hash32, trie_root, \
33
big_endian_int, address, int256, encode_hex, encode_int, \
44
big_endian_to_int, int_to_addr, zpad, parse_as_bin, parse_as_int, \
5-
decode_hex, sha3
5+
decode_hex, sha3, is_string, is_numeric
66
from rlp.sedes import big_endian_int, Binary, binary, CountableList
77
from ethereum import utils
88
from ethereum import trie
99
from ethereum.trie import Trie
1010
from ethereum.securetrie import SecureTrie
11-
from config import default_config, Env
11+
from ethereum.config import default_config, Env
1212
from ethereum.block import FakeHeader
13-
from db import BaseDB, EphemDB, OverlayDB
13+
from ethereum.db import BaseDB, EphemDB, OverlayDB
1414
import copy
1515
import sys
1616
if sys.version_info.major == 2:
@@ -33,9 +33,9 @@ def get_block(db, blockhash):
3333

3434

3535
def snapshot_form(val):
36-
if isinstance(val, (int, long)):
36+
if is_numeric(val):
3737
return str(val)
38-
elif isinstance(val, (str, bytes)):
38+
elif is_string(val):
3939
return '0x' + encode_hex(val)
4040

4141

@@ -59,7 +59,7 @@ def snapshot_form(val):
5959

6060
class State():
6161

62-
def __init__(self, root='', env=Env(), **kwargs):
62+
def __init__(self, root=b'', env=Env(), **kwargs):
6363
self.env = env
6464
self.trie = SecureTrie(Trie(self.db, root))
6565
for k, v in STATE_DEFAULTS.items():
@@ -96,19 +96,19 @@ def add_block_header(self, block_header):
9696

9797
def typecheck_storage(self, k, v):
9898
if k == 'nonce' or k == 'balance':
99-
assert isinstance(v, (int, long))
99+
assert is_numeric(v)
100100
elif k == 'code':
101-
assert isinstance(v, (str, bytes))
101+
assert is_string(v)
102102
elif k == 'storage':
103-
assert isinstance(v, (str, bytes)) and len(v) == 32
103+
assert is_string(v) and len(v) == 32
104104
elif k == 'deleted':
105105
assert isinstance(v, bool)
106106
else:
107-
assert isinstance(v, (str, bytes))
107+
assert is_string(v)
108108
return True
109109

110110
def set_storage(self, addr, k, v):
111-
if isinstance(k, (int, long)):
111+
if is_numeric(k):
112112
k = zpad(encode_int(k), 32)
113113
self.typecheck_storage(k, v)
114114
addr = normalize_address(addr)
@@ -143,7 +143,7 @@ def _get_account_unsafe(self, addr):
143143
return Account.blank_account(self.db, self.config['ACCOUNT_INITIAL_NONCE'])
144144

145145
def get_storage(self, addr, k):
146-
if isinstance(k, (int, long)):
146+
if is_numeric(k):
147147
k = zpad(encode_int(k), 32)
148148
addr = normalize_address(addr)
149149
if addr not in self.cache:
@@ -195,7 +195,7 @@ def get_storage_data (self, addr, k):
195195

196196
# set_storage_data = lambda self, addr, k, v: self.set_storage(addr, k, encode_int(v) if isinstance(v, (int, long)) else v)
197197
def set_storage_data (self, addr, k, v):
198-
self.set_storage(addr, k, encode_int(v) if isinstance(v, (int, long)) and k not in ACCOUNT_SPECIAL_PARAMS else v)
198+
self.set_storage(addr, k, encode_int(v) if is_numeric(v) and k not in ACCOUNT_SPECIAL_PARAMS else v)
199199

200200
def account_exists(self, addr):
201201
if self.is_CLEARING():
@@ -221,7 +221,7 @@ def reset_storage(self, addr):
221221
self.set_storage(addr, k, '')
222222

223223
# Commit the cache to the trie
224-
def commit(self):
224+
def commit(self, allow_empties=False):
225225
rt = self.trie.root_hash
226226
for addr, subcache in self.cache.items():
227227
if addr not in self.modified:
@@ -251,7 +251,8 @@ def commit(self):
251251
if addr in self.modified or True:
252252
if not acct.deleted:
253253
acct._cached_rlp = None
254-
if self.is_CLEARING() and acct.is_blank():
254+
# print 'moose', addr.encode('hex'), self.is_CLEARING(), acct.is_blank(), acct.nonce, acct.balance, repr(acct.code)
255+
if self.is_CLEARING() and acct.is_blank() and not allow_empties:
255256
self.trie.delete(addr)
256257
else:
257258
self.trie.update(addr, rlp.encode(acct))
@@ -372,9 +373,9 @@ def from_snapshot(cls, snapshot_data, env):
372373
for k, default in STATE_DEFAULTS.items():
373374
default = copy.copy(default)
374375
v = snapshot_data[k] if k in snapshot_data else None
375-
if isinstance(default, (int, long)):
376+
if is_numeric(default):
376377
setattr(state, k, parse_as_int(v) if k in snapshot_data else default)
377-
elif isinstance(default, (str, bytes)):
378+
elif is_string(default):
378379
setattr(state, k, parse_as_bin(v) if k in snapshot_data else default)
379380
elif k == 'prev_headers':
380381
if k in snapshot_data:
@@ -409,7 +410,7 @@ def to_snapshot(self, root_only=False, no_prevblocks=False):
409410
for k, default in STATE_DEFAULTS.items():
410411
default = copy.copy(default)
411412
v = getattr(self, k)
412-
if isinstance(default, (int, long)):
413+
if is_numeric(default):
413414
snapshot[k] = str(v)
414415
elif isinstance(default, (str, bytes)):
415416
snapshot[k] = '0x'+encode_hex(v)
@@ -543,4 +544,4 @@ def blank_account(cls, db, initial_nonce=0):
543544
return o
544545

545546
def is_blank(self):
546-
return self.nonce == 0 and self.balance == 0 and self.storage == trie.BLANK_ROOT and self.code_hash == BLANK_HASH
547+
return self.nonce == 0 and self.balance == 0 and self.code_hash == BLANK_HASH

ethereum/state_transition.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
from ethereum.consensus_strategy import get_consensus_strategy
2121
from ethereum import vm
2222
from ethereum.specials import specials as default_specials
23-
from config import Env, default_config
24-
from db import BaseDB, EphemDB
23+
from ethereum.config import Env, default_config
24+
from ethereum.db import BaseDB, EphemDB
2525
from ethereum.exceptions import InvalidNonce, InsufficientStartGas, UnsignedTransaction, \
2626
BlockGasLimitReached, InsufficientBalance, VerificationFailed
2727
import sys
@@ -119,7 +119,7 @@ def apply_block(state, block):
119119
assert verify_execution_results(state, block)
120120
# Post-sealing finalization steps
121121
post_seal_finalize(state, block)
122-
except Exception, e:
122+
except Exception as e:
123123
state.revert(snapshot)
124124
raise ValueError(str(e))
125125
return state

ethereum/tester.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from ethereum.config import Env
1414
from ethereum.slogging import LogRecorder
1515
from ethereum._solidity import get_solidity
16-
from ethereum.utils import to_string, sha3, privtoaddr, int_to_addr
16+
from ethereum.utils import to_string, sha3, privtoaddr, int_to_addr, encode_hex
1717
from ethereum import parse_genesis_declaration, state_transition
1818
from ethereum.state import State
1919

@@ -176,16 +176,16 @@ def __init__(self2):
176176

177177
for i in range(num_accounts):
178178
account = accounts[i]
179-
initial_balances[account.encode('hex')] = {'wei': str(10**24)}
179+
initial_balances[encode_hex(account)] = {'wei': str(10**24)}
180180

181181
for i in range(1, 5):
182182
address = int_to_addr(i)
183-
initial_balances[address.encode('hex')] = {'wei': "1"}
183+
initial_balances[encode_hex(address)] = {'wei': "1"}
184184

185185
self.state = State.from_snapshot({
186186
"alloc": initial_balances,
187187
"timestamp": "1410973349",
188-
"coinbase": DEFAULT_ACCOUNT.encode('hex'),
188+
"coinbase": encode_hex(DEFAULT_ACCOUNT),
189189
"gas_limit": "1000000000"
190190
}, self.env)
191191

ethereum/tests/test_blocks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def run_block_test(params, config_overrides = {}):
8282
success = c.add_block(rlp.decode(rlpdata, Block))
8383
except (ValueError, TypeError, AttributeError, VerificationFailed,
8484
DecodingError, DeserializationError, InvalidTransaction,
85-
InvalidNonce, KeyError), e:
85+
InvalidNonce, KeyError) as e:
8686
success = False
8787
assert not success
8888
else:

0 commit comments

Comments
 (0)