mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-07-31 20:11:23 +00:00
Fix various bugs and documented unintuitive/suspicious behavior in equals/hashCode/compareTo implementations.
This commit is contained in:
committed by
Andreas Schildbach
parent
abffd1927e
commit
a53b508049
@@ -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));
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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));
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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); }
|
||||
}
|
||||
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
@@ -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());
|
||||
}
|
||||
}
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user