/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.bitcoinj.tools;

import org.bitcoinj.core.*;
import org.bitcoinj.kits.WalletAppKit;
import org.bitcoinj.params.MainNetParams;
import org.bitcoinj.utils.BriefLogFormatter;

import java.io.File;

/**
 * A program that sends a transaction with the specified fee and measures how long it takes to confirm.
 */
public class TestFeeLevel {

    private static final MainNetParams PARAMS = MainNetParams.get();
    private static final int NUM_OUTPUTS = 2;
    private static WalletAppKit kit;

    public static void main(String[] args) throws Exception {
        BriefLogFormatter.initWithSilentBitcoinJ();
        if (args.length == 0) {
            System.err.println("Specify the fee level to test in satoshis as the first argument.");
            return;
        }

        Coin feeToTest = Coin.valueOf(Long.parseLong(args[0]));
        System.out.println("Fee to test is " + feeToTest.toFriendlyString());

        kit = new WalletAppKit(PARAMS, new File("."), "testfeelevel");
        kit.startAsync();
        kit.awaitRunning();
        try {
            go(feeToTest, NUM_OUTPUTS);
        } finally {
            kit.stopAsync();
            kit.awaitTerminated();
        }
    }

    private static void go(Coin feeToTest, int numOutputs) throws InterruptedException, java.util.concurrent.ExecutionException, InsufficientMoneyException {
        kit.peerGroup().setMaxConnections(25);

        if (kit.wallet().getBalance().compareTo(feeToTest) < 0) {
            System.out.println("Send some money to " + kit.wallet().currentReceiveAddress());
            System.out.println("... and wait for it to confirm");
            kit.wallet().getBalanceFuture(feeToTest, Wallet.BalanceType.AVAILABLE).get();
        }

        int heightAtStart = kit.chain().getBestChainHeight();
        System.out.println("Height at start is " + heightAtStart);

        Coin value = kit.wallet().getBalance().subtract(feeToTest);
        Coin outputValue = value.divide(numOutputs);
        Transaction transaction = new Transaction(PARAMS);
        for (int i = 0; i < numOutputs - 1; i++) {
            transaction.addOutput(outputValue, kit.wallet().freshReceiveAddress());
            value = value.subtract(outputValue);
        }
        transaction.addOutput(value, kit.wallet().freshReceiveAddress());
        Wallet.SendRequest request = Wallet.SendRequest.forTx(transaction);
        request.feePerKb = feeToTest;
        request.ensureMinRequiredFee = false;
        kit.wallet().completeTx(request);
        System.out.println("Size in bytes is " + request.tx.bitcoinSerialize().length);
        System.out.println("TX is " + request.tx);
        kit.peerGroup().broadcastTransaction(request.tx).future().get();
        System.out.println("Send complete, waiting for confirmation");
        request.tx.getConfidence().getDepthFuture(1).get();

        int heightNow = kit.chain().getBestChainHeight();
        System.out.println("Height after confirmation is " + heightNow);
        System.out.println("Result: took " + (heightNow - heightAtStart) + " blocks to confirm at this fee level");
    }
}

@@ -612,16 +612,80 @@
 core.*;%0A
+import org.bitcoinj.core.listeners.PeerConnectionEventListener;%0A
 import o
@@ -823,16 +823,38 @@
 io.File;
+%0Aimport java.util.Set;
 %0A%0A/**%0A *
@@ -3293,16 +3293,680 @@
 st.tx);%0A
+        System.out.println(%22Waiting for %22 + kit.peerGroup().getMaxConnections() + %22 connected peers%22);%0A        kit.peerGroup().addConnectionEventListener(new PeerConnectionEventListener() %7B%0A%0A            @Override%0A            public void onPeersDiscovered(Set%3CPeerAddress%3E peerAddresses) %7B%0A            %7D%0A%0A            @Override%0A            public void onPeerDisconnected(Peer peer, int peerCount) %7B%0A                System.out.println(peerCount + %22 peers connected%22);%0A            %7D%0A%0A            @Override%0A            public void onPeerConnected(Peer peer, int peerCount) %7B%0A                System.out.println(peerCount + %22 peers connected%22);%0A            %7D%0A        %7D);%0A
         

Print the number of peers already connected.