Skip to content

Commit

Permalink
bonsai trie: fix account has an empty state change (hyperledger#2051)
Browse files Browse the repository at this point in the history
Signed-off-by: Karim TAAM karim.t2am@gmail.com
Co-authored-by: Danno Ferrin danno.ferrin@gmail.com
  • Loading branch information
matkt authored Mar 26, 2021
1 parent 36ef9e7 commit e13dd77
Showing 1 changed file with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ public void commit() {
}
updatedAccount.getUpdatedStorage().clear();

if (pendingStorageUpdates.isEmpty()) {
storageToUpdate.remove(updatedAddress);
}

// TODO maybe add address preimage?
}
}
Expand Down Expand Up @@ -290,17 +294,18 @@ public UInt256 getStorageValue(final Address address, final UInt256 storageKey)

@Override
public Optional<UInt256> getStorageValueBySlotHash(final Address address, final Hash slotHash) {
final Map<Hash, BonsaiValue<UInt256>> localAccountStorage =
storageToUpdate.computeIfAbsent(address, key -> new HashMap<>());
final BonsaiValue<UInt256> value = localAccountStorage.get(slotHash);
if (value != null) {
return Optional.ofNullable(value.getUpdated());
} else {
final Optional<UInt256> valueUInt =
wrappedWorldView().getStorageValueBySlotHash(address, slotHash);
valueUInt.ifPresent(v -> localAccountStorage.put(slotHash, new BonsaiValue<>(v, v)));
return valueUInt;
final Map<Hash, BonsaiValue<UInt256>> localAccountStorage = storageToUpdate.get(address);
if (localAccountStorage != null && localAccountStorage.containsKey(slotHash)) {
return Optional.ofNullable(localAccountStorage.get(slotHash).getUpdated());
}
final Optional<UInt256> valueUInt =
wrappedWorldView().getStorageValueBySlotHash(address, slotHash);
valueUInt.ifPresent(
v ->
storageToUpdate
.computeIfAbsent(address, key -> new HashMap<>())
.put(slotHash, new BonsaiValue<>(v, v)));
return valueUInt;
}

@Override
Expand All @@ -309,11 +314,10 @@ public UInt256 getPriorStorageValue(final Address address, final UInt256 storage
if (storageToClear.contains(address)) {
return UInt256.ZERO;
}
final Map<Hash, BonsaiValue<UInt256>> localAccountStorage =
storageToUpdate.computeIfAbsent(address, key -> new HashMap<>());
final Hash slotHashBytes = Hash.hash(storageKey.toBytes());
final BonsaiValue<UInt256> value = localAccountStorage.get(slotHashBytes);
if (value != null) {
final Map<Hash, BonsaiValue<UInt256>> localAccountStorage = storageToUpdate.get(address);
final Hash slotHash = Hash.hash(storageKey.toBytes());
if (localAccountStorage != null && localAccountStorage.containsKey(slotHash)) {
final BonsaiValue<UInt256> value = localAccountStorage.get(slotHash);
final UInt256 updated = value.getUpdated();
if (updated != null) {
return updated;
Expand Down

0 comments on commit e13dd77

Please sign in to comment.