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

Commit 6658f70

Browse files
vubvub
authored andcommitted
Merge branch 'state_revamp' of github.com:ethereum/pyethereum into state_revamp
2 parents dc77771 + 0f41cfd commit 6658f70

File tree

4 files changed

+33
-32
lines changed

4 files changed

+33
-32
lines changed

ethereum/chain.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import time
2+
import itertools
23
from ethereum import utils
34
from ethereum.utils import parse_as_bin, big_endian_to_int
45
from ethereum import parse_genesis_declaration
@@ -59,11 +60,11 @@ def __init__(self, genesis=None, env=None, coinbase=b'\x00' * 20, \
5960

6061
self.head_hash = self.state.prev_headers[0].hash
6162
self.genesis = Block(self.state.prev_headers[0], [], [])
62-
self.db.put('state:'+self.head_hash, self.state.trie.root_hash)
63+
self.db.put(b'state:' + self.head_hash, self.state.trie.root_hash)
6364
self.db.put('GENESIS_NUMBER', str(self.state.block_number))
64-
self.db.put('GENESIS_HASH', str(self.state.prev_headers[0].hash))
65+
self.db.put('GENESIS_HASH', self.state.prev_headers[0].hash)
6566
assert self.state.block_number == self.state.prev_headers[0].number
66-
self.db.put('score:' + self.state.prev_headers[0].hash, "0")
67+
self.db.put(b'score:' + self.state.prev_headers[0].hash, "0")
6768
self.db.put('GENESIS_STATE', json.dumps(self.state.to_snapshot()))
6869
self.db.put(self.head_hash, 'GENESIS')
6970
self.min_gasprice = kwargs.get('min_gasprice', 5 * 10**9)
@@ -90,7 +91,7 @@ def mk_poststate_of_blockhash(self, blockhash):
9091
if self.db.get(blockhash) == 'GENESIS':
9192
return State.from_snapshot(json.loads(self.db.get('GENESIS_STATE')), self.env)
9293
state = State(env=self.env)
93-
state.trie.root_hash = self.db.get('state:'+blockhash)
94+
state.trie.root_hash = self.db.get(b'state:' + blockhash)
9495
block = rlp.decode(self.db.get(blockhash), Block)
9596
update_block_env_variables(state, block)
9697
state.gas_used = block.header.gas_used
@@ -141,14 +142,14 @@ def get_block(self, blockhash):
141142
# parent hash and see that it is one of its children
142143
def add_child(self, child):
143144
try:
144-
existing = self.db.get('child:' + child.header.prevhash)
145+
existing = self.db.get(b'child:' + child.header.prevhash)
145146
except:
146-
existing = ''
147+
existing = b''
147148
existing_hashes = []
148149
for i in range(0, len(existing), 32):
149150
existing_hashes.append(existing[i: i+32])
150151
if child.header.hash not in existing_hashes:
151-
self.db.put('child:' + child.header.prevhash, existing + child.header.hash)
152+
self.db.put(b'child:' + child.header.prevhash, existing + child.header.hash)
152153

153154
def get_blockhash_by_number(self, number):
154155
try:
@@ -163,7 +164,7 @@ def get_block_by_number(self, number):
163164
def get_child_hashes(self, blockhash):
164165
o = []
165166
try:
166-
data = self.db.get('child:' + blockhash)
167+
data = self.db.get(b'child:' + blockhash)
167168
for i in range(0, len(data), 32):
168169
o.append(data[i:i + 32])
169170
return o
@@ -181,14 +182,14 @@ def get_children(self, block):
181182
def get_score(self, block):
182183
if not block:
183184
return 0
184-
key = 'score:' + block.header.hash
185+
key = b'score:' + block.header.hash
185186
if key not in self.db:
186187
try:
187188
parent_score = self.get_score(self.get_parent(block))
188189
self.db.put(key, str(parent_score + block.difficulty +
189190
random.randrange(block.difficulty // 10**6 + 1)))
190191
except:
191-
return int(self.db.get('score:' + block.prevhash))
192+
return int(self.db.get(b'score:' + block.prevhash))
192193
return int(self.db.get(key))
193194

194195
# These two functions should be called periodically so as to
@@ -234,20 +235,20 @@ def add_block(self, block):
234235
log.info('Block %s with parent %s invalid, reason: %s' % (encode_hex(block.header.hash), encode_hex(block.header.prevhash), e))
235236
return False
236237
self.db.put('block:' + str(block.header.number), block.header.hash)
237-
self.db.put('state:' + block.header.hash, self.state.trie.root_hash)
238+
self.db.put(b'state:' + block.header.hash, self.state.trie.root_hash)
238239
self.head_hash = block.header.hash
239240
for i, tx in enumerate(block.transactions):
240-
self.db.put('txindex:' + tx.hash, rlp.encode([block.number, i]))
241+
self.db.put(b'txindex:' + tx.hash, rlp.encode([block.number, i]))
241242
elif block.header.prevhash in self.env.db:
242243
log.info('Receiving block not on head, adding to secondary post state',
243244
prevhash=encode_hex(block.header.prevhash))
244245
temp_state = self.mk_poststate_of_blockhash(block.header.prevhash)
245246
try:
246247
apply_block(temp_state, block)
247-
except (KeyError, ValueError), e: # FIXME add relevant exceptions here
248+
except (KeyError, ValueError) as e: # FIXME add relevant exceptions here
248249
log.info('Block %s with parent %s invalid, reason: %s' % (encode_hex(block.header.hash), encode_hex(block.header.prevhash), e))
249250
return False
250-
self.db.put('state:' + block.header.hash, temp_state.trie.root_hash)
251+
self.db.put(b'state:' + block.header.hash, temp_state.trie.root_hash)
251252
block_score = self.get_score(block)
252253
# Replace the head
253254
if block_score > self.get_score(self.head):
@@ -263,21 +264,21 @@ def add_block(self, block):
263264
break
264265
b = self.get_parent(b)
265266
replace_from = b.header.number
266-
for i in xrange(replace_from, 2**63 - 1):
267+
for i in itertools.count(replace_from):
267268
log.info('Rewriting height %d' % i)
268269
key = 'block:' + str(i)
269270
orig_at_height = self.db.get(key) if key in self.db else None
270271
if orig_at_height:
271272
self.db.delete(key)
272273
orig_block_at_height = self.get_block(orig_at_height)
273274
for tx in orig_block_at_height.transactions:
274-
if 'txindex:' + tx.hash in self.db:
275-
self.db.delete('txindex:' + tx.hash)
275+
if b'txindex:' + tx.hash in self.db:
276+
self.db.delete(b'txindex:' + tx.hash)
276277
if i in new_chain:
277278
new_block_at_height = new_chain[i]
278279
self.db.put(key, new_block_at_height.header.hash)
279280
for i, tx in enumerate(new_block_at_height.transactions):
280-
self.db.put('txindex:' + tx.hash,
281+
self.db.put(b'txindex:' + tx.hash,
281282
rlp.encode([new_block_at_height.number, i]))
282283
if i not in new_chain and not orig_at_height:
283284
break
@@ -324,7 +325,7 @@ def get_chain(self, frm=None, to=2**63 - 1):
324325
if frm is None:
325326
frm = int(self.db.get('GENESIS_NUMBER')) + 1
326327
chain = []
327-
for i in xrange(frm, to):
328+
for i in itertools.islice(itertools.count(), frm, to):
328329
h = self.get_blockhash_by_number(i)
329330
if not h:
330331
return chain
@@ -334,8 +335,8 @@ def get_chain(self, frm=None, to=2**63 - 1):
334335
def get_transaction(self, tx):
335336
if not isinstance(tx, (str, bytes)):
336337
tx = tx.hash
337-
if 'txindex:' + tx in self.db:
338-
data = rlp.decode(self.db.get('txindex:' + tx))
338+
if b'txindex:' + tx in self.db:
339+
data = rlp.decode(self.db.get(b'txindex:' + tx))
339340
blk, index = self.get_block_by_number(
340341
big_endian_to_int(data[0])), big_endian_to_int(data[1])
341342
tx = blk.transactions[index]

ethereum/consensus_strategy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def __init__(self, header_check, header_validate, uncle_validate, block_setup, b
1010

1111
def get_consensus_strategy(config):
1212
if config['CONSENSUS_STRATEGY'] in ('pow', 'ethpow', 'ethash', 'ethereum1'):
13-
from ethpow_utils import ethereum1_check_header, ethereum1_validate_header, \
13+
from ethereum.ethpow_utils import ethereum1_check_header, ethereum1_validate_header, \
1414
ethereum1_validate_uncle, ethereum1_pre_finalize_block, \
1515
ethereum1_post_finalize_block, ethereum1_setup_block
1616
return ConsensusStrategy(
@@ -23,7 +23,7 @@ def get_consensus_strategy(config):
2323
state_initialize=None
2424
)
2525
elif config['CONSENSUS_STRATEGY'] == 'casper':
26-
from casper_utils import casper_validate_header, casper_state_initialize, casper_post_finalize_block, casper_setup_block
26+
from ethereum.casper_utils import casper_validate_header, casper_state_initialize, casper_post_finalize_block, casper_setup_block
2727
return ConsensusStrategy(
2828
header_check=None,
2929
header_validate=casper_validate_header,

ethereum/state_transition.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ def apply_message(state, msg=None, **kwargs):
214214
msg = vm.Message(**kwargs)
215215
else:
216216
assert not kwargs
217-
ext = VMExt(state, transactions.Transaction(0, 0, 21000, '', 0, ''))
217+
ext = VMExt(state, transactions.Transaction(0, 0, 21000, b'', 0, b''))
218218
result, gas_remained, data = apply_msg(ext, msg)
219-
return ''.join(map(chr, data)) if result else None
219+
return b''.join(map(lambda d: bytes([d]), data)) if result else None
220220

221221

222222
def apply_transaction(state, tx):
@@ -464,7 +464,7 @@ def __init__(self, state):
464464
for k, v in state.config['CUSTOM_SPECIALS']:
465465
self.specials[k] = v
466466
self._state = state
467-
self.get_code = lambda addr: ''
467+
self.get_code = lambda addr: b''
468468
self.set_code = lambda addr, code: None
469469
self.get_balance = lambda addr: 0
470470
self.set_balance = lambda addr, value: None
@@ -485,10 +485,10 @@ def __init__(self, state):
485485
self.block_difficulty = 0
486486
self.block_gas_limit = 0
487487
self.log = lambda addr, topics, data: None
488-
self.create = lambda msg: 0, 0, ''
488+
self.create = lambda msg: 0, 0, b''
489489
self.msg = lambda msg: _apply_msg(
490-
self, msg, '') if msg.code_address in self.specials else (0, 0, '')
491-
self.blackbox_msg = lambda msg, code: 0, 0, ''
490+
self, msg, '') if msg.code_address in self.specials else (0, 0, b'')
491+
self.blackbox_msg = lambda msg, code: 0, 0, b''
492492
self.account_exists = lambda addr: False
493493
self.post_homestead_hardfork = lambda: state.is_HOMESTEAD()
494494
self.post_metropolis_hardfork = lambda: state.is_METROPOLIS()

ethereum/tests/test_chain.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@ def test_invalid_transaction(db):
212212
def test_prevhash(db):
213213
chain = Chain({}, difficulty=1, min_gasprice=0)
214214
L1 = mine_on_chain(chain)
215-
assert chain.state.get_block_hash(0) != '\x00'*32
216-
assert chain.state.get_block_hash(1) != '\x00'*32
217-
assert chain.state.get_block_hash(2) == '\x00'*32
215+
assert chain.state.get_block_hash(0) != b'\x00'*32
216+
assert chain.state.get_block_hash(1) != b'\x00'*32
217+
assert chain.state.get_block_hash(2) == b'\x00'*32
218218

219219

220220
def test_genesis_chain(db):

0 commit comments

Comments
 (0)