Commit 65300e9b authored by Daniel Connolly's avatar Daniel Connolly Committed by Daniel Connolly

refactored and added send & receive tx test

parent 88df8584
......@@ -22,27 +22,41 @@ import org.bitcoinj.net.discovery.DnsDiscovery;
import org.bitcoinj.store.BlockStore;
import org.bitcoinj.store.BlockStoreException;
import org.bitcoinj.store.MemoryBlockStore;
import org.bitcoinj.wallet.Wallet;
public class ChainDownloadParent {
import java.util.ArrayList;
import java.util.List;
protected NetworkParameters parameters;
protected Context context;
protected BlockStore blockStore;
protected BlockChain blockChain;
protected ClientConnectionManager connectionManager;
protected PeerGroup peerGroup;
/**
* helper class to hold a downloaded chain and wallet
* this is bad practice, but necessary for the MainnetDownloadIT and TnetDownloadIT integration tests
*/
public class DownloadedChainData {
public NetworkParameters parameters;
public Context context;
public BlockStore blockStore;
public BlockChain blockChain;
public ClientConnectionManager connectionManager;
public PeerGroup peerGroup;
public Wallet wallet;
public ChainDownloadParent(NetworkParameters parameters) throws BlockStoreException {
public DownloadedChainData(NetworkParameters parameters) {
this.parameters = parameters;
}
public void setupAndSync(Wallet wallet) throws InterruptedException, BlockStoreException {
List<Wallet> wallets = new ArrayList<>(1);
this.wallet = wallet;
if (wallet != null) {
wallets.add(wallet);
}
this.context = new Context(parameters);
this.blockStore = new MemoryBlockStore(parameters);
this.blockChain = new BlockChain(context, blockStore);
this.blockChain = new BlockChain(context, wallets, blockStore);
this.connectionManager = new BlockingClientManager();
this.peerGroup = new PeerGroup(context, blockChain, connectionManager);
peerGroup.addPeerDiscovery(new DnsDiscovery(parameters));
}
public void sync() throws InterruptedException {
DownloadProgressTracker listener = new DownloadProgressTracker();
peerGroup.start();
peerGroup.startBlockChainDownload(listener);
......
......@@ -17,18 +17,28 @@ package org.bitcoinj.core;
import org.bitcoinj.params.MainNetParams;
import org.bitcoinj.store.BlockStoreException;
import org.junit.Before;
import org.junit.Test;
public class MainnetDownloadIT extends ChainDownloadParent {
public class MainnetDownloadIT {
public MainnetDownloadIT() throws BlockStoreException {
super(new MainNetParams());
static DownloadedChainData data = null;
@Before
public void setup() throws InterruptedException, BlockStoreException {
if (data == null) {
data = new DownloadedChainData(new MainNetParams());
data.setupAndSync(null);
}
}
/**
* check that the chain is higher than a given height and that a specific block is included
* @throws BlockStoreException
*/
@Test
public void testDownloadedChain() throws InterruptedException, BlockStoreException {
sync();
assert(blockChain.getBestChainHeight() > 538009);
assert(blockStore.get(new Sha256Hash("000000000000000000079fc7ce821f88f4864358decd958b676235447e34619b")).getHeight() == 538007);
public void testDownloadedChain() throws BlockStoreException {
assert(data.blockChain.getBestChainHeight() > 538009);
assert(data.blockStore.get(new Sha256Hash("000000000000000000079fc7ce821f88f4864358decd958b676235447e34619b")).getHeight() == 538007);
}
}
......@@ -15,23 +15,71 @@
*/
package org.bitcoinj.core;
import org.bitcoinj.params.MainNetParams;
import org.bitcoinj.params.TestNet3Params;
import org.bitcoinj.store.BlockStoreException;
import org.bitcoinj.wallet.DeterministicSeed;
import org.bitcoinj.wallet.SendRequest;
import org.bitcoinj.wallet.Wallet;
import org.bitcoinj.wallet.listeners.WalletCoinsReceivedEventListener;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
// note: can call this TestnetDownloadIT otherwise it gets included in the unit tests
public class TnetDownloadIT extends ChainDownloadParent {
public class TnetDownloadIT {
static DownloadedChainData data = null;
@Before
public void setup() throws InterruptedException, BlockStoreException {
if (data == null) {
data = new DownloadedChainData(new TestNet3Params());
List<String> mnemonic = Arrays.asList("topic", "scorpion", "vehicle", "mimic", "kidney", "focus", "weekend",
"certain", "version", "area", "topple", "file");
DeterministicSeed seed = new DeterministicSeed(mnemonic, null, "", 1531520999);
Wallet wallet = Wallet.fromSeed(data.parameters, seed);
data.setupAndSync(wallet);
}
}
public TnetDownloadIT() throws BlockStoreException {
super(new MainNetParams());
/**
* check that the chain is higher than a given height and that a specific block is included
* @throws BlockStoreException
*/
@Test
public void testDownloadedChain() throws BlockStoreException {
assertTrue(data.blockChain.getBestChainHeight() > 1245531);
assertEquals(1245530, data.blockStore.get(new Sha256Hash("000000000000f8d83b0341531b39685b1cc2963d0086a9a64cf2de684b804be5")).getHeight());
}
/**
* send & receive to self
*/
@Test
public void testDownloadedChain() throws InterruptedException, BlockStoreException {
sync();
assert (blockChain.getBestChainHeight() > 1245531);
assert (blockStore.get(new Sha256Hash("000000000000f8d83b0341531b39685b1cc2963d0086a9a64cf2de684b804be5")).getHeight() == 1245530);
public void testSendReceive() throws InsufficientMoneyException, ExecutionException, InterruptedException {
assertNotEquals("wallet balance is zero", data.wallet.getBalance(), Coin.ZERO);
// send to self
Address destination = data.wallet.freshReceiveAddress();
Coin startBalance = data.wallet.getBalance();
SendRequest req = SendRequest.to(destination, startBalance.div(4));
req.setUseForkId(true);
data.wallet.completeTx(req);
TransactionBroadcast broadcast = data.peerGroup.broadcastTransaction(req.tx);
Transaction sent = broadcast.future().get();
System.out.println(String.format("sent tx, txid=%s", sent.getHashAsString()));
assertTrue(sent.isMature());
// check my balance, it should be greater than 0.9* initial balance
assertTrue(data.wallet.getBalance(Wallet.BalanceType.ESTIMATED).isGreaterThan(startBalance.minus(startBalance.div(10))));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment