Ejemplo n.º 1
0
def diff_state(
        expected_state: AccountState,
        state: BaseState) -> AccountDiff:

    for account, account_data in sorted(expected_state.items()):
        expected_balance = account_data['balance']
        expected_nonce = account_data['nonce']
        expected_code = account_data['code']

        actual_nonce = state.get_nonce(account)
        actual_code = state.get_code(account)
        actual_balance = state.get_balance(account)

        if actual_nonce != expected_nonce:
            yield (account, 'nonce', actual_nonce, expected_nonce)
        if actual_code != expected_code:
            yield (account, 'code', actual_code, expected_code)
        if actual_balance != expected_balance:
            yield (account, 'balance', actual_balance, expected_balance)

        for slot, expected_storage_value in sorted(account_data['storage'].items()):
            actual_storage_value = state.get_storage(account, slot)
            if actual_storage_value != expected_storage_value:
                yield (
                    account,
                    f'storage[{slot}]',
                    actual_storage_value,
                    expected_storage_value,
                )
Ejemplo n.º 2
0
def diff_account_db(expected_state: AccountState,
                    account_db: BaseAccountDB) -> AccountDiff:

    for account, account_data in sorted(expected_state.items()):
        expected_balance = account_data['balance']
        expected_nonce = account_data['nonce']
        expected_code = account_data['code']

        actual_nonce = account_db.get_nonce(account)
        actual_code = account_db.get_code(account)
        actual_balance = account_db.get_balance(account)

        if actual_nonce != expected_nonce:
            yield (account, 'nonce', actual_nonce, expected_nonce)
        if actual_code != expected_code:
            yield (account, 'code', actual_code, expected_code)
        if actual_balance != expected_balance:
            yield (account, 'balance', actual_balance, expected_balance)

        for slot, expected_storage_value in sorted(
                account_data['storage'].items()):
            actual_storage_value = account_db.get_storage(account, slot)
            if actual_storage_value != expected_storage_value:
                yield (
                    account,
                    'storage[{0}]'.format(slot),
                    actual_storage_value,
                    expected_storage_value,
                )
Ejemplo n.º 3
0
def apply_state_dict(state: BaseState, state_dict: AccountState) -> None:
    for account, account_data in state_dict.items():
        state.set_balance(account, account_data["balance"])
        state.set_nonce(account, account_data["nonce"])
        state.set_code(account, account_data["code"])

        for slot, value in account_data["storage"].items():
            state.set_storage(account, slot, value)
Ejemplo n.º 4
0
def normalize_account_state(account_state: AccountState) -> AccountState:
    return {
        to_canonical_address(address): {
            'balance': to_int(state['balance']),
            'code': decode_hex(state['code']),
            'nonce': to_int(state['nonce']),
            'storage': {
                to_int(slot): big_endian_to_int(decode_hex(value))
                for slot, value in state['storage'].items()
            },
        } for address, state in account_state.items()
    }
Ejemplo n.º 5
0
Archivo: db.py Proyecto: sjyi/py-evm
def apply_state_dict(account_db: BaseAccountDB,
                     state_dict: AccountState) -> BaseAccountDB:

    for account, account_data in state_dict.items():
        account_db.set_balance(account, account_data["balance"])
        account_db.set_nonce(account, account_data["nonce"])
        account_db.set_code(account, account_data["code"])

        for slot, value in account_data["storage"].items():
            account_db.set_storage(account, slot, value)

    return account_db
Ejemplo n.º 6
0
def setup_state(desired_state: AccountState, state: StateAPI) -> None:
    for account, account_data in desired_state.items():
        for slot, value in account_data['storage'].items():
            state.set_storage(account, slot, value)

        nonce = account_data['nonce']
        code = account_data['code']
        balance = account_data['balance']

        state.set_nonce(account, nonce)
        state.set_code(account, code)
        state.set_balance(account, balance)
    state.persist()
Ejemplo n.º 7
0
def setup_account_db(desired_state: AccountState, account_db: BaseAccountDB) -> None:
    for account, account_data in desired_state.items():
        for slot, value in account_data['storage'].items():
            account_db.set_storage(account, slot, value)

        nonce = account_data['nonce']
        code = account_data['code']
        balance = account_data['balance']

        account_db.set_nonce(account, nonce)
        account_db.set_code(account, code)
        account_db.set_balance(account, balance)
    account_db.persist()