Skip to content

Commit

Permalink
Wrapped WorldUpdater into EVMWorldupdater (hyperledger#7434)
Browse files Browse the repository at this point in the history
* wrapped WorldUpdater into `EVMWorldupdater` to remove the authority code injection from the implementation of the actual world updaters

Signed-off-by: Daniel Lehrner <[email protected]>

* add CHANGELOG entry

Signed-off-by: Daniel Lehrner <[email protected]>

---------

Signed-off-by: Daniel Lehrner <[email protected]>
Co-authored-by: Justin Florentine <[email protected]>
Co-authored-by: Sally MacFarlane <[email protected]>
Signed-off-by: gconnect <[email protected]>
  • Loading branch information
3 people authored and gconnect committed Aug 26, 2024
1 parent ccbfcf9 commit 0487799
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 156 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
### Additions and Improvements
- Expose set finalized/safe block in plugin api BlockchainService. These method can be used by plugins to set finalized/safe block for a PoA network (such as QBFT, IBFT and Clique).[#7382](https://github.com/hyperledger/besu/pull/7382)
- In process RPC service [#7395](https://github.com/hyperledger/besu/pull/7395)
- Wrap WorldUpdater into EVMWorldupdater [#7434](https://github.com/hyperledger/besu/pull/7434)

### Bug fixes
- Correct entrypoint in Docker evmtool [#7430](https://github.com/hyperledger/besu/pull/7430)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.account.AccountState;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.worldstate.AuthorizedCodeService;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
import org.hyperledger.besu.evm.worldstate.EVMWorldUpdater;

import java.math.BigInteger;
import java.util.Optional;
Expand All @@ -38,10 +37,7 @@ public AuthorityProcessor(final Optional<BigInteger> maybeChainId) {
}

public void addContractToAuthority(
final WorldUpdater worldState,
final AuthorizedCodeService authorizedCodeService,
final Transaction transaction) {

final EVMWorldUpdater evmWorldUpdater, final Transaction transaction) {
transaction
.getAuthorizationList()
.get()
Expand All @@ -60,7 +56,7 @@ public void addContractToAuthority(
}

final Optional<MutableAccount> maybeAccount =
Optional.ofNullable(worldState.getAccount(authorityAddress));
Optional.ofNullable(evmWorldUpdater.getAccount(authorityAddress));
final long accountNonce =
maybeAccount.map(AccountState::getNonce).orElse(0L);

Expand All @@ -69,20 +65,24 @@ public void addContractToAuthority(
return;
}

if (authorizedCodeService.hasAuthorizedCode(authorityAddress)) {
if (evmWorldUpdater
.authorizedCodeService()
.hasAuthorizedCode(authorityAddress)) {
return;
}

Optional<Account> codeAccount =
Optional.ofNullable(worldState.get(payload.address()));
Optional.ofNullable(evmWorldUpdater.get(payload.address()));
final Bytes code;
if (codeAccount.isPresent()) {
code = codeAccount.get().getCode();
} else {
code = Bytes.EMPTY;
}

authorizedCodeService.addAuthorizedCode(authorityAddress, code);
evmWorldUpdater
.authorizedCodeService()
.addAuthorizedCode(authorityAddress, code);
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.hyperledger.besu.evm.processor.AbstractMessageProcessor;
import org.hyperledger.besu.evm.tracing.OperationTracer;
import org.hyperledger.besu.evm.worldstate.AuthorizedCodeService;
import org.hyperledger.besu.evm.worldstate.EVMWorldUpdater;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;

import java.util.Deque;
Expand Down Expand Up @@ -286,9 +286,8 @@ public TransactionProcessingResult processTransaction(
final TransactionValidationParams transactionValidationParams,
final PrivateMetadataUpdater privateMetadataUpdater,
final Wei blobGasPrice) {
final EVMWorldUpdater evmWorldUpdater = new EVMWorldUpdater(worldState);
try {
final AuthorizedCodeService authorizedCodeService = new AuthorizedCodeService();
worldState.setAuthorizedCodeService(authorizedCodeService);
final var transactionValidator = transactionValidatorFactory.get();
LOG.trace("Starting execution of {}", transaction);
ValidationResult<TransactionInvalidReason> validationResult =
Expand All @@ -306,7 +305,7 @@ public TransactionProcessingResult processTransaction(
}

final Address senderAddress = transaction.getSender();
final MutableAccount sender = worldState.getOrCreateSenderAccount(senderAddress);
final MutableAccount sender = evmWorldUpdater.getOrCreateSenderAccount(senderAddress);

validationResult =
transactionValidator.validateForSender(transaction, sender, transactionValidationParams);
Expand All @@ -315,7 +314,7 @@ public TransactionProcessingResult processTransaction(
return TransactionProcessingResult.invalid(validationResult);
}

operationTracer.tracePrepareTransaction(worldState, transaction);
operationTracer.tracePrepareTransaction(evmWorldUpdater, transaction);

final Set<Address> addressList = new BytesTrieSet<>(Address.SIZE);

Expand All @@ -324,10 +323,8 @@ public TransactionProcessingResult processTransaction(
throw new RuntimeException("Authority processor is required for 7702 transactions");
}

maybeAuthorityProcessor
.get()
.addContractToAuthority(worldState, authorizedCodeService, transaction);
addressList.addAll(authorizedCodeService.getAuthorities());
maybeAuthorityProcessor.get().addContractToAuthority(evmWorldUpdater, transaction);
addressList.addAll(evmWorldUpdater.authorizedCodeService().getAuthorities());
}

final long previousNonce = sender.incrementNonce();
Expand Down Expand Up @@ -384,8 +381,7 @@ public TransactionProcessingResult processTransaction(
accessListGas,
setCodeGas);

final WorldUpdater worldUpdater = worldState.updater();
worldUpdater.setAuthorizedCodeService(authorizedCodeService);
final WorldUpdater worldUpdater = evmWorldUpdater.updater();
final ImmutableMap.Builder<String, Object> contextVariablesBuilder =
ImmutableMap.<String, Object>builder()
.put(KEY_IS_PERSISTING_PRIVATE_STATE, isPersistingPrivateState)
Expand Down Expand Up @@ -437,12 +433,11 @@ public TransactionProcessingResult processTransaction(
.contract(contractAddress)
.inputData(initCodeBytes.slice(code.getSize()))
.code(code)
.authorizedCodeService(authorizedCodeService)
.build();
} else {
@SuppressWarnings("OptionalGetWithoutIsPresent") // isContractCall tests isPresent
final Address to = transaction.getTo().get();
final Optional<Account> maybeContract = Optional.ofNullable(worldState.get(to));
final Optional<Account> maybeContract = Optional.ofNullable(evmWorldUpdater.get(to));
initialFrame =
commonMessageFrameBuilder
.type(MessageFrame.Type.MESSAGE_CALL)
Expand All @@ -453,7 +448,6 @@ public TransactionProcessingResult processTransaction(
maybeContract
.map(c -> messageCallProcessor.getCodeFromEVM(c.getCodeHash(), c.getCode()))
.orElse(CodeV0.EMPTY_CODE))
.authorizedCodeService(authorizedCodeService)
.build();
}
Deque<MessageFrame> messageFrameStack = initialFrame.getMessageFrameStack();
Expand Down Expand Up @@ -532,9 +526,9 @@ public TransactionProcessingResult processTransaction(

operationTracer.traceBeforeRewardTransaction(worldUpdater, transaction, coinbaseWeiDelta);

final var coinbase = worldState.getOrCreate(miningBeneficiary);
final var coinbase = evmWorldUpdater.getOrCreate(miningBeneficiary);
coinbase.incrementBalance(coinbaseWeiDelta);
authorizedCodeService.resetAuthorities();
evmWorldUpdater.authorizedCodeService().resetAuthorities();

operationTracer.traceEndTransaction(
worldUpdater,
Expand All @@ -546,10 +540,10 @@ public TransactionProcessingResult processTransaction(
initialFrame.getSelfDestructs(),
0L);

initialFrame.getSelfDestructs().forEach(worldState::deleteAccount);
initialFrame.getSelfDestructs().forEach(evmWorldUpdater::deleteAccount);

if (clearEmptyAccounts) {
worldState.clearAccountsThatAreEmpty();
evmWorldUpdater.clearAccountsThatAreEmpty();
}

if (initialFrame.getState() == MessageFrame.State.COMPLETED_SUCCESS) {
Expand Down Expand Up @@ -577,7 +571,7 @@ public TransactionProcessingResult processTransaction(
}
} catch (final MerkleTrieException re) {
operationTracer.traceEndTransaction(
worldState.updater(),
evmWorldUpdater.updater(),
transaction,
false,
Bytes.EMPTY,
Expand All @@ -590,7 +584,7 @@ public TransactionProcessingResult processTransaction(
throw re;
} catch (final RuntimeException re) {
operationTracer.traceEndTransaction(
worldState.updater(),
evmWorldUpdater.updater(),
transaction,
false,
Bytes.EMPTY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.worldstate.AuthorizedCodeService;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;

import java.util.Collection;
Expand All @@ -31,39 +30,35 @@ public class PrivateMutableWorldStateUpdater implements WorldUpdater {

protected final WorldUpdater publicWorldUpdater;
protected final WorldUpdater privateWorldUpdater;
private AuthorizedCodeService authorizedCodeService;

public PrivateMutableWorldStateUpdater(
final WorldUpdater publicWorldUpdater, final WorldUpdater privateWorldUpdater) {
this.publicWorldUpdater = publicWorldUpdater;
this.privateWorldUpdater = privateWorldUpdater;
this.authorizedCodeService = new AuthorizedCodeService();
}

@Override
public MutableAccount createAccount(final Address address, final long nonce, final Wei balance) {
return authorizedCodeService.processMutableAccount(
this, privateWorldUpdater.createAccount(address, nonce, balance), address);
return privateWorldUpdater.createAccount(address, nonce, balance);
}

@Override
public MutableAccount createAccount(final Address address) {
return authorizedCodeService.processMutableAccount(
this, privateWorldUpdater.createAccount(address), address);
return privateWorldUpdater.createAccount(address);
}

@Override
public MutableAccount getAccount(final Address address) {
final MutableAccount privateAccount = privateWorldUpdater.getAccount(address);
if (privateAccount != null && !privateAccount.isEmpty()) {
return authorizedCodeService.processMutableAccount(this, privateAccount, address);
return privateAccount;
}
final MutableAccount publicAccount = publicWorldUpdater.getAccount(address);
if (publicAccount != null && !publicAccount.isEmpty()) {
publicAccount.becomeImmutable();
return authorizedCodeService.processMutableAccount(this, publicAccount, address);
return publicAccount;
}
return authorizedCodeService.processMutableAccount(this, privateAccount, address);
return privateAccount;
}

@Override
Expand Down Expand Up @@ -109,9 +104,4 @@ public WorldUpdater updater() {
public Optional<WorldUpdater> parentUpdater() {
return privateWorldUpdater.parentUpdater();
}

@Override
public void setAuthorizedCodeService(final AuthorizedCodeService authorizedCodeService) {
this.authorizedCodeService = authorizedCodeService;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ public MutableAccount createAccount(final Address address, final long nonce, fin
accountsToUpdate.put(address, diffBasedValue);
} else if (diffBasedValue.getUpdated() != null) {
if (diffBasedValue.getUpdated().isEmpty()) {
return authorizedCodeService.processMutableAccount(
this, track(new UpdateTrackingAccount<>(diffBasedValue.getUpdated())), address);
return track(new UpdateTrackingAccount<>(diffBasedValue.getUpdated()));
} else {
throw new IllegalStateException("Cannot create an account when one already exists");
}
Expand All @@ -268,8 +267,7 @@ public MutableAccount createAccount(final Address address, final long nonce, fin
Hash.EMPTY,
true);
diffBasedValue.setUpdated(newAccount);
return authorizedCodeService.processMutableAccount(
this, track(new UpdateTrackingAccount<>(newAccount)), address);
return track(new UpdateTrackingAccount<>(newAccount));
}

@Override
Expand Down
23 changes: 7 additions & 16 deletions evm/src/main/java/org/hyperledger/besu/evm/fluent/SimpleWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.worldstate.AuthorizedCodeService;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;

import java.util.Collection;
Expand All @@ -35,8 +34,6 @@ public class SimpleWorld implements WorldUpdater {
/** The Accounts. */
Map<Address, SimpleAccount> accounts = new HashMap<>();

private AuthorizedCodeService authorizedCodeService;

/** Instantiates a new Simple world. */
public SimpleWorld() {
this(null);
Expand All @@ -49,7 +46,6 @@ public SimpleWorld() {
*/
public SimpleWorld(final SimpleWorld parent) {
this.parent = parent;
this.authorizedCodeService = new AuthorizedCodeService();
}

@Override
Expand All @@ -60,11 +56,11 @@ public WorldUpdater updater() {
@Override
public Account get(final Address address) {
if (accounts.containsKey(address)) {
return authorizedCodeService.processAccount(this, accounts.get(address), address);
return accounts.get(address);
} else if (parent != null) {
return authorizedCodeService.processAccount(this, parent.get(address), address);
return parent.get(address);
} else {
return authorizedCodeService.processAccount(this, null, address);
return null;
}
}

Expand All @@ -75,14 +71,14 @@ public MutableAccount createAccount(final Address address, final long nonce, fin
}
SimpleAccount account = new SimpleAccount(address, nonce, balance);
accounts.put(address, account);
return authorizedCodeService.processMutableAccount(this, account, address);
return account;
}

@Override
public MutableAccount getAccount(final Address address) {
SimpleAccount account = accounts.get(address);
if (account != null) {
return authorizedCodeService.processMutableAccount(this, account, address);
return account;
}
Account parentAccount = parent == null ? null : parent.getAccount(address);
if (parentAccount != null) {
Expand All @@ -94,9 +90,9 @@ public MutableAccount getAccount(final Address address) {
parentAccount.getBalance(),
parentAccount.getCode());
accounts.put(address, account);
return authorizedCodeService.processMutableAccount(this, account, address);
return account;
}
return authorizedCodeService.processMutableAccount(this, null, address);
return null;
}

@Override
Expand Down Expand Up @@ -136,9 +132,4 @@ public void commit() {
public Optional<WorldUpdater> parentUpdater() {
return Optional.ofNullable(parent);
}

@Override
public void setAuthorizedCodeService(final AuthorizedCodeService authorizedCodeService) {
this.authorizedCodeService = authorizedCodeService;
}
}
Loading

0 comments on commit 0487799

Please sign in to comment.