Example #1
0
    # What's the delta fee that we need to get to our desired fees per byte at
    # the current tx size?
    delta_fee = math.ceil((desired_fees_per_byte * len(tx.serialize())) -
                          (value_in - value_out))

    # Ensure termination; the loop converges so it can get stuck at no fee.
    if delta_fee < 1:
        break

    logging.debug('Delta fee: %s' % str_money_value(delta_fee))

    # Can we simply reduce the value of the change output?
    new_change_txout_nValue = change_txout.nValue - delta_fee
    if new_change_txout_nValue > DUST and new_change_txout_nValue >= min_change_txout_nValue:
        value_out -= change_txout.nValue - new_change_txout_nValue
        change_txout.nValue = new_change_txout_nValue

    else:
        # Looks like we need to add another input. We could be clever about
        # this, but nah, just add the largest unspent input to the tx and try
        # again.
        try:
            new_unspent = unspent[-1]
            unspent = unspent[:-1]
        except IndexError:
            parser.exit('Not enough confirmed funds left unspent to bump fees')

        new_outpoint = new_unspent['outpoint']
        new_amount = new_unspent['amount']

        logging.debug('Adding new input %s:%d with value %s BTC' % \

if not args.dryrun:
    txid = rpc.sendrawtransaction(tx)
    logging.info('Sent payment tx: %s' % b2lx(txid))

if not args.dryrun:
    logging.info('Sleeping for %d seconds' % args.delay)
    time.sleep(args.delay)


# Double-spend! Remove all but the change output
tx.vout = tx.vout[0:1]
change_txout = tx.vout[0]
value_out = value_in
change_txout.nValue = value_out

# FIXME: need to modularize this code
while (value_in - value_out) / len(tx.serialize()) < feeperbyte2:
    # What's the delta fee that we need to get to our desired fees per byte at
    # the current tx size?
    delta_fee = math.ceil((feeperbyte2 * len(tx.serialize())) - (value_in - value_out))

    logging.debug('Delta fee: %s' % str_money_value(delta_fee))

    # If we simply subtract that from the change outpoint are we still above
    # the dust threshold?
    if change_txout.nValue - delta_fee > args.dust:
        change_txout.nValue -= delta_fee
        value_out -= delta_fee
              str_money_value((value_in-value_out) / len(tx.serialize()) * 1000)))



txid = rpc.sendrawtransaction(tx)
print('Sent payment with txid: %s' % b2lx(txid))


print('Waiting for %d seconds before double spending' % 2)
time.sleep(10)


tx.vout = tx.vout[0:1]
change_txout = tx.vout[0]
value_out = value_in
change_txout.nValue = value_out

while (value_in - value_out) / len(tx.serialize()) < feeperbyte2:
    # What's the delta fee that we need to get to our desired fees per byte at
    # the current tx size?
    delta_fee = math.ceil((feeperbyte2 * len(tx.serialize())) - (value_in - value_out))

    print('Delta fee: %s' % str_money_value(delta_fee))

    if change_txout.nValue - delta_fee > dust_amount:
        change_txout.nValue -= delta_fee
        value_out -= delta_fee

    if value_in - value_out < 0:
        new_outpoint = unspent[-1]['outpoint']
        new_amount = unspent[-1]['amount']
    # What's the delta fee that we need to get to our desired fees per byte at
    # the current tx size?
    delta_fee = math.ceil((desired_fees_per_byte * len(tx.serialize())) - (value_in - value_out))

    # Ensure termination; the loop converges so it can get stuck at no fee.
    if delta_fee < 1:
        break

    logging.debug('Delta fee: %s' % str_money_value(delta_fee))

    # Can we simply reduce the value of the change output?
    new_change_txout_nValue = change_txout.nValue - delta_fee
    if new_change_txout_nValue > DUST and new_change_txout_nValue >= min_change_txout_nValue:
        value_out -= change_txout.nValue - new_change_txout_nValue
        change_txout.nValue = new_change_txout_nValue

    else:
        # Looks like we need to add another input. We could be clever about
        # this, but nah, just add the largest unspent input to the tx and try
        # again.
        try:
            new_unspent = unspent[-1]
            unspent = unspent[:-1]
        except IndexError:
            parser.exit('Not enough confirmed funds left unspent to bump fees')

        new_outpoint = new_unspent['outpoint']
        new_amount = new_unspent['amount']

        logging.debug('Adding new input %s:%d with value %s BTC' % \