Skip to content

Commit

Permalink
refactor SimpleWorld to use Optional for storing accounts and extra n…
Browse files Browse the repository at this point in the history
…ull checks (#7532)

Signed-off-by: Luis Pinto <luis.pinto@consensys.net>
  • Loading branch information
lu-pinto authored Sep 2, 2024
1 parent e1dd400 commit 4c4f2f3
Show file tree
Hide file tree
Showing 3 changed files with 357 additions and 38 deletions.
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() {
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<>();

/** 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);
}
}
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();
}

@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

0 comments on commit 4c4f2f3

Please sign in to comment.