Skip to content

Commit

Permalink
print as a function
Browse files Browse the repository at this point in the history
  • Loading branch information
pipermerriam committed Jun 20, 2016
1 parent 724ca05 commit 81e4304
Show file tree
Hide file tree
Showing 17 changed files with 56 additions and 57 deletions.
2 changes: 1 addition & 1 deletion ethereum/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def calc_difficulty(parent, timestamp):
period_count = (parent.number + 1) // config['EXPDIFF_PERIOD']
if period_count >= config['EXPDIFF_FREE_PERIODS']:
o = max(o + 2**(period_count - config['EXPDIFF_FREE_PERIODS']), config['MIN_DIFF'])
# print 'Calculating difficulty of block %d, timestamp difference %d, parent diff %d, child diff %d' % (parent.number + 1, timestamp - parent.timestamp, parent.difficulty, o)
# print('Calculating difficulty of block %d, timestamp difference %d, parent diff %d, child diff %d' % (parent.number + 1, timestamp - parent.timestamp, parent.difficulty, o))
return o


Expand Down
2 changes: 1 addition & 1 deletion ethereum/bloom.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def bloom(val):

def bloom_insert(bloom, val):
h = utils.sha3(val)
# print 'bloom_insert', bloom_bits(val), repr(val)
# print('bloom_insert', bloom_bits(val), repr(val))
for i in range(0, BUCKETS_PER_VAL * 2, 2):
bloom |= 1 << ((safe_ord(h[i + 1]) + (safe_ord(h[i]) << 8)) & 2047)
return bloom
Expand Down
8 changes: 4 additions & 4 deletions ethereum/fast_rlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,20 @@ def run():
x = trie.Trie(db.EphemDB())
for i in range(10000):
x.update(str(i), str(i**3))
print 'elapsed', time.time() - st
print('elapsed', time.time() - st)
return x.root_hash

trie.rlp_encode = _encode_optimized
print 'trie.rlp_encode = encode_optimized'
print('trie.rlp_encode = encode_optimized')
r3 = run()

trie.rlp_encode = rlp.codec.encode_raw
print 'trie.rlp_encode = rlp.codec.encode_raw'
print('trie.rlp_encode = rlp.codec.encode_raw')
r2 = run()
assert r2 == r3

trie.rlp_encode = rlp.encode
print 'trie.rlp_encode = rlp.encode'
print('trie.rlp_encode = rlp.encode')
r = run()
assert r == r2

Expand Down
2 changes: 1 addition & 1 deletion ethereum/fastvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def vm_execute(ext, msg, code):
_prevop = None # for trace only

while 1:
# print 'op: ', op, time.time() - s
# print('op: ', op, time.time() - s)
# s = time.time()
# stack size limit error
if compustate.pc not in processed_code:
Expand Down
2 changes: 1 addition & 1 deletion ethereum/processblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def __repr__(self):
def apply_transaction(block, tx):
validate_transaction(block, tx)

# print block.get_nonce(tx.sender), '@@@'
# print(block.get_nonce(tx.sender), '@@@')

