def fill_state_test(filler): """ Filler function for filling state tests. """ test_name = get_test_name(filler) test = filler[test_name] environment = normalize_environment(test["env"]) pre_state = normalize_state(test["pre"]) transaction_group = normalize_transaction_group(test["transaction"]) post = defaultdict(list) # type: Dict[int, List[Dict[str, str]]] for expect in test["expect"]: indexes = expect["indexes"] networks = normalize_networks(expect["networks"]) result = normalize_state(expect["result"]) post_state = deep_merge(pre_state, result) for network in networks: account_db_class = ACCOUNT_STATE_DB_CLASSES[network] post_state_root = calc_state_root(post_state, account_db_class) post[network].append({ "hash": encode_hex(post_state_root), "indexes": indexes, }) return { test_name: { "env": environment, "pre": pre_state, "transaction": transaction_group, "post": post } }
def _expect(post_state, networks, transaction, filler): test_name = get_test_name(filler) test = filler[test_name] test_update = {test_name: {}} # type: Dict[str, Dict[Any, Any]] pre_state = test.get("pre", {}) post_state = normalize_state(post_state or {}) defaults = { address: { "balance": 0, "nonce": 0, "code": b"", "storage": {}, } for address in post_state } result = deep_merge(defaults, pre_state, normalize_state(post_state)) new_expect = {"result": result} if transaction is not None: transaction = normalize_transaction( merge(get_default_transaction(networks), transaction)) if "transaction" not in test: transaction_group = apply_formatters_to_dict( { "data": wrap_in_list, "gasLimit": wrap_in_list, "value": wrap_in_list, }, transaction) indexes = { index_key: 0 for transaction_key, index_key in [ ("gasLimit", "gas"), ("value", "value"), ("data", "data"), ] if transaction_key in transaction_group } else: transaction_group, indexes = add_transaction_to_group( test["transaction"], transaction) new_expect = assoc(new_expect, "indexes", indexes) test_update = assoc_in(test_update, [test_name, "transaction"], transaction_group) if networks is not None: networks = normalize_networks(networks) new_expect = assoc(new_expect, "networks", networks) existing_expects = test.get("expect", []) expect = existing_expects + [new_expect] test_update = assoc_in(test_update, [test_name, "expect"], expect) return deep_merge(filler, test_update)
def _fill_and_normalize_state(simple_state): base_state = normalize_state(simple_state) defaults = {address: { "balance": 0, "nonce": 0, "code": b"", "storage": {}, } for address in base_state.keys()} state = deep_merge(defaults, base_state) return state
def fill_vm_test(filler, *, call_creates=None, gas_price=None, gas_remaining=0, logs=None, output=b""): test_name = get_test_name(filler) test = filler[test_name] environment = normalize_environment(test["env"]) pre_state = normalize_state(test["pre"]) execution = normalize_execution(test["exec"]) assert len(test["expect"]) == 1 expect = test["expect"][0] assert "network" not in test assert "indexes" not in test result = normalize_state(expect["result"]) post_state = deep_merge(pre_state, result) call_creates = normalize_call_creates(call_creates or []) gas_remaining = normalize_int(gas_remaining) output = normalize_bytes(output) logs = normalize_logs(logs or []) log_hash = hash_log_entries(logs) return { test_name: { "env": environment, "pre": pre_state, "exec": execution, "post": post_state, "callcreates": call_creates, "gas": gas_remaining, "output": output, "logs": log_hash, } }
def _pre_state(filler): test_name = get_test_name(filler) old_pre_state = filler[test_name].get("pre_state", {}) pre_state = normalize_state(raw_state) defaults = {address: { "balance": 0, "nonce": 0, "code": b"", "storage": {}, } for address in pre_state} new_pre_state = deep_merge(defaults, old_pre_state, pre_state) return assoc_in(filler, [test_name, "pre"], new_pre_state)
def test_normalize_state_detects_bad_keys(): with pytest.raises(ValidationError, match="not-a-key"): normalize_state(((ADDRESS_A, 'not-a-key', 3), ))
def test_normalize_state_detects_duplicates(value): with pytest.raises(ValidationError, match="Some state item is defined multiple times"): normalize_state(value)
def test_normalize_state(value, expected): actual = normalize_state(value) assert actual == expected