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, )
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, )
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)
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() }
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
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()
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()