Exemplo n.º 1
0
def test_sync_network_unfreeze_on_graph():
    feed_graph = CurrencyNetworkGraph(is_frozen=True)
    network_unfreeze_update = NetworkUnfreezeFeedUpdate(address=ZERO_ADDRESS,
                                                        timestamp=0)

    assert feed_graph.is_frozen
    feed_graph.update_from_feed(network_unfreeze_update)
    assert feed_graph.is_frozen is False
Exemplo n.º 2
0
def community_with_trustlines(currency_network_with_trustlines):
    community = CurrencyNetworkGraph(100)
    community.gen_network(
        currency_network_with_trustlines.gen_graph_representation())
    greenlets = link_graph(currency_network_with_trustlines, community)
    gevent.sleep(0.0001)
    yield community
    gevent.killall(greenlets)
Exemplo n.º 3
0
def get_mediation_fee_from_balance_updates(
    graph: CurrencyNetworkGraph,
    user_address: str,
    first_event: BalanceUpdateEvent,
    second_event: BalanceUpdateEvent,
) -> MediationFee:
    """
    Get the balance change of a user that mediated a transfer A -> user -> B
    The two events must be given as ordered by log index
    """
    assert (first_event.log_index == second_event.log_index -
            1), "Received unexpected non-consecutive events."

    first_balance_change = (graph.get_balance_with_interests(
        first_event.from_,
        first_event.to,
        first_event.timestamp,
    ) - first_event.value)
    second_balance_change = (graph.get_balance_with_interests(
        second_event.from_,
        second_event.to,
        second_event.timestamp,
    ) - second_event.value)
    fee_value = abs(second_balance_change - first_balance_change)

    if first_event.from_ == user_address:
        # transfer sender pays, emits first user->B and second A-> user
        transfer_sender = second_event.from_
        transfer_receiver = first_event.to
    elif first_event.to == user_address:
        # transfer receiver pays emits first A->user and second user->B
        transfer_sender = first_event.from_
        transfer_receiver = second_event.to
    else:
        raise RuntimeError("Could not find user_address in first_event.")

    fee = MediationFee(
        value=fee_value,
        from_=transfer_sender,
        to=transfer_receiver,
        transaction_hash=first_event.transaction_hash,
        timestamp=first_event.timestamp,
    )

    return fee
Exemplo n.º 4
0
def apply_event_on_graph(graph: CurrencyNetworkGraph,
                         event: Union[TrustlineUpdateEvent,
                                      BalanceUpdateEvent]):
    if event.type == TrustlineUpdateEventType:
        event = cast(TrustlineUpdateEvent, event)
        graph.update_trustline(
            event.from_,
            event.to,
            event.creditline_given,
            event.creditline_received,
            event.interest_rate_given,
            event.interest_rate_received,
        )
    elif event.type == BalanceUpdateEventType:
        event = cast(BalanceUpdateEvent, event)
        graph.update_balance(event.from_, event.to, event.value,
                             event.timestamp)
    else:
        raise RuntimeError(
            f"Invalid event to be applied on graph, type received: {event.type}"
        )
Exemplo n.º 5
0
def assert_equal_graphs(feed_graph: CurrencyNetworkGraph,
                        event_graph: CurrencyNetworkGraph):
    trustlines_of_feed = feed_graph.get_trustlines_list()
    trustlines_of_event = event_graph.get_trustlines_list()
    for trustline in trustlines_of_feed:
        if trustline not in trustlines_of_event:
            assert (
                False
            ), f"Found trustline in graph from feed not in graph from events {trustline}"
        else:
            feed_data = feed_graph.graph.get_edge_data(trustline[0],
                                                       trustline[1])
            event_data = event_graph.graph.get_edge_data(
                trustline[0], trustline[1])
            assert feed_data["creditline_ab"] == event_data["creditline_ab"]
            assert feed_data["creditline_ba"] == event_data["creditline_ba"]
            assert feed_data["interest_ab"] == event_data["interest_ab"]
            assert feed_data["interest_ba"] == event_data["interest_ba"]
            assert feed_data["is_frozen"] == event_data["is_frozen"]
            assert feed_data["balance_ab"] == event_data["balance_ab"]
            if feed_data["balance_ab"] != 0:
                assert feed_data["m_time"] == event_data["m_time"]
