Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bonsai accounts don't have equals() or hashCode() implementations #7845

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@ public String toString() {
+ '}';
}

@Override
public boolean equals(final Object o) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move as much of this up into DiffBasedAccount as possible so that other implementations inherit this (like Verkle). perhaps implement equals in DiffBasedAccount and delegate to an abstract method that implementations are forced to provide.

if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final BonsaiAccount that = (BonsaiAccount) o;
return super.equals(o) && storageRoot.equals(that.storageRoot);
}

@Override
public int hashCode() {
return Objects.hash(nonce, balance, storageRoot, codeHash);
}

/**
* Throws an exception if the two accounts represent different stored states
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
Expand Down Expand Up @@ -226,4 +227,17 @@ public String toString() {
+ codeHash
+ '}';
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final DiffBasedAccount that = (DiffBasedAccount) o;
return nonce == that.nonce && balance.equals(that.balance) && codeHash.equals(that.codeHash);
}

@Override
public int hashCode() {
return Objects.hash(nonce, balance, codeHash);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
public class BonsaiAccountTest {

@Mock BonsaiWorldState bonsaiWorldState;
@Mock BonsaiWorldState bonsaiWorldStateAlternate;

@Test
void shouldCopyTrackedBonsaiAccountCorrectly() {
Expand Down Expand Up @@ -71,4 +72,171 @@ void shouldCopyBonsaiAccountCorrectly() {
assertThat(new BonsaiAccount(account, bonsaiWorldState, true))
.isEqualToComparingFieldByField(account);
}

@Test
void shouldBeEqualIfAttributesMatch() {
// Basic equality test
final BonsaiAccount account1 =
new BonsaiAccount(
bonsaiWorldState,
Address.ZERO,
Hash.hash(Address.ZERO),
0,
Wei.ONE,
Hash.EMPTY_TRIE_HASH,
Hash.EMPTY,
true);
final BonsaiAccount account2 =
new BonsaiAccount(
bonsaiWorldState,
Address.ZERO,
Hash.hash(Address.ZERO),
0,
Wei.ONE,
Hash.EMPTY_TRIE_HASH,
Hash.EMPTY,
true);
assertThat(account1.equals(account2)).isTrue();

// Another equality test
final BonsaiAccount account3 =
new BonsaiAccount(
bonsaiWorldState,
Address.ZERO,
Hash.hash(Address.ZERO),
99,
Wei.of(UInt256.fromHexString("0x123")),
Hash.hash(Bytes.fromHexString("0xabcdef")),
Hash.hash(Bytes.fromHexString("0x9876")),
true);
final BonsaiAccount account4 =
new BonsaiAccount(
bonsaiWorldState,
Address.ZERO,
Hash.hash(Address.ZERO),
99,
Wei.of(UInt256.fromHexString("0x123")),
Hash.hash(Bytes.fromHexString("0xabcdef")),
Hash.hash(Bytes.fromHexString("0x9876")),
true);
assertThat(account3.equals(account4)).isTrue();

// Context and mutability should not affect equality
final BonsaiAccount account5 =
new BonsaiAccount(
bonsaiWorldState,
Address.ZERO,
Hash.hash(Address.ZERO),
0,
Wei.ONE,
Hash.EMPTY_TRIE_HASH,
Hash.EMPTY,
true);
final BonsaiAccount account6 =
new BonsaiAccount(
bonsaiWorldStateAlternate,
Address.ZERO,
Hash.hash(Address.ZERO),
0,
Wei.ONE,
Hash.EMPTY_TRIE_HASH,
Hash.EMPTY,
false);
assertThat(account5.equals(account6)).isTrue();
}

@Test
void shouldBeUnEqualIfAnyAttributesDiffer() {
// Nonce
final BonsaiAccount account1 =
new BonsaiAccount(
bonsaiWorldState,
Address.ZERO,
Hash.hash(Address.ZERO),
99,
Wei.ONE,
Hash.EMPTY_TRIE_HASH,
Hash.EMPTY,
true);
final BonsaiAccount account2 =
new BonsaiAccount(
bonsaiWorldState,
Address.ZERO,
Hash.hash(Address.ZERO),
100,
Wei.ONE,
Hash.EMPTY_TRIE_HASH,
Hash.EMPTY,
true);
assertThat(account1.equals(account2)).isFalse();

// Balance
final BonsaiAccount account3 =
new BonsaiAccount(
bonsaiWorldState,
Address.ZERO,
Hash.hash(Address.ZERO),
99,
Wei.of(UInt256.fromHexString("0x123")),
Hash.hash(Bytes.fromHexString("0xabcdef")),
Hash.hash(Bytes.fromHexString("0x9876")),
true);
final BonsaiAccount account4 =
new BonsaiAccount(
bonsaiWorldState,
Address.ZERO,
Hash.hash(Address.ZERO),
99,
Wei.of(UInt256.fromHexString("0x456")),
Hash.hash(Bytes.fromHexString("0xabcdef")),
Hash.hash(Bytes.fromHexString("0x9876")),
true);
assertThat(account3.equals(account4)).isFalse();

// Storage root
final BonsaiAccount account5 =
new BonsaiAccount(
bonsaiWorldState,
Address.ZERO,
Hash.hash(Address.ZERO),
99,
Wei.of(UInt256.fromHexString("0x123")),
Hash.hash(Bytes.fromHexString("0xabcdef")),
Hash.hash(Bytes.fromHexString("0x9876")),
true);
final BonsaiAccount account6 =
new BonsaiAccount(
bonsaiWorldState,
Address.ZERO,
Hash.hash(Address.ZERO),
99,
Wei.of(UInt256.fromHexString("0x456")),
Hash.hash(Bytes.fromHexString("0xfedcba")),
Hash.hash(Bytes.fromHexString("0x9876")),
true);
assertThat(account5.equals(account6)).isFalse();

// Code hash
final BonsaiAccount account7 =
new BonsaiAccount(
bonsaiWorldState,
Address.ZERO,
Hash.hash(Address.ZERO),
99,
Wei.of(UInt256.fromHexString("0x123")),
Hash.hash(Bytes.fromHexString("0xabcdef")),
Hash.hash(Bytes.fromHexString("0x6789")),
true);
final BonsaiAccount account8 =
new BonsaiAccount(
bonsaiWorldState,
Address.ZERO,
Hash.hash(Address.ZERO),
99,
Wei.of(UInt256.fromHexString("0x456")),
Hash.hash(Bytes.fromHexString("0xabcdef")),
Hash.hash(Bytes.fromHexString("0x9876")),
true);
assertThat(account7.equals(account8)).isFalse();
}
}
Loading