Skip to content

JKey has no equals() or hashCode() methods implemented #7137

Closed
@artemananiev

Description

This issue was found during investigation of #7087.

OnDiskAccount has the following implementation of hashCode():

    @Override
    public int hashCode() {
        int result = Objects.hash(
                flags,
                key,
                memo,
                alias,
                hbarAllowances,
                fungibleAllowances,
                nftOperatorApprovals,
                firstStorageKeyNonZeroBytes);
        result = 31 * result + Arrays.hashCode(firstStorageKey);
        result = 31 * result + Arrays.hashCode(ints);
        result = 31 * result + Arrays.hashCode(longs);
        return result;
    }

One of the field above is key, which is of type JKey. This class has no hashCode() or equals() defined. Its hash code is different, even if two JKey objects are equal. Note that OnDiskAccount.equals() doesn't compare its key using its equals() method, but uses a different method instead:

                && equalUpToDecodability(this.key, that.key)

However, it doesn't make any sense. If this and that are actually equal, this equals() method may not be called as hashCode() may return different values. It makes OnDiskAccount unusable in Java collections, e.g. as a key in a map, or in a hash set, as well as all other similar classes with JKeys in their fields.

Metadata

Assignees

Labels

ModularizationIssues or PRs related to modularization

Type

No type

Projects

  • Status

    ✅ Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions