Ejemplo n.º 1
0
def test_download_single_squeak(
    admin_stub,
    other_admin_stub,
    signing_profile_id,
    saved_squeak_hash,
    admin_peer,
):

    with subscribe_squeak_entry(other_admin_stub, saved_squeak_hash) as subscription_queue, \
            subscribe_squeak_ancestor_entries(other_admin_stub, saved_squeak_hash) as ancestor_subscription_queue:

        # Get the squeak display item (should be empty)
        squeak_display_entry = get_squeak_display(
            other_admin_stub, saved_squeak_hash)
        assert squeak_display_entry is None

        # Get buy offers for the squeak hash (should be empty)
        get_buy_offers_response = other_admin_stub.GetBuyOffers(
            squeak_admin_pb2.GetBuyOffersRequest(
                squeak_hash=saved_squeak_hash,
            )
        )
        # print(get_buy_offers_response)
        assert len(get_buy_offers_response.offers) == 0

        # Download squeak
        download_result = download_squeak(other_admin_stub, saved_squeak_hash)
        # Download secret key
        download_squeak_secret_key(other_admin_stub, saved_squeak_hash)
        assert download_result.number_downloaded == 1
        assert download_result.number_requested == 1

        # Get the squeak display item
        squeak_display_entry = get_squeak_display(
            other_admin_stub, saved_squeak_hash)
        assert squeak_display_entry is not None

        # Get the buy offer
        get_buy_offers_response = other_admin_stub.GetBuyOffers(
            squeak_admin_pb2.GetBuyOffersRequest(
                squeak_hash=saved_squeak_hash,
            )
        )
        # print(get_buy_offers_response)
        assert len(get_buy_offers_response.offers) > 0

        item = subscription_queue.get()
        print("subscription_queue item:")
        print(item)
        assert item.squeak_hash == saved_squeak_hash

        item = ancestor_subscription_queue.get()
        print("ancestor_subscription_queue item:")
        print(item)
        assert item[0].squeak_hash == saved_squeak_hash
Ejemplo n.º 2
0
def test_download_free_squeak(
    admin_stub,
    other_admin_stub,
    saved_squeak_hash,
    admin_peer,
):
    with free_price(admin_stub):
        # Download squeak
        download_result = download_squeak(other_admin_stub, saved_squeak_hash)
        # Download secret key
        download_squeak_secret_key(other_admin_stub, saved_squeak_hash)
        print('download_result:')
        print(download_result)
        assert download_result.number_downloaded == 1
        assert download_result.number_requested == 1

    # Get the squeak display item
    get_squeak_display_entry = get_squeak_display(
        other_admin_stub, saved_squeak_hash)
    assert (
        get_squeak_display_entry.content_str == "Hello from the profile on the server!"
    )
Ejemplo n.º 3
0
def test_download_free_squeak(
    admin_stub,
    other_admin_stub,
    connected_tcp_peer_id,
    saved_squeak_hash,
):
    with free_price(admin_stub):
        # Download squeak
        download_result = download_squeak(other_admin_stub, saved_squeak_hash)
        print('download_result:')
        print(download_result)
        assert download_result.number_downloaded == 1
        assert download_result.number_requested == 1

        # Download offer
        download_offers(other_admin_stub, saved_squeak_hash)
        time.sleep(5)

    # Get the squeak display item
    get_squeak_display_entry = get_squeak_display(other_admin_stub,
                                                  saved_squeak_hash)
    assert (get_squeak_display_entry.content_str ==
            "Hello from the profile on the server!")
