mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-01-30 23:02:15 +00:00
Clear some more FindBugs warnings, including a real bugfix to the bloom filter calculations.
This commit is contained in:
parent
883bf03007
commit
4b4eb7b623
@ -96,7 +96,7 @@ public abstract class AbstractBlockChain {
|
||||
private final List<BlockChainListener> listeners;
|
||||
|
||||
// Holds a block header and, optionally, a list of tx hashes or block's transactions
|
||||
class OrphanBlock {
|
||||
protected static class OrphanBlock {
|
||||
Block block;
|
||||
Set<Sha256Hash> filteredTxHashes;
|
||||
List<Transaction> filteredTxn;
|
||||
|
@ -302,7 +302,7 @@ public class BitcoinSerializer {
|
||||
}
|
||||
|
||||
|
||||
public class BitcoinPacketHeader {
|
||||
public static class BitcoinPacketHeader {
|
||||
public final byte[] header;
|
||||
public final String command;
|
||||
public final int size;
|
||||
|
@ -35,7 +35,7 @@ import java.util.Arrays;
|
||||
public class BloomFilter extends Message {
|
||||
/** The BLOOM_UPDATE_* constants control when the bloom filter is auto-updated by the peer using
|
||||
it as a filter, either never, for all outputs or only for pay-2-pubkey outputs (default) */
|
||||
public enum bloomUpdate {
|
||||
public enum BloomUpdate {
|
||||
UPDATE_NONE, // 0
|
||||
UPDATE_ALL, // 1
|
||||
/** Only adds outpoints to the filter if the output is a pay-to-pubkey/pay-to-multisig script */
|
||||
@ -64,7 +64,7 @@ public class BloomFilter extends Message {
|
||||
* Constructs a filter with the given parameters which is updated on pay2pubkey outputs only.
|
||||
*/
|
||||
public BloomFilter(int elements, double falsePositiveRate, long randomNonce) {
|
||||
this(elements, falsePositiveRate, randomNonce, bloomUpdate.UPDATE_P2PUBKEY_ONLY);
|
||||
this(elements, falsePositiveRate, randomNonce, BloomUpdate.UPDATE_P2PUBKEY_ONLY);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -98,14 +98,14 @@ public class BloomFilter extends Message {
|
||||
*
|
||||
* <p>updateFlag is used to control filter behavior</p>
|
||||
*/
|
||||
public BloomFilter(int elements, double falsePositiveRate, long randomNonce, bloomUpdate updateFlag) {
|
||||
public BloomFilter(int elements, double falsePositiveRate, long randomNonce, BloomUpdate updateFlag) {
|
||||
// The following formulas were stolen from Wikipedia's page on Bloom Filters (with the addition of min(..., MAX_...))
|
||||
// Size required for a given number of elements and false-positive rate
|
||||
int size = Math.min((int)(-1 / (Math.pow(Math.log(2), 2)) * elements * Math.log(falsePositiveRate)),
|
||||
(int)MAX_FILTER_SIZE * 8) / 8;
|
||||
data = new byte[size <= 0 ? 1 : size];
|
||||
// Optimal number of hash functions for a given filter size and element count.
|
||||
hashFuncs = Math.min((int)(data.length * 8 / elements * Math.log(2)), MAX_HASH_FUNCS);
|
||||
hashFuncs = Math.min((int)(data.length * 8 / (double)elements * Math.log(2)), MAX_HASH_FUNCS);
|
||||
this.nTweak = randomNonce;
|
||||
this.nFlags = (byte)(0xff & updateFlag.ordinal());
|
||||
}
|
||||
|
@ -92,7 +92,7 @@ public class PartialMerkleTree extends Message {
|
||||
return (transactionCount+(1 << height)-1) >> height;
|
||||
}
|
||||
|
||||
class ValuesUsed {
|
||||
private static class ValuesUsed {
|
||||
public int bitsUsed = 0, hashesUsed = 0;
|
||||
}
|
||||
|
||||
|
@ -139,7 +139,7 @@ public class PeerGroup extends AbstractIdleService {
|
||||
// The false positive rate for bloomFilter
|
||||
private double bloomFilterFPRate = DEFAULT_BLOOM_FILTER_FP_RATE;
|
||||
// We use a constant tweak to avoid giving up privacy when we regenerate our filter with new keys
|
||||
private final long bloomFilterTweak = new Random().nextLong();
|
||||
private final long bloomFilterTweak = (long) (Math.random() * Long.MAX_VALUE);
|
||||
private int lastBloomFilterElementCount;
|
||||
|
||||
/**
|
||||
|
@ -286,7 +286,7 @@ public class Wallet implements Serializable, BlockChainListener {
|
||||
createTransientState();
|
||||
}
|
||||
|
||||
private void createTransientState() {
|
||||
private synchronized void createTransientState() {
|
||||
eventListeners = new ArrayList<WalletEventListener>();
|
||||
ignoreNextNewBlock = new HashSet<Sha256Hash>();
|
||||
txConfidenceListener = new TransactionConfidence.Listener() {
|
||||
@ -2392,7 +2392,7 @@ public class Wallet implements Serializable, BlockChainListener {
|
||||
* See the docs for {@link BloomFilter#BloomFilter(int, double)} for a brief explanation of anonymity when using bloom filters.
|
||||
*/
|
||||
public BloomFilter getBloomFilter(double falsePositiveRate) {
|
||||
return getBloomFilter(getBloomFilterElementCount(), falsePositiveRate, new Random().nextLong());
|
||||
return getBloomFilter(getBloomFilterElementCount(), falsePositiveRate, (long)(Math.random()*Long.MAX_VALUE));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -308,7 +308,7 @@ class TransactionalMultiKeyHashMap<UniqueKeyType, MultiKeyType, ValueType> {
|
||||
* Used primarily for unit testing.
|
||||
*/
|
||||
public class MemoryFullPrunedBlockStore implements FullPrunedBlockStore {
|
||||
class StoredBlockAndWasUndoableFlag {
|
||||
protected static class StoredBlockAndWasUndoableFlag {
|
||||
public StoredBlock block;
|
||||
public boolean wasUndoable;
|
||||
public StoredBlockAndWasUndoableFlag(StoredBlock block, boolean wasUndoable) { this.block = block; this.wasUndoable = wasUndoable; }
|
||||
|
@ -1,15 +1,16 @@
|
||||
package com.google.bitcoin.core;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import org.junit.Test;
|
||||
import org.spongycastle.util.encoders.Hex;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class BloomFilterTest {
|
||||
@Test
|
||||
public void insertSerializeTest() {
|
||||
BloomFilter filter = new BloomFilter(3, 0.01, 0, BloomFilter.bloomUpdate.UPDATE_ALL);
|
||||
BloomFilter filter = new BloomFilter(3, 0.01, 0, BloomFilter.BloomUpdate.UPDATE_ALL);
|
||||
|
||||
filter.insert(Hex.decode("99108ad8ed9bb6274d3980bab5a85c048f0950c8"));
|
||||
assertTrue (filter.contains(Hex.decode("99108ad8ed9bb6274d3980bab5a85c048f0950c8")));
|
||||
|
@ -290,7 +290,7 @@ public class WalletProtobufSerializerTest {
|
||||
/**
|
||||
* An extension of a wallet that stores a number.
|
||||
*/
|
||||
public class WalletExtension extends Wallet {
|
||||
public static class WalletExtension extends Wallet {
|
||||
public byte[] random_bytes;
|
||||
|
||||
public WalletExtension(NetworkParameters params) {
|
||||
@ -298,7 +298,7 @@ public class WalletProtobufSerializerTest {
|
||||
}
|
||||
}
|
||||
|
||||
public class WalletExtensionSerializerRandom extends WalletExtensionSerializer {
|
||||
public static class WalletExtensionSerializerRandom extends WalletExtensionSerializer {
|
||||
@Override
|
||||
public Collection<Protos.Extension> getExtensionsToWrite(Wallet wallet) {
|
||||
List<Protos.Extension> lst = new LinkedList<Protos.Extension>();
|
||||
|
Loading…
Reference in New Issue
Block a user