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

Featue/fix trielog account unchanged #1

Merged
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 @@ -37,6 +37,7 @@

public class VerkleAccount extends DiffBasedAccount {
private Hash storageRoot; // TODO REMOVE AS USELESS
private int hashCode;

public VerkleAccount(
final DiffBasedWorldView context,
Expand Down Expand Up @@ -180,4 +181,33 @@ public static void assertCloseEnoughForDiffing(
public boolean isStorageEmpty() {
return true; // TODO need to find a way to manage that with verkle
}

@Override
public boolean equals(final Object other) {
if (this == other) {
return true;
} else if (!(other instanceof VerkleAccount)) {
return false;
}
VerkleAccount otherVerkleAccount = (VerkleAccount) other;
return Objects.equals(this.address, otherVerkleAccount.address)
&& this.nonce == otherVerkleAccount.nonce
&& Objects.equals(this.balance, otherVerkleAccount.balance)
&& Objects.equals(this.codeHash, otherVerkleAccount.codeHash);
}

@Override
public int hashCode() {
if (!immutable) {
return computeHashCode();
}
if (hashCode == 0) {
hashCode = computeHashCode();
}
return hashCode;
}

private int computeHashCode() {
return Objects.hash(address, nonce, balance, codeHash);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.hyperledger.besu.plugin.services.storage.SegmentedKeyValueStorageTransaction;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -119,17 +120,19 @@ protected Hash internalCalculateRootHash(

final Map<Address, HasherContext> preloadedHashers = new ConcurrentHashMap<>();

worldStateUpdater.getAccountsToUpdate().entrySet().parallelStream()
final Set<Address> addressesToPersist = getAddressesToPersist(worldStateUpdater);
addressesToPersist.parallelStream()
.forEach(
accountUpdate -> {
final Address accountKey = accountUpdate.getKey();
// generate account triekeys
accountKey -> {
final DiffBasedValue<VerkleAccount> accountUpdate =
worldStateUpdater.getAccountsToUpdate().get(accountKey);
// generate triekeys for account
final List<Bytes32> accountKeyIds = new ArrayList<>();
if (!accountUpdate.getValue().isUnchanged()) {
if (accountUpdate != null && !accountUpdate.isUnchanged()) {
accountKeyIds.add(trieKeyPreloader.generateAccountKeyId());
}

// generate storage triekeys
// generate triekeys for storage
final List<Bytes32> storageKeyIds = new ArrayList<>();
final StorageConsumingMap<StorageSlotKey, DiffBasedValue<UInt256>>
storageAccountUpdate = worldStateUpdater.getStorageToUpdate().get(accountKey);
Expand All @@ -142,7 +145,7 @@ protected Hash internalCalculateRootHash(
}
}

// generate code triekeys
// generate triekeys for code
final List<Bytes32> codeKeyIds = new ArrayList<>();
final DiffBasedValue<Bytes> codeUpdate =
worldStateUpdater.getCodeToUpdate().get(accountKey);
Expand All @@ -167,7 +170,7 @@ protected Hash internalCalculateRootHash(
accountKey, accountKeyIds, storageKeyIds, codeKeyIds));
});

for (final Address accountKey : worldStateUpdater.getAccountsToUpdate().keySet()) {
for (final Address accountKey : addressesToPersist) {
updateState(
accountKey,
stateTrie,
Expand Down Expand Up @@ -205,7 +208,7 @@ private void generateAccountValues(
final VerkleEntryFactory verkleEntryFactory,
final Optional<VerkleWorldStateKeyValueStorage.Updater> maybeStateUpdater,
final DiffBasedValue<VerkleAccount> accountUpdate) {
if (accountUpdate.isUnchanged()) {
if (accountUpdate == null || accountUpdate.isUnchanged()) {
return;
}
if (accountUpdate.getUpdated() == null) {
Expand Down Expand Up @@ -355,6 +358,16 @@ private void updateState(
});
}

public Set<Address> getAddressesToPersist(
final DiffBasedWorldStateUpdateAccumulator<?> accumulator) {
Set<Address> mergedAddresses =
new HashSet<>(accumulator.getAccountsToUpdate().keySet()); // accountsToUpdate
mergedAddresses.addAll(accumulator.getCodeToUpdate().keySet()); // codeToUpdate
mergedAddresses.addAll(accumulator.getStorageToClear()); // storageToClear
mergedAddresses.addAll(accumulator.getStorageToUpdate().keySet()); // storageToUpdate
return mergedAddresses;
}

@Override
public MutableWorldState freeze() {
this.worldStateConfig.setFrozen(true);
Expand Down
Loading