def test_duplicate_txid():
    ###two txes with the same txid, built history
    dummy_spk, containing_block_height1, dummy_tx1 = create_dummy_funding_tx()
    dummy_spk, containing_block_height2, dummy_tx2 = create_dummy_funding_tx(
        output_spk=dummy_spk)
    dummy_spk, containing_block_height3, dummy_tx3 = create_dummy_funding_tx(
        output_spk=dummy_spk)
    dummy_tx2["txid"] = dummy_tx1["txid"]
    dummy_tx3["txid"] = dummy_tx1["txid"]
    sh = script_to_scripthash(dummy_spk)
    rpc = DummyJsonRpc(
        [dummy_tx1, dummy_tx2], [], {
            dummy_tx1["blockhash"]: containing_block_height1,
            dummy_tx2["blockhash"]: containing_block_height2,
            dummy_tx3["blockhash"]: containing_block_height3
        })
    txmonitor = TransactionMonitor(rpc, deterministic_wallets, logger)
    assert txmonitor.build_address_history([dummy_spk])
    assert len(txmonitor.get_electrum_history(sh)) == 1
    txmonitor.subscribe_address(sh)
    assert txmonitor.get_electrum_history(
        sh)[0]["tx_hash"] == dummy_tx1["txid"]
    rpc.add_transaction(dummy_tx3)
    assert len(list(txmonitor.check_for_updated_txes())) == 1
    assert len(txmonitor.get_electrum_history(sh)) == 1
    assert txmonitor.get_electrum_history(
        sh)[0]["tx_hash"] == dummy_tx1["txid"]
def test_tx_arrival_then_confirmation():
    ###build empty address history, subscribe one address
    ### an unconfirmed tx appears, then confirms
    dummy_spk, containing_block_height, dummy_tx = create_dummy_funding_tx(
        confirmations=0)

    rpc = DummyJsonRpc([], [dummy_tx["vin"][0]],
                       {dummy_tx["blockhash"]: containing_block_height})
    txmonitor = TransactionMonitor(rpc, deterministic_wallets, logger)
    assert txmonitor.build_address_history([dummy_spk])
    assert len(txmonitor.address_history) == 1
    sh = script_to_scripthash(dummy_spk)
    assert len(txmonitor.get_electrum_history(sh)) == 0
    txmonitor.subscribe_address(sh)
    # unconfirm transaction appears
    assert len(list(txmonitor.check_for_updated_txes())) == 0
    rpc.add_transaction(dummy_tx)
    assert len(list(txmonitor.check_for_updated_txes())) == 1
    assert_address_history_tx(txmonitor.address_history,
                              spk=dummy_spk,
                              height=0,
                              txid=dummy_tx["txid"],
                              subscribed=True)
    # transaction confirms
    dummy_tx["confirmations"] = 1
    assert len(list(txmonitor.check_for_updated_txes())) == 1
    assert_address_history_tx(txmonitor.address_history,
                              spk=dummy_spk,
                              height=containing_block_height,
                              txid=dummy_tx["txid"],
                              subscribed=True)
def test_tx_with_unconfirmed_input():
    ###unconfirmed tx arrives with unconfirmed input, which then both confirm

    dummy_spk1, containing_block_height1, dummy_tx1 = create_dummy_funding_tx(
        confirmations=0)
    dummy_spk2, containing_block_height2, dummy_tx2 = create_dummy_funding_tx(
        confirmations=0, input_txid=dummy_tx1["txid"], input_confirmations=0)

    rpc = DummyJsonRpc(
        [], [dummy_tx1["vin"][0], dummy_tx2["vin"][0]], {
            dummy_tx1["blockhash"]: containing_block_height1,
            dummy_tx2["blockhash"]: containing_block_height2
        })
    txmonitor = TransactionMonitor(rpc, deterministic_wallets, logger)

    assert txmonitor.build_address_history([dummy_spk1, dummy_spk2])
    assert len(txmonitor.address_history) == 2

    sh1 = script_to_scripthash(dummy_spk1)
    sh2 = script_to_scripthash(dummy_spk2)
    assert len(txmonitor.get_electrum_history(sh1)) == 0
    assert len(txmonitor.get_electrum_history(sh2)) == 0
    txmonitor.subscribe_address(sh1)
    txmonitor.subscribe_address(sh2)

    #the unconfirmed transactions appear
    assert len(list(txmonitor.check_for_updated_txes())) == 0
    rpc.add_transaction(dummy_tx1)
    rpc.add_transaction(dummy_tx2)
    assert len(list(txmonitor.check_for_updated_txes())) == 2
    assert_address_history_tx(txmonitor.address_history,
                              spk=dummy_spk1,
                              height=0,
                              txid=dummy_tx1["txid"],
                              subscribed=True)
    assert_address_history_tx(txmonitor.address_history,
                              spk=dummy_spk2,
                              height=-1,
                              txid=dummy_tx2["txid"],
                              subscribed=True)

    #the transactions confirm
    dummy_tx1["confirmations"] = 1
    dummy_tx2["confirmations"] = 1

    assert len(list(txmonitor.check_for_updated_txes())) == 2
    assert_address_history_tx(txmonitor.address_history,
                              spk=dummy_spk1,
                              height=containing_block_height1,
                              txid=dummy_tx1["txid"],
                              subscribed=True)
    assert_address_history_tx(txmonitor.address_history,
                              spk=dummy_spk2,
                              height=containing_block_height2,
                              txid=dummy_tx2["txid"],
                              subscribed=True)