Repository: Chia-Network/chia-blockchain
File: chia/server/address_manager.py (Python)

Code before:
            now = int(math.floor(time.time()))
        chance = 1.0
        since_last_try = max(now - self.last_try, 0)
        # deprioritize very recent attempts away
        if since_last_try < 60 * 10:
            chance *= 0.01

        # deprioritize 66% after each failed attempt,
        # but at most 1/28th to avoid the search taking forever or overly penalizing outages.
        chance *= pow(0.66, min(self.num_attempts, 8))
        return chance


def create_tried_matrix() -> list[list[int]]:
    return [[-1 for x in range(BUCKET_SIZE)] for y in range(TRIED_BUCKET_COUNT)]


def create_new_matrix() -> list[list[int]]:
    return [[-1 for x in range(BUCKET_SIZE)] for y in range(NEW_BUCKET_COUNT)]


# This is a Python port from 'CAddrMan' class from Bitcoin core code.
@dataclass
class AddressManager:
    id_count: int = 0
    key: int = randbits(256)
    random_pos: list[int] = field(default_factory=list)
    tried_matrix: list[list[int]] = field(default_factory=create_tried_matrix)
    new_matrix: list[list[int]] = field(default_factory=create_new_matrix)
    tried_count: int = 0
    new_count: int = 0
    map_addr: dict[str, int] = field(default_factory=dict)
    map_info: dict[int, ExtendedPeerInfo] = field(default_factory=dict)
    last_good: int = 1
    tried_collisions: list[int] = field(default_factory=list)
    used_new_matrix_positions: set[tuple[int, int]] = field(default_factory=set)
    used_tried_matrix_positions: set[tuple[int, int]] = field(default_factory=set)
    allow_private_subnets: bool = False
    lock: Lock = field(default_factory=Lock)

    def clear(self) -> None:
        self.id_count = 0
        self.key = randbits(256)
        self.random_pos = []
        self.tried_matrix = [[-1 for x in range(BUCKET_SIZE)] for y in range(TRIED_BUCKET_COUNT)]
        self.new_matrix = [[-1 for x in range(BUCKET_SIZE)] for y in range(NEW_BUCKET_COUNT)]
        self.tried_count = 0
        self.new_count = 0
        self.map_addr = {}
        self.map_info = {}
        self.last_good = 1

Review comment (suggestion):
```suggestion
    key: int = field(default_factory=lambda: randbits(256))
```
or
```suggestion
    key: int = field(default_factory=functools.partial(randbits, 256))
```
depending your preference

Code after:
        if now is None:
            now = int(math.floor(time.time()))
        chance = 1.0
        since_last_try = max(now - self.last_try, 0)
        # deprioritize very recent attempts away
        if since_last_try < 60 * 10:
            chance *= 0.01

        # deprioritize 66% after each failed attempt,
        # but at most 1/28th to avoid the search taking forever or overly penalizing outages.
        chance *= pow(0.66, min(self.num_attempts, 8))
        return chance


def create_tried_matrix() -> list[list[int]]:
    return [[-1 for x in range(BUCKET_SIZE)] for y in range(TRIED_BUCKET_COUNT)]


def create_new_matrix() -> list[list[int]]:
    return [[-1 for x in range(BUCKET_SIZE)] for y in range(NEW_BUCKET_COUNT)]


# This is a Python port from 'CAddrMan' class from Bitcoin core code.
@dataclass
class AddressManager:
    id_count: int = 0
    key: int = field(default_factory=functools.partial(randbits, 256))
    random_pos: list[int] = field(default_factory=list)
    tried_matrix: list[list[int]] = field(default_factory=create_tried_matrix)
    new_matrix: list[list[int]] = field(default_factory=create_new_matrix)
    tried_count: int = 0
    new_count: int = 0
    map_addr: dict[str, int] = field(default_factory=dict)
    map_info: dict[int, ExtendedPeerInfo] = field(default_factory=dict)
    last_good: int = 1
    tried_collisions: list[int] = field(default_factory=list)
    used_new_matrix_positions: set[tuple[int, int]] = field(default_factory=set)
    used_tried_matrix_positions: set[tuple[int, int]] = field(default_factory=set)
    allow_private_subnets: bool = False
    lock: Lock = field(default_factory=Lock)

    def clear(self) -> None:
        self.id_count = 0
        self.key = randbits(256)
        self.random_pos = []
        self.tried_matrix = create_tried_matrix()
        self.new_matrix = create_new_matrix()
        self.tried_count = 0
        self.new_count = 0
        self.map_addr = {}
        self.map_info = {}