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

refactor SimpleWorld to use Optional for storing accounts and extra null checks #7532

Merged
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 @@ -214,21 +214,4 @@ public boolean commit() {
return false;
}
}

/**
* Push changes into the parent account, if one exists
*
* @return true if a parent account was updated, false if not (this indicates the account should
* be inserted into the parent contact).
*/
public boolean updateParent() {
lu-pinto marked this conversation as resolved.
Show resolved Hide resolved
if (parent instanceof SimpleAccount simpleAccount) {
simpleAccount.balance = balance;
simpleAccount.nonce = nonce;
simpleAccount.storage.putAll(storage);
return true;
} else {
return false;
}
}
}
47 changes: 26 additions & 21 deletions evm/src/main/java/org/hyperledger/besu/evm/fluent/SimpleWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
public class SimpleWorld implements WorldUpdater {

/** The Parent. */
SimpleWorld parent;
private final SimpleWorld parent;

/** The Accounts. */
Map<Address, SimpleAccount> accounts = new HashMap<>();
private Map<Address, Optional<SimpleAccount>> accounts = new HashMap<>();
garyschulte marked this conversation as resolved.
Show resolved Hide resolved

/** Instantiates a new Simple world. */
public SimpleWorld() {
Expand All @@ -55,13 +55,15 @@ public WorldUpdater updater() {

@Override
public Account get(final Address address) {
Optional<SimpleAccount> account = Optional.empty();
if (accounts.containsKey(address)) {
return accounts.get(address);
account = accounts.get(address);
} else if (parent != null) {
return parent.get(address);
} else {
return null;
if (parent.get(address) instanceof SimpleAccount accountFromParent) {
account = Optional.of(accountFromParent);
}
garyschulte marked this conversation as resolved.
Show resolved Hide resolved
}
return account.orElse(null);
}

@Override
Expand All @@ -70,45 +72,46 @@ public MutableAccount createAccount(final Address address, final long nonce, fin
throw new IllegalStateException("Cannot create an account when one already exists");
}
SimpleAccount account = new SimpleAccount(address, nonce, balance);
accounts.put(address, account);
accounts.put(address, Optional.of(account));
return account;
}

@Override
public MutableAccount getAccount(final Address address) {
SimpleAccount account = accounts.get(address);
Optional<SimpleAccount> account = accounts.get(address);
if (account != null) {
return account;
return account.orElse(null);
}
Account parentAccount = parent == null ? null : parent.getAccount(address);
if (parentAccount != null) {
account =
new SimpleAccount(
parentAccount,
parentAccount.getAddress(),
parentAccount.getNonce(),
parentAccount.getBalance(),
parentAccount.getCode());
Optional.of(
new SimpleAccount(
parentAccount,
parentAccount.getAddress(),
parentAccount.getNonce(),
parentAccount.getBalance(),
parentAccount.getCode()));
accounts.put(address, account);
return account;
return account.get();
}
return null;
}

@Override
public void deleteAccount(final Address address) {
accounts.put(address, null);
accounts.put(address, Optional.empty());
}

@Override
public Collection<? extends Account> getTouchedAccounts() {
return accounts.values();
return accounts.values().stream().filter(Optional::isPresent).map(Optional::get).toList();
garyschulte marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public Collection<Address> getDeletedAccountAddresses() {
return accounts.entrySet().stream()
.filter(e -> e.getValue() == null)
.filter(e -> e.getValue().isEmpty())
.map(Map.Entry::getKey)
.toList();
}
Expand All @@ -122,8 +125,10 @@ public void revert() {
public void commit() {
accounts.forEach(
(address, account) -> {
if (!account.updateParent()) {
parent.accounts.put(address, account);
if (account.isEmpty() || !account.get().commit()) {
if (parent != null) {
parent.accounts.put(address, account);
}
}
});
}
Expand Down
Loading
Loading