diff --git a/core/src/main/java/org/bitcoinj/core/Peer.java b/core/src/main/java/org/bitcoinj/core/Peer.java index 13209d335..683b1c8f3 100644 --- a/core/src/main/java/org/bitcoinj/core/Peer.java +++ b/core/src/main/java/org/bitcoinj/core/Peer.java @@ -402,11 +402,7 @@ public class Peer extends PeerSocketHandler { close(); } } else if (m instanceof UTXOsMessage) { - if (getutxoFutures != null) { - SettableFuture future = getutxoFutures.pollFirst(); - if (future != null) - future.set((UTXOsMessage) m); - } + processUTXOMessage((UTXOsMessage) m); } else if (m instanceof RejectMessage) { log.error("{} {}: Received {}", this, getPeerVersionMessage().subVer, m); } else { @@ -414,6 +410,19 @@ public class Peer extends PeerSocketHandler { } } + private void processUTXOMessage(UTXOsMessage m) { + SettableFuture future = null; + lock.lock(); + try { + if (getutxoFutures != null) + future = getutxoFutures.pollFirst(); + } finally { + lock.unlock(); + } + if (future != null) + future.set(m); + } + private void processAddressMessage(AddressMessage m) { SettableFuture future; synchronized (getAddrFutures) { diff --git a/core/src/main/java/org/bitcoinj/core/UTXOsMessage.java b/core/src/main/java/org/bitcoinj/core/UTXOsMessage.java index d93848823..753b8ed15 100644 --- a/core/src/main/java/org/bitcoinj/core/UTXOsMessage.java +++ b/core/src/main/java/org/bitcoinj/core/UTXOsMessage.java @@ -79,10 +79,11 @@ public class UTXOsMessage extends Message { stream.write(new VarInt(hits.length).encode()); stream.write(hits); stream.write(new VarInt(outputs.size()).encode()); - for (TransactionOutput output : outputs) { + for (int i = 0; i < outputs.size(); i++) { + TransactionOutput output = outputs.get(i); Transaction tx = output.getParentTransaction(); Utils.uint32ToByteStreamLE(tx != null ? tx.getVersion() : 0L, stream); // Version - Utils.uint32ToByteStreamLE(height, stream); // Height + Utils.uint32ToByteStreamLE(heights[i], stream); // Height output.bitcoinSerializeToStream(stream); } } diff --git a/core/src/test/java/org/bitcoinj/core/PeerTest.java b/core/src/test/java/org/bitcoinj/core/PeerTest.java index 5e2f636e1..0d3c5e5ea 100644 --- a/core/src/test/java/org/bitcoinj/core/PeerTest.java +++ b/core/src/test/java/org/bitcoinj/core/PeerTest.java @@ -57,7 +57,6 @@ public class PeerTest extends TestWithNetworkConnections { private Peer peer; private InboundMessageQueuer writeTarget; private static final int OTHER_PEER_CHAIN_HEIGHT = 110; - private TxConfidenceTable confidenceTable; private final AtomicBoolean fail = new AtomicBoolean(false); @@ -77,8 +76,6 @@ public class PeerTest extends TestWithNetworkConnections { @Before public void setUp() throws Exception { super.setUp(); - - confidenceTable = Context.get().getConfidenceTable(); VersionMessage ver = new VersionMessage(unitTestParams, 100); InetSocketAddress address = new InetSocketAddress("127.0.0.1", 4000); peer = new Peer(unitTestParams, ver, new PeerAddress(address), blockChain); @@ -820,10 +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); - Sha256Hash hash1 = Sha256Hash.hash("foo".getBytes()); - TransactionOutPoint op1 = new TransactionOutPoint(unitTestParams, 1, hash1); - Sha256Hash hash2 = Sha256Hash.hash("bar".getBytes()); - TransactionOutPoint op2 = new TransactionOutPoint(unitTestParams, 2, hash1); + TransactionOutPoint op1 = new TransactionOutPoint(unitTestParams, 1, Sha256Hash.hash("foo".getBytes())); + TransactionOutPoint op2 = new TransactionOutPoint(unitTestParams, 2, Sha256Hash.hash("bar".getBytes())); ListenableFuture future1 = peer.getUTXOs(ImmutableList.of(op1)); ListenableFuture future2 = peer.getUTXOs(ImmutableList.of(op2)); @@ -839,14 +834,14 @@ public class PeerTest extends TestWithNetworkConnections { ECKey key = new ECKey(); TransactionOutput out1 = new TransactionOutput(unitTestParams, null, Coin.CENT, key); - UTXOsMessage response1 = new UTXOsMessage(unitTestParams, ImmutableList.of(out1), new long[]{-1}, Sha256Hash.ZERO_HASH, 1234); + UTXOsMessage response1 = new UTXOsMessage(unitTestParams, 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[]{-1}, Sha256Hash.ZERO_HASH, 1234); + UTXOsMessage response2 = new UTXOsMessage(unitTestParams, ImmutableList.of(out2), new long[]{1000}, Sha256Hash.ZERO_HASH, 1234); inbound(writeTarget, response2); - assertEquals(future1.get(), response2); + assertEquals(future2.get(), response2); } @Test