def create_txprobe(input1, input2, n):
    """Creates several kinds of transactions:
      PARENT[i]:
         spends input1
         creates output p[i]
      ORPHAN[i]:
         spends input2, and p[i]
         creates output o[i] for recovery.
     
      FLOOD:
         spends input1, blocks parent[i]
    """
    PARENTS = []
    ORPHANS = []
    for i in range(n):
        tx_parent = Transaction()
        tx_parent.vin = [input1]
        _tx_parent_out, tx_parent_in = txpair_from_p2sh(nValue=0.008 * COIN)
        tx_parent.append_txout(_tx_parent_out)
        tx_parent.finalize()
        PARENTS.append(tx_parent)

        tx_orphan = Transaction()
        tx_orphan.vin = [input2, tx_parent_in]
        _tx_orphan_out, tx_orphan_in = txpair_from_p2sh(nValue=0.005 * COIN)
        tx_orphan.append_txout(_tx_orphan_out)
        tx_orphan.finalize()
        ORPHANS.append(tx_orphan)

    FLOOD = Transaction()
    FLOOD.vin = [input1]
    _flood_out, tx_flood_in = txpair_from_p2sh(nValue=0.008 * COIN)
    FLOOD.append_txout(_flood_out)
    FLOOD.finalize()
    return PARENTS, ORPHANS, FLOOD
def create_txprobe(input1, input2, n):
    """Creates several kinds of transactions:
      PARENT[i]:
         spends input1
         creates output p[i]
      ORPHAN[i]:
         spends input2, and p[i]
         creates output o[i] for recovery.
     
      FLOOD:
         spends input1, blocks parent[i]
    """
    PARENTS = []
    ORPHANS = []
    for i in range(n):
        tx_parent = Transaction()
        tx_parent.vin = [input1]
        _tx_parent_out,tx_parent_in = txpair_from_p2sh(nValue=0.008*COIN)
        tx_parent.append_txout(_tx_parent_out)
        tx_parent.finalize()
        PARENTS.append(tx_parent)

        tx_orphan = Transaction()
        tx_orphan.vin = [input2, tx_parent_in]
        _tx_orphan_out,tx_orphan_in = txpair_from_p2sh(nValue=0.005*COIN)
        tx_orphan.append_txout(_tx_orphan_out)
        tx_orphan.finalize()
        ORPHANS.append(tx_orphan)

    FLOOD = Transaction()
    FLOOD.vin = [input1]
    _flood_out,tx_flood_in = txpair_from_p2sh(nValue=0.008*COIN)
    FLOOD.append_txout(_flood_out)
    FLOOD.finalize()
    return PARENTS, ORPHANS, FLOOD
def make_experiment2(path='./experiment2_payload.dat'):
    import time
    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0)
    #socket.create_connection
    sock.connect("/tmp/bitcoin_control")

    # Reset all the connections
    print 'Resetting connections'
    n = 79
    cmsg = command_msg(commands.COMMAND_DISCONNECT, 0, [targets.BROADCAST])
    ser = cmsg.serialize()
    do_send(sock, ser)
    for i in range(1, n + 1):
        msg = connect_msg('127.0.0.1', 8332 + i, '0.0.0.0', 0)
        ser = msg.serialize()
        do_send(sock, ser)
    print 'Connecting'
    time.sleep(2)

    nodes = list(get_cxns())
    print 'Nodes:', nodes

    import math
    sn = int(math.ceil(math.sqrt(n)))
    sched = schedule(range(n))
    print 'sqrt(n):', sn
    print 'schedule:', len(sched)

    # 1. Create a setup transaction with enough inputs for 2 boosters per trial
    tx_setup = Transaction()
    tx_setup.vin = [get_txin_second()]
    tx_setup_ins = []
    for _ in sched:
        for _ in range(2):
            _out, _in = txpair_from_p2sh(nValue=0.01 * COIN)
            tx_setup.append_txout(_out)
            tx_setup_ins.append(_in)
    tx_setup.finalize()

    # 1a. Add tx_setup to a block
    block = make_block()
    block.vtx.append(tx_setup._ctx)
    block.hashMerkleRoot = block.calc_merkle_root()

    PAYLOADS = []
    for i, (tgt, tst) in enumerate(sched):
        PARENTS, ORPHANS, FLOOD = create_txprobe(tx_setup_ins[2 * i + 0],
                                                 tx_setup_ins[2 * i + 1],
                                                 len(tgt))
        PAYLOADS.append((PARENTS, ORPHANS, FLOOD))
    return nodes, block, PAYLOADS
def make_experiment2(path='./experiment2_payload.dat'):
    import time
    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0)
    #socket.create_connection
    sock.connect("/tmp/bitcoin_control")

    # Reset all the connections
    print 'Resetting connections'
    n = 79
    cmsg = command_msg(commands.COMMAND_DISCONNECT, 0, [targets.BROADCAST])
    ser = cmsg.serialize()
    do_send(sock, ser)
    for i in range(1,n+1):
        msg = connect_msg('127.0.0.1', 8332+i, '0.0.0.0', 0)
        ser = msg.serialize()
        do_send(sock, ser)
    print 'Connecting'
    time.sleep(2)

    nodes = list(get_cxns())
    print 'Nodes:', nodes

    import math
    sn = int(math.ceil(math.sqrt(n)))
    sched = schedule(range(n))
    print 'sqrt(n):', sn
    print 'schedule:', len(sched)

    # 1. Create a setup transaction with enough inputs for 2 boosters per trial
    tx_setup = Transaction()
    tx_setup.vin = [get_txin_second()]
    tx_setup_ins = []
    for _ in sched:
        for _ in range(2):
            _out,_in = txpair_from_p2sh(nValue=0.01*COIN)
            tx_setup.append_txout(_out)
            tx_setup_ins.append(_in)
    tx_setup.finalize()

    # 1a. Add tx_setup to a block
    block = make_block()
    block.vtx.append(tx_setup._ctx)
    block.hashMerkleRoot = block.calc_merkle_root()

    PAYLOADS = []
    for i,(tgt,tst) in enumerate(sched):
        PARENTS, ORPHANS, FLOOD = create_txprobe(tx_setup_ins[2*i+0], tx_setup_ins[2*i+1], len(tgt))
        PAYLOADS.append((PARENTS, ORPHANS, FLOOD))
    return nodes, block, PAYLOADS