コード例 #1
0
def test_payee_deposit_expired(connected_clients, server_db):
    alice, bob, charlie, david, eric, fred = connected_clients
    try:

        # let client deposit expire
        c2h_deposit_ttl = 9999999999999999
        while c2h_deposit_ttl > 0:
            util_test.create_next_block(server_db)
            status = alice.get_status(clearance=0)
            c2h_deposit_ttl = status["send_deposit_ttl"]

        # attempt to transfer funds
        bob.micro_send(alice.handle, 1337)
        bob.sync()

        assert False
    except err.DepositExpired:
        assert True
コード例 #2
0
def test_standard_usage(server_db):

    # fund server
    for i in range(4):
        address = lib.get_funding_address()
        unsigned_rawtx = api.create_send(**{
            'source': FUNDING_ADDRESS,
            'destination': address,
            'asset': ASSET,
            'quantity': 1000000,
            'regular_dust_size': 1000000
        })
        signed_rawtx = scripts.sign_deposit(
            get_txs, FUNDING_WIF, unsigned_rawtx
        )
        api.sendrawtransaction(tx_hex=signed_rawtx)

    # connect clients
    status = api.mph_status()
    assert len(status["connections"]) == 0
    clients = []
    for i in range(4):
        auth_wif = util.gen_funded_wif(ASSET, 1000000, 1000000)
        client = Mph(util.MockAPI(auth_wif=auth_wif))
        txid = client.connect(1000000, 42, asset=ASSET)
        assert txid is not None

        status = client.get_status()
        assert status["send_balance"] == 1000000
        assert status["send_deposit_ttl"] is not None
        assert status["recv_deposit_ttl"] is None  # hub deposit not yet made
        clients.append(client)
    assert len(api.mph_status()["connections"]) == 4

    # server funds deposits
    assert len(cron.fund_deposits()) == 4
    assert len(cron.fund_deposits()) == 0
    for client in clients:
        status = client.get_status()
        assert status["recv_deposit_ttl"] is not None  # hub deposit now made

    # before status
    alpha, beta, gamma, delta = clients
    alpha_before_status = alpha.get_status()
    beta_before_status = beta.get_status()
    gamma_before_status = gamma.get_status()

    # can send multiple payments
    alpha.micro_send(beta.handle, 5, "0000")
    alpha.micro_send(gamma.handle, 6, "0001")
    assert alpha.sync() == []
    assert beta.sync() == [{
        "payer_handle": alpha.handle,
        "amount": 5,
        "token": "0000"
    }]
    assert gamma.sync(), [{
        "payer_handle": alpha.handle,
        "amount": 6,
        "token": "0001"
    }]

    # send more back, commits are revoked to maximize liquidity
    beta.micro_send(alpha.handle, 42, "0003")
    assert beta.sync() == []
    assert alpha.sync() == [{
        "payer_handle": beta.handle,
        "amount": 42,
        "token": "0003"
    }]

    # multiple syncs/commtis from single client
    alpha.micro_send(beta.handle, 1, "0004")
    assert alpha.sync() == []

    # get after status
    alpha_after_status = alpha.get_status()
    beta_after_status = beta.get_status()
    gamma_after_status = gamma.get_status()

    # compare statuses
    alpha_after_status["send_balance"] == alpha_before_status[
        "send_balance"] + 27
    beta_after_status["send_balance"] == beta_before_status[
        "send_balance"] - 40
    gamma_after_status["send_balance"] == gamma_before_status[
        "send_balance"] + 5

    # client | c2h active | h2c active
    # -------+------------+-----------
    # alpha  | 1 commit   | 1 commit
    # beta   | 1 commit   | 0 commit
    # gamma  | 0 commit   | 1 commit
    # delta  | 0 commit   | 0 commit
    assert len(alpha.c2h_state["commits_active"]) == 1
    assert len(alpha.h2c_state["commits_active"]) == 1
    assert len(beta.c2h_state["commits_active"]) == 1
    assert len(beta.h2c_state["commits_active"]) == 0
    assert len(gamma.c2h_state["commits_active"]) == 0
    assert len(gamma.h2c_state["commits_active"]) == 1
    assert len(delta.c2h_state["commits_active"]) == 0
    assert len(delta.h2c_state["commits_active"]) == 0

    # close alpha payment channel
    #        | h2c | c2h |
    # -------+-----+-----+
    # commit |  X  |  X  |
    # payout |  X  |  X  |
    # change |  X  |  X  |
    assert len(api.mph_status()["connections"]) == 4
    assert alpha.close() is not None        # H2C COMMIT TX
    assert len(api.mph_status()["connections"]) == 3
    assert _check_rawtxs(alpha.update())  # h2c payout delay not yet passed
    util_test.create_next_block(server_db)  # let payout delay pass
    assert _check_rawtxs(alpha.update(), payout=1)  # H2C PAYOUT TX

    # H2C CHANGE TX, C2H COMMIT TX
    assert _check_rawtxs(cron.run_all(), change=1, commit=1)

    util_test.create_next_block(server_db)  # let c2h payout delay pass
    assert _check_rawtxs(cron.run_all(), payout=1)  # C2H PAYOUT TX
    assert _check_rawtxs(alpha.update(), change=1)  # C2H CHANGE TX

    # close beta payment channel
    # recover | h2c | c2h |
    # --------+-----+-----+
    # commit  |  -  |  X  |
    # payout  |  -  |  X  |
    # change  |  X  |  X  |
    assert len(api.mph_status()["connections"]) == 3
    assert beta.close() is None
    assert len(api.mph_status()["connections"]) == 2
    # H2C CHANGE TX, C2H COMMIT TX
    assert _check_rawtxs(cron.run_all(), change=1, commit=1)
    util_test.create_next_block(server_db)  # let payout delay pass
    assert _check_rawtxs(cron.run_all(), payout=1)  # C2H PAYOUT TX
    assert _check_rawtxs(beta.update(), change=1)  # C2H CHANGE TX

    # close gamma payment channel
    # recover | h2c | c2h |
    # --------+-----+-----+
    # commit  |  X  |  -  |
    # payout  |  X  |  -  |
    # change  |  X  |  X  |
    assert len(api.mph_status()["connections"]) == 2
    assert gamma.close() is not None        # H2C COMMIT TX
    assert len(api.mph_status()["connections"]) == 1
    assert _check_rawtxs(gamma.update(), change=1)  # C2H CHANGE TX
    assert _check_rawtxs(gamma.update(), payout=1)  # H2C PAYOUT TX
    assert _check_rawtxs(cron.run_all(), change=1)  # H2C CHANGE TX

    # close delta payment channel
    # recover | h2c | c2h |
    # --------+-----+-----+
    # commit  |  -  |  -  |
    # payout  |  -  |  -  |
    # change  |  X  |  X  |
    assert len(api.mph_status()["connections"]) == 1
    assert delta.close() is None
    assert len(api.mph_status()["connections"]) == 0
    assert _check_rawtxs(delta.update(), change=1)  # C2H CHANGE TX
    assert _check_rawtxs(cron.run_all(), change=1)  # H2C CHANGE TX

    _assert_states_synced(alpha.handle, alpha.c2h_state, alpha.h2c_state)
    _assert_states_synced(beta.handle, beta.c2h_state, beta.h2c_state)
    _assert_states_synced(gamma.handle, gamma.c2h_state, gamma.h2c_state)
    _assert_states_synced(delta.handle, delta.c2h_state, delta.h2c_state)
