Exemplo n.º 1
0
    def handle_channel_opened_event(
        self,
        channel_identifier: ChannelID,
        participant1: Address,
        participant2: Address,
        settle_timeout: int,
    ) -> Channel:
        """ Register the channel in the graph, add participants to graph if necessary.

        Corresponds to the ChannelOpened event. Called by the contract event listener.
        We swap participants unless participant1 < participant2."""

        if participant1 > participant2:
            (participant1, participant2) = (participant2, participant1)

        channel = Channel(
            token_network_address=self.address,
            channel_id=channel_identifier,
            participant1=participant1,
            participant2=participant2,
            settle_timeout=settle_timeout,
        )
        views = channel.views

        for cv in views:
            self.add_channel_view(cv)

        return channel
Exemplo n.º 2
0
def test_channels(pathfinding_service_mock):
    # Participants need to be ordered
    parts = sorted([make_address(), make_address(), make_address()])

    token_network_address = TokenNetworkAddress(b"1" * 20)

    # register token network internally
    database = pathfinding_service_mock.database
    database.upsert_token_network(token_network_address)

    channel1 = Channel(
        token_network_address=token_network_address,
        channel_id=ChannelID(1),
        participant1=parts[0],
        participant2=parts[1],
        settle_timeout=BlockTimeout(100),
    )
    channel2 = Channel(
        token_network_address=token_network_address,
        channel_id=ChannelID(2),
        participant1=parts[1],
        participant2=parts[2],
        settle_timeout=BlockTimeout(100),
    )

    # Test `upsert_channel` and `get_channels`
    database.upsert_channel(channel1)
    assert [chan.channel_id
            for chan in database.get_channels()] == [channel1.channel_id]

    database.upsert_channel(channel2)
    assert [chan.channel_id for chan in database.get_channels()] == [
        channel1.channel_id,
        channel2.channel_id,
    ]

    # Test `delete_channel`
    assert database.delete_channel(channel1.token_network_address,
                                   channel1.channel_id)
    assert [chan.channel_id
            for chan in database.get_channels()] == [channel2.channel_id]

    assert not database.delete_channel(channel1.token_network_address,
                                       channel1.channel_id)
    assert [chan.channel_id
            for chan in database.get_channels()] == [channel2.channel_id]
Exemplo n.º 3
0
 def get_channels(self) -> Iterator[Channel]:
     for row in self.conn.execute("SELECT * FROM channel"):
         channel_dict = dict(zip(row.keys(), row))
         channel_dict["fee_schedule1"] = json.loads(
             channel_dict["fee_schedule1"])
         channel_dict["fee_schedule2"] = json.loads(
             channel_dict["fee_schedule2"])
         yield Channel.Schema().load(channel_dict)
Exemplo n.º 4
0
def test_channel_constraints(pathfinding_service_mock):
    """Regression test for https://github.com/raiden-network/raiden-services/issues/693"""

    # Participants need to be ordered
    parts = sorted([make_address(), make_address()])

    token_network_address1 = make_token_network_address()
    token_network_address2 = make_token_network_address()

    # register token network internally
    database = pathfinding_service_mock.database
    database.upsert_token_network(token_network_address1,
                                  DEFAULT_TOKEN_NETWORK_SETTLE_TIMEOUT)
    database.upsert_token_network(token_network_address2,
                                  DEFAULT_TOKEN_NETWORK_SETTLE_TIMEOUT)

    channel1 = Channel(
        token_network_address=token_network_address1,
        channel_id=ChannelID(1),
        participant1=parts[0],
        participant2=parts[1],
    )
    channel2 = Channel(
        token_network_address=token_network_address2,
        channel_id=ChannelID(1),
        participant1=parts[0],
        participant2=parts[1],
    )

    # Test `upsert_channel` and `get_channels`
    database.upsert_channel(channel1)
    assert [chan.channel_id
            for chan in database.get_channels()] == [channel1.channel_id]

    database.upsert_channel(channel2)
    assert [chan.channel_id for chan in database.get_channels()] == [
        channel1.channel_id,
        channel2.channel_id,
    ]
Exemplo n.º 5
0
 def upsert_channel(self, channel: Channel) -> None:
     channel_dict = Channel.Schema().dump(channel)
     for key in (
         "channel_id",
         "settle_timeout",
         "capacity1",
         "reveal_timeout1",
         "update_nonce1",
         "capacity2",
         "reveal_timeout2",
         "update_nonce2",
     ):
         channel_dict[key] = hex256(int(channel_dict[key]))
     channel_dict["fee_schedule1"] = json.dumps(channel_dict["fee_schedule1"])
     channel_dict["fee_schedule2"] = json.dumps(channel_dict["fee_schedule2"])
     self.upsert("channel", channel_dict)
Exemplo n.º 6
0
def test_edge_weight(addresses):
    # pylint: disable=assigning-non-slot
    channel_id = ChannelID(1)
    participant1 = addresses[0]
    participant2 = addresses[1]
    capacity = TokenAmount(int(20 * 1e18))
    capacity_partner = TokenAmount(int(10 * 1e18))
    channel = Channel(
        token_network_address=TokenNetworkAddress(bytes([1])),
        channel_id=channel_id,
        participant1=participant1,
        participant2=participant2,
        capacity1=capacity,
        capacity2=capacity_partner,
    )
    view, view_partner = channel.views
    amount = PaymentAmount(int(1e18))  # one RDN

    # no penalty
    assert (TokenNetwork.edge_weight(visited={},
                                     view=view,
                                     view_from_partner=view_partner,
                                     amount=amount,
                                     fee_penalty=0) == 1)

    # channel already used in a previous route
    assert (TokenNetwork.edge_weight(
        visited={channel_id: 2},
        view=view,
        view_from_partner=view_partner,
        amount=amount,
        fee_penalty=0,
    ) == 3)

    # absolute fee
    view.fee_schedule_sender.flat = FeeAmount(int(0.03e18))
    assert (TokenNetwork.edge_weight(
        visited={},
        view=view,
        view_from_partner=view_partner,
        amount=amount,
        fee_penalty=100,
    ) == 4)

    # relative fee
    view.fee_schedule_sender.flat = FeeAmount(0)
    view.fee_schedule_sender.proportional = ProportionalFeeAmount(int(0.01e6))
    assert (TokenNetwork.edge_weight(
        visited={},
        view=view,
        view_from_partner=view_partner,
        amount=amount,
        fee_penalty=100,
    ) == 2)

    # partner has not enough capacity for refund (no_refund_weight) -> edge weight +1
    view_partner.capacity = TokenAmount(0)
    assert (TokenNetwork.edge_weight(
        visited={},
        view=view,
        view_from_partner=view_partner,
        amount=amount,
        fee_penalty=100,
    ) == 3)