From 1a5f74a148b13799c364d5cf08ca82c6dc509579 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Thu, 21 Feb 2013 15:42:14 +0100 Subject: [PATCH] Add/fix hashCodes/equals() implementations in some cases. Clears more FindBugs warnings. --- .../com/google/bitcoin/core/BloomFilter.java | 6 +++++ .../google/bitcoin/core/DumpedPrivateKey.java | 24 ++++++++++++++++++- .../bitcoin/core/FullPrunedBlockChain.java | 4 ++-- .../bitcoin/core/NetworkParameters.java | 9 ++++++- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/BloomFilter.java b/core/src/main/java/com/google/bitcoin/core/BloomFilter.java index 4fb5f1ba..706f2071 100644 --- a/core/src/main/java/com/google/bitcoin/core/BloomFilter.java +++ b/core/src/main/java/com/google/bitcoin/core/BloomFilter.java @@ -16,6 +16,7 @@ package com.google.bitcoin.core; +import com.google.common.base.Objects; import com.google.common.base.Preconditions; import java.io.IOException; @@ -243,4 +244,9 @@ public class BloomFilter extends Message { return true; return false; } + + @Override + public int hashCode() { + return Objects.hashCode(hashFuncs, nTweak, Arrays.hashCode(data)); + } } diff --git a/core/src/main/java/com/google/bitcoin/core/DumpedPrivateKey.java b/core/src/main/java/com/google/bitcoin/core/DumpedPrivateKey.java index 6ed16738..ed5ea76d 100644 --- a/core/src/main/java/com/google/bitcoin/core/DumpedPrivateKey.java +++ b/core/src/main/java/com/google/bitcoin/core/DumpedPrivateKey.java @@ -17,9 +17,10 @@ package com.google.bitcoin.core; import com.google.common.base.Preconditions; -import org.spongycastle.util.Arrays; import java.math.BigInteger; +import java.util.Arrays; +import java.util.Objects; /** * Parses and generates private keys in the form used by the Bitcoin "dumpprivkey" command. This is the private key @@ -76,4 +77,25 @@ public class DumpedPrivateKey extends VersionedChecksummedBytes { public ECKey getKey() { return new ECKey(new BigInteger(1, bytes), null, compressed); } + + @Override + public boolean equals(Object other) { + // This odd construction is to avoid anti-symmetry of equality: where a.equals(b) != b.equals(a). + boolean result = false; + if (other instanceof VersionedChecksummedBytes) { + result = Arrays.equals(bytes, ((VersionedChecksummedBytes)other).bytes); + } + if (other instanceof DumpedPrivateKey) { + DumpedPrivateKey o = (DumpedPrivateKey) other; + result = Arrays.equals(bytes, o.bytes) && + version == o.version && + compressed == o.compressed; + } + return result; + } + + @Override + public int hashCode() { + return Objects.hash(bytes, version, compressed); + } } diff --git a/core/src/main/java/com/google/bitcoin/core/FullPrunedBlockChain.java b/core/src/main/java/com/google/bitcoin/core/FullPrunedBlockChain.java index f014a8ea..b12c359f 100644 --- a/core/src/main/java/com/google/bitcoin/core/FullPrunedBlockChain.java +++ b/core/src/main/java/com/google/bitcoin/core/FullPrunedBlockChain.java @@ -107,7 +107,7 @@ public class FullPrunedBlockChain extends AbstractBlockChain { LinkedList txOutsSpent = new LinkedList(); LinkedList txOutsCreated = new LinkedList(); long sigOps = 0; - final boolean enforceBIP16 = block.getTimeSeconds() >= params.BIP16_ENFORCE_TIME; + final boolean enforceBIP16 = block.getTimeSeconds() >= NetworkParameters.BIP16_ENFORCE_TIME; if (scriptVerificationExecutor.isShutdown()) scriptVerificationExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); @@ -261,7 +261,7 @@ public class FullPrunedBlockChain extends AbstractBlockChain { LinkedList txOutsSpent = new LinkedList(); LinkedList txOutsCreated = new LinkedList(); long sigOps = 0; - final boolean enforcePayToScriptHash = newBlock.getHeader().getTimeSeconds() >= params.BIP16_ENFORCE_TIME; + final boolean enforcePayToScriptHash = newBlock.getHeader().getTimeSeconds() >= NetworkParameters.BIP16_ENFORCE_TIME; if (!params.isCheckpoint(newBlock.getHeight())) { for(Transaction tx : transactions) { Sha256Hash hash = tx.getHash(); diff --git a/core/src/main/java/com/google/bitcoin/core/NetworkParameters.java b/core/src/main/java/com/google/bitcoin/core/NetworkParameters.java index e1d9c7ca..17e1d129 100644 --- a/core/src/main/java/com/google/bitcoin/core/NetworkParameters.java +++ b/core/src/main/java/com/google/bitcoin/core/NetworkParameters.java @@ -16,6 +16,7 @@ package com.google.bitcoin.core; +import com.google.common.base.Objects; import org.spongycastle.util.encoders.Hex; import java.io.ByteArrayOutputStream; @@ -166,7 +167,7 @@ public class NetworkParameters implements Serializable { * network rules in a soft-forking manner, that is, blocks that don't follow the rules are accepted but not * mined upon and thus will be quickly re-orged out as long as the majority are enforcing the rule. */ - public final int BIP16_ENFORCE_TIME = 1333238400; + public static final int BIP16_ENFORCE_TIME = 1333238400; /** * The maximum money to be generated @@ -306,12 +307,18 @@ public class NetworkParameters implements Serializable { return id; } + @Override public boolean equals(Object other) { if (!(other instanceof NetworkParameters)) return false; NetworkParameters o = (NetworkParameters) other; return o.getId().equals(getId()); } + @Override + public int hashCode() { + return Objects.hashCode(getId()); + } + /** Returns the network parameters for the given string ID or NULL if not recognized. */ public static NetworkParameters fromID(String id) { if (id.equals(ID_PRODNET)) {