Exemplo n.º 6
0
def does_trustline_update_trigger_balance_update(
        graph: CurrencyNetworkGraph,
        trustline_update_event: TrustlineUpdateEvent):
    # If the interests rate change and the balance was not 0
    # a BalanceUpdate event will be emitted due to application of interests
    trustline = graph.get_account_summary(
        trustline_update_event.from_,
        trustline_update_event.to,
        trustline_update_event.timestamp,
    )
    return trustline.balance != 0 and (
        trustline.interest_rate_given !=
        trustline_update_event.interest_rate_given
        or trustline.interest_rate_received !=
        trustline_update_event.interest_rate_received)
Exemplo n.º 7
0
def test_get_mediation_fees(
    ethindex_db_for_currency_network_with_trustlines_and_interests,
    currency_network_with_trustlines_and_interests_session,
    web3,
    accounts,
    fee_payer,
    transfer_value,
    fee_value,
    wait_for_ethindex_to_sync,
):
    currency_network = currency_network_with_trustlines_and_interests_session
    tx_hash = make_transfer(
        currency_network,
        transfer_value,
        [accounts[0], accounts[1], accounts[2]],
        fee_payer,
    )
    timestamp = web3.eth.getBlock("latest")["timestamp"]

    wait_for_ethindex_to_sync()
    graph = CurrencyNetworkGraph(
        capacity_imbalance_fee_divisor=currency_network.
        capacity_imbalance_fee_divisor,
        default_interest_rate=currency_network.default_interest_rate,
        custom_interests=currency_network.custom_interests,
        prevent_mediator_interests=currency_network.prevent_mediator_interests,
    )
    fees = EventsInformationFetcher(
        ethindex_db_for_currency_network_with_trustlines_and_interests
    ).get_earned_mediation_fees(accounts[1], graph)

    assert len(fees) == 1
    assert_fee(
        fees[0],
        value=fee_value,
        from_=accounts[0],
        to=accounts[2],
        tx_hash=tx_hash,
        timestamp=timestamp,
    )
Exemplo n.º 8
0
def complex_community_with_trustlines(complextrustlines):
    community = CurrencyNetworkGraph()
    community.gen_network(complextrustlines)
    return community
Exemplo n.º 9
0
def complex_community_with_trustlines_and_fees_10(complextrustlines):
    community = CurrencyNetworkGraph(capacity_imbalance_fee_divisor=10)
    community.gen_network(complextrustlines)
    return community
Exemplo n.º 10
0
def test_no_creditlines():
    community = CurrencyNetworkGraph()
    assert community.total_creditlines == 0
Exemplo n.º 11
0
def balances_community(balance_trustlines):
    community = CurrencyNetworkGraph()
    community.gen_network(balance_trustlines)
    return community
Exemplo n.º 12
0
def community_with_trustlines_and_fees(trustlines):
    community = CurrencyNetworkGraph(100)
    community.gen_network(trustlines)
    return community
Exemplo n.º 13
0
def fresh_community(currency_network):
    community = CurrencyNetworkGraph(100)
    link_graph(currency_network, community)
    gevent.sleep(0.0001)
    return community
Exemplo n.º 14
0
def community_with_trustlines(currency_network_with_trustlines):
    community = CurrencyNetworkGraph(100)
    community.gen_network(
        currency_network_with_trustlines.gen_graph_representation())
    link_graph(currency_network_with_trustlines, community)
    return community
Exemplo n.º 15
0
def fresh_community(currency_network):
    community = CurrencyNetworkGraph(100)
    greenlets = link_graph(currency_network, community)
    gevent.sleep(0.0001)
    yield community
    gevent.killall(greenlets)
