Skip to content

Commit

Permalink
javadoc
Browse files Browse the repository at this point in the history
Signed-off-by: Danno Ferrin <[email protected]>
  • Loading branch information
shemnon committed Sep 25, 2023
1 parent 5880090 commit b430ee9
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public long lastUpdate() {
return undoLog.get(undoLog.size() - 1).level;
}


/**
* Has the map been changed
* @return true if there are any undo entries in the log
*/
public boolean updated() {
return !undoLog.isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ public UndoNavigableMap(final NavigableMap<K, V> delegate) {
super(delegate);
}

/**
* Create an undo navigable map backed by a specific map.
* @param map The map storing the current state
* @return an undoable map
* @param <K> the key type
* @param <V> the value type
*/
public static <K, V> UndoNavigableMap<K, V> of(final NavigableMap<K, V> map) {
return new UndoNavigableMap<>(map);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
import java.util.List;
import java.util.Objects;

/**
* An undoable value that tracks the value across time.
*
* @param <T> The type of the scaler.
*/
public class UndoScalar<T> implements Undoable {
record UndoEntry<T>(T value, long level) {
UndoEntry(final T value) {
Expand All @@ -29,10 +34,22 @@ record UndoEntry<T>(T value, long level) {
T value;
final List<UndoEntry<T>> undoLog;

/**
* Create an undoable scalar with an initial value
*
* @param value the initial value
* @return the undoable scalar
* @param <T> the type of the scalar
*/
public static <T> UndoScalar<T> of(final T value) {
return new UndoScalar<>(value);
}

/**
* Create an undo scalar with an initial value
*
* @param value the initial value
*/
public UndoScalar(final T value) {
undoLog = new ArrayList<>();
this.value = value;
Expand All @@ -43,14 +60,29 @@ public long lastUpdate() {
return undoLog.isEmpty() ? 0L : undoLog.get(undoLog.size() - 1).level;
}

/**
* Has this scalar had any change since the inital value
*
* @return true if there are any changes to undo
*/
public boolean updated() {
return !undoLog.isEmpty();
}

/**
* Get the current value of the scalar.
*
* @return the current value
*/
public T get() {
return value;
}

/**
* Set a new value in the scalar.
*
* @param value new value
*/
public void set(final T value) {
if (!Objects.equals(this.value, value)) {
undoLog.add(new UndoEntry<>(this.value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,20 @@ public Map<UInt256, UInt256> getUpdatedStorage() {
public void becomeImmutable() {
immutable = true;
}

/**
* Commit this simple account entry to the parent.
*
* @return true if there was a parent account that was committed to
*/
public boolean commit() {
if (parent instanceof SimpleAccount simpleAccount) {
simpleAccount.balance = balance;
simpleAccount.nonce = nonce;
simpleAccount.storage.putAll(storage);
return true;
} else {
return false;
}
}
}
30 changes: 25 additions & 5 deletions evm/src/main/java/org/hyperledger/besu/evm/fluent/SimpleWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public Account get(final Address address) {

@Override
public MutableAccount createAccount(final Address address, final long nonce, final Wei balance) {
if (getAccount(address) != null) {
throw new IllegalStateException("Cannot create an account when one already exists");
}
SimpleAccount account = new SimpleAccount(address, nonce, balance);
accounts.put(address, account);
return account;
Expand All @@ -75,11 +78,23 @@ public MutableAccount createAccount(final Address address, final long nonce, fin
public MutableAccount getAccount(final Address address) {
if (accounts.containsKey(address)) {
return accounts.get(address);
} else if (parent != null) {
return parent.getAccount(address);
} else {
}
if (parent == null) {
return null;
}
Account parentAccount = parent.getAccount(address);
if (parentAccount == null) {
return null;
}
SimpleAccount account =
new SimpleAccount(
parentAccount,
parentAccount.getAddress(),
parentAccount.getNonce(),
parentAccount.getBalance(),
parentAccount.getCode());
accounts.put(address, account);
return account;
}

@Override
Expand Down Expand Up @@ -107,11 +122,16 @@ public void revert() {

@Override
public void commit() {
parent.accounts.putAll(accounts);
accounts.forEach(
(address, account) -> {
if (!account.commit()) {
accounts.put(address, account);
}
});
}

@Override
public Optional<WorldUpdater> parentUpdater() {
return Optional.empty();
return Optional.ofNullable(parent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ public PrecompileContractResult computePrecompile(
* Compute default precompile contract.
*
* @param input the input
* @param base base of the exponent
* @param exp the exponent
* @param mod the modulus
* @param modulusLength the length of the modulus, in bytes
* @return the precompile contract result
*/
@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,21 @@ public boolean hasCode() {
return !code.get().isEmpty();
}

/**
* Mark the account as deleted/not deleted
* @param accountDeleted delete or don't delete this account.
*/
public void setDeleted(final boolean accountDeleted) {
if (immutable) {
throw new ModificationNotAllowedException();
}
deleted.set(accountDeleted);
}

/**
* Is the account marked as deleted?
* @return is the account deleted?
*/
public Boolean getDeleted() {
return deleted.get();
}
Expand Down Expand Up @@ -368,6 +376,7 @@ public void undo(final long mark) {
updatedStorage.undo(mark);
}

/** Commit this journaled account entry to the parent, if it is not a journaled account. */
public void commit() {
if (!(account instanceof JournaledAccount)) {
if (nonce.updated()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public JournaledUpdater(final WorldUpdater world) {
undoMark = accounts.mark();
}

/**
* Get an account suitable for mutation. Defer to parent if not tracked locally.
*
* @param address the account at the address, for mutaton.
* @return the mutable account
*/
protected MutableAccount getForMutation(final Address address) {
final JournaledAccount wrappedTracker = accounts.get(address);
if (wrappedTracker != null) {
Expand All @@ -83,6 +89,9 @@ public Collection<Address> getDeletedAccountAddresses() {
return new ArrayList<>(deleted);
}

/**
* Remove all changes done by this layer. Rollback to the state prior to the updater's changes.
*/
protected void reset() {
accounts.values().forEach(a -> a.undo(undoMark));
accounts.undo(undoMark);
Expand Down

0 comments on commit b430ee9

Please sign in to comment.