Fix various bugs and documented unintuitive/suspicious behavior in equals/hashCode/compareTo implementations.

This commit is contained in:
Amichai Rothman
2015-07-07 14:24:25 +03:00
committed by Andreas Schildbach
parent abffd1927e
commit a53b508049
11 changed files with 13 additions and 10 deletions

View File

@@ -90,6 +90,6 @@ public class DumpedPrivateKey extends VersionedChecksummedBytes {
@Override
public int hashCode() {
return Objects.hashCode(bytes, version, compressed);
return Objects.hashCode(version, compressed, Arrays.hashCode(bytes));
}
}

View File

@@ -102,14 +102,14 @@ public class GetBlocksMessage extends Message {
GetBlocksMessage other = (GetBlocksMessage) o;
return version == other.version &&
locator.size() == other.locator.size() &&
locator.containsAll(other.locator) &&
locator.containsAll(other.locator) && // ignores locator ordering
stopHash.equals(other.stopHash);
}
@Override
public int hashCode() {
int hashCode = (int) version ^ "getblocks".hashCode();
for (Sha256Hash aLocator : locator) hashCode ^= aLocator.hashCode();
for (Sha256Hash aLocator : locator) hashCode ^= aLocator.hashCode(); // ignores locator ordering
hashCode ^= stopHash.hashCode();
return hashCode;
}

View File

@@ -49,14 +49,14 @@ public class GetHeadersMessage extends GetBlocksMessage {
GetHeadersMessage other = (GetHeadersMessage) o;
return version == other.version &&
locator.size() == other.locator.size() &&
locator.containsAll(other.locator) &&
locator.containsAll(other.locator) && // ignores locator ordering
stopHash.equals(other.stopHash);
}
@Override
public int hashCode() {
int hashCode = (int) version ^ "getheaders".hashCode();
for (Sha256Hash aLocator : locator) hashCode ^= aLocator.hashCode();
for (Sha256Hash aLocator : locator) hashCode ^= aLocator.hashCode(); // ignores locator ordering
hashCode ^= stopHash.hashCode();
return hashCode;
}

View File

@@ -265,6 +265,7 @@ public class Sha256Hash implements Serializable, Comparable<Sha256Hash> {
@Override
public int compareTo(Sha256Hash o) {
return this.hashCode() - o.hashCode();
// note that in this implementation compareTo() is not consistent with equals()
return this.hashCode() - o.hashCode(); // arbitrary but consistent
}
}

View File

@@ -479,7 +479,6 @@ public class TransactionInput extends ChildMessage implements Serializable {
if (sequence != input.sequence) return false;
if (!outpoint.equals(input.outpoint)) return false;
if (!Arrays.equals(scriptBytes, input.scriptBytes)) return false;
if (scriptSig != null ? !scriptSig.equals(input.scriptSig) : input.scriptSig != null) return false;
if (parent != input.parent) return false;
return true;
@@ -490,7 +489,6 @@ public class TransactionInput extends ChildMessage implements Serializable {
int result = (int) (sequence ^ (sequence >>> 32));
result = 31 * result + outpoint.hashCode();
result = 31 * result + (scriptBytes != null ? Arrays.hashCode(scriptBytes) : 0);
result = 31 * result + (scriptSig != null ? scriptSig.hashCode() : 0);
return result;
}
}

View File

@@ -239,6 +239,6 @@ public class TransactionOutPoint extends ChildMessage implements Serializable {
@Override
public int hashCode() {
return 31 * hash.hashCode() + (int) (index ^ (index >>> 32));
return 31 * getHash().hashCode() + (int) (getIndex() ^ (getIndex() >>> 32));
}
}

View File

@@ -444,7 +444,7 @@ public class TransactionOutput extends ChildMessage implements Serializable {
int result = (int) (value ^ (value >>> 32));
result = 31 * result + Arrays.hashCode(scriptBytes);
if (parent != null)
result *= parent.getHash().hashCode() + getIndex();
result *= parent.getHash().hashCode();
return result;
}
}

View File

@@ -578,6 +578,7 @@ public class Utils {
private static class Pair implements Comparable<Pair> {
int item, count;
public Pair(int item, int count) { this.count = count; this.item = item; }
// note that in this implementation compareTo() is not consistent with equals()
@Override public int compareTo(Pair o) { return -Ints.compare(count, o.count); }
}

View File

@@ -4050,6 +4050,7 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha
}
@Override public int compareTo(TxOffsetPair o) {
// note that in this implementation compareTo() is not consistent with equals()
return Ints.compare(offset, o.offset);
}
}

View File

@@ -91,6 +91,7 @@ public class ChildNumber implements Comparable<ChildNumber> {
@Override
public int compareTo(ChildNumber other) {
// note that in this implementation compareTo() is not consistent with equals()
return Ints.compare(this.num(), other.num());
}
}

View File

@@ -90,6 +90,7 @@ public class ExponentialBackoff implements Comparable<ExponentialBackoff> {
@Override
public int compareTo(ExponentialBackoff other) {
// note that in this implementation compareTo() is not consistent with equals()
if (retryTime < other.retryTime)
return -1;
if (retryTime > other.retryTime)