mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-01-19 05:33:44 +01:00
getutxo: Bugfixes identified by static analysis
This commit is contained in:
parent
037ec5aef9
commit
7576a44ef4
@ -402,11 +402,7 @@ public class Peer extends PeerSocketHandler {
|
|||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
} else if (m instanceof UTXOsMessage) {
|
} else if (m instanceof UTXOsMessage) {
|
||||||
if (getutxoFutures != null) {
|
processUTXOMessage((UTXOsMessage) m);
|
||||||
SettableFuture<UTXOsMessage> future = getutxoFutures.pollFirst();
|
|
||||||
if (future != null)
|
|
||||||
future.set((UTXOsMessage) m);
|
|
||||||
}
|
|
||||||
} else if (m instanceof RejectMessage) {
|
} else if (m instanceof RejectMessage) {
|
||||||
log.error("{} {}: Received {}", this, getPeerVersionMessage().subVer, m);
|
log.error("{} {}: Received {}", this, getPeerVersionMessage().subVer, m);
|
||||||
} else {
|
} else {
|
||||||
@ -414,6 +410,19 @@ public class Peer extends PeerSocketHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void processUTXOMessage(UTXOsMessage m) {
|
||||||
|
SettableFuture<UTXOsMessage> 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) {
|
private void processAddressMessage(AddressMessage m) {
|
||||||
SettableFuture<AddressMessage> future;
|
SettableFuture<AddressMessage> future;
|
||||||
synchronized (getAddrFutures) {
|
synchronized (getAddrFutures) {
|
||||||
|
@ -79,10 +79,11 @@ public class UTXOsMessage extends Message {
|
|||||||
stream.write(new VarInt(hits.length).encode());
|
stream.write(new VarInt(hits.length).encode());
|
||||||
stream.write(hits);
|
stream.write(hits);
|
||||||
stream.write(new VarInt(outputs.size()).encode());
|
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();
|
Transaction tx = output.getParentTransaction();
|
||||||
Utils.uint32ToByteStreamLE(tx != null ? tx.getVersion() : 0L, stream); // Version
|
Utils.uint32ToByteStreamLE(tx != null ? tx.getVersion() : 0L, stream); // Version
|
||||||
Utils.uint32ToByteStreamLE(height, stream); // Height
|
Utils.uint32ToByteStreamLE(heights[i], stream); // Height
|
||||||
output.bitcoinSerializeToStream(stream);
|
output.bitcoinSerializeToStream(stream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,6 @@ public class PeerTest extends TestWithNetworkConnections {
|
|||||||
private Peer peer;
|
private Peer peer;
|
||||||
private InboundMessageQueuer writeTarget;
|
private InboundMessageQueuer writeTarget;
|
||||||
private static final int OTHER_PEER_CHAIN_HEIGHT = 110;
|
private static final int OTHER_PEER_CHAIN_HEIGHT = 110;
|
||||||
private TxConfidenceTable confidenceTable;
|
|
||||||
private final AtomicBoolean fail = new AtomicBoolean(false);
|
private final AtomicBoolean fail = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
|
||||||
@ -77,8 +76,6 @@ public class PeerTest extends TestWithNetworkConnections {
|
|||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
|
|
||||||
confidenceTable = Context.get().getConfidenceTable();
|
|
||||||
VersionMessage ver = new VersionMessage(unitTestParams, 100);
|
VersionMessage ver = new VersionMessage(unitTestParams, 100);
|
||||||
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 4000);
|
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 4000);
|
||||||
peer = new Peer(unitTestParams, ver, new PeerAddress(address), blockChain);
|
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
|
// Basic test of support for BIP 64: getutxos support. The Lighthouse unit tests exercise this stuff more
|
||||||
// thoroughly.
|
// thoroughly.
|
||||||
connectWithVersion(GetUTXOsMessage.MIN_PROTOCOL_VERSION, VersionMessage.NODE_NETWORK | VersionMessage.NODE_GETUTXOS);
|
connectWithVersion(GetUTXOsMessage.MIN_PROTOCOL_VERSION, VersionMessage.NODE_NETWORK | VersionMessage.NODE_GETUTXOS);
|
||||||
Sha256Hash hash1 = Sha256Hash.hash("foo".getBytes());
|
TransactionOutPoint op1 = new TransactionOutPoint(unitTestParams, 1, Sha256Hash.hash("foo".getBytes()));
|
||||||
TransactionOutPoint op1 = new TransactionOutPoint(unitTestParams, 1, hash1);
|
TransactionOutPoint op2 = new TransactionOutPoint(unitTestParams, 2, Sha256Hash.hash("bar".getBytes()));
|
||||||
Sha256Hash hash2 = Sha256Hash.hash("bar".getBytes());
|
|
||||||
TransactionOutPoint op2 = new TransactionOutPoint(unitTestParams, 2, hash1);
|
|
||||||
|
|
||||||
ListenableFuture<UTXOsMessage> future1 = peer.getUTXOs(ImmutableList.of(op1));
|
ListenableFuture<UTXOsMessage> future1 = peer.getUTXOs(ImmutableList.of(op1));
|
||||||
ListenableFuture<UTXOsMessage> future2 = peer.getUTXOs(ImmutableList.of(op2));
|
ListenableFuture<UTXOsMessage> future2 = peer.getUTXOs(ImmutableList.of(op2));
|
||||||
@ -839,14 +834,14 @@ public class PeerTest extends TestWithNetworkConnections {
|
|||||||
|
|
||||||
ECKey key = new ECKey();
|
ECKey key = new ECKey();
|
||||||
TransactionOutput out1 = new TransactionOutput(unitTestParams, null, Coin.CENT, key);
|
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);
|
inbound(writeTarget, response1);
|
||||||
assertEquals(future1.get(), response1);
|
assertEquals(future1.get(), response1);
|
||||||
|
|
||||||
TransactionOutput out2 = new TransactionOutput(unitTestParams, null, Coin.FIFTY_COINS, key);
|
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);
|
inbound(writeTarget, response2);
|
||||||
assertEquals(future1.get(), response2);
|
assertEquals(future2.get(), response2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user