3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-12 10:15:52 +00:00

Add a dependency on Guava base libraries and replace a few asserts with Preconditions, which means they will always run including in production code. Fix a bug revealed by this (IntelliJ does not run unit tests with assertions enabled!)

This commit is contained in:
Mike Hearn 2012-04-02 13:40:20 +02:00
parent 2e451800d1
commit 3e5f796407
4 changed files with 27 additions and 23 deletions

View File

@ -218,6 +218,11 @@
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-base</artifactId>
<version>r03</version>
</dependency>
</dependencies>
</project>

View File

@ -16,6 +16,7 @@
package com.google.bitcoin.core;
import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -87,9 +88,9 @@ public class Block extends Message {
length = 80;
}
/** Constructs a block object from the BitCoin wire format. */
/** Constructs a block object from the Bitcoin wire format. */
public Block(NetworkParameters params, byte[] payloadBytes) throws ProtocolException {
super(params, payloadBytes, 0);
super(params, payloadBytes, 0, false, false, payloadBytes.length);
}
/**
@ -170,8 +171,9 @@ public class Block extends Message {
// Ignore the header since it has fixed length. If length is not provided we will have to
// invoke a light parse of transactions to calculate the length.
if (length == UNKNOWN_LENGTH) {
assert !parseLazy : "Performing lite parse of block transaction as block was initialised from byte array " +
"without providing length. This should never need to happen. parseLazy: " + parseLazy;
Preconditions.checkState(parseLazy,
"Performing lite parse of block transaction as block was initialised from byte array " +
"without providing length. This should never need to happen.");
parseTransactions();
length = cursor - offset;
} else {
@ -334,7 +336,7 @@ public class Block extends Message {
// we have completely cached byte array.
if (headerBytesValid && transactionBytesValid) {
assert bytes != null : "Bytes should never be null if headerBytesValid && transactionBytesValid";
Preconditions.checkNotNull(bytes, "Bytes should never be null if headerBytesValid && transactionBytesValid");
if (length == bytes.length) {
return bytes;
} else {
@ -674,7 +676,7 @@ public class Block extends Message {
// an invalid block, but if we didn't validate this then an untrusted man-in-the-middle could obtain the next
// valid block from the network and simply replace the transactions in it with their own fictional
// transactions that reference spent or non-existant inputs.
assert transactions.size() > 0;
Preconditions.checkState(!transactions.isEmpty());
maybeParseTransactions();
checkTransactions();
checkMerkleRoot();

View File

@ -19,6 +19,7 @@ package com.google.bitcoin.core;
import com.google.bitcoin.store.BlockStore;
import com.google.bitcoin.store.BlockStoreException;
import com.google.bitcoin.utils.EventListenerInvoker;
import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -255,11 +256,12 @@ public class Peer {
private void processHeaders(HeadersMessage m) throws IOException, ProtocolException {
// Runs in network loop thread for this peer.
//
// This can happen if a peer just randomly sends us a "headers" message (should never happen), or more likely
// when we've requested them as part of chain download using fast catchup. We need to add each block to the
// chain if it pre-dates the fast catchup time. If we go past it, we can stop processing the headers and request
// the full blocks from that point on instead.
assert !downloadBlockBodies;
// This method can run if a peer just randomly sends us a "headers" message (should never happen), or more
// likely when we've requested them as part of chain download using fast catchup. We need to add each block to
// the chain if it pre-dates the fast catchup time. If we go past it, we can stop processing the headers and
// request the full blocks from that point on instead.
Preconditions.checkState(!downloadBlockBodies);
try {
for (int i = 0; i < m.getBlockHeaders().size(); i++) {
Block header = m.getBlockHeaders().get(i);
@ -446,7 +448,7 @@ public class Peer {
synchronized (announcedTransactionHashes) {
if (announcedTransactionHashes.containsKey(tx.getHash())) {
Transaction storedTx = announcedTransactionHashes.get(tx.getHash());
assert storedTx == tx || storedTx == null : "single Transaction instance";
Preconditions.checkState(storedTx == tx || storedTx == null, "single Transaction instance");
log.debug("Provided with a downloaded transaction we have seen before: {}", tx.getHash());
tx.getConfidence().markBroadcastBy(address);
} else {
@ -553,15 +555,13 @@ public class Peer {
public T get() throws InterruptedException, ExecutionException {
latch.await();
assert result != null;
return result;
return Preconditions.checkNotNull(result);
}
public T get(long l, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException {
if (!latch.await(l, timeUnit))
throw new TimeoutException();
assert result != null;
return result;
return Preconditions.checkNotNull(result);
}
InventoryItem getItem() {
@ -686,12 +686,10 @@ public class Peer {
public int getPeerBlockHeightDifference() {
// Chain will overflow signed int blocks in ~41,000 years.
int chainHeight = (int) conn.getVersionMessage().bestHeight;
if (chainHeight <= 0) {
// This should not happen because we shouldn't have given the user a Peer that is to another client-mode
// node, nor should it be unconnected. If that happens it means the user overrode us somewhere or there is
// a bug in the peer management code.
throw new RuntimeException("Connected to peer advertising zero/negative chain height.");
}
// chainHeight should not be zero/negative because we shouldn't have given the user a Peer that is to another
// client-mode node, nor should it be unconnected. If that happens it means the user overrode us somewhere or
// there is a bug in the peer management code.
Preconditions.checkState(chainHeight > 0, "Connected to peer with zero/negative chain height", chainHeight);
return chainHeight - blockChain.getChainHead().getHeight();
}

View File

@ -203,7 +203,6 @@ public class BitcoinSerializerTest {
*/
@Test
public void testHeaders1() throws Exception {
BitcoinSerializer bs = new BitcoinSerializer(NetworkParameters.prodNet(), true,
null);