mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-03-10 09:20:04 +01: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:
parent
2e451800d1
commit
3e5f796407
4 changed files with 27 additions and 23 deletions
|
@ -218,6 +218,11 @@
|
||||||
<groupId>com.google.protobuf</groupId>
|
<groupId>com.google.protobuf</groupId>
|
||||||
<artifactId>protobuf-java</artifactId>
|
<artifactId>protobuf-java</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.guava</groupId>
|
||||||
|
<artifactId>guava-base</artifactId>
|
||||||
|
<version>r03</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package com.google.bitcoin.core;
|
package com.google.bitcoin.core;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -87,9 +88,9 @@ public class Block extends Message {
|
||||||
length = 80;
|
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 {
|
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
|
// 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.
|
// invoke a light parse of transactions to calculate the length.
|
||||||
if (length == UNKNOWN_LENGTH) {
|
if (length == UNKNOWN_LENGTH) {
|
||||||
assert !parseLazy : "Performing lite parse of block transaction as block was initialised from byte array " +
|
Preconditions.checkState(parseLazy,
|
||||||
"without providing length. This should never need to happen. parseLazy: " + parseLazy;
|
"Performing lite parse of block transaction as block was initialised from byte array " +
|
||||||
|
"without providing length. This should never need to happen.");
|
||||||
parseTransactions();
|
parseTransactions();
|
||||||
length = cursor - offset;
|
length = cursor - offset;
|
||||||
} else {
|
} else {
|
||||||
|
@ -334,7 +336,7 @@ public class Block extends Message {
|
||||||
|
|
||||||
// we have completely cached byte array.
|
// we have completely cached byte array.
|
||||||
if (headerBytesValid && transactionBytesValid) {
|
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) {
|
if (length == bytes.length) {
|
||||||
return bytes;
|
return bytes;
|
||||||
} else {
|
} 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
|
// 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
|
// valid block from the network and simply replace the transactions in it with their own fictional
|
||||||
// transactions that reference spent or non-existant inputs.
|
// transactions that reference spent or non-existant inputs.
|
||||||
assert transactions.size() > 0;
|
Preconditions.checkState(!transactions.isEmpty());
|
||||||
maybeParseTransactions();
|
maybeParseTransactions();
|
||||||
checkTransactions();
|
checkTransactions();
|
||||||
checkMerkleRoot();
|
checkMerkleRoot();
|
||||||
|
|
|
@ -19,6 +19,7 @@ package com.google.bitcoin.core;
|
||||||
import com.google.bitcoin.store.BlockStore;
|
import com.google.bitcoin.store.BlockStore;
|
||||||
import com.google.bitcoin.store.BlockStoreException;
|
import com.google.bitcoin.store.BlockStoreException;
|
||||||
import com.google.bitcoin.utils.EventListenerInvoker;
|
import com.google.bitcoin.utils.EventListenerInvoker;
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -255,11 +256,12 @@ public class Peer {
|
||||||
private void processHeaders(HeadersMessage m) throws IOException, ProtocolException {
|
private void processHeaders(HeadersMessage m) throws IOException, ProtocolException {
|
||||||
// Runs in network loop thread for this peer.
|
// 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
|
// This method can run if a peer just randomly sends us a "headers" message (should never happen), or more
|
||||||
// when we've requested them as part of chain download using fast catchup. We need to add each block to the
|
// likely when we've requested them as part of chain download using fast catchup. We need to add each block to
|
||||||
// chain if it pre-dates the fast catchup time. If we go past it, we can stop processing the headers and request
|
// the chain if it pre-dates the fast catchup time. If we go past it, we can stop processing the headers and
|
||||||
// the full blocks from that point on instead.
|
// request the full blocks from that point on instead.
|
||||||
assert !downloadBlockBodies;
|
Preconditions.checkState(!downloadBlockBodies);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (int i = 0; i < m.getBlockHeaders().size(); i++) {
|
for (int i = 0; i < m.getBlockHeaders().size(); i++) {
|
||||||
Block header = m.getBlockHeaders().get(i);
|
Block header = m.getBlockHeaders().get(i);
|
||||||
|
@ -446,7 +448,7 @@ public class Peer {
|
||||||
synchronized (announcedTransactionHashes) {
|
synchronized (announcedTransactionHashes) {
|
||||||
if (announcedTransactionHashes.containsKey(tx.getHash())) {
|
if (announcedTransactionHashes.containsKey(tx.getHash())) {
|
||||||
Transaction storedTx = announcedTransactionHashes.get(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());
|
log.debug("Provided with a downloaded transaction we have seen before: {}", tx.getHash());
|
||||||
tx.getConfidence().markBroadcastBy(address);
|
tx.getConfidence().markBroadcastBy(address);
|
||||||
} else {
|
} else {
|
||||||
|
@ -553,15 +555,13 @@ public class Peer {
|
||||||
|
|
||||||
public T get() throws InterruptedException, ExecutionException {
|
public T get() throws InterruptedException, ExecutionException {
|
||||||
latch.await();
|
latch.await();
|
||||||
assert result != null;
|
return Preconditions.checkNotNull(result);
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public T get(long l, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException {
|
public T get(long l, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException {
|
||||||
if (!latch.await(l, timeUnit))
|
if (!latch.await(l, timeUnit))
|
||||||
throw new TimeoutException();
|
throw new TimeoutException();
|
||||||
assert result != null;
|
return Preconditions.checkNotNull(result);
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
InventoryItem getItem() {
|
InventoryItem getItem() {
|
||||||
|
@ -686,12 +686,10 @@ public class Peer {
|
||||||
public int getPeerBlockHeightDifference() {
|
public int getPeerBlockHeightDifference() {
|
||||||
// Chain will overflow signed int blocks in ~41,000 years.
|
// Chain will overflow signed int blocks in ~41,000 years.
|
||||||
int chainHeight = (int) conn.getVersionMessage().bestHeight;
|
int chainHeight = (int) conn.getVersionMessage().bestHeight;
|
||||||
if (chainHeight <= 0) {
|
// chainHeight should not be zero/negative because we shouldn't have given the user a Peer that is to another
|
||||||
// This should not happen because we shouldn't have given the user a Peer that is to another client-mode
|
// client-mode node, nor should it be unconnected. If that happens it means the user overrode us somewhere or
|
||||||
// node, nor should it be unconnected. If that happens it means the user overrode us somewhere or there is
|
// there is a bug in the peer management code.
|
||||||
// a bug in the peer management code.
|
Preconditions.checkState(chainHeight > 0, "Connected to peer with zero/negative chain height", chainHeight);
|
||||||
throw new RuntimeException("Connected to peer advertising zero/negative chain height.");
|
|
||||||
}
|
|
||||||
return chainHeight - blockChain.getChainHead().getHeight();
|
return chainHeight - blockChain.getChainHead().getHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -203,7 +203,6 @@ public class BitcoinSerializerTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testHeaders1() throws Exception {
|
public void testHeaders1() throws Exception {
|
||||||
|
|
||||||
BitcoinSerializer bs = new BitcoinSerializer(NetworkParameters.prodNet(), true,
|
BitcoinSerializer bs = new BitcoinSerializer(NetworkParameters.prodNet(), true,
|
||||||
null);
|
null);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue