Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ethereum/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ 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))
return o


Expand Down
35 changes: 21 additions & 14 deletions ethereum/processblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,13 @@ class VMExt():
def __init__(self, block, tx):
self._block = block
self.get_code = block.get_code
self.set_code = block.set_code
self.get_balance = block.get_balance
self.set_balance = block.set_balance
self.delta_balance = block.delta_balance
self.get_nonce = block.get_nonce
self.set_nonce = block.set_nonce
self.increment_nonce = block.increment_nonce
self.set_storage_data = block.set_storage_data
self.get_storage_data = block.get_storage_data
self.log_storage = lambda x: block.account_to_dict(x)['storage']
Expand All @@ -249,6 +252,10 @@ def __init__(self, block, tx):
self.msg = lambda msg: _apply_msg(self, msg, self.get_code(msg.code_address))
self.account_exists = block.account_exists
self.post_homestead_hardfork = lambda: block.number >= block.config['HOMESTEAD_FORK_BLKNUM']
self.post_metropolis_hardfork = lambda: block.number >= block.config['METROPOLIS_FORK_BLKNUM']
self.snapshot = block.snapshot
self.revert = block.revert
self.transfer_value = block.transfer_value


def apply_msg(ext, msg):
Expand All @@ -270,9 +277,9 @@ def _apply_msg(ext, msg, code):
state=ext.log_storage(msg.to))
# log_state.trace('CODE', code=code)
# Transfer value, instaquit if not enough
snapshot = ext._block.snapshot()
snapshot = ext.snapshot()
if msg.transfers_value:
if not ext._block.transfer_value(msg.sender, msg.to, msg.value):
if not ext.transfer_value(msg.sender, msg.to, msg.value):
log_msg.debug('MSG TRANSFER FAILED', have=ext.get_balance(msg.to),
want=msg.value)
return 1, msg.gas, []
Expand All @@ -296,7 +303,7 @@ def _apply_msg(ext, msg, code):

if res == 0:
log_msg.debug('REVERTING')
ext._block.revert(snapshot)
ext.revert(snapshot)

return res, gas, dat

Expand All @@ -306,7 +313,7 @@ def create_contract(ext, msg):
#print('CREATING WITH GAS', msg.gas)
sender = decode_hex(msg.sender) if len(msg.sender) == 40 else msg.sender
code = msg.data.extract_all()
if ext._block.number >= ext._block.config['METROPOLIS_FORK_BLKNUM']:
if ext.post_metropolis_hardfork():
msg.to = mk_metropolis_contract_address(msg.sender, code)
if ext.get_code(msg.to):
if ext.get_nonce(msg.to) >= 2 ** 40:
Expand All @@ -317,19 +324,19 @@ def create_contract(ext, msg):
msg.to = normalize_address((ext.get_nonce(msg.to) - 1) % 2 ** 160)
else:
if ext.tx_origin != msg.sender:
ext._block.increment_nonce(msg.sender)
nonce = utils.encode_int(ext._block.get_nonce(msg.sender) - 1)
ext.increment_nonce(msg.sender)
nonce = utils.encode_int(ext.get_nonce(msg.sender) - 1)
msg.to = mk_contract_address(sender, nonce)
b = ext.get_balance(msg.to)
if b > 0:
ext.set_balance(msg.to, b)
ext._block.set_nonce(msg.to, 0)
ext._block.set_code(msg.to, b'')
ext._block.reset_storage(msg.to)
ext.set_nonce(msg.to, 0)
ext.set_code(msg.to, b'')
ext.reset_storage(msg.to)
msg.is_create = True
# assert not ext.get_code(msg.to)
msg.data = vm.CallData([], 0, 0)
snapshot = ext._block.snapshot()
snapshot = ext.snapshot()
res, gas, dat = _apply_msg(ext, msg, code)
assert utils.is_numeric(gas)

Expand All @@ -341,11 +348,11 @@ def create_contract(ext, msg):
gas -= gcost
else:
dat = []
log_msg.debug('CONTRACT CREATION OOG', have=gas, want=gcost, block_number=ext._block.number)
if ext._block.number >= ext._block.config['HOMESTEAD_FORK_BLKNUM']:
ext._block.revert(snapshot)
log_msg.debug('CONTRACT CREATION OOG', have=gas, want=gcost, block_number=ext.block_number)
if ext.post_homestead_hardfork():
ext.revert(snapshot)
return 0, 0, b''
ext._block.set_code(msg.to, b''.join(map(ascii_chr, dat)))
ext.set_code(msg.to, b''.join(map(ascii_chr, dat)))
return 1, gas, msg.to
else:
return 0, gas, b''
99 changes: 99 additions & 0 deletions ethereum/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from ethereum.utils import to_string, safe_ord, parse_int_or_hex
from ethereum.utils import remove_0x_head, int_to_hex, normalize_address
from ethereum.config import Env
from ethereum import state_transition
from state import State
import json
import os
import time
Expand Down Expand Up @@ -275,6 +277,103 @@ def normalize_value(k, p):
return time_post - time_pre


