mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2024-11-19 18:00:39 +01:00
Add a program that measures confirmation time for a given fee level.
This is a complementary approach to what the estimatefees code for Core does, because Core cannot really measure confirmation time for a fee level if nobody is setting it.
This commit is contained in:
parent
c63f5f7553
commit
1f776c3f19
@ -0,0 +1,70 @@
|
|||||||
|
package com.google.bitcoin.tools;
|
||||||
|
|
||||||
|
import com.google.bitcoin.core.*;
|
||||||
|
import com.google.bitcoin.kits.WalletAppKit;
|
||||||
|
import com.google.bitcoin.params.MainNetParams;
|
||||||
|
import com.google.bitcoin.utils.BriefLogFormatter;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A program that sends a transaction with the specified fee and measures how long it takes to confirm.
|
||||||
|
*/
|
||||||
|
public class TestFeeLevel {
|
||||||
|
private static Logger log = LoggerFactory.getLogger(TestFeeLevel.class);
|
||||||
|
|
||||||
|
public static final MainNetParams PARAMS = MainNetParams.get();
|
||||||
|
private static WalletAppKit kit;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
BriefLogFormatter.init();
|
||||||
|
if (args.length == 0) {
|
||||||
|
System.err.println("Specify the fee level to test in satoshis as the first argument.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
BigInteger feeToTest = new BigInteger(args[0]);
|
||||||
|
|
||||||
|
kit = new WalletAppKit(PARAMS, new File("."), "testfeelevel");
|
||||||
|
kit.startAsync();
|
||||||
|
kit.awaitRunning();
|
||||||
|
try {
|
||||||
|
go(feeToTest);
|
||||||
|
} finally {
|
||||||
|
kit.stopAsync();
|
||||||
|
kit.awaitTerminated();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void go(BigInteger feeToTest) throws InterruptedException, java.util.concurrent.ExecutionException, InsufficientMoneyException {
|
||||||
|
kit.peerGroup().setMaxConnections(50);
|
||||||
|
|
||||||
|
final Address address = kit.wallet().currentReceiveKey().toAddress(PARAMS);
|
||||||
|
|
||||||
|
if (kit.wallet().getBalance().compareTo(feeToTest) < 0) {
|
||||||
|
log.info("Send some money to {}", address);
|
||||||
|
log.info("... and wait for it to confirm");
|
||||||
|
kit.wallet().getBalanceFuture(feeToTest, Wallet.BalanceType.AVAILABLE).get();
|
||||||
|
}
|
||||||
|
|
||||||
|
int heightAtStart = kit.chain().getBestChainHeight();
|
||||||
|
log.info("Height at start is {}", heightAtStart);
|
||||||
|
|
||||||
|
Wallet.SendRequest request = Wallet.SendRequest.to(address, kit.wallet().getBalance().subtract(feeToTest));
|
||||||
|
request.feePerKb = feeToTest;
|
||||||
|
request.ensureMinRequiredFee = false;
|
||||||
|
kit.wallet().completeTx(request);
|
||||||
|
log.info("Fee paid is {}", request.fee);
|
||||||
|
log.info("TX is {}", request.tx);
|
||||||
|
kit.peerGroup().broadcastTransaction(request.tx).get();
|
||||||
|
log.info("Send complete, waiting for confirmation");
|
||||||
|
request.tx.getConfidence().getDepthFuture(1).get();
|
||||||
|
|
||||||
|
int heightNow = kit.chain().getBestChainHeight();
|
||||||
|
log.info("Height after confirmation is {}", heightNow);
|
||||||
|
log.info("Result: took {} blocks to confirm at this fee level", heightNow - heightAtStart);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user