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

Commit fbac9ee

Browse files
authored
Merge pull request #776 from karlfloersch/karl/develop_edits
Update tester, slogging, & transactions
2 parents 4a73f8a + adce623 commit fbac9ee

File tree

5 files changed

+65
-23
lines changed

5 files changed

+65
-23
lines changed

ethereum/slogging.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,16 @@ def configure(config_string=None, log_json=False, log_file=None):
312312
if hasattr(logger, 'setLevel'):
313313
# Guard against `logging.PlaceHolder` instances
314314
logger.setLevel(logging.NOTSET)
315-
logger.propagate = True
315+
if config_string == ":{}".format(DEFAULT_LOGLEVEL):
316+
logger.propagate = True
317+
else:
318+
logger.propagate = False
316319

317320
for name_levels in config_string.split(','):
318321
name, _, level = name_levels.partition(':')
319322
logger = getLogger(name)
320323
logger.setLevel(level.upper())
324+
logger.propagate = True
321325

322326
configure_logging = configure
323327

ethereum/tests/test_tester.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_abicontract_interface():
3434
abi_json = json.dumps(simple_data['abi']).encode('utf-8')
3535

3636
abi = ABIContract(
37-
_chain=tester_state,
37+
_tester=tester_state,
3838
_abi=abi_json,
3939
address=simple_address,
4040
)

ethereum/tools/tester.py

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from ethereum.utils import sha3, privtoaddr, int_to_addr, to_string, big_endian_to_int, checksum_encode, int_to_big_endian, encode_hex
22
from ethereum.genesis_helpers import mk_basic_state
3-
from ethereum.pow import chain
3+
from ethereum.pow import chain as pow_chain
44
from ethereum.transactions import Transaction
55
from ethereum.consensus_strategy import get_consensus_strategy
66
from ethereum.config import config_homestead, config_tangerine, config_spurious, config_metropolis, default_config, Env
77
from ethereum.pow.ethpow import Miner
8-
from ethereum.messages import apply_transaction
8+
from ethereum.messages import apply_transaction, apply_message
99
from ethereum.common import verify_execution_results, mk_block_from_prevstate, set_execution_results
1010
from ethereum.meta import make_head_candidate
1111
from ethereum.abi import ContractTranslator
@@ -63,9 +63,10 @@ class TransactionFailed(Exception):
6363
config_string = ':info'
6464
# configure_logging(config_string=config_string)
6565

66+
6667
class ABIContract(object): # pylint: disable=too-few-public-methods
6768

68-
def __init__(self, _chain, _abi, address):
69+
def __init__(self, _tester, _abi, address):
6970
self.address = address
7071

7172
if isinstance(_abi, ContractTranslator):
@@ -76,25 +77,29 @@ def __init__(self, _chain, _abi, address):
7677
self.translator = abi_translator
7778

7879
for function_name in self.translator.function_data:
79-
function = self.method_factory(_chain, function_name)
80+
if self.translator.function_data[function_name]['is_constant']:
81+
function = self.method_factory(_tester.call, function_name)
82+
else:
83+
function = self.method_factory(_tester.tx, function_name)
8084
method = types.MethodType(function, self)
8185
setattr(self, function_name, method)
8286

8387
@staticmethod
84-
def method_factory(test_chain, function_name):
88+
def method_factory(tx_or_call, function_name):
8589
""" Return a proxy for calling a contract method with automatic encoding of
8690
argument and decoding of results.
8791
"""
8892

8993
def kall(self, *args, **kwargs):
9094
key = kwargs.get('sender', k0)
9195

92-
result = test_chain.tx( # pylint: disable=protected-access
96+
result = tx_or_call( # pylint: disable=protected-access
9397
sender=key,
9498
to=self.address,
9599
value=kwargs.get('value', 0),
96100
data=self.translator.encode(function_name, args),
97-
startgas=kwargs.get('startgas', STARTGAS)
101+
startgas=kwargs.get('startgas', STARTGAS),
102+
gasprice=kwargs.get('gasprice', GASPRICE)
98103
)
99104

100105
if result is False:
@@ -117,14 +122,32 @@ def get_env(env):
117122
return env if isinstance(env, Env) else Env(config=d[env])
118123

119124

125+
class State(object):
126+
def __init__(self, genesis):
127+
self.state = genesis
128+
129+
def tx(self, sender=k0, to=b'\x00' * 20, value=0, data=b'', startgas=STARTGAS, gasprice=GASPRICE):
130+
sender_addr = privtoaddr(sender)
131+
transaction = Transaction(self.state.get_nonce(sender_addr), gasprice, startgas,
132+
to, value, data).sign(sender)
133+
success, output = apply_transaction(self.state, transaction)
134+
if not success:
135+
raise TransactionFailed()
136+
return output
137+
138+
def call(self, sender=k0, to=b'\x00' * 20, value=0, data=b'', startgas=STARTGAS, gasprice=GASPRICE):
139+
sender_addr = privtoaddr(sender)
140+
result = apply_message(self.state.ephemeral_clone(), sender=sender_addr, to=to, value=value, data=data, gas=startgas)
141+
if result is None:
142+
raise TransactionFailed()
143+
return result
144+
120145
class Chain(object):
121-
def __init__(self, alloc=None, env=None):
122-
self.chain = chain.Chain(
123-
genesis=mk_basic_state(base_alloc if alloc is None else alloc,
124-
None,
125-
get_env(env)),
126-
reset_genesis=True
127-
)
146+
def __init__(self, alloc=base_alloc, env=None, genesis=None):
147+
if genesis:
148+
self.chain = pow_chain.Chain(genesis, reset_genesis=True)
149+
else:
150+
self.chain = pow_chain.Chain(mk_basic_state(alloc, None, get_env(env)), reset_genesis=True)
128151
self.cs = get_consensus_strategy(self.chain.env.config)
129152
self.block = mk_block_from_prevstate(self.chain, timestamp=self.chain.state.timestamp + 1)
130153
self.head_state = self.chain.state.ephemeral_clone()
@@ -144,9 +167,16 @@ def tx(self, sender=k0, to=b'\x00' * 20, value=0, data=b'', startgas=STARTGAS, g
144167
sender_addr = privtoaddr(sender)
145168
transaction = Transaction(self.head_state.get_nonce(sender_addr), gasprice, startgas,
146169
to, value, data).sign(sender)
147-
o = self.direct_tx(transaction)
170+
output = self.direct_tx(transaction)
148171
self.last_sender = sender
149-
return o
172+
return output
173+
174+
def call(self, sender=k0, to=b'\x00' * 20, value=0, data=b'', startgas=STARTGAS, gasprice=GASPRICE):
175+
sender_addr = privtoaddr(sender)
176+
result = apply_message(self.head_state.ephemeral_clone(), sender=sender_addr, to=to, value=value, data=data, gas=startgas)
177+
if result is None:
178+
raise TransactionFailed()
179+
return result
150180

151181
def last_gas_used(self, with_tx=False):
152182
if len(self.head_state.receipts) == 1:
@@ -174,13 +204,17 @@ def mine(self, number_of_blocks=1, coinbase=a0):
174204
set_execution_results(self.head_state, self.block)
175205
self.block = Miner(self.block).mine(rounds=100, start_nonce=0)
176206
assert self.chain.add_block(self.block)
177-
assert self.head_state.trie.root_hash == self.chain.state.trie.root_hash
207+
b = self.block
178208
for i in range(1, number_of_blocks):
179-
b, _ = make_head_candidate(self.chain, timestamp=self.chain.state.timestamp + 14)
209+
b, _ = make_head_candidate(self.chain, parent=b, timestamp=self.chain.state.timestamp + 14, coinbase=coinbase)
180210
b = Miner(b).mine(rounds=100, start_nonce=0)
181211
assert self.chain.add_block(b)
182-
self.block = mk_block_from_prevstate(self.chain, timestamp=self.chain.state.timestamp + 14)
183-
self.head_state = self.chain.state.ephemeral_clone()
212+
self.change_head(b.header.hash, coinbase)
213+
return b
214+
215+
def change_head(self, parent, coinbase=a0):
216+
self.head_state = self.chain.mk_poststate_of_blockhash(parent).ephemeral_clone()
217+
self.block = mk_block_from_prevstate(self.chain, self.head_state, timestamp=self.chain.state.timestamp, coinbase=coinbase)
184218
self.cs.initialize(self.head_state, self.block)
185219

186220
def snapshot(self):
@@ -199,7 +233,7 @@ def int_to_0x_hex(v):
199233
return '0x' + o[1:]
200234
else:
201235
return '0x' + o
202-
236+
203237

204238
def mk_state_test_prefill(c):
205239
env = {

ethereum/transactions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ def creates(self):
155155
def __eq__(self, other):
156156
return isinstance(other, self.__class__) and self.hash == other.hash
157157

158+
def __lt__(self, other):
159+
return isinstance(other, self.__class__) and self.hash < other.hash
160+
158161
def __hash__(self):
159162
return utils.big_endian_to_int(self.hash)
160163

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ scrypt
66
py_ecc
77
rlp>=0.4.7
88
https://github.com/ethereum/ethash/tarball/master
9+
pycryptodome==3.4.6

0 commit comments

Comments
 (0)