def rp(what, actual, target):
return '%r: %r actual:%r target:%r' % (tx, what, actual, target)
Expand Down
2 changes: 1 addition & 1 deletion ethereum/pruning_trie.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def set_root_hash(self, root_hash):
if root_hash == BLANK_ROOT:
self.root_node = BLANK_NODE
return
# print repr(root_hash)
# print(repr(root_hash))
self.root_node = self._decode_to_node(root_hash)
# dummy to increase reference count
# self._encode_node(self.root_node)
Expand Down
2 changes: 1 addition & 1 deletion ethereum/tests/benchmark_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ def benchmark(size):
nsz.append(sum([len(v) for k, v in ldb2.kv.items()]))
t.db = odb
ldbsz = sum([len(v) for k, v in ldb.kv.items()])
print sz, sum(nsz) // len(nsz), ldbsz
print(sz, sum(nsz) // len(nsz), ldbsz)

benchmark(int(sys.argv[1]) if len(sys.argv) > 1 else 1000)
4 changes: 2 additions & 2 deletions ethereum/tests/bintrie.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ def test(n, m=100):
k = hashfunc(str(i))
v = hashfunc('v'+str(i))
x = update(x, db, [int(a) for a in encode_bin(rlp.encode(k))], v)
print x
print sum([len(val) for key, val in db.db.items()])
print(x)
print(sum([len(val) for key, val in db.db.items()]))
l1 = ListeningDB(db)
o = 0
p = 0
Expand Down
17 changes: 8 additions & 9 deletions ethereum/tests/profile_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ def do_test_vm(filename, testname=None, testdata=None, limit=99999999, profiler=

if __name__ == '__main__':
num = 5000
print 'profile_vm.py [no_cprofile]'
print 'loading tests'
print('profile_vm.py [no_cprofile]')
print('loading tests')
fixtures = testutils.get_tests_from_file_or_dir(
os.path.join(testutils.fixture_path, 'VMTests'))

def run(profiler=None):
print 'running'
print('running')
i = 0
seen = b''
for filename, tests in fixtures.items():
Expand All @@ -33,8 +33,8 @@ def run(profiler=None):
do_test_vm(filename, testname, testdata, profiler=profiler)
seen += str(testname)
i += 1
print 'ran %d tests' % i
print 'test key', sha3(seen).encode('hex')
print('ran %d tests' % i)
print('test key', sha3(seen).encode('hex'))

if len(sys.argv) == 1:
pr = cProfile.Profile()
Expand All @@ -43,15 +43,14 @@ def run(profiler=None):
sortby = 'tottime'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats(50)
print s.getvalue()
print(s.getvalue())
else:
# pypy version
st = time.time()
run()
print
print 'took total', time.time() - st
print('took total', time.time() - st)
try: # pypy branch
from ethereum.utils import sha3_call_counter
print 'took w/o sha3', time.time() - st - sha3_call_counter[3]
print('took w/o sha3', time.time() - st - sha3_call_counter[3])
except ImportError:
pass
2 changes: 1 addition & 1 deletion ethereum/tests/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def run_block_test(params, config_overrides = {}):
blockmap[b2.hash] = b2
env.db.put(b2.hash, rlp.encode(b2))
if b2:
print 'Block %d with state root %s' % (b2.number, b2.state.root_hash.encode('hex'))
print('Block %d with state root %s' % (b2.number, b2.state.root_hash.encode('hex')))
# blkdict = b.to_dict(False, True, False, True)
# assert blk["blockHeader"] == \
# translate_keys(blkdict["header"], translator_list, lambda y, x: x, [])
Expand Down
4 changes: 2 additions & 2 deletions ethereum/tests/test_difficulty.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def test_difficulty(filename, testname, testdata):

calculated_dif = blocks.calc_difficulty(block, cur_blk_timestamp)

print calculated_dif
print reference_dif
print(calculated_dif)
print(reference_dif)
assert calculated_dif == reference_dif


Expand Down
6 changes: 3 additions & 3 deletions ethereum/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,10 @@ def blkhash(n):

time_pre = time.time()
try:
print 'trying'
print('trying')
success, output = pb.apply_transaction(blk, tx)
blk.commit_state()
print 'success', blk.get_receipts()[-1].gas_used
print('success', blk.get_receipts()[-1].gas_used)
except InvalidTransaction:
success, output = False, b''
blk.commit_state()
Expand Down Expand Up @@ -508,7 +508,7 @@ def run_genesis_test(params, mode):
assert b.mixhash == decode_hex(remove_0x_head(params['mixhash']))
assert b.prevhash == decode_hex(remove_0x_head(params['parentHash']))
assert b.nonce == decode_hex(remove_0x_head(params['nonce']))
print 9
print(9)
if mode == FILL:
params['result'] = encode_hex(rlp.encode(b))
return params
Expand Down
50 changes: 25 additions & 25 deletions ethereum/trie.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,29 +458,29 @@ def _update_kv_node(self, node, key, value):
return new_node

def _getany(self, node, reverse=False, path=[]):
# print 'getany', node, 'reverse=', reverse, path
# print('getany', node, 'reverse=', reverse, path)
node_type = self._get_node_type(node)
if node_type == NODE_TYPE_BLANK:
return None
if node_type == NODE_TYPE_BRANCH:
if node[16] and not reverse:
# print 'found!', [16], path
# print('found!', [16], path)
return [16]
scan_range = list(range(16))
if reverse:
scan_range.reverse()
for i in scan_range:
o = self._getany(self._decode_to_node(node[i]), reverse=reverse, path=path + [i])
if o is not None:
# print 'found@', [i] + o, path
# print('found@', [i] + o, path)
return [i] + o
if node[16] and reverse:
# print 'found!', [16], path
# print('found!', [16], path)
return [16]
return None
curr_key = without_terminator(unpack_to_nibbles(node[0]))
if node_type == NODE_TYPE_LEAF:
# print 'found#', curr_key, path
# print('found#', curr_key, path)
return curr_key

if node_type == NODE_TYPE_EXTENSION:
Expand Down Expand Up @@ -580,7 +580,7 @@ def _merge(self, node1, node2):
] if descend_key2[prefix_length:] else sub2
return [pack_nibbles(descend_key1[:prefix_length]),
self._encode_node(self._merge(new_sub1, new_sub2))]

nodes = [[node1], [node2]]
for (node, node_type) in zip(nodes, [node_type1, node_type2]):
if node_type != NODE_TYPE_BRANCH:
Expand All @@ -597,83 +597,83 @@ def _merge(self, node1, node2):
return new_node

@classmethod
def unsafe_merge(cls, trie1, trie2):
def unsafe_merge(cls, trie1, trie2):
t = Trie(trie1.db)
t.root_node = t._merge(trie1.root_node, trie2.root_node)
return t

def _iter(self, node, key, reverse=False, path=[]):
# print 'iter', node, key, 'reverse =', reverse, 'path =', path
# print('iter', node, key, 'reverse =', reverse, 'path =', path)
node_type = self._get_node_type(node)

if node_type == NODE_TYPE_BLANK:
return None

elif node_type == NODE_TYPE_BRANCH:
# print 'b'
# print('b')
if len(key):
sub_node = self._decode_to_node(node[key[0]])
o = self._iter(sub_node, key[1:], reverse, path + [key[0]])
if o is not None:
# print 'returning', [key[0]] + o, path
# print('returning', [key[0]] + o, path)
return [key[0]] + o
if reverse:
scan_range = reversed(list(range(key[0] if len(key) else 0)))
else:
scan_range = list(range(key[0] + 1 if len(key) else 0, 16))
for i in scan_range:
sub_node = self._decode_to_node(node[i])
# print 'prelim getany', path+[i]
# print('prelim getany', path+[i])
o = self._getany(sub_node, reverse, path + [i])
if o is not None:
# print 'returning', [i] + o, path
# print('returning', [i] + o, path)
return [i] + o
if reverse and key and node[16]:
# print 'o'
# print('o')
return [16]
return None

descend_key = without_terminator(unpack_to_nibbles(node[0]))
if node_type == NODE_TYPE_LEAF:
if reverse:
# print 'L', descend_key, key, descend_key if descend_key < key else None, path
# print('L', descend_key, key, descend_key if descend_key < key else None, path)
return descend_key if descend_key < key else None
else:
# print 'L', descend_key, key, descend_key if descend_key > key else None, path
# print('L', descend_key, key, descend_key if descend_key > key else None, path)
return descend_key if descend_key > key else None

if node_type == NODE_TYPE_EXTENSION:
# traverse child nodes
sub_node = self._decode_to_node(node[1])
sub_key = key[len(descend_key):]
# print 'amhere', key, descend_key, descend_key > key[:len(descend_key)]
# print('amhere', key, descend_key, descend_key > key[:len(descend_key)])
if starts_with(key, descend_key):
o = self._iter(sub_node, sub_key, reverse, path + descend_key)
elif descend_key > key[:len(descend_key)] and not reverse:
# print 1
# print 'prelim getany', path+descend_key
# print(1)
# print('prelim getany', path+descend_key)
o = self._getany(sub_node, False, path + descend_key)
elif descend_key < key[:len(descend_key)] and reverse:
# print 2
# print 'prelim getany', path+descend_key
# print(2)
# print('prelim getany', path+descend_key)
o = self._getany(sub_node, True, path + descend_key)
else:
o = None
# print 'returning@', descend_key + o if o else None, path
# print('returning@', descend_key + o if o else None, path)
return descend_key + o if o else None

def next(self, key):
# print 'nextting'
# print('nextting')
key = bin_to_nibbles(key)
o = self._iter(self.root_node, key)
# print 'answer', o
# print('answer', o)
return nibbles_to_bin(without_terminator(o)) if o else None

def prev(self, key):
# print 'prevving'
# print('prevving')
key = bin_to_nibbles(key)
o = self._iter(self.root_node, key, reverse=True)
# print 'answer', o
# print('answer', o)
return nibbles_to_bin(without_terminator(o)) if o else None

def _delete_node_storage(self, node):
Expand Down
4 changes: 2 additions & 2 deletions ethereum/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def extract_copy(self, mem, memstart, datastart, size):

class Message(object):

def __init__(self, sender, to, value, gas, data, depth=0,
def __init__(self, sender, to, value, gas, data, depth=0,
code_address=None, is_create=False, transfers_value=True):
self.sender = sender
self.to = to
Expand Down Expand Up @@ -170,7 +170,7 @@ def vm_execute(ext, msg, code):
_prevop = None # for trace only

while 1:
# print 'op: ', op, time.time() - s
# print('op: ', op, time.time() - s)
# s = time.time()
# stack size limit error
if compustate.pc >= codelen:
Expand Down
2 changes: 1 addition & 1 deletion tools/keystorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
assert pw == pw2, "Password mismatch"
print("Applying hard key derivation function. Wait a little")
j = keys.make_keystore_json(key, pw)
print j
print(j)
open(j["id"]+'.json', 'w').write(json.dumps(j, indent=4))
print("Wallet creation successful, file saved at: " + j["id"] + ".json")
# Decode a json
Expand Down
2 changes: 1 addition & 1 deletion tools/random_vm_test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ def apply_msg_wrapper(_block, _tx, msg, code):

if __name__ == "__main__":
o = gen_test((sys.argv + [str(random.randrange(10**50))])[2])
print json.dumps({sys.argv[1]: o}, indent=4)
print(json.dumps({sys.argv[1]: o}, indent=4))
2 changes: 1 addition & 1 deletion tools/vm_test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ def apply_msg_wrapper(_block, _tx, msg, code):

if __name__ == "__main__":
o = gen_test(sys.argv[2], int(sys.argv[3]), sys.argv[4:])
print json.dumps({sys.argv[1]: o}, indent=4)
print(json.dumps({sys.argv[1]: o}, indent=4))

0 comments on commit 81e4304

Please sign in to comment.