Exemplo n.º 16
0
def test_get_mediation_fees_with_pollution(
    ethindex_db_for_currency_network_with_trustlines_and_interests,
    currency_network_with_trustlines_and_interests_session:
    CurrencyNetworkProxy,
    web3,
    accounts,
    fee_payer,
    transfer_value,
    fee_value,
    wait_for_ethindex_to_sync,
):
    """test getting the mediation fees while there are other trustline updates / transfer polluting events"""
    currency_network = currency_network_with_trustlines_and_interests_session
    custom_make_transfer = functools.partial(
        make_transfer,
        currency_network,
        transfer_value,
        [accounts[0], accounts[1], accounts[2]],
        fee_payer,
    )

    tx_hash0 = custom_make_transfer()
    timestamp0 = web3.eth.getBlock("latest")["timestamp"]

    currency_network.update_trustline_and_reject(accounts[0], accounts[1],
                                                 100_000, 100_000)
    tx_hash1 = custom_make_transfer()
    timestamp1 = web3.eth.getBlock("latest")["timestamp"]

    currency_network.update_trustline_with_accept(accounts[0], accounts[1],
                                                  10_000, 10_000)
    tx_hash2 = custom_make_transfer()
    timestamp2 = web3.eth.getBlock("latest")["timestamp"]

    currency_network.transfer_on_path(123, [accounts[1], accounts[0]])
    currency_network.transfer_receiver_pays_on_path(
        123, [accounts[1], accounts[2], accounts[3]])

    tx_hash3 = custom_make_transfer()
    timestamp3 = web3.eth.getBlock("latest")["timestamp"]

    # Pollution from opening trustline with a transfer
    open_trustline_transfer_value = 123

    currency_network.settle_and_close_trustline(accounts[1], accounts[2])
    currency_network.update_trustline_with_accept(
        accounts[1],
        accounts[2],
        12345,
        12345,
        0,
        0,
        False,
        open_trustline_transfer_value,
    )

    currency_network.settle_and_close_trustline(accounts[1], accounts[2])
    currency_network.update_trustline_with_accept(
        accounts[1],
        accounts[2],
        12345,
        12345,
        0,
        0,
        False,
        -open_trustline_transfer_value,
    )

    currency_network.settle_and_close_trustline(accounts[1], accounts[2])
    currency_network.update_trustline_with_accept(
        accounts[2],
        accounts[1],
        12345,
        12345,
        0,
        0,
        False,
        open_trustline_transfer_value,
    )

    currency_network.settle_and_close_trustline(accounts[1], accounts[2])
    currency_network.update_trustline_with_accept(
        accounts[2],
        accounts[1],
        12345,
        12345,
        0,
        0,
        False,
        -open_trustline_transfer_value,
    )

    tx_hash4 = custom_make_transfer()
    timestamp4 = web3.eth.getBlock("latest")["timestamp"]

    wait_for_ethindex_to_sync()
    graph = CurrencyNetworkGraph(
        capacity_imbalance_fee_divisor=currency_network.
        capacity_imbalance_fee_divisor,
        default_interest_rate=currency_network.default_interest_rate,
        custom_interests=currency_network.custom_interests,
        prevent_mediator_interests=currency_network.prevent_mediator_interests,
    )
    fees = EventsInformationFetcher(
        ethindex_db_for_currency_network_with_trustlines_and_interests
    ).get_earned_mediation_fees(accounts[1], graph)

    assert len(fees) == 5
    custom_assert_fee = functools.partial(assert_fee,
                                          value=fee_value,
                                          from_=accounts[0],
                                          to=accounts[2])
    custom_assert_fee(fee=fees[0], tx_hash=tx_hash0, timestamp=timestamp0)
    custom_assert_fee(fee=fees[1], tx_hash=tx_hash1, timestamp=timestamp1)
    custom_assert_fee(fee=fees[2], tx_hash=tx_hash2, timestamp=timestamp2)
    custom_assert_fee(fee=fees[3], tx_hash=tx_hash3, timestamp=timestamp3)
    custom_assert_fee(fee=fees[4], tx_hash=tx_hash4, timestamp=timestamp4)