Ejemplo n.º 4
0
def test_buy_squeak(
    admin_stub,
    other_admin_stub,
    connected_tcp_peer_id,
    signing_profile_id,
    saved_squeak_hash,
):
    # Download squeak
    download_squeak(other_admin_stub, saved_squeak_hash)

    # Download offer
    download_offers(other_admin_stub, saved_squeak_hash)
    time.sleep(5)

    # Get the sent offers from the seller node
    get_sent_offers_response = admin_stub.GetSentOffers(
        squeak_admin_pb2.GetSentOffersRequest(), )
    squeak_hashes = [
        sent_offer.squeak_hash
        for sent_offer in get_sent_offers_response.sent_offers
    ]
    assert saved_squeak_hash in squeak_hashes

    # Get the buy offer
    get_buy_offers_response = other_admin_stub.GetBuyOffers(
        squeak_admin_pb2.GetBuyOffersRequest(squeak_hash=saved_squeak_hash, ))
    # print(get_buy_offers_response)
    assert len(get_buy_offers_response.offers) > 0

    offer = get_buy_offers_response.offers[0]

    print("Tring to connect LND peer with offer: {}".format(offer))
    with peer_connection(other_admin_stub, offer.node_host,
                         offer.node_pubkey), channel(other_admin_stub,
                                                     offer.node_pubkey,
                                                     1000000):

        print("Channel context manager opened.")

        # Pay the offer
        list_channels_response = other_admin_stub.LndListChannels(
            ln.ListChannelsRequest(active_only=True, ))
        print("list_channels_response: {}".format(list_channels_response))

        # Pay the offer
        pay_offer_response = other_admin_stub.PayOffer(
            squeak_admin_pb2.PayOfferRequest(offer_id=offer.offer_id, ))
        # print(pay_offer_response)
        assert pay_offer_response.sent_payment_id > 0

        # Get the squeak display item
        get_squeak_display_entry = get_squeak_display(other_admin_stub,
                                                      saved_squeak_hash)
        assert (get_squeak_display_entry.content_str ==
                "Hello from the profile on the server!")

        # Get all sent payments
        get_sent_payments_response = other_admin_stub.GetSentPayments(
            squeak_admin_pb2.GetSentPaymentsRequest(limit=10, ), )
        squeak_hashes = [
            sent_payment.squeak_hash
            for sent_payment in get_sent_payments_response.sent_payments
        ]
        assert saved_squeak_hash in squeak_hashes

        # Get the single sent payment
        for sent_payment in get_sent_payments_response.sent_payments:
            if sent_payment.squeak_hash == saved_squeak_hash:
                sent_payment_id = sent_payment.sent_payment_id
        get_sent_payment_response = other_admin_stub.GetSentPayment(
            squeak_admin_pb2.GetSentPaymentRequest(
                sent_payment_id=sent_payment_id, ), )
        assert saved_squeak_hash == get_sent_payment_response.sent_payment.squeak_hash
        assert get_sent_payment_response.sent_payment.price_msat == 1000000
        assert get_sent_payment_response.sent_payment.valid

        # Get the received payment from the seller node
        get_received_payments_response = admin_stub.GetReceivedPayments(
            squeak_admin_pb2.GetReceivedPaymentsRequest(limit=100, ), )
        # print(
        #     "get_received_payments_response: {}".format(
        #         get_received_payments_response)
        # )
        payment_hashes = [
            received_payment.payment_hash for received_payment in
            get_received_payments_response.received_payments
        ]
        assert sent_payment.payment_hash in payment_hashes
        for received_payment in get_received_payments_response.received_payments:
            received_payment_time_ms = received_payment.time_ms
            # print("received_payment_time_s: {}".format(
            #     received_payment_time_s))
            received_payment_time = datetime.datetime.fromtimestamp(
                received_payment_time_ms / 1000, )
            five_minutes = datetime.timedelta(minutes=5)
            assert received_payment_time > datetime.datetime.now(
            ) - five_minutes
            assert received_payment_time < datetime.datetime.now()
            assert len(received_payment.peer_address.host) > 4

        # Subscribe to received payments starting from index zero
        subscribe_received_payments_response = admin_stub.SubscribeReceivedPayments(
            squeak_admin_pb2.SubscribeReceivedPaymentsRequest(
                payment_index=0, ), )
        for payment in subscribe_received_payments_response:
            # print("Got payment from subscription: {}".format(payment))
            assert payment.received_payment_id == 1
            break

        # Get the payment summary from the seller node
        get_payment_summary_response = admin_stub.GetPaymentSummary(
            squeak_admin_pb2.GetPaymentSummaryRequest(), )
        # print(
        #     "get_payment_summary_response from seller: {}".format(
        #         get_payment_summary_response)
        # )
        assert get_payment_summary_response.payment_summary.num_received_payments > 0
        assert get_payment_summary_response.payment_summary.amount_earned_msat > 0

        # Get the payment summary from the buyer node
        get_payment_summary_response = other_admin_stub.GetPaymentSummary(
            squeak_admin_pb2.GetPaymentSummaryRequest(), )
        # print(
        #     "get_payment_summary_response from buyer: {}".format(
        #         get_payment_summary_response)
        # )
        assert get_payment_summary_response.payment_summary.num_sent_payments > 0
        assert get_payment_summary_response.payment_summary.amount_spent_msat > 0

    print("Channel context manager closed.")