3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-14 19:25:51 +00:00

TestFeeLevel: Adapt to fee rate (per kB).

This commit is contained in:
Andreas Schildbach 2016-06-14 20:12:32 +02:00
parent f416dec2ea
commit 0d0358fe92

View File

@ -39,37 +39,37 @@ public class TestFeeLevel {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
BriefLogFormatter.initWithSilentBitcoinJ(); BriefLogFormatter.initWithSilentBitcoinJ();
if (args.length == 0) { if (args.length == 0) {
System.err.println("Specify the fee level to test in satoshis as the first argument."); System.err.println("Specify the fee rate to test in satoshis/kB as the first argument.");
return; return;
} }
Coin feeToTest = Coin.valueOf(Long.parseLong(args[0])); Coin feeRateToTest = Coin.valueOf(Long.parseLong(args[0]));
System.out.println("Fee to test is " + feeToTest.toFriendlyString()); System.out.println("Fee rate to test is " + feeRateToTest.toFriendlyString() + "/kB");
kit = new WalletAppKit(PARAMS, new File("."), "testfeelevel"); kit = new WalletAppKit(PARAMS, new File("."), "testfeelevel");
kit.startAsync(); kit.startAsync();
kit.awaitRunning(); kit.awaitRunning();
try { try {
go(feeToTest, NUM_OUTPUTS); go(feeRateToTest, NUM_OUTPUTS);
} finally { } finally {
kit.stopAsync(); kit.stopAsync();
kit.awaitTerminated(); kit.awaitTerminated();
} }
} }
private static void go(Coin feeToTest, int numOutputs) throws InterruptedException, java.util.concurrent.ExecutionException, InsufficientMoneyException { private static void go(Coin feeRateToTest, int numOutputs) throws InterruptedException, java.util.concurrent.ExecutionException, InsufficientMoneyException {
kit.peerGroup().setMaxConnections(25); kit.peerGroup().setMaxConnections(25);
if (kit.wallet().getBalance().compareTo(feeToTest) < 0) { if (kit.wallet().getBalance().compareTo(feeRateToTest) < 0) {
System.out.println("Send some money to " + kit.wallet().currentReceiveAddress()); System.out.println("Send some money to " + kit.wallet().currentReceiveAddress());
System.out.println("... and wait for it to confirm"); System.out.println("... and wait for it to confirm");
kit.wallet().getBalanceFuture(feeToTest, Wallet.BalanceType.AVAILABLE).get(); kit.wallet().getBalanceFuture(feeRateToTest, Wallet.BalanceType.AVAILABLE).get();
} }
int heightAtStart = kit.chain().getBestChainHeight(); int heightAtStart = kit.chain().getBestChainHeight();
System.out.println("Height at start is " + heightAtStart); System.out.println("Height at start is " + heightAtStart);
Coin value = kit.wallet().getBalance().subtract(feeToTest); Coin value = kit.wallet().getBalance().divide(2); // Keep a chunk for the fee.
Coin outputValue = value.divide(numOutputs); Coin outputValue = value.divide(numOutputs);
Transaction transaction = new Transaction(PARAMS); Transaction transaction = new Transaction(PARAMS);
for (int i = 0; i < numOutputs - 1; i++) { for (int i = 0; i < numOutputs - 1; i++) {
@ -78,7 +78,7 @@ public class TestFeeLevel {
} }
transaction.addOutput(value, kit.wallet().freshReceiveAddress()); transaction.addOutput(value, kit.wallet().freshReceiveAddress());
SendRequest request = SendRequest.forTx(transaction); SendRequest request = SendRequest.forTx(transaction);
request.feePerKb = feeToTest; request.feePerKb = feeRateToTest;
request.ensureMinRequiredFee = false; request.ensureMinRequiredFee = false;
kit.wallet().completeTx(request); kit.wallet().completeTx(request);
System.out.println("Size in bytes is " + request.tx.unsafeBitcoinSerialize().length); System.out.println("Size in bytes is " + request.tx.unsafeBitcoinSerialize().length);