Skip to content
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 @@ -63,27 +63,40 @@ private static Hash accumulateAccessListAndComputeRoot(

for (AccountChanges accountChanges : blockAccessList.accountChanges()) {
final Address address = accountChanges.address();
final MutableAccount account = accumulator.getOrCreate(address);

final List<BalanceChange> balanceChanges = accountChanges.balanceChanges();
final List<NonceChange> nonceChanges = accountChanges.nonceChanges();
final List<CodeChange> codeChanges = accountChanges.codeChanges();
final List<SlotChanges> storageChanges = accountChanges.storageChanges();

final boolean anyChange =
!balanceChanges.isEmpty()
|| !nonceChanges.isEmpty()
|| !codeChanges.isEmpty()
|| !storageChanges.isEmpty();

if (!anyChange) {
continue;
}

final MutableAccount account = accumulator.getOrCreate(address);

if (!balanceChanges.isEmpty()) {
final BalanceChange change = balanceChanges.get(balanceChanges.size() - 1);
account.setBalance(Wei.wrap(change.postBalance()));
}

final List<NonceChange> nonceChanges = accountChanges.nonceChanges();
if (!nonceChanges.isEmpty()) {
final NonceChange change = nonceChanges.get(nonceChanges.size() - 1);
account.setNonce(change.newNonce());
}

final List<CodeChange> codeChanges = accountChanges.codeChanges();
if (!codeChanges.isEmpty()) {
final CodeChange change = codeChanges.get(codeChanges.size() - 1);
account.setCode(change.newCode());
}

for (SlotChanges slotChanges : accountChanges.storageChanges()) {
for (SlotChanges slotChanges : storageChanges) {
final List<StorageChange> changes = slotChanges.changes();
if (!changes.isEmpty()) {
final StorageChange change = changes.get(changes.size() - 1);
Expand All @@ -97,6 +110,7 @@ private static Hash accumulateAccessListAndComputeRoot(
}
}

accumulator.clearAccountsThatAreEmpty();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is probably not necessary but I think it's safer to keep it.

accumulator.commit();
return worldState.calculateRootHash(Optional.empty(), accumulator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ void multipleAccountsProduceMatchingRoots() throws Exception {
final Address addressOne = Address.fromHexString("0x00000000000000000000000000000000000000cc");
final Address addressTwo = Address.fromHexString("0x00000000000000000000000000000000000000dd");
final StorageSlotKey slotKey = new StorageSlotKey(UInt256.valueOf(1));
final Bytes newCode = Bytes.fromHexString("0x60016000");

final BlockAccessList bal =
new BlockAccessList(
Expand All @@ -159,7 +160,7 @@ void multipleAccountsProduceMatchingRoots() throws Exception {
List.of(),
List.of(),
List.of(),
List.of())));
List.of(new CodeChange(0, newCode)))));

final Hash accumulatorRoot =
computeRootFromAccumulator(
Expand All @@ -169,6 +170,7 @@ void multipleAccountsProduceMatchingRoots() throws Exception {
first.setNonce(3L);

final MutableAccount second = accumulator.getOrCreate(addressTwo);
second.setCode(newCode);
second.setStorageValue(slotKey.getSlotKey().orElseThrow(), UInt256.valueOf(99));
});

Expand Down Expand Up @@ -237,6 +239,43 @@ void mismatchedStorageUpdateProducesDifferentRoots() throws Exception {
assertThat(balRoot).isNotEqualTo(accumulatorRoot);
}

@Test
void accountPresentButNoChangesDoesNotAlterRoot() throws Exception {
final Address readOnlyAddress =
Address.fromHexString("0x0000000000000000000000000000000000000aaa");
final Address updatedAddress =
Address.fromHexString("0x0000000000000000000000000000000000000bbb");

final Wei newBalance = Wei.of(12345);
final long newNonce = 9L;

final BlockAccessList bal =
new BlockAccessList(
List.of(
new AccountChanges(
readOnlyAddress, List.of(), List.of(), List.of(), List.of(), List.of()),
new AccountChanges(
updatedAddress,
List.of(),
List.of(),
List.of(new BalanceChange(0, newBalance)),
List.of(new NonceChange(0, newNonce)),
List.of())));

final Hash expectedRoot =
computeRootFromAccumulator(
accumulator -> {
final MutableAccount account = accumulator.getOrCreate(updatedAddress);
account.setBalance(newBalance);
account.setNonce(newNonce);
});

final Hash balRoot =
computeRootFromBalAsync(bal).get(FUTURE_TIMEOUT.toSeconds(), TimeUnit.SECONDS);

assertThat(balRoot).isEqualTo(expectedRoot);
}

private Hash computeRootFromAccumulator(
final Consumer<BonsaiWorldStateUpdateAccumulator> accumulatorConsumer) {
final BonsaiWorldState worldState =
Expand Down