-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
examples/src/main/java/com/google/bitcoin/examples/DoubleSpend.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.google.bitcoin.examples; | ||
|
||
import com.google.bitcoin.core.*; | ||
import com.google.bitcoin.kits.WalletAppKit; | ||
import com.google.bitcoin.params.RegTestParams; | ||
import com.google.bitcoin.utils.BriefLogFormatter; | ||
import com.google.bitcoin.utils.Threading; | ||
|
||
import java.io.File; | ||
import java.math.BigInteger; | ||
|
||
/** | ||
* This is a little test app that waits for a coin on a local regtest node, then generates two transactions that double | ||
* spend the same output and sends them. It's useful for testing double spend codepaths but is otherwise not something | ||
* you would normally want to do. | ||
*/ | ||
public class DoubleSpend { | ||
public static void main(String[] args) throws Exception { | ||
BriefLogFormatter.init(); | ||
final RegTestParams params = RegTestParams.get(); | ||
WalletAppKit kit = new WalletAppKit(params, new File("."), "doublespend"); | ||
kit.connectToLocalHost(); | ||
kit.setAutoSave(false); | ||
kit.startAsync(); | ||
kit.awaitRunning(); | ||
|
||
System.out.println(kit.wallet()); | ||
|
||
kit.wallet().getBalanceFuture(Utils.COIN, Wallet.BalanceType.AVAILABLE).get(); | ||
Transaction tx1 = kit.wallet().createSend(new Address(params, "muYPFNCv7KQEG2ZLM7Z3y96kJnNyXJ53wm"), Utils.CENT); | ||
Transaction tx2 = kit.wallet().createSend(new Address(params, "muYPFNCv7KQEG2ZLM7Z3y96kJnNyXJ53wm"), Utils.CENT.add(BigInteger.TEN)); | ||
final Peer peer = kit.peerGroup().getConnectedPeers().get(0); | ||
peer.addEventListener(new AbstractPeerEventListener() { | ||
@Override | ||
public Message onPreMessageReceived(Peer peer, Message m) { | ||
System.err.println("Got a message!" + m.getClass().getSimpleName() + ": " + m); | ||
return m; | ||
} | ||
}, Threading.SAME_THREAD); | ||
peer.sendMessage(tx1); | ||
peer.sendMessage(tx2); | ||
|
||
Thread.sleep(5000); | ||
kit.stopAsync(); | ||
kit.awaitTerminated(); | ||
} | ||
} |