A bit of unit test cleanup.

This commit is contained in:
Mike Hearn
2015-04-06 16:31:04 +02:00
parent 7576a44ef4
commit b5994e3788
6 changed files with 155 additions and 197 deletions

View File

@@ -44,7 +44,8 @@ import static com.google.common.base.Preconditions.checkState;
*/
public class TestWithNetworkConnections {
public static final int PEER_SERVERS = 5;
protected NetworkParameters unitTestParams;
protected final NetworkParameters params = UnitTestParams.get();
protected Context context;
protected BlockStore blockStore;
protected BlockChain blockChain;
protected Wallet wallet;
@@ -80,16 +81,16 @@ public class TestWithNetworkConnections {
public void setUp(BlockStore blockStore) throws Exception {
BriefLogFormatter.init();
unitTestParams = UnitTestParams.get();
new Context(unitTestParams);
context = new Context(params);
Wallet.SendRequest.DEFAULT_FEE_PER_KB = Coin.ZERO;
this.blockStore = blockStore;
// Allow subclasses to override the wallet object with their own.
if (wallet == null)
wallet = new Wallet(unitTestParams);
key = wallet.freshReceiveKey();
address = key.toAddress(unitTestParams);
blockChain = new BlockChain(unitTestParams, wallet, blockStore);
if (wallet == null) {
wallet = new Wallet(params);
key = wallet.freshReceiveKey();
address = key.toAddress(params);
}
blockChain = new BlockChain(params, wallet, blockStore);
startPeerServers();
if (clientType == ClientType.NIO_CLIENT_MANAGER || clientType == ClientType.BLOCKING_CLIENT_MANAGER) {
@@ -111,7 +112,7 @@ public class TestWithNetworkConnections {
@Nullable
@Override
public StreamParser getNewParser(InetAddress inetAddress, int port) {
return new InboundMessageQueuer(unitTestParams) {
return new InboundMessageQueuer(params) {
@Override
public void connectionClosed() {
}

View File

@@ -16,22 +16,17 @@
package org.bitcoinj.testing;
import com.google.common.base.*;
import com.google.common.util.concurrent.*;
import org.bitcoinj.core.*;
import org.bitcoinj.net.BlockingClientManager;
import org.bitcoinj.net.ClientConnectionManager;
import org.bitcoinj.net.NioClientManager;
import org.bitcoinj.params.UnitTestParams;
import org.bitcoinj.store.BlockStore;
import org.bitcoinj.store.MemoryBlockStore;
import com.google.common.base.Preconditions;
import org.bitcoinj.net.*;
import org.bitcoinj.store.*;
import org.bitcoinj.utils.*;
import java.net.InetSocketAddress;
import java.net.*;
import java.util.concurrent.*;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Preconditions.*;
/**
* You can derive from this class and call peerGroup.start() in your tests to get a functional PeerGroup that can be
@@ -39,7 +34,6 @@ import static com.google.common.base.Preconditions.checkState;
* mock, but means unit tests cannot be run simultaneously.
*/
public class TestWithPeerGroup extends TestWithNetworkConnections {
protected static final NetworkParameters params = UnitTestParams.get();
protected PeerGroup peerGroup;
protected VersionMessage remoteVersionMessage;
@@ -61,7 +55,7 @@ public class TestWithPeerGroup extends TestWithNetworkConnections {
public void setUp(BlockStore blockStore) throws Exception {
super.setUp(blockStore);
remoteVersionMessage = new VersionMessage(unitTestParams, 1);
remoteVersionMessage = new VersionMessage(params, 1);
remoteVersionMessage.localServices = VersionMessage.NODE_NETWORK;
remoteVersionMessage.clientVersion = NotFoundMessage.MIN_PROTOCOL_VERSION;
blockJobs = false;
@@ -95,7 +89,7 @@ public class TestWithPeerGroup extends TestWithNetworkConnections {
protected final Semaphore jobBlocks = new Semaphore(0);
private PeerGroup createPeerGroup(final ClientConnectionManager manager) {
return new PeerGroup(unitTestParams, blockChain, manager) {
return new PeerGroup(params, blockChain, manager) {
@Override
protected ListeningScheduledExecutorService createPrivateExecutor() {
return MoreExecutors.listeningDecorator(new ScheduledThreadPoolExecutor(1, new ContextPropagatingThreadFactory("PeerGroup test thread")) {

File diff suppressed because one or more lines are too long

View File

@@ -17,39 +17,23 @@
package org.bitcoinj.core;
import org.bitcoinj.net.discovery.PeerDiscovery;
import org.bitcoinj.net.discovery.PeerDiscoveryException;
import org.bitcoinj.params.UnitTestParams;
import org.bitcoinj.testing.FakeTxBuilder;
import org.bitcoinj.testing.InboundMessageQueuer;
import org.bitcoinj.testing.TestWithPeerGroup;
import org.bitcoinj.utils.Threading;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.net.InetAddresses;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import com.google.common.collect.*;
import com.google.common.net.*;
import com.google.common.util.concurrent.*;
import org.bitcoinj.net.discovery.*;
import org.bitcoinj.testing.*;
import org.bitcoinj.utils.*;
import org.junit.*;
import org.junit.runner.*;
import org.junit.runners.*;
import java.io.IOException;
import java.net.BindException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import static org.bitcoinj.core.Coin.COIN;
import static org.bitcoinj.core.Coin.valueOf;
import static org.bitcoinj.core.Coin.*;
import static org.junit.Assert.*;
@@ -57,7 +41,6 @@ import static org.junit.Assert.*;
@RunWith(value = Parameterized.class)
public class PeerGroupTest extends TestWithPeerGroup {
static final NetworkParameters params = UnitTestParams.get();
private BlockingQueue<Peer> connectedPeers;
private BlockingQueue<Peer> disconnectedPeers;
private PeerEventListener listener;
@@ -218,8 +201,8 @@ public class PeerGroupTest extends TestWithPeerGroup {
assertEquals(tmp, expectedPeers);
Coin value = COIN;
Transaction t1 = FakeTxBuilder.createFakeTx(unitTestParams, value, address);
InventoryMessage inv = new InventoryMessage(unitTestParams);
Transaction t1 = FakeTxBuilder.createFakeTx(params, value, address);
InventoryMessage inv = new InventoryMessage(params);
inv.addTransaction(t1);
// Note: we start with p2 here to verify that transactions are downloaded from whichever peer announces first
@@ -233,7 +216,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
// Asks for dependency.
GetDataMessage getdata = (GetDataMessage) outbound(p2);
assertNotNull(getdata);
inbound(p2, new NotFoundMessage(unitTestParams, getdata.getItems()));
inbound(p2, new NotFoundMessage(params, getdata.getItems()));
pingAndWait(p2);
assertEquals(value, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
}
@@ -247,9 +230,9 @@ public class PeerGroupTest extends TestWithPeerGroup {
// Create a peer.
InboundMessageQueuer p1 = connectPeer(1);
Wallet wallet2 = new Wallet(unitTestParams);
Wallet wallet2 = new Wallet(params);
ECKey key2 = wallet2.freshReceiveKey();
Address address2 = key2.toAddress(unitTestParams);
Address address2 = key2.toAddress(params);
peerGroup.addWallet(wallet2);
blockChain.addWallet(wallet2);
@@ -258,8 +241,8 @@ public class PeerGroupTest extends TestWithPeerGroup {
assertEquals(MemoryPoolMessage.class, waitForOutbound(p1).getClass());
Coin value = COIN;
Transaction t1 = FakeTxBuilder.createFakeTx(unitTestParams, value, address2);
InventoryMessage inv = new InventoryMessage(unitTestParams);
Transaction t1 = FakeTxBuilder.createFakeTx(params, value, address2);
InventoryMessage inv = new InventoryMessage(params);
inv.addTransaction(t1);
inbound(p1, inv);
@@ -268,7 +251,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
// Asks for dependency.
GetDataMessage getdata = (GetDataMessage) outbound(p1);
assertNotNull(getdata);
inbound(p1, new NotFoundMessage(unitTestParams, getdata.getItems()));
inbound(p1, new NotFoundMessage(params, getdata.getItems()));
pingAndWait(p1);
assertEquals(value, wallet2.getBalance(Wallet.BalanceType.ESTIMATED));
}

View File

@@ -76,9 +76,9 @@ public class PeerTest extends TestWithNetworkConnections {
@Before
public void setUp() throws Exception {
super.setUp();
VersionMessage ver = new VersionMessage(unitTestParams, 100);
VersionMessage ver = new VersionMessage(params, 100);
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 4000);
peer = new Peer(unitTestParams, ver, new PeerAddress(address), blockChain);
peer = new Peer(params, ver, new PeerAddress(address), blockChain);
peer.addWallet(wallet);
}
@@ -94,7 +94,7 @@ public class PeerTest extends TestWithNetworkConnections {
}
private void connectWithVersion(int version, int flags) throws Exception {
VersionMessage peerVersion = new VersionMessage(unitTestParams, OTHER_PEER_CHAIN_HEIGHT);
VersionMessage peerVersion = new VersionMessage(params, OTHER_PEER_CHAIN_HEIGHT);
peerVersion.clientVersion = version;
peerVersion.localServices = flags;
writeTarget = connect(peer, peerVersion);
@@ -132,7 +132,7 @@ public class PeerTest extends TestWithNetworkConnections {
assertEquals(blockStore.getChainHead().getHeader().getHash(), getblocks.getLocator().get(0));
assertEquals(Sha256Hash.ZERO_HASH, getblocks.getStopHash());
// Remote peer sends us an inv with some blocks.
InventoryMessage inv = new InventoryMessage(unitTestParams);
InventoryMessage inv = new InventoryMessage(params);
inv.addBlock(b2);
inv.addBlock(b3);
// We do a getdata on them.
@@ -146,7 +146,7 @@ public class PeerTest extends TestWithNetworkConnections {
inbound(writeTarget, b2);
inbound(writeTarget, b3);
inv = new InventoryMessage(unitTestParams);
inv = new InventoryMessage(params);
inv.addBlock(b5);
// We request the head block.
inbound(writeTarget, inv);
@@ -165,7 +165,7 @@ public class PeerTest extends TestWithNetworkConnections {
// because we walk backwards down the orphan chain and then discover we already asked for those blocks, so
// nothing is done.
Block b6 = makeSolvedTestBlock(b5);
inv = new InventoryMessage(unitTestParams);
inv = new InventoryMessage(params);
inv.addBlock(b6);
inbound(writeTarget, inv);
getdata = (GetDataMessage)outbound(writeTarget);
@@ -174,7 +174,7 @@ public class PeerTest extends TestWithNetworkConnections {
inbound(writeTarget, b6);
assertNull(outbound(writeTarget)); // Nothing is sent at this point.
// We're still waiting for the response to the getblocks (b3,b5) sent above.
inv = new InventoryMessage(unitTestParams);
inv = new InventoryMessage(params);
inv.addBlock(b4);
inv.addBlock(b5);
inbound(writeTarget, inv);
@@ -200,7 +200,7 @@ public class PeerTest extends TestWithNetworkConnections {
Block b2 = makeSolvedTestBlock(b1);
Block b3 = makeSolvedTestBlock(b2);
inbound(writeTarget, b3);
InventoryMessage inv = new InventoryMessage(unitTestParams);
InventoryMessage inv = new InventoryMessage(params);
InventoryItem item = new InventoryItem(InventoryItem.Type.Block, b3.getHash());
inv.addItem(item);
inbound(writeTarget, inv);
@@ -208,7 +208,7 @@ public class PeerTest extends TestWithNetworkConnections {
GetBlocksMessage getblocks = (GetBlocksMessage)outbound(writeTarget);
List<Sha256Hash> expectedLocator = new ArrayList<Sha256Hash>();
expectedLocator.add(b1.getHash());
expectedLocator.add(unitTestParams.getGenesisBlock().getHash());
expectedLocator.add(params.getGenesisBlock().getHash());
assertEquals(getblocks.getLocator(), expectedLocator);
assertEquals(getblocks.getStopHash(), b3.getHash());
@@ -229,7 +229,7 @@ public class PeerTest extends TestWithNetworkConnections {
Block b2 = makeSolvedTestBlock(b1);
// Receive an inv.
InventoryMessage inv = new InventoryMessage(unitTestParams);
InventoryMessage inv = new InventoryMessage(params);
InventoryItem item = new InventoryItem(InventoryItem.Type.Block, b2.getHash());
inv.addItem(item);
inbound(writeTarget, inv);
@@ -245,8 +245,8 @@ public class PeerTest extends TestWithNetworkConnections {
peer.setDownloadData(true);
// Make a transaction and tell the peer we have it.
Coin value = COIN;
Transaction tx = createFakeTx(unitTestParams, value, address);
InventoryMessage inv = new InventoryMessage(unitTestParams);
Transaction tx = createFakeTx(params, value, address);
InventoryMessage inv = new InventoryMessage(params);
InventoryItem item = new InventoryItem(InventoryItem.Type.Transaction, tx.getHash());
inv.addItem(item);
inbound(writeTarget, inv);
@@ -257,7 +257,7 @@ public class PeerTest extends TestWithNetworkConnections {
inbound(writeTarget, tx);
// Ask for the dependency, it's not in the mempool (in chain).
getdata = (GetDataMessage) outbound(writeTarget);
inbound(writeTarget, new NotFoundMessage(unitTestParams, getdata.getItems()));
inbound(writeTarget, new NotFoundMessage(params, getdata.getItems()));
pingAndWait(writeTarget);
assertEquals(value, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
}
@@ -265,11 +265,11 @@ public class PeerTest extends TestWithNetworkConnections {
@Test
public void invDownloadTxMultiPeer() throws Exception {
// Check co-ordination of which peer to download via the memory pool.
VersionMessage ver = new VersionMessage(unitTestParams, 100);
VersionMessage ver = new VersionMessage(params, 100);
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 4242);
Peer peer2 = new Peer(unitTestParams, ver, new PeerAddress(address), blockChain);
Peer peer2 = new Peer(params, ver, new PeerAddress(address), blockChain);
peer2.addWallet(wallet);
VersionMessage peerVersion = new VersionMessage(unitTestParams, OTHER_PEER_CHAIN_HEIGHT);
VersionMessage peerVersion = new VersionMessage(params, OTHER_PEER_CHAIN_HEIGHT);
peerVersion.clientVersion = 70001;
peerVersion.localServices = VersionMessage.NODE_NETWORK;
@@ -278,8 +278,8 @@ public class PeerTest extends TestWithNetworkConnections {
// Make a tx and advertise it to one of the peers.
Coin value = COIN;
Transaction tx = createFakeTx(unitTestParams, value, this.address);
InventoryMessage inv = new InventoryMessage(unitTestParams);
Transaction tx = createFakeTx(params, value, this.address);
InventoryMessage inv = new InventoryMessage(params);
InventoryItem item = new InventoryItem(InventoryItem.Type.Transaction, tx.getHash());
inv.addItem(item);
@@ -304,7 +304,7 @@ public class PeerTest extends TestWithNetworkConnections {
blockChain.add(b1);
final Block b2 = makeSolvedTestBlock(b1);
// Receive notification of a new block.
final InventoryMessage inv = new InventoryMessage(unitTestParams);
final InventoryMessage inv = new InventoryMessage(params);
InventoryItem item = new InventoryItem(InventoryItem.Type.Block, b2.getHash());
inv.addItem(item);
@@ -379,7 +379,7 @@ public class PeerTest extends TestWithNetworkConnections {
List<Sha256Hash> expectedLocator = new ArrayList<Sha256Hash>();
expectedLocator.add(b2.getHash());
expectedLocator.add(b1.getHash());
expectedLocator.add(unitTestParams.getGenesisBlock().getHash());
expectedLocator.add(params.getGenesisBlock().getHash());
GetBlocksMessage message = (GetBlocksMessage) outbound(writeTarget);
assertEquals(message.getLocator(), expectedLocator);
@@ -415,9 +415,9 @@ public class PeerTest extends TestWithNetworkConnections {
Block b1 = createFakeBlock(blockStore).block;
blockChain.add(b1);
Block b2 = makeSolvedTestBlock(b1);
Transaction t = new Transaction(unitTestParams);
Transaction t = new Transaction(params);
t.addInput(b1.getTransactions().get(0).getOutput(0));
t.addOutput(new TransactionOutput(unitTestParams, t, Coin.ZERO, new byte[Block.MAX_BLOCK_SIZE - 1000]));
t.addOutput(new TransactionOutput(params, t, Coin.ZERO, new byte[Block.MAX_BLOCK_SIZE - 1000]));
b2.addTransaction(t);
// Request the block.
@@ -460,23 +460,23 @@ public class PeerTest extends TestWithNetworkConnections {
GetHeadersMessage getheaders = (GetHeadersMessage) outbound(writeTarget);
List<Sha256Hash> expectedLocator = new ArrayList<Sha256Hash>();
expectedLocator.add(b1.getHash());
expectedLocator.add(unitTestParams.getGenesisBlock().getHash());
expectedLocator.add(params.getGenesisBlock().getHash());
assertEquals(getheaders.getLocator(), expectedLocator);
assertEquals(getheaders.getStopHash(), Sha256Hash.ZERO_HASH);
// Now send all the headers.
HeadersMessage headers = new HeadersMessage(unitTestParams, b2.cloneAsHeader(),
HeadersMessage headers = new HeadersMessage(params, b2.cloneAsHeader(),
b3.cloneAsHeader(), b4.cloneAsHeader());
// We expect to be asked for b3 and b4 again, but this time, with a body.
expectedLocator.clear();
expectedLocator.add(b2.getHash());
expectedLocator.add(b1.getHash());
expectedLocator.add(unitTestParams.getGenesisBlock().getHash());
expectedLocator.add(params.getGenesisBlock().getHash());
inbound(writeTarget, headers);
GetBlocksMessage getblocks = (GetBlocksMessage) outbound(writeTarget);
assertEquals(expectedLocator, getblocks.getLocator());
assertEquals(Sha256Hash.ZERO_HASH, getblocks.getStopHash());
// We're supposed to get an inv here.
InventoryMessage inv = new InventoryMessage(unitTestParams);
InventoryMessage inv = new InventoryMessage(params);
inv.addItem(new InventoryItem(InventoryItem.Type.Block, b3.getHash()));
inbound(writeTarget, inv);
GetDataMessage getdata = (GetDataMessage) outbound(writeTarget);
@@ -523,7 +523,7 @@ public class PeerTest extends TestWithNetworkConnections {
peer.setDownloadTxDependencies(false);
connect();
// Check that if we request dependency download to be disabled and receive a relevant tx, things work correctly.
Transaction tx = FakeTxBuilder.createFakeTx(unitTestParams, COIN, address);
Transaction tx = FakeTxBuilder.createFakeTx(params, COIN, address);
final Transaction[] result = new Transaction[1];
wallet.addEventListener(new AbstractWalletEventListener() {
@Override
@@ -557,29 +557,29 @@ public class PeerTest extends TestWithNetworkConnections {
// -> [t7]
// -> [t8]
// The ones in brackets are assumed to be in the chain and are represented only by hashes.
Transaction t2 = FakeTxBuilder.createFakeTx(unitTestParams, COIN, to);
Transaction t2 = FakeTxBuilder.createFakeTx(params, COIN, to);
Sha256Hash t5 = t2.getInput(0).getOutpoint().getHash();
Transaction t4 = FakeTxBuilder.createFakeTx(unitTestParams, COIN, new ECKey());
Transaction t4 = FakeTxBuilder.createFakeTx(params, COIN, new ECKey());
Sha256Hash t6 = t4.getInput(0).getOutpoint().getHash();
t4.addOutput(COIN, new ECKey());
Transaction t3 = new Transaction(unitTestParams);
Transaction t3 = new Transaction(params);
t3.addInput(t4.getOutput(0));
t3.addOutput(COIN, new ECKey());
Transaction t1 = new Transaction(unitTestParams);
Transaction t1 = new Transaction(params);
t1.addInput(t2.getOutput(0));
t1.addInput(t3.getOutput(0));
Sha256Hash someHash = new Sha256Hash("2b801dd82f01d17bbde881687bf72bc62e2faa8ab8133d36fcb8c3abe7459da6");
t1.addInput(new TransactionInput(unitTestParams, t1, new byte[]{}, new TransactionOutPoint(unitTestParams, 0, someHash)));
t1.addInput(new TransactionInput(params, t1, new byte[]{}, new TransactionOutPoint(params, 0, someHash)));
Sha256Hash anotherHash = new Sha256Hash("3b801dd82f01d17bbde881687bf72bc62e2faa8ab8133d36fcb8c3abe7459da6");
t1.addInput(new TransactionInput(unitTestParams, t1, new byte[]{}, new TransactionOutPoint(unitTestParams, 1, anotherHash)));
t1.addInput(new TransactionInput(params, t1, new byte[]{}, new TransactionOutPoint(params, 1, anotherHash)));
t1.addOutput(COIN, to);
t1 = FakeTxBuilder.roundTripTransaction(unitTestParams, t1);
t2 = FakeTxBuilder.roundTripTransaction(unitTestParams, t2);
t3 = FakeTxBuilder.roundTripTransaction(unitTestParams, t3);
t4 = FakeTxBuilder.roundTripTransaction(unitTestParams, t4);
t1 = FakeTxBuilder.roundTripTransaction(params, t1);
t2 = FakeTxBuilder.roundTripTransaction(params, t2);
t3 = FakeTxBuilder.roundTripTransaction(params, t3);
t4 = FakeTxBuilder.roundTripTransaction(params, t4);
// Announce the first one. Wait for it to be downloaded.
InventoryMessage inv = new InventoryMessage(unitTestParams);
InventoryMessage inv = new InventoryMessage(params);
inv.addTransaction(t1);
inbound(writeTarget, inv);
GetDataMessage getdata = (GetDataMessage) outbound(writeTarget);
@@ -601,7 +601,7 @@ public class PeerTest extends TestWithNetworkConnections {
// Deliver the requested transactions.
inbound(writeTarget, t2);
inbound(writeTarget, t3);
NotFoundMessage notFound = new NotFoundMessage(unitTestParams);
NotFoundMessage notFound = new NotFoundMessage(params);
notFound.addItem(new InventoryItem(InventoryItem.Type.Transaction, someHash));
notFound.addItem(new InventoryItem(InventoryItem.Type.Transaction, anotherHash));
inbound(writeTarget, notFound);
@@ -610,7 +610,7 @@ public class PeerTest extends TestWithNetworkConnections {
getdata = (GetDataMessage) outbound(writeTarget);
assertEquals(getdata.getItems().get(0).hash, t2.getInput(0).getOutpoint().getHash());
// t5 isn't found and t4 is.
notFound = new NotFoundMessage(unitTestParams);
notFound = new NotFoundMessage(params);
notFound.addItem(new InventoryItem(InventoryItem.Type.Transaction, t5));
inbound(writeTarget, notFound);
assertFalse(futures.isDone());
@@ -621,7 +621,7 @@ public class PeerTest extends TestWithNetworkConnections {
// Continue to explore the t4 branch and ask for t6, which is in the chain.
getdata = (GetDataMessage) outbound(writeTarget);
assertEquals(t6, getdata.getItems().get(0).hash);
notFound = new NotFoundMessage(unitTestParams);
notFound = new NotFoundMessage(params);
notFound.addItem(new InventoryItem(InventoryItem.Type.Transaction, t6));
inbound(writeTarget, notFound);
pingAndWait(writeTarget);
@@ -638,7 +638,7 @@ public class PeerTest extends TestWithNetworkConnections {
connectWithVersion(70001, VersionMessage.NODE_NETWORK);
// Test that if we receive a relevant transaction that has a lock time, it doesn't result in a notification
// until we explicitly opt in to seeing those.
Wallet wallet = new Wallet(unitTestParams);
Wallet wallet = new Wallet(params);
ECKey key = wallet.freshReceiveKey();
peer.addWallet(wallet);
final Transaction[] vtx = new Transaction[1];
@@ -649,16 +649,16 @@ public class PeerTest extends TestWithNetworkConnections {
}
});
// Send a normal relevant transaction, it's received correctly.
Transaction t1 = FakeTxBuilder.createFakeTx(unitTestParams, COIN, key);
Transaction t1 = FakeTxBuilder.createFakeTx(params, COIN, key);
inbound(writeTarget, t1);
GetDataMessage getdata = (GetDataMessage) outbound(writeTarget);
inbound(writeTarget, new NotFoundMessage(unitTestParams, getdata.getItems()));
inbound(writeTarget, new NotFoundMessage(params, getdata.getItems()));
pingAndWait(writeTarget);
Threading.waitForUserCode();
assertNotNull(vtx[0]);
vtx[0] = null;
// Send a timelocked transaction, nothing happens.
Transaction t2 = FakeTxBuilder.createFakeTx(unitTestParams, valueOf(2, 0), key);
Transaction t2 = FakeTxBuilder.createFakeTx(params, valueOf(2, 0), key);
t2.setLockTime(999999);
inbound(writeTarget, t2);
Threading.waitForUserCode();
@@ -667,7 +667,7 @@ public class PeerTest extends TestWithNetworkConnections {
wallet.setAcceptRiskyTransactions(true);
inbound(writeTarget, t2);
getdata = (GetDataMessage) outbound(writeTarget);
inbound(writeTarget, new NotFoundMessage(unitTestParams, getdata.getItems()));
inbound(writeTarget, new NotFoundMessage(params, getdata.getItems()));
pingAndWait(writeTarget);
Threading.waitForUserCode();
assertEquals(t2, vtx[0]);
@@ -689,7 +689,7 @@ public class PeerTest extends TestWithNetworkConnections {
private void checkTimeLockedDependency(boolean shouldAccept) throws Exception {
// Initial setup.
connectWithVersion(70001, VersionMessage.NODE_NETWORK);
Wallet wallet = new Wallet(unitTestParams);
Wallet wallet = new Wallet(params);
ECKey key = wallet.freshReceiveKey();
wallet.setAcceptRiskyTransactions(shouldAccept);
peer.addWallet(wallet);
@@ -701,18 +701,18 @@ public class PeerTest extends TestWithNetworkConnections {
}
});
// t1 -> t2 [locked] -> t3 (not available)
Transaction t2 = new Transaction(unitTestParams);
Transaction t2 = new Transaction(params);
t2.setLockTime(999999);
// Add a fake input to t3 that goes nowhere.
Sha256Hash t3 = Sha256Hash.hash("abc".getBytes(Charset.forName("UTF-8")));
t2.addInput(new TransactionInput(unitTestParams, t2, new byte[]{}, new TransactionOutPoint(unitTestParams, 0, t3)));
t2.addInput(new TransactionInput(params, t2, new byte[]{}, new TransactionOutPoint(params, 0, t3)));
t2.getInput(0).setSequenceNumber(0xDEADBEEF);
t2.addOutput(COIN, new ECKey());
Transaction t1 = new Transaction(unitTestParams);
Transaction t1 = new Transaction(params);
t1.addInput(t2.getOutput(0));
t1.addOutput(COIN, key); // Make it relevant.
// Announce t1.
InventoryMessage inv = new InventoryMessage(unitTestParams);
InventoryMessage inv = new InventoryMessage(params);
inv.addTransaction(t1);
inbound(writeTarget, inv);
// Send it.
@@ -729,7 +729,7 @@ public class PeerTest extends TestWithNetworkConnections {
getdata = (GetDataMessage) outbound(writeTarget);
assertEquals(t3, getdata.getItems().get(0).hash);
// Can't find it: bottom of tree.
NotFoundMessage notFound = new NotFoundMessage(unitTestParams);
NotFoundMessage notFound = new NotFoundMessage(params);
notFound.addItem(new InventoryItem(InventoryItem.Type.Transaction, t3));
inbound(writeTarget, notFound);
pingAndWait(writeTarget);
@@ -796,15 +796,15 @@ public class PeerTest extends TestWithNetworkConnections {
}
});
connect();
Transaction t1 = new Transaction(unitTestParams);
t1.addInput(new TransactionInput(unitTestParams, t1, new byte[]{}));
t1.addOutput(COIN, new ECKey().toAddress(unitTestParams));
Transaction t2 = new Transaction(unitTestParams);
Transaction t1 = new Transaction(params);
t1.addInput(new TransactionInput(params, t1, new byte[]{}));
t1.addOutput(COIN, new ECKey().toAddress(params));
Transaction t2 = new Transaction(params);
t2.addInput(t1.getOutput(0));
t2.addOutput(COIN, wallet.getChangeAddress());
inbound(writeTarget, t2);
final InventoryItem inventoryItem = new InventoryItem(InventoryItem.Type.Transaction, t2.getInput(0).getOutpoint().getHash());
final NotFoundMessage nfm = new NotFoundMessage(unitTestParams, Lists.newArrayList(inventoryItem));
final NotFoundMessage nfm = new NotFoundMessage(params, Lists.newArrayList(inventoryItem));
inbound(writeTarget, nfm);
pingAndWait(writeTarget);
Threading.waitForUserCode();
@@ -817,8 +817,8 @@ public class PeerTest extends TestWithNetworkConnections {
// Basic test of support for BIP 64: getutxos support. The Lighthouse unit tests exercise this stuff more
// thoroughly.
connectWithVersion(GetUTXOsMessage.MIN_PROTOCOL_VERSION, VersionMessage.NODE_NETWORK | VersionMessage.NODE_GETUTXOS);
TransactionOutPoint op1 = new TransactionOutPoint(unitTestParams, 1, Sha256Hash.hash("foo".getBytes()));
TransactionOutPoint op2 = new TransactionOutPoint(unitTestParams, 2, Sha256Hash.hash("bar".getBytes()));
TransactionOutPoint op1 = new TransactionOutPoint(params, 1, Sha256Hash.hash("foo".getBytes()));
TransactionOutPoint op2 = new TransactionOutPoint(params, 2, Sha256Hash.hash("bar".getBytes()));
ListenableFuture<UTXOsMessage> future1 = peer.getUTXOs(ImmutableList.of(op1));
ListenableFuture<UTXOsMessage> future2 = peer.getUTXOs(ImmutableList.of(op2));
@@ -833,13 +833,13 @@ public class PeerTest extends TestWithNetworkConnections {
assertFalse(future1.isDone());
ECKey key = new ECKey();
TransactionOutput out1 = new TransactionOutput(unitTestParams, null, Coin.CENT, key);
UTXOsMessage response1 = new UTXOsMessage(unitTestParams, ImmutableList.of(out1), new long[]{UTXOsMessage.MEMPOOL_HEIGHT}, Sha256Hash.ZERO_HASH, 1234);
TransactionOutput out1 = new TransactionOutput(params, null, Coin.CENT, key);
UTXOsMessage response1 = new UTXOsMessage(params, ImmutableList.of(out1), new long[]{UTXOsMessage.MEMPOOL_HEIGHT}, Sha256Hash.ZERO_HASH, 1234);
inbound(writeTarget, response1);
assertEquals(future1.get(), response1);
TransactionOutput out2 = new TransactionOutput(unitTestParams, null, Coin.FIFTY_COINS, key);
UTXOsMessage response2 = new UTXOsMessage(unitTestParams, ImmutableList.of(out2), new long[]{1000}, Sha256Hash.ZERO_HASH, 1234);
TransactionOutput out2 = new TransactionOutput(params, null, Coin.FIFTY_COINS, key);
UTXOsMessage response2 = new UTXOsMessage(params, ImmutableList.of(out2), new long[]{1000}, Sha256Hash.ZERO_HASH, 1234);
inbound(writeTarget, response2);
assertEquals(future2.get(), response2);
}

View File

@@ -18,30 +18,21 @@
package org.bitcoinj.core;
import com.google.common.util.concurrent.*;
import org.bitcoinj.params.UnitTestParams;
import org.bitcoinj.testing.FakeTxBuilder;
import org.bitcoinj.testing.InboundMessageQueuer;
import org.bitcoinj.testing.TestWithPeerGroup;
import org.bitcoinj.utils.Threading;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.bitcoinj.testing.*;
import org.bitcoinj.utils.*;
import org.junit.*;
import org.junit.runner.*;
import org.junit.runners.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.Random;
import java.util.*;
import java.util.concurrent.*;
import static com.google.common.base.Preconditions.*;
import static org.bitcoinj.core.Coin.*;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.junit.Assert.*;
@RunWith(value = Parameterized.class)
public class TransactionBroadcastTest extends TestWithPeerGroup {
static final NetworkParameters params = UnitTestParams.get();
@Parameterized.Parameters
public static Collection<ClientType[]> parameters() {
return Arrays.asList(new ClientType[] {ClientType.NIO_CLIENT_MANAGER},