コード例 #3
0
def test_user_doesnt_publish_commit(server_db):

    # fund server
    for i in range(2):
        address = lib.get_funding_address()
        unsigned_rawtx = api.create_send(**{
            'source': FUNDING_ADDRESS,
            'destination': address,
            'asset': ASSET,
            'quantity': 1000000,
            'regular_dust_size': 1000000
        })
        signed_rawtx = scripts.sign_deposit(get_txs, FUNDING_WIF,
                                            unsigned_rawtx)
        api.sendrawtransaction(tx_hex=signed_rawtx)

    # connect clients
    assert len(api.mph_status()["connections"]) == 0
    clients = []
    for i in range(2):
        auth_wif = util.gen_funded_wif(ASSET, 1000000, 1000000)
        client = Mph(util.MockAPI(auth_wif=auth_wif))
        txid = client.connect(1000000, 42, asset=ASSET)
        assert txid is not None

        status = client.get_status()
        assert status["send_balance"] == 1000000
        assert status["send_deposit_ttl"] is not None
        assert status["recv_deposit_ttl"] is None  # hub deposit not yet made
        clients.append(client)
    assert len(api.mph_status()["connections"]) == 2

    # server funds deposits
    assert len(cron.fund_deposits()) == 2
    assert len(cron.fund_deposits()) == 0
    for client in clients:
        status = client.get_status()
        assert status["recv_deposit_ttl"] is not None  # hub deposit now made

    # before status
    alpha, beta = clients

    # send funds to beta
    alpha.micro_send(beta.handle, 42)
    alpha.sync()
    alpha_status = alpha.get_status()
    assert alpha_status["send_balance"] == 1000000 - 42 - 1

    # beta received funds
    beta.sync()
    beta_status = beta.get_status()
    assert beta_status["send_balance"] == 1000000 + 42 - 1

    # return some funds to alpha
    beta.micro_send(alpha.handle, 13)
    beta.sync()
    beta_status = beta.get_status()
    assert beta_status["send_balance"] == 1000000 + 42 - 1 - 13 - 1

    # alpha received funds
    alpha.sync()
    alpha_status = alpha.get_status()
    assert alpha_status["send_balance"] == 1000000 - 42 - 1 + 13 - 1

    # beta settles
    assert len(api.mph_status()["connections"]) == 2
    assert beta.close() is not None  # H2C COMMIT TX
    assert len(api.mph_status()["connections"]) == 1
    assert _check_rawtxs(beta.update())  # h2c payout delay not yet passed
    util_test.create_next_block(server_db)  # let payout delay pass
    assert _check_rawtxs(beta.update(), payout=1)  # H2C PAYOUT TX
    # H2C CHANGE TX, C2H COMMIT TX
    assert _check_rawtxs(cron.run_all(), change=1, commit=1)
    util_test.create_next_block(server_db)  # let c2h payout delay pass
    assert _check_rawtxs(cron.run_all(), payout=1)  # C2H PAYOUT TX
    assert _check_rawtxs(beta.update(), change=1)  # C2H CHANGE TX

    # let channel expire
    for i in range(50):
        util_test.create_next_block(server_db)

    # hub closes alice channel
    #        | h2c | c2h |
    # -------+-----+-----+
    # commit |  -  |  X  |
    # payout |  -  |  X  |
    # change |  -  |  -  |
    # expire |  X  |  -  |
    # C2H COMMIT TX, H2C EXPIRE TX
    assert _check_rawtxs(cron.run_all(), commit=1, expire=1)
    assert _check_rawtxs(cron.run_all(), payout=1)  # C2H PAYOUT TX
コード例 #4
0
def test_standard_usage(server_db):

    # fund server
    for i in range(3):
        address = lib.get_funding_addresses([ASSET])[ASSET]
        rawtx = api.create_send(
            **{
                'source': FUNDING_ADDRESS,
                'destination': address,
                'asset': ASSET,
                'quantity': 1000000,
                'regular_dust_size': 1000000
            })
        api.sendrawtransaction(tx_hex=rawtx)

    # connect clients
    assert len(api.mph_connections()) == 0
    clients = []
    for i in range(3):
        auth_wif = util.gen_funded_wif(ASSET, 1000000, 1000000)
        client = Mph(util.MockAPI(auth_wif=auth_wif))
        txid = client.connect(1000000, 65535, asset=ASSET)
        assert txid is not None

        status = client.get_status()
        assert status["balance"] == 1000000
        assert status["c2h_deposit_ttl"] is not None
        assert status["h2c_deposit_ttl"] is None  # hub deposit not yet made
        clients.append(client)
    assert len(api.mph_connections()) == 3

    # server funds deposits
    assert len(cron.fund_deposits()) == 3
    assert len(cron.fund_deposits()) == 0
    for client in clients:
        status = client.get_status()
        assert status["h2c_deposit_ttl"] is not None  # hub deposit now made

    # after before status
    alpha, beta, gamma = clients
    alpha_before_status = alpha.get_status()
    beta_before_status = beta.get_status()
    gamma_before_status = gamma.get_status()

    # can send multiple payments
    alpha.micro_send(beta.handle, 5, "0000")
    alpha.micro_send(gamma.handle, 6, "0001")
    assert alpha.sync() == []
    assert beta.sync() == [{
        "payer_handle": alpha.handle,
        "amount": 5,
        "token": "0000"
    }]
    assert gamma.sync(), [{
        "payer_handle": alpha.handle,
        "amount": 6,
        "token": "0001"
    }]

    # send more back, commits are revoked to maximize liquidity
    beta.micro_send(alpha.handle, 42, "0003")
    assert beta.sync() == []
    assert alpha.sync() == [{
        "payer_handle": beta.handle,
        "amount": 42,
        "token": "0003"
    }]

    # multiple syncs/commtis from single client
    alpha.micro_send(beta.handle, 1, "0004")
    assert alpha.sync() == []

    # get after status
    alpha_after_status = alpha.get_status()
    beta_after_status = beta.get_status()
    gamma_after_status = gamma.get_status()

    # compare statuses
    alpha_after_status["balance"] == alpha_before_status["balance"] + 27
    beta_after_status["balance"] == beta_before_status["balance"] - 40
    gamma_after_status["balance"] == gamma_before_status["balance"] + 5

    # close alpha payment channel
    assert alpha.close() is not None  # commit txid returned (block created)
    assert len(alpha.update()) == 0  # payout delay not yet passed
    for i in range(alpha.c2h_commit_delay_time - 1):  # let payout delay pass
        util_test.create_next_block(server_db)
    assert len(alpha.update()) == 1  # payout txid

    # close beta payment channel
    assert beta.close() is not None  # commit txid returned (block created)
    assert len(beta.update()) == 0  # payout delay not yet passed
    for i in range(beta.c2h_commit_delay_time - 1):  # let payout delay pass
        util_test.create_next_block(server_db)
    assert len(beta.update()) == 1  # payout txid

    # close gamma payment channel
    assert gamma.close() is not None  # commit txid returned (block created)
    assert len(gamma.update()) == 0  # payout delay not yet passed
    for i in range(gamma.c2h_commit_delay_time - 1):  # let payout delay pass
        util_test.create_next_block(server_db)
    assert len(gamma.update()) == 1  # payout txid

    # hub close connections cron
    cron.run_all()
    assert len(api.mph_connections()) == 0

    # recover c2h change
    assert len(alpha.update()) == 1  # txid
    assert len(beta.update()) == 1  # txid
    assert len(gamma.update()) == 1  # txid
コード例 #5
0
def test_standard_usage(server_db):

    # fund server
    for i in range(4):
        address = lib.get_funding_addresses([ASSET])[ASSET]
        unsigned_rawtx = api.create_send(
            **{
                'source': FUNDING_ADDRESS,
                'destination': address,
                'asset': ASSET,
                'quantity': 1000000,
                'regular_dust_size': 1000000
            })
        signed_rawtx = scripts.sign_deposit(get_tx, FUNDING_WIF,
                                            unsigned_rawtx)
        api.sendrawtransaction(tx_hex=signed_rawtx)

    # connect clients
    assert len(api.mph_status()["connections"]) == 0
    clients = []
    for i in range(4):
        auth_wif = util.gen_funded_wif(ASSET, 1000000, 1000000)
        client = Mph(util.MockAPI(auth_wif=auth_wif))
        txid = client.connect(1000000, 42, asset=ASSET)
        assert txid is not None

        status = client.get_status()
        assert status["send_balance"] == 1000000
        assert status["send_deposit_ttl"] is not None
        assert status["recv_deposit_ttl"] is None  # hub deposit not yet made
        clients.append(client)
    assert len(api.mph_status()["connections"]) == 4

    # server funds deposits
    assert len(cron.fund_deposits()) == 4
    assert len(cron.fund_deposits()) == 0
    for client in clients:
        status = client.get_status()
        assert status["recv_deposit_ttl"] is not None  # hub deposit now made

    # before status
    alpha, beta, gamma, delta = clients
    alpha_before_status = alpha.get_status()
    beta_before_status = beta.get_status()
    gamma_before_status = gamma.get_status()

    # can send multiple payments
    alpha.micro_send(beta.handle, 5, "0000")
    alpha.micro_send(gamma.handle, 6, "0001")
    assert alpha.sync() == []
    assert beta.sync() == [{
        "payer_handle": alpha.handle,
        "amount": 5,
        "token": "0000"
    }]
    assert gamma.sync(), [{
        "payer_handle": alpha.handle,
        "amount": 6,
        "token": "0001"
    }]

    # send more back, commits are revoked to maximize liquidity
    beta.micro_send(alpha.handle, 42, "0003")
    assert beta.sync() == []
    assert alpha.sync() == [{
        "payer_handle": beta.handle,
        "amount": 42,
        "token": "0003"
    }]

    # multiple syncs/commtis from single client
    alpha.micro_send(beta.handle, 1, "0004")
    assert alpha.sync() == []

    # get after status
    alpha_after_status = alpha.get_status()
    beta_after_status = beta.get_status()
    gamma_after_status = gamma.get_status()

    # compare statuses
    alpha_after_status[
        "send_balance"] == alpha_before_status["send_balance"] + 27
    beta_after_status[
        "send_balance"] == beta_before_status["send_balance"] - 40
    gamma_after_status[
        "send_balance"] == gamma_before_status["send_balance"] + 5

    # client | c2h active | h2c active
    # -------+------------+-----------
    # alpha  | 1 commit   | 1 commit
    # beta   | 1 commit   | 0 commit
    # gamma  | 0 commit   | 1 commit
    # delta  | 0 commit   | 0 commit
    assert len(alpha.c2h_state["commits_active"]) == 1
    assert len(alpha.h2c_state["commits_active"]) == 1
    assert len(beta.c2h_state["commits_active"]) == 1
    assert len(beta.h2c_state["commits_active"]) == 0
    assert len(gamma.c2h_state["commits_active"]) == 0
    assert len(gamma.h2c_state["commits_active"]) == 1
    assert len(delta.c2h_state["commits_active"]) == 0
    assert len(delta.h2c_state["commits_active"]) == 0

    # close alpha payment channel
    #        | h2c | c2h |
    # -------+-----+-----+
    # commit |  X  |  X  |
    # payout |  X  |  X  |
    # change |  X  |  X  |
    assert len(api.mph_status()["connections"]) == 4
    assert alpha.close() is not None  # H2C COMMIT TX
    assert len(api.mph_status()["connections"]) == 3
    assert _check_rawtxs(alpha.update())  # h2c payout delay not yet passed
    util_test.create_next_block(server_db)  # let payout delay pass
    assert _check_rawtxs(alpha.update(), payout=1)  # H2C PAYOUT TX

    # H2C CHANGE TX, C2H COMMIT TX
    assert _check_rawtxs(cron.run_all(), change=1, commit=1)

    util_test.create_next_block(server_db)  # let c2h payout delay pass
    assert _check_rawtxs(cron.run_all(), payout=1)  # C2H PAYOUT TX
    assert _check_rawtxs(alpha.update(), change=1)  # C2H CHANGE TX

    # close beta payment channel
    # recover | h2c | c2h |
    # --------+-----+-----+
    # commit  |  -  |  X  |
    # payout  |  -  |  X  |
    # change  |  X  |  X  |
    assert len(api.mph_status()["connections"]) == 3
    assert beta.close() is None
    assert len(api.mph_status()["connections"]) == 2
    # H2C CHANGE TX, C2H COMMIT TX
    assert _check_rawtxs(cron.run_all(), change=1, commit=1)
    util_test.create_next_block(server_db)  # let payout delay pass
    assert _check_rawtxs(cron.run_all(), payout=1)  # C2H PAYOUT TX
    assert _check_rawtxs(beta.update(), change=1)  # C2H CHANGE TX

    # close gamma payment channel
    # recover | h2c | c2h |
    # --------+-----+-----+
    # commit  |  X  |  -  |
    # payout  |  X  |  -  |
    # change  |  X  |  X  |
    assert len(api.mph_status()["connections"]) == 2
    assert gamma.close() is not None  # H2C COMMIT TX
    assert len(api.mph_status()["connections"]) == 1
    assert _check_rawtxs(gamma.update(), change=1)  # C2H CHANGE TX
    assert _check_rawtxs(gamma.update(), payout=1)  # H2C PAYOUT TX
    assert _check_rawtxs(cron.run_all(), change=1)  # H2C CHANGE TX

    # close delta payment channel
    # recover | h2c | c2h |
    # --------+-----+-----+
    # commit  |  -  |  -  |
    # payout  |  -  |  -  |
    # change  |  X  |  X  |
    assert len(api.mph_status()["connections"]) == 1
    assert delta.close() is None
    assert len(api.mph_status()["connections"]) == 0
    assert _check_rawtxs(delta.update(), change=1)  # C2H CHANGE TX
    assert _check_rawtxs(cron.run_all(), change=1)  # H2C CHANGE TX

    _assert_states_synced(alpha.handle, alpha.c2h_state, alpha.h2c_state)
    _assert_states_synced(beta.handle, beta.c2h_state, beta.h2c_state)
    _assert_states_synced(gamma.handle, gamma.c2h_state, gamma.h2c_state)
    _assert_states_synced(delta.handle, delta.c2h_state, delta.h2c_state)
コード例 #6
0
def test_user_doesnt_publish_commit(server_db):

    # fund server
    for i in range(2):
        address = lib.get_funding_addresses([ASSET])[ASSET]
        unsigned_rawtx = api.create_send(
            **{
                'source': FUNDING_ADDRESS,
                'destination': address,
                'asset': ASSET,
                'quantity': 1000000,
                'regular_dust_size': 1000000
            })
        signed_rawtx = scripts.sign_deposit(get_tx, FUNDING_WIF,
                                            unsigned_rawtx)
        api.sendrawtransaction(tx_hex=signed_rawtx)

    # connect clients
    assert len(api.mph_status()["connections"]) == 0
    clients = []
    for i in range(2):
        auth_wif = util.gen_funded_wif(ASSET, 1000000, 1000000)
        client = Mph(util.MockAPI(auth_wif=auth_wif))
        txid = client.connect(1000000, 42, asset=ASSET)
        assert txid is not None

        status = client.get_status()
        assert status["send_balance"] == 1000000
        assert status["send_deposit_ttl"] is not None
        assert status["recv_deposit_ttl"] is None  # hub deposit not yet made
        clients.append(client)
    assert len(api.mph_status()["connections"]) == 2

    # server funds deposits
    assert len(cron.fund_deposits()) == 2
    assert len(cron.fund_deposits()) == 0
    for client in clients:
        status = client.get_status()
        assert status["recv_deposit_ttl"] is not None  # hub deposit now made

    # before status
    alpha, beta = clients

    # send funds to beta
    alpha.micro_send(beta.handle, 42)
    alpha.sync()
    alpha_status = alpha.get_status()
    assert alpha_status["send_balance"] == 1000000 - 42 - 1

    # beta received funds
    beta.sync()
    beta_status = beta.get_status()
    assert beta_status["send_balance"] == 1000000 + 42 - 1

    # return some funds to alpha
    beta.micro_send(alpha.handle, 13)
    beta.sync()
    beta_status = beta.get_status()
    assert beta_status["send_balance"] == 1000000 + 42 - 1 - 13 - 1

    # alpha received funds
    alpha.sync()
    alpha_status = alpha.get_status()
    assert alpha_status["send_balance"] == 1000000 - 42 - 1 + 13 - 1

    # beta settles
    assert len(api.mph_status()["connections"]) == 2
    assert beta.close() is not None  # H2C COMMIT TX
    assert len(api.mph_status()["connections"]) == 1
    assert _check_rawtxs(beta.update())  # h2c payout delay not yet passed
    util_test.create_next_block(server_db)  # let payout delay pass
    assert _check_rawtxs(beta.update(), payout=1)  # H2C PAYOUT TX
    # H2C CHANGE TX, C2H COMMIT TX
    assert _check_rawtxs(cron.run_all(), change=1, commit=1)
    util_test.create_next_block(server_db)  # let c2h payout delay pass
    assert _check_rawtxs(cron.run_all(), payout=1)  # C2H PAYOUT TX
    assert _check_rawtxs(beta.update(), change=1)  # C2H CHANGE TX

    # let channel expire
    for i in range(50):
        util_test.create_next_block(server_db)

    # hub closes alice channel
    #        | h2c | c2h |
    # -------+-----+-----+
    # commit |  -  |  X  |
    # payout |  -  |  X  |
    # change |  -  |  -  |
    # expire |  X  |  -  |
    # C2H COMMIT TX, H2C EXPIRE TX
    assert _check_rawtxs(cron.run_all(), commit=1, expire=1)
    assert _check_rawtxs(cron.run_all(), payout=1)  # C2H PAYOUT TX
コード例 #7
0
def test_alice_bob(server_db):
    alice = ADDR[0]
    bob = "miJqNkHhC5xsB61gsiSWXeTLnEGSQnWbXB"

    # check alices UTXOs
    utxos = util.api('get_unspent_txouts', {"address": alice})
    assert len(utxos) == 1
    assert utxos[0]['address'] == alice
    assert utxos[0][
        'txid'] == "ae241be7be83ebb14902757ad94854f787d9730fc553d6f695346c9375c0d8c1"
    assert utxos[0]['amount'] == 1.9990914
    assert utxos[0]['confirmations'] == 74

    # balance before send
    alice_balance = util.get_balance(server_db, alice, 'XCP')
    bob_balance = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance == 91875000000
    assert bob_balance == 0

    # create send
    v = int(100 * 1e8)
    send1hex = util.api('create_send', {
        'source': alice,
        'destination': bob,
        'asset': 'XCP',
        'quantity': v
    })
    assert send1hex == "0100000001c1d8c075936c3495f6d653c50f73d987f75448d97a750249b1eb83bee71b24ae000000001976a9144838d8b3588c4c7ba7c1d06f866e9b3739c6303788acffffffff0336150000000000001976a9141e9d9c2c34d4dda3cd71603d9ce1e447c3cc5c0588ac00000000000000001e6a1c8a5dda15fb6f05628a061e67576e926dc71a7fa2f0cceb951120a9322f30ea0b000000001976a9144838d8b3588c4c7ba7c1d06f866e9b3739c6303788ac00000000"

    # insert send, this automatically also creates a block
    tx1hash, tx1 = util_test.insert_raw_transaction(send1hex, server_db)

    # balances after send
    alice_balance2 = util.get_balance(server_db, alice, 'XCP')
    bob_balance2 = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance2 == alice_balance - v
    assert bob_balance2 == bob_balance + v

    # check API result
    result = util.api(
        "get_balances", {
            "filters": [
                {
                    'field': 'address',
                    'op': '==',
                    'value': alice
                },
                {
                    'field': 'asset',
                    'op': '==',
                    'value': 'XCP'
                },
            ]
        })

    assert result[0]['quantity'] == alice_balance2

    # -- do another TX

    # check alices UTXOs
    utxos = util.api('get_unspent_txouts', {"address": alice})
    assert len(utxos) == 1
    assert utxos[0]['address'] == alice
    assert utxos[0]['txid'] == tx1['tx_hash']
    assert utxos[0]['amount'] == 1.99897135
    assert utxos[0]['confirmations'] == 1

    # balances before send
    alice_balance = util.get_balance(server_db, alice, 'XCP')
    bob_balance = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance == alice_balance2
    assert bob_balance == bob_balance2

    # create send
    v = int(100 * 1e8)
    send2hex = util.api('create_send', {
        'source': alice,
        'destination': bob,
        'asset': 'XCP',
        'quantity': v
    })
    assert send2hex == "0100000001cd2d431037d1d0cfe05daeb1d08b975f27488e383f7f169e09d2f405fb618f39020000001976a9144838d8b3588c4c7ba7c1d06f866e9b3739c6303788acffffffff0336150000000000001976a9141e9d9c2c34d4dda3cd71603d9ce1e447c3cc5c0588ac00000000000000001e6a1c8a5dda15fb6f05628a061e67576e926dc71a7fa2f0cceb951120a9324a01ea0b000000001976a9144838d8b3588c4c7ba7c1d06f866e9b3739c6303788ac00000000"

    # insert send, this automatically also creates a block
    tx2hash, tx2 = util_test.insert_raw_transaction(send2hex, server_db)

    # balances after send
    alice_balance2 = util.get_balance(server_db, alice, 'XCP')
    bob_balance2 = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance2 == alice_balance - v
    assert bob_balance2 == bob_balance + v

    # -- do another TX, now unconfirmed

    # check alices UTXOs
    utxos = util.api('get_unspent_txouts', {"address": alice})
    assert len(utxos) == 1
    assert utxos[0]['address'] == alice
    assert utxos[0]['txid'] == tx2['tx_hash']
    assert utxos[0]['amount'] == 1.9988513
    assert utxos[0]['confirmations'] == 1

    # balances before send
    alice_balance = util.get_balance(server_db, alice, 'XCP')
    bob_balance = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance == alice_balance2
    assert bob_balance == bob_balance2

    # create send
    v = int(100 * 1e8)
    send3hex = util.api('create_send', {
        'source': alice,
        'destination': bob,
        'asset': 'XCP',
        'quantity': v
    })
    assert send3hex == "01000000019aea7b78c8fffa50c51bbadb87824a202b3e6b53727e543e9c6846845205b5ce020000001976a9144838d8b3588c4c7ba7c1d06f866e9b3739c6303788acffffffff0336150000000000001976a9141e9d9c2c34d4dda3cd71603d9ce1e447c3cc5c0588ac00000000000000001e6a1c8a5dda15fb6f05628a061e67576e926dc71a7fa2f0cceb951120a93265d2e90b000000001976a9144838d8b3588c4c7ba7c1d06f866e9b3739c6303788ac00000000"

    # insert send, as unconfirmed! won't create a block!
    tx3 = util_test.insert_unconfirmed_raw_transaction(send3hex, server_db)

    # balances after send, unaffected
    alice_balance2 = util.get_balance(server_db, alice, 'XCP')
    bob_balance2 = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance2 == alice_balance
    assert bob_balance2 == bob_balance

    # no confirmed UTXOs left, we use the 1 we had
    utxos = util.api('get_unspent_txouts', {"address": alice})
    assert len(utxos) == 0

    # unconfirmed UTXO is there, we can use it!
    utxos = util.api('get_unspent_txouts', {
        "address": alice,
        "unconfirmed": True
    })
    assert len(utxos) == 1
    assert utxos[0]['address'] == alice
    assert utxos[0]['txid'] == tx3['tx_hash']
    assert utxos[0]['amount'] == 1.99873125
    assert utxos[0]['confirmations'] == 0

    # atm there's no way to confirm this unconfirmed TX
    # even doing this won't make it confirmed because it just mocks an empty block
    util_test.create_next_block(server_db)

    utxos = util.api('get_unspent_txouts', {"address": alice})
    assert len(utxos) == 1
    assert utxos[0]['address'] == alice
    assert utxos[0]['txid'] == tx3['tx_hash']
    assert utxos[0]['amount'] == 1.99873125
    assert utxos[0]['confirmations'] == 1

    # we can eventually make this mocking better to be able to do that,
    # but for now you'll have to micro manage if you want to confirm a unconfirmed TX in 1 test
    # by just calling the `insert_raw_transaction` again for it
    tx3bhash, tx3b = util_test.insert_raw_transaction(send3hex, server_db)

    # balances after send
    alice_balance2 = util.get_balance(server_db, alice, 'XCP')
    bob_balance2 = util.get_balance(server_db, bob, 'XCP')
    assert alice_balance2 == alice_balance - v
    assert bob_balance2 == bob_balance + v
