getutxo: Bugfixes identified by static analysis

This commit is contained in:
Mike Hearn 2015-04-06 15:32:02 +02:00
parent 037ec5aef9
commit 7576a44ef4
3 changed files with 22 additions and 17 deletions

View File

@ -402,11 +402,7 @@ public class Peer extends PeerSocketHandler {
close();
}
} else if (m instanceof UTXOsMessage) {
if (getutxoFutures != null) {
SettableFuture<UTXOsMessage> 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<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) {
SettableFuture<AddressMessage> future;
synchronized (getAddrFutures) {

View File

@ -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);
}
}

View File

@ -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<UTXOsMessage> future1 = peer.getUTXOs(ImmutableList.of(op1));
ListenableFuture<UTXOsMessage> 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