class FakeHeader():
def __init__(self, h):
self.hash = h
self.uncles = []

fake_headers = {}

def mk_fake_header(blknum):
if blknum not in fake_headers:
fake_headers[blknum] = FakeHeader(utils.sha3(to_string(blknum)))
return fake_headers[blknum]

def run_state_test1(params, mode):
pre = params['pre']
exek = params['transaction']
env = params['env']

assert set(env.keys()) == set(['currentGasLimit', 'currentTimestamp',
'previousHash', 'currentCoinbase',
'currentDifficulty', 'currentNumber'])
assert len(env['currentCoinbase']) == 40

state = State(db=db,
prev_headers=[mk_fake_header(i) for i in range(parse_int_or_hex(env['currentNumber']) -1, max(-1, parse_int_or_hex(env['currentNumber']) -257), -1)],
block_number=parse_int_or_hex(env['currentNumber']),
block_coinbase=utils.normalize_address(env['currentCoinbase']),
timestamp=parse_int_or_hex(env['currentTimestamp']),
gas_limit=parse_int_or_hex(env['currentGasLimit']))
# setup state
for address, h in list(pre.items()):
assert len(address) == 40
address = decode_hex(address)
assert set(h.keys()) == set(['code', 'nonce', 'balance', 'storage'])
state.set_nonce(address, parse_int_or_hex(h['nonce']))
state.set_balance(address, parse_int_or_hex(h['balance']))
state.set_code(address, decode_hex(h['code'][2:]))
for k, v in h['storage'].items():
state.set_storage_data(address,
utils.big_endian_to_int(decode_hex(k[2:])),
decode_hex(v[2:]))

for address, h in list(pre.items()):
address = decode_hex(address)
assert state.get_nonce(address) == parse_int_or_hex(h['nonce'])
assert state.get_balance(address) == parse_int_or_hex(h['balance'])
assert state.get_code(address) == decode_hex(h['code'][2:])
for k, v in h['storage'].items():
assert state.get_storage_data(address, utils.big_endian_to_int(
decode_hex(k[2:]))) == utils.big_endian_to_int(decode_hex(v[2:]))

try:
tx = transactions.Transaction(
nonce=parse_int_or_hex(exek['nonce'] or b"0"),
gasprice=parse_int_or_hex(exek['gasPrice'] or b"0"),
startgas=parse_int_or_hex(exek['gasLimit'] or b"0"),
to=normalize_address(exek['to'], allow_blank=True),
value=parse_int_or_hex(exek['value'] or b"0"),
data=decode_hex(remove_0x_head(exek['data'])))
except InvalidTransaction:
tx = None
success, output = False, b''
time_pre = time.time()
time_post = time_pre
else:
if 'secretKey' in exek:
tx.sign(exek['secretKey'])
elif all(key in exek for key in ['v', 'r', 's']):
tx.v = decode_hex(remove_0x_head(exek['v']))
tx.r = decode_hex(remove_0x_head(exek['r']))
tx.s = decode_hex(remove_0x_head(exek['s']))
else:
assert False

time_pre = time.time()
state.commit()
print(state.to_dict())
snapshot = state.snapshot()
try:
print('trying')
success, output, logs = state_transition.apply_transaction(state, tx)
assert success
state.commit()
print('success')
except InvalidTransaction:
success, output = False, b''
state.commit()
pass
except AssertionError:
success, output = False, b''
state.commit()
pass
time_post = time.time()

if tx.to == b'':
output = state.get_code(output) if output else b''


# Fills up a vm test without post data, or runs the test
def run_state_test(params, mode):
pre = params['pre']
Expand Down
1 change: 1 addition & 0 deletions ethereum/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def int_to_32bytearray(i):

def sha3(seed):
sha3_count[0] += 1
# print seed
return sha3_256(to_string(seed))

assert encode_hex(sha3(b'')) == b'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
Expand Down