Skip to content

Commit

Permalink
Merge branch 'develop' into pr_process_time_queue
Browse files Browse the repository at this point in the history
  • Loading branch information
vbuterin authored Oct 19, 2017
2 parents d4e19af + 32c1ca3 commit a7573a0
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 80 deletions.
2 changes: 1 addition & 1 deletion ethereum/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def transaction_count(self):

class FakeHeader():

def __init__(self, hash='\x00' * 32, number=0, timestamp=0, difficulty=1,
def __init__(self, hash=b'\x00' * 32, number=0, timestamp=0, difficulty=1,
gas_limit=3141592, gas_used=0, uncles_hash=BLANK_UNCLES_HASH):
self.hash = hash
self.number = number
Expand Down
18 changes: 9 additions & 9 deletions ethereum/experimental/refcount_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def get(self, k):
# deathrow record. Also delete old journals.
def cleanup(self, epoch):
try:
death_row_node = self.db.get('deathrow:' + str(epoch))
death_row_node = self.db.get(b'deathrow:' + utils.to_string(epoch))
except BaseException:
death_row_node = rlp.encode([])
death_row_nodes = rlp.decode(death_row_node)
Expand All @@ -97,12 +97,12 @@ def cleanup(self, epoch):
sys.stderr.write('%d nodes successfully pruned\n' % pruned)
# Delete the deathrow after processing it
try:
self.db.delete('deathrow:' + str(epoch))
self.db.delete(b'deathrow:' + utils.to_string(epoch))
except BaseException:
pass
# Delete journals that are too old
try:
self.db.delete('journal:' + str(epoch - self.ttl))
self.db.delete(b'journal:' + utils.to_string(epoch - self.ttl))
except BaseException:
pass

Expand All @@ -114,7 +114,7 @@ def commit_refcount_changes(self, epoch):
death_row_nodes = rlp.decode(
self.db.get(
'deathrow:' +
str(timeout_epoch)))
utils.to_string(timeout_epoch)))
except BaseException:
death_row_nodes = []
for nodekey in self.death_row:
Expand All @@ -128,28 +128,28 @@ def commit_refcount_changes(self, epoch):
(len(self.death_row), timeout_epoch))
death_row_nodes.extend(self.death_row)
self.death_row = []
self.db.put('deathrow:' + str(timeout_epoch),
self.db.put(b'deathrow:' + utils.to_string(timeout_epoch),
rlp.encode(death_row_nodes))
# Save journal
try:
journal = rlp.decode(self.db.get('journal:' + str(epoch)))
journal = rlp.decode(self.db.get(b'journal:' + utils.to_string(epoch)))
except BaseException:
journal = []
journal.extend(self.journal)
self.journal = []
self.db.put('journal:' + str(epoch), rlp.encode(journal))
self.db.put(b'journal:' + utils.to_string(epoch), rlp.encode(journal))

# Revert changes made during an epoch
def revert_refcount_changes(self, epoch):
timeout_epoch = epoch + self.ttl
# Delete death row additions
try:
self.db.delete('deathrow:' + str(timeout_epoch))
self.db.delete(b'deathrow:' + utils.to_string(timeout_epoch))
except BaseException:
pass
# Revert journal changes
try:
journal = rlp.decode(self.db.get('journal:' + str(epoch)))
journal = rlp.decode(self.db.get(b'journal:' + utils.to_string(epoch)))
for new_refcount, hashkey in journal[::-1]:
node_object = rlp.decode(self.db.get(b'r:' + hashkey))
self.db.put(b'r:' + hashkey,
Expand Down
11 changes: 8 additions & 3 deletions ethereum/fast_rlp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import sys
import rlp
from .utils import int_to_big_endian, big_endian_to_int, safe_ord
from . import db
from ethereum.utils import (
int_to_big_endian,
big_endian_to_int,
safe_ord,
to_string,
)
from ethereum import db


def _encode_optimized(item):
Expand Down Expand Up @@ -110,7 +115,7 @@ def run():
st = time.time()
x = trie.Trie(db.EphemDB())
for i in range(10000):
x.update(str(i), str(i**3))
x.update(to_string(i), to_string(i**3))
print('elapsed', time.time() - st)
return x.root_hash

Expand Down
2 changes: 1 addition & 1 deletion ethereum/full_casper/casper_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def get_dunkle_candidates(chain, state, scan_limit=10):
descendants = chain.get_descendants(anc)
else:
descendants = chain.get_descendants(
chain.get_block(chain.db.get('GENESIS_HASH')))
chain.get_block(chain.db.get(b'GENESIS_HASH')))
potential_uncles = [
x for x in descendants if x not in chain and isinstance(
x, Block)]
Expand Down
21 changes: 14 additions & 7 deletions ethereum/genesis_helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from ethereum.state import State
from ethereum.block import Block, BlockHeader, BLANK_UNCLES_HASH
from ethereum.utils import decode_hex, big_endian_to_int, encode_hex, \
parse_as_bin, parse_as_int, normalize_address
from ethereum.utils import (
decode_hex,
big_endian_to_int,
encode_hex,
parse_as_bin,
parse_as_int,
normalize_address,
to_string,
)
from ethereum.config import Env
from ethereum.consensus_strategy import get_consensus_strategy
from ethereum.db import OverlayDB, RefcountDB
Expand Down Expand Up @@ -67,14 +74,14 @@ def state_from_genesis_declaration(

def initialize_genesis_keys(state, genesis):
db = state.db
db.put('GENESIS_NUMBER', str(genesis.header.number))
db.put('GENESIS_HASH', str(genesis.header.hash))
db.put('GENESIS_STATE', json.dumps(state.to_snapshot()))
db.put('GENESIS_RLP', rlp.encode(genesis))
db.put(b'GENESIS_NUMBER', to_string(genesis.header.number))
db.put(b'GENESIS_HASH', to_string(genesis.header.hash))
db.put(b'GENESIS_STATE', json.dumps(state.to_snapshot()))
db.put(b'GENESIS_RLP', rlp.encode(genesis))
db.put(b'block:0', genesis.header.hash)
db.put(b'score:' + genesis.header.hash, "0")
db.put(b'state:' + genesis.header.hash, state.trie.root_hash)
db.put(genesis.header.hash, 'GENESIS')
db.put(genesis.header.hash, b'GENESIS')
db.commit()


Expand Down
47 changes: 26 additions & 21 deletions ethereum/hybrid_casper/chain.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from builtins import range
import json
import random
import time
import itertools
from ethereum import utils
from ethereum.utils import parse_as_bin, big_endian_to_int
from ethereum.utils import (
parse_as_bin,
big_endian_to_int,
to_string,
)
from ethereum.hybrid_casper import casper_utils
from ethereum.meta import apply_block
from ethereum.common import update_block_env_variables
Expand Down Expand Up @@ -32,8 +37,8 @@ def __init__(self, genesis=None, env=None, coinbase=b'\x00' * 20,
new_head_cb=None, reset_genesis=False, localtime=None, **kwargs):
self.env = env or Env()
# Initialize the state
if 'head_hash' in self.db: # new head tag
self.state = self.mk_poststate_of_blockhash(self.db.get('head_hash'))
if b'head_hash' in self.db: # new head tag
self.state = self.mk_poststate_of_blockhash(self.db.get(b'head_hash'))
print('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:
Expand Down Expand Up @@ -76,7 +81,7 @@ def __init__(self, genesis=None, env=None, coinbase=b'\x00' * 20,
self.db.put(b'cp_subtree_score' + b'\x00' * 32, 2/3.)
self.commit_logs = []
self.casper_address = self.config['CASPER_ADDRESS']
self.db.put('GENESIS_NUMBER', str(self.state.block_number))
self.db.put(b'GENESIS_NUMBER', to_string(self.state.block_number))
assert self.state.block_number == self.state.prev_headers[0].number
if reset_genesis:
self.genesis = Block(self.state.prev_headers[0], [], [])
Expand All @@ -95,7 +100,7 @@ def __init__(self, genesis=None, env=None, coinbase=b'\x00' * 20,
def head(self):
try:
block_rlp = self.db.get(self.head_hash)
if block_rlp == 'GENESIS':
if block_rlp == b'GENESIS':
return self.genesis
else:
return rlp.decode(block_rlp, Block)
Expand Down Expand Up @@ -248,7 +253,7 @@ def should_add_block(self, block):
def add_block_to_head(self, block):
log.info('Adding to head', head=encode_hex(block.header.prevhash))
apply_block(self.state, block)
self.db.put('block:' + str(block.header.number), block.header.hash)
self.db.put(b'block:' + to_string(block.header.number), block.header.hash)
self.get_pow_difficulty(block) # side effect: put 'score:' cache in db
self.head_hash = block.header.hash
for i, tx in enumerate(block.transactions):
Expand All @@ -264,7 +269,7 @@ def set_head(self, block):
log.info('Receiving block not on head, adding to secondary post state',
prevhash=encode_hex(block.header.prevhash))
self.reorganize_head_to(block)
self.db.put('head_hash', self.head_hash)
self.db.put(b'head_hash', self.head_hash)
self.db.commit()
log.info('Reorganizing chain to block %d (%s) with %d txs and %d gas' %
(block.header.number, encode_hex(block.header.hash)[:8],
Expand Down Expand Up @@ -341,19 +346,19 @@ def reorganize_head_to(self, block):
log.info('Replacing head')
b = block
new_chain = {}
while b.header.number >= int(self.db.get('GENESIS_NUMBER')):
while b.header.number >= int(self.db.get(b'GENESIS_NUMBER')):
new_chain[b.header.number] = b
key = 'block:' + str(b.header.number)
key = b'block:' + to_string(b.header.number)
orig_at_height = self.db.get(key) if key in self.db else None
if orig_at_height == b.header.hash:
break
if b.prevhash not in self.db or self.db.get(b.prevhash) == 'GENESIS':
if b.prevhash not in self.db or self.db.get(b.prevhash) == b'GENESIS':
break
b = self.get_parent(b)
replace_from = b.header.number
for i in itertools.count(replace_from):
log.info('Rewriting height %d' % i)
key = 'block:' + str(i)
key = b'block:' + to_string(i)
orig_at_height = self.db.get(key) if key in self.db else None
if orig_at_height:
self.db.delete(key)
Expand Down Expand Up @@ -426,8 +431,8 @@ def mk_poststate_of_blockhash(self, blockhash, convert=False):
raise Exception("Block hash %s not found" % encode_hex(blockhash))

block_rlp = self.db.get(blockhash)
if block_rlp == 'GENESIS':
return State.from_snapshot(json.loads(self.db.get('GENESIS_STATE')), self.env)
if block_rlp == b'GENESIS':
return State.from_snapshot(json.loads(self.db.get(b'GENESIS_STATE')), self.env)
block = rlp.decode(block_rlp, Block)

state = State(env=self.env)
Expand All @@ -450,8 +455,8 @@ def mk_poststate_of_blockhash(self, blockhash, convert=False):
except:
break
if i < header_depth:
if state.db.get(b.header.prevhash) == 'GENESIS':
jsondata = json.loads(state.db.get('GENESIS_STATE'))
if state.db.get(b.header.prevhash) == b'GENESIS':
jsondata = json.loads(state.db.get(b'GENESIS_STATE'))
for h in jsondata["prev_headers"][:header_depth - i]:
state.prev_headers.append(dict_to_prev_header(h))
for blknum, uncles in jsondata["recent_uncles"].items():
Expand All @@ -463,16 +468,16 @@ def mk_poststate_of_blockhash(self, blockhash, convert=False):
return state

def get_parent(self, block):
if block.header.number == int(self.db.get('GENESIS_NUMBER')):
if block.header.number == int(self.db.get(b'GENESIS_NUMBER')):
return None
return self.get_block(block.header.prevhash)

def get_block(self, blockhash):
try:
block_rlp = self.db.get(blockhash)
if block_rlp == 'GENESIS':
if block_rlp == b'GENESIS':
if not hasattr(self, 'genesis'):
self.genesis = rlp.decode(self.db.get('GENESIS_RLP'), sedes=Block)
self.genesis = rlp.decode(self.db.get(b'GENESIS_RLP'), sedes=Block)
return self.genesis
else:
return rlp.decode(block_rlp, Block)
Expand All @@ -495,7 +500,7 @@ def add_child(self, child):

def get_blockhash_by_number(self, number):
try:
return self.db.get('block:' + str(number))
return self.db.get(b'block:' + to_string(number))
except:
return None

Expand Down Expand Up @@ -547,7 +552,7 @@ def has_blockhash(self, blockhash):

def get_chain(self, frm=None, to=2**63 - 1):
if frm is None:
frm = int(self.db.get('GENESIS_NUMBER')) + 1
frm = int(self.db.get(b'GENESIS_NUMBER')) + 1
chain = []
for i in itertools.islice(itertools.count(), frm, to):
h = self.get_blockhash_by_number(i)
Expand Down Expand Up @@ -585,7 +590,7 @@ def get_blockhashes_from_hash(self, hash, max):

header = block.header
hashes = []
for i in xrange(max):
for i in range(max):
hash = header.prevhash
block = self.get_block(hash)
if block is None:
Expand Down
2 changes: 1 addition & 1 deletion ethereum/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def __init__(self, state, tx):
self.revert = state.revert
self.transfer_value = state.transfer_value
self.reset_storage = state.reset_storage
self.tx_origin = tx.sender if tx else '\x00' * 20
self.tx_origin = tx.sender if tx else b'\x00' * 20
self.tx_gasprice = tx.gasprice if tx else 0


Expand Down
2 changes: 1 addition & 1 deletion ethereum/new_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def snapshot_form(val):
"gas_used": 0,
"gas_limit": 3141592,
"block_number": 0,
"block_coinbase": '\x00' * 20,
"block_coinbase": b'\x00' * 20,
"block_difficulty": 1,
"timestamp": 0,
"logs": [],
Expand Down
Loading

0 comments on commit a7573a0

Please sign in to comment.