def _handle_delta(database, delta): # Check for and resolve forks old_block = database.fetch("blocks", delta.block_num) if old_block is not None: if old_block["block_id"] != delta.block_id: drop_results = database.drop_fork(delta.block_num) if drop_results["deleted"] == 0: LOGGER.warning( "Failed to drop forked resources since block: %s", delta.block_num ) else: return # Parse changes and update database update = get_updater(database, delta.block_num) for change in delta.state_changes: if namespace_ok(change.address): resources = data_to_dicts(change.address, change.value) for resource in resources: update_results = update(change.address, resource) if update_results["inserted"] == 0: LOGGER.warning( "Failed to insert resource from address: %s", change.address ) # Add new block to database new_block = {"block_num": delta.block_num, "block_id": delta.block_id} block_results = database.insert("blocks", new_block) if block_results["inserted"] == 0: LOGGER.warning( "Failed to insert block #%s: %s", delta.block_num, delta.block_id )
def update_database(conn, state_change): """Takes in a delta and database object, parses the change in the delta, and writes the changes to the database. """ update = get_updater(conn, state_change.block_num) remove = get_remover(conn) for change in state_change.state_changes: if addresser.family.is_family(change.address): if not change.value: remove(change.address) else: resource = data_to_dicts(change.address, change.value)[0] update(change.address, resource)
def _handle_delta(database, delta): """ Handle state changes """ try: # Check for and resolve forks delta.block_num = int(delta.block_num) old_block = database.fetch("blocks", delta.block_num) if old_block is not None: if old_block["block_id"] != delta.block_id: drop_results = database.drop_fork(delta.block_num) if drop_results["deleted"] == 0: LOGGER.warning( "Failed to drop forked resources since block: %s", str(delta.block_num), ) else: return # Parse changes and update database update = get_updater(database, delta.block_num) remove = get_remover(database, delta.block_num) for change in delta.state_changes: if addresser.family.is_family(change.address): if not change.value: remove(change.address) else: resources = data_to_dicts(change.address, change.value) for resource in resources: update(change.address, resource) # Add new block to database new_block = { "block_num": int(delta.block_num), "block_id": delta.block_id, "previous_block_id": delta.previous_block_id, "state_root_hash": delta.state_root_hash, "block_datetime": r.now(), } block_results = database.insert("blocks", new_block) if block_results["inserted"] == 0: LOGGER.warning( "Failed to insert block #%s: %s", str(delta.block_num), delta.block_id ) except Exception as err: # pylint: disable=broad-except LOGGER.exception("%s error handling delta:", type(err)) LOGGER.exception(err)
def update_database(conn, state_change): """Takes in a delta and database object, parses the change in the delta, and writes the changes to the database. """ update = get_updater(conn, state_change.block_num) remove = get_remover(conn) for change in state_change.state_changes: if addresser.family.is_family(change.address): if not change.value: remove(change.address) else: resource = data_to_dicts(change.address, change.value)[0] data_type = addresser.get_address_type(change.address) if data_type in TABLE_NAMES and TABLE_NAMES[data_type] == "roles": clear_role(conn, resource["role_id"], resource["created_date"]) update(change.address, resource)