Change PeerGroup.broadcastTransaction() to wait for propagation.

It means a send won't be considered completed until it's accepted by the net.
Also is for the case where you receive a transaction (eg, via Bluetooth) and
you want to broadcast it such that you can know it's valid.

Make WalletTool use --peers instead of --peer, a comma separated list of
addresses to use. Remove the crappy sleep after send now we can wait.

Resolves issue 167.
This commit is contained in:
Mike Hearn
2012-07-21 23:07:00 +02:00
parent 42152c2483
commit fd9eba1697
7 changed files with 234 additions and 133 deletions

View File

@@ -24,6 +24,7 @@ import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.net.InetAddress;
import java.util.concurrent.ExecutionException;
/**
* PingService demonstrates basic usage of the library. It sits on the network and when it receives coins, simply
@@ -82,9 +83,10 @@ public class DerbyPingService {
BigInteger value = tx.getValueSentToMe(w);
System.out.println("Received " + Utils.bitcoinValueToFriendlyString(value) + " from " + from.toString());
// Now send the coins back!
Transaction sendTx = w.sendCoins(peerGroup, from, value);
assert sendTx != null; // We should never try to send more coins than we have!
System.out.println("Sent coins back! Transaction hash is " + sendTx.getHashAsString());
Wallet.SendResult sendTx = w.sendCoins(peerGroup, from, value);
assert sendTx.tx != null; // We should never try to send more coins than we have!
System.out.println("Sent coins back! Transaction hash is " + sendTx.tx.getHashAsString());
sendTx.broadcastComplete.get();
w.saveToFile(walletFile);
} catch (ScriptException e) {
// If we didn't understand the scriptSig, just crash.
@@ -93,6 +95,10 @@ public class DerbyPingService {
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
});

View File

@@ -180,7 +180,7 @@ public class PingService {
}
private void bounceCoins(Transaction tx) {
// It's impossible to pick one specific identity that you receive coins from in BitCoin as there
// It's impossible to pick one specific identity that you receive coins from in Bitcoin as there
// could be inputs from many addresses. So instead we just pick the first and assume they were all
// owned by the same person.
try {
@@ -189,7 +189,7 @@ public class PingService {
Address from = input.getFromAddress();
System.out.println("Received " + Utils.bitcoinValueToFriendlyString(value) + " from " + from.toString());
// Now send the coins back!
Transaction sendTx = w.sendCoins(peerGroup, from, value);
Transaction sendTx = w.sendCoins(peerGroup, from, value).tx;
assert sendTx != null; // We should never try to send more coins than we have!
System.out.println("Sent coins back! Transaction hash is " + sendTx.getHashAsString());
w.saveToFile(walletFile);