コード例 #8
0
def test_cron_check_for_expire_before_commit(connected_clients, server_db):
    alice, bob, charlie, david, eric, fred = connected_clients

    bob_before_status = bob.get_status()
    assert bob_before_status["send_balance"] == 1000000

    alice.micro_send(bob.handle, 1337, "0000")
    assert alice.sync() == []

    bob.micro_send(charlie.handle, 42, "0001")
    assert bob.sync() == [{
        'amount': 1337,
        'payer_handle': alice.handle,
        'token': '0000'
    }]

    # let channel expire
    for i in range(50):
        util_test.create_next_block(server_db)

    rawtxs = alice.update()
    rawtxs = bob.update()
    rawtxs = charlie.update()
    rawtxs = david.update()
    rawtxs = eric.update()
    rawtxs = fred.update()

    rawtxs = cron.recover_funds()
    assert rawtxs is not None
    assert len(rawtxs["change"]) == 0
    assert len(rawtxs["commit"]) == 0
    assert len(rawtxs["revoke"]) == 0
    assert len(rawtxs["payout"]) == 0
    assert len(rawtxs["expire"]) == 5  # missing deposit where commit published

    # let channel expire
    for i in range(50):
        util_test.create_next_block(server_db)

    rawtxs = cron.recover_funds()
    assert rawtxs is not None
    assert len(rawtxs["change"]) == 0
    assert len(rawtxs["commit"]) == 0
    assert len(rawtxs["revoke"]) == 0
    assert len(rawtxs["payout"]) == 0
    assert len(rawtxs["expire"]) == 1  # now recoverable after commit timeout

    balances = alice.get_status()["recv_deposit_balances"]
    assert balances == {"BTC": 0, "XCP": 0}
    address = alice.get_status()["recv_deposit_address"]
    utxos = api.get_unspent_txouts(address=address, unconfirmed=False)
    assert len(utxos) == 0

    balances = bob.get_status()["recv_deposit_balances"]
    assert balances == {"BTC": 0, "XCP": 0}
    address = bob.get_status()["recv_deposit_address"]
    utxos = api.get_unspent_txouts(address=address, unconfirmed=False)
    assert len(utxos) == 0

    balances = charlie.get_status()["recv_deposit_balances"]
    assert balances == {"BTC": 0, "XCP": 0}
    address = charlie.get_status()["recv_deposit_address"]
    utxos = api.get_unspent_txouts(address=address, unconfirmed=False)
    assert len(utxos) == 0

    balances = david.get_status()["recv_deposit_balances"]
    assert balances == {"BTC": 0, "A7736697071037023001": 0}
    address = david.get_status()["recv_deposit_address"]
    utxos = api.get_unspent_txouts(address=address, unconfirmed=False)
    assert len(utxos) == 0

    balances = eric.get_status()["recv_deposit_balances"]
    assert balances == {"BTC": 0, "A7736697071037023001": 0}
    address = eric.get_status()["recv_deposit_address"]
    utxos = api.get_unspent_txouts(address=address, unconfirmed=False)
    assert len(utxos) == 0

    balances = fred.get_status()["recv_deposit_balances"]
    assert balances == {"BTC": 0, "A7736697071037023001": 0}
    address = fred.get_status()["recv_deposit_address"]
    utxos = api.get_unspent_txouts(address=address, unconfirmed=False)
    assert len(utxos) == 0

    rawtxs = cron.publish_commits()
    assert rawtxs == []