Skip to content

Refactor add_target_to_bin and remove_target_from_bin #994

@lukpueh

Description

@lukpueh

Description of issue or feature request:
The TUF repository tool provides a functionality to distribute a large number of target files over multiple delegated roles (see delegate_hashed_bins).

To add a target to the corresponding bin, identified by the hash of the target path, or remove one from a bin the methods remove add_target_to_bin and remove_target_from_bin are provided. These methods are wrappers around add_target and remove_target, which don't call those methods directly but via a dispatcher helper _locate_and_update_target_in_bin, which also implements common functionality for the wrappers.

These methods should be refactored for the reasons and as outlined below:

Current behavior:

https://github.com/theupdateframework/tuf/blob/4fe29d138df02035eed9716657cfe0a76e17e178/tuf/repository_tool.py#L2749-L2756

Expected behavior:

  • simplify architecture to allow adding a custom argument to add_target_to_bin that gets passed through
  • remove redundant code
  • pass hash_prefix_length or number_of_bins instead of inferring it from the metadata (the calling code should know this).
  • make bin search O(1), e.g.:
def find_bin_for_hash(hash_prefix, bin_size):
  prefix_len = len(hash_prefix)
  hash_prefix = int(hash_prefix, 16)
  low = hash_prefix  - (hash_prefix % bin_size)
  high = low + bin_size - 1
  return f"{low:0{prefix_len}x}-{high:0{prefix_len}x}"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions