Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -16,6 +16,7 @@

import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;

import org.apache.tuweni.units.bigints.UInt256;
import org.jetbrains.annotations.NotNull;
Expand All @@ -31,7 +32,7 @@
*/
public class StorageSlotKey implements Comparable<StorageSlotKey> {

private final Hash slotHash;
private final Supplier<Hash> slotHash;
private final Optional<UInt256> slotKey;

/**
Expand All @@ -41,6 +42,11 @@ public class StorageSlotKey implements Comparable<StorageSlotKey> {
* @param slotKey Optional UInt256 storage slot key.
*/
public StorageSlotKey(final Hash slotHash, final Optional<UInt256> slotKey) {
this.slotHash = () -> slotHash;
this.slotKey = slotKey;
}

public StorageSlotKey(final Supplier<Hash> slotHash, final Optional<UInt256> slotKey) {
this.slotHash = slotHash;
this.slotKey = slotKey;
}
Expand All @@ -60,7 +66,7 @@ public StorageSlotKey(final UInt256 slotKey) {
* @return the hash of the storage slot key.
*/
public Hash getSlotHash() {
return slotHash;
return slotHash.get();
}

/**
Expand Down Expand Up @@ -89,23 +95,29 @@ public boolean equals(final Object o) {
return false;
}
StorageSlotKey that = (StorageSlotKey) o;
return Objects.equals(slotHash, that.slotHash);
if (slotKey.isPresent() && that.slotKey.isPresent()) {
return slotKey.map(key -> key.equals(that.slotKey.get())).get();
}
return Objects.equals(getSlotHash(), that.getSlotHash());
}

@Override
public int hashCode() {
return Objects.hash(slotHash.hashCode());
return Objects.hash(getSlotHash().hashCode());
}

@Override
public String toString() {
return String.format(
"StorageSlotKey{slotHash=%s, slotKey=%s}",
slotHash, slotKey.map(UInt256::toString).orElse("null"));
getSlotHash(), slotKey.map(UInt256::toString).orElse("null"));
}

@Override
public int compareTo(@NotNull final StorageSlotKey other) {
return this.slotHash.compareTo(other.slotHash);
if (getSlotKey().isPresent() && other.getSlotKey().isPresent()) {
return getSlotKey().map(key -> key.compareTo(other.slotKey.get())).get();
}
return this.getSlotHash().compareTo(other.getSlotHash());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,10 @@ public void commit() {
entries.forEach(
storageUpdate -> {
final UInt256 keyUInt = storageUpdate.getKey();
final Hash slotHash = hashAndSavePreImage(keyUInt);
final StorageSlotKey slotKey =
new StorageSlotKey(slotHash, Optional.of(keyUInt));
new StorageSlotKey(
() -> hashAndSavePreImage(keyUInt),
Optional.of(keyUInt)); // no compute Hash in this case
final UInt256 value = storageUpdate.getValue();
final BonsaiValue<UInt256> pendingValue = pendingStorageUpdates.get(slotKey);
if (pendingValue == null) {
Expand Down