Example #1
0
def test_reachability_target(
    token_network_model: TokenNetwork,
    reachability_state: SimpleReachabilityContainer,
    addresses: List[Address],
):

    assert get_paths(
        token_network_model=token_network_model,
        reachability_state=reachability_state,
        addresses=addresses,
    ) == [
        [0, 7, 8],
        [0, 1, 2, 3, 4, 8],
        [0, 7, 6, 8],
        [0, 7, 9, 10, 8],
        [0, 7, 6, 5, 8],
    ]

    reachability_state.reachabilities[
        addresses[8]] = AddressReachability.UNREACHABLE
    assert (get_paths(
        token_network_model=token_network_model,
        reachability_state=reachability_state,
        addresses=addresses,
    ) == [])

    reachability_state.reachabilities[
        addresses[8]] = AddressReachability.UNKNOWN
    assert (get_paths(
        token_network_model=token_network_model,
        reachability_state=reachability_state,
        addresses=addresses,
    ) == [])
Example #2
0
    def __init__(self, channels: List[dict], default_capacity: TA = TA(1000)):
        super().__init__(
            token_network_address=TokenNetworkAddress(a(255)),
            settle_timeout=DEFAULT_TOKEN_NETWORK_SETTLE_TIMEOUT,
        )

        # open channels
        channel_ids = itertools.count(100)
        for chan in channels:
            self.handle_channel_opened_event(
                channel_identifier=ChannelID(next(channel_ids)),
                participant1=a(chan["participant1"]),
                participant2=a(chan["participant2"]),
            )

            cv1: ChannelView = self.G[a(chan["participant1"])][a(
                chan["participant2"])]["view"]
            cv1.capacity = chan.get("capacity1", default_capacity)
            cv2: ChannelView = self.G[a(chan["participant2"])][a(
                chan["participant1"])]["view"]
            cv2.capacity = chan.get("capacity2", default_capacity)

        # create reachability mapping for testing
        self.reachability_state = SimpleReachabilityContainer(
            {node: AddressReachability.REACHABLE
             for node in self.G.nodes})
Example #3
0
def test_suggest_partner(
    token_network_model: TokenNetwork,
    addresses: List[Address],
):
    a = addresses  # pylint: disable=invalid-name

    reachability = SimpleReachabilityContainer(
        {a[i]: AddressReachability.REACHABLE
         for i in range(3)})
    suggestions = token_network_model.suggest_partner(reachability)
    assert len(suggestions) == 3
    assert set(s["address"] for s in suggestions) == set(
        to_checksum_address(a[i]) for i in range(3))
    assert suggestions[0]["address"] == to_checksum_address(a[1])

    # Increasing uptime of node 0 should move it to first place
    reachability.times[a[0]] -= timedelta(seconds=10)
    suggestions = token_network_model.suggest_partner(reachability)
    assert suggestions[0]["address"] == to_checksum_address(a[0])
Example #4
0
def test_routing_benchmark(token_network_model: TokenNetwork):  # pylint: disable=too-many-locals
    value = PaymentAmount(100)
    G = token_network_model.G
    addresses_to_reachabilities = SimpleReachabilityContainer({
        node: random.choice((
            AddressReachability.REACHABLE,
            AddressReachability.UNKNOWN,
            AddressReachability.UNREACHABLE,
        ))
        for node in G.nodes
    })

    times = []
    start = time.time()
    for _ in range(100):
        tic = time.time()
        source, target = random.sample(G.nodes, 2)
        paths = token_network_model.get_paths(
            source=source,
            target=target,
            value=value,
            max_paths=5,
            reachability_state=addresses_to_reachabilities,
        )

        toc = time.time()
        times.append(toc - tic)
    end = time.time()

    for path_object in paths:
        path = path_object.nodes
        fees = path_object.estimated_fee
        for node1, node2 in zip(path[:-1], path[1:]):
            view: ChannelView = G[to_canonical_address(node1)][
                to_canonical_address(node2)]["view"]
            print("capacity = ", view.capacity)
        print("fee sum = ", fees)
    print("Paths: ", paths)
    print("Mean runtime: ", sum(times) / len(times))
    print("Min runtime: ", min(times))
    print("Max runtime: ", max(times))
    print("Total runtime: ", end - start)