Skip to content

Commit

Permalink
revert wrapping of world updater, as its updater() method creates a…
Browse files Browse the repository at this point in the history
… type of nesting that is not compatible with wrapping it. Instead a service is injected in the world updater to inject the code into the authorized accounts

Signed-off-by: Daniel Lehrner <[email protected]>
  • Loading branch information
daniellehrner committed Jul 16, 2024
1 parent 680a834 commit 239387e
Show file tree
Hide file tree
Showing 64 changed files with 248 additions and 334 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,42 @@ public static SetCodeAuthorization createSetCodeAuthorizationEntry(
chainId, address, nonces, SIGNATURE_ALGORITHM.get().createSignature(r, s, v));
}

/**
* Return the list of nonces
*
* @return all the nonces
*/
@Override
@JsonProperty("nonce")
public List<Long> nonces() {
return nonces;
}

/**
* Return the recovery id.
*
* @return byte
*/
@JsonProperty("v")
public byte v() {
return signature.getRecId();
}

/**
* Return the r value of the signature.
*
* @return r value
*/
@JsonProperty("r")
public BigInteger r() {
return signature.getR();
}

/**
* Return the s value of the signature.
*
* @return s value
*/
@JsonProperty("s")
public BigInteger s() {
return signature.getS();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ default Optional<? extends Quantity> getMaxFeePerBlobGas() {
Optional<List<SetCodeAuthorization>> getAuthorizationList();

/**
* Returns the size of the set code transaction payload list.
* Returns the size of the authorization list.
*
* @return the size of the set code transaction payload list
* @return the size of the authorization list
*/
int setCodeTransactionPayloadSize();
int authorizationListSize();
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected Object resultByBlockHash(

Optional<Account> account =
transactionTrace.get().getTraceFrames().stream()
.map(traceFrame -> traceFrame.getWorldUpdaterService().get(address))
.map(traceFrame -> traceFrame.getWorldUpdater().get(address))
.filter(Objects::nonNull)
.filter(a -> a.getAddress().equals(address))
.findFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Stream<Trace> generateStateDiff(final TransactionTrace transactionTrace)
// This corresponds to the world state after the TX executed
// It is two deep because of the way we addressed Spurious Dragon.
final WorldUpdater transactionUpdater =
traceFrames.get(0).getWorldUpdaterService().parentUpdater().get().parentUpdater().get();
traceFrames.get(0).getWorldUpdater().parentUpdater().get().parentUpdater().get();
// This corresponds to the world state prior to the TX execution,
// Either the initial block state or the state of the prior TX
final WorldUpdater previousUpdater = transactionUpdater.parentUpdater().get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.debug.TraceFrame;
import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.frame.WorldUpdaterService;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;

import java.util.Collections;
import java.util.Optional;
Expand Down Expand Up @@ -70,7 +70,7 @@ class DebugAccountAtTest {
@Mock private TransactionTrace transactionTrace;
@Mock private TraceFrame traceFrame;
@Mock private Transaction transaction;
@Mock private WorldUpdaterService worldUpdaterService;
@Mock private WorldUpdater worldUpdater;
@Mock private MutableWorldState worldState;

@Mock private Account account;
Expand Down Expand Up @@ -242,8 +242,8 @@ void shouldBeSuccessfulWhenTransactionsAndAccountArePresent() {
private void setupMockAccount() {
Mockito.when(transactionTrace.getTraceFrames())
.thenReturn(Collections.singletonList(traceFrame));
Mockito.when(traceFrame.getWorldUpdaterService()).thenReturn(worldUpdaterService);
Mockito.when(worldUpdaterService.get(any())).thenReturn(account);
Mockito.when(traceFrame.getWorldUpdater()).thenReturn(worldUpdater);
Mockito.when(worldUpdater.get(any())).thenReturn(account);
Mockito.when(account.getAddress()).thenReturn(Address.ZERO);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.frame.WorldUpdaterService;
import org.hyperledger.besu.evm.gascalculator.SpuriousDragonGasCalculator;
import org.hyperledger.besu.evm.operation.BlockHashOperation.BlockHashLookup;
import org.hyperledger.besu.evm.precompile.PrecompiledContract;
Expand Down Expand Up @@ -105,8 +104,8 @@ private PrivateTransactionProcessor mockPrivateTxProcessor() {
TransactionProcessingResult.successful(
null, 0, 0, Bytes.fromHexString(DEFAULT_OUTPUT), null);
when(mockPrivateTransactionProcessor.processTransaction(
new WorldUpdaterService(nullable(WorldUpdater.class)),
new WorldUpdaterService(nullable(WorldUpdater.class)),
nullable(WorldUpdater.class),
nullable(WorldUpdater.class),
nullable(ProcessableBlockHeader.class),
nullable(Hash.class),
nullable(PrivateTransaction.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public MessageFrame.Builder createMessageFrameBuilder() {
return MessageFrame.builder()
.parentMessageFrame(messageFrame)
.type(MessageFrame.Type.MESSAGE_CALL)
.worldUpdaterService(messageFrame.getWorldUpdaterService())
.worldUpdater(messageFrame.getWorldUpdater())
.initialGas(messageFrame.getRemainingGas())
.address(messageFrame.getContractAddress())
.contract(messageFrame.getRecipientAddress())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ public Optional<List<SetCodeAuthorization>> getAuthorizationList() {
}

@Override
public int setCodeTransactionPayloadSize() {
public int authorizationListSize() {
return maybeAuthorizationList.map(List::size).orElse(0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.evm.Code;
import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
import org.hyperledger.besu.evm.frame.WorldUpdaterService;
import org.hyperledger.besu.evm.internal.MemoryEntry;
import org.hyperledger.besu.evm.internal.StorageEntry;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;

import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -48,7 +48,7 @@ public class TraceFrame {
private final Optional<Bytes[]> memory;
private final Optional<Map<UInt256, UInt256>> storage;

private final WorldUpdaterService worldUpdaterService;
private final WorldUpdater worldUpdater;
private final Optional<Bytes> revertReason;
private final Optional<Map<Address, Wei>> maybeRefunds;
private final Optional<Code> maybeCode;
Expand Down Expand Up @@ -77,7 +77,7 @@ public TraceFrame(
final Optional<Bytes[]> stack,
final Optional<Bytes[]> memory,
final Optional<Map<UInt256, UInt256>> storage,
final WorldUpdaterService worldUpdaterService,
final WorldUpdater worldUpdater,
final Optional<Bytes> revertReason,
final Optional<Map<Address, Wei>> maybeRefunds,
final Optional<Code> maybeCode,
Expand All @@ -101,7 +101,7 @@ public TraceFrame(
this.stack = stack;
this.memory = memory;
this.storage = storage;
this.worldUpdaterService = worldUpdaterService;
this.worldUpdater = worldUpdater;
this.revertReason = revertReason;
this.maybeRefunds = maybeRefunds;
this.maybeCode = maybeCode;
Expand Down Expand Up @@ -177,8 +177,8 @@ public Optional<Map<UInt256, UInt256>> getStorage() {
return storage;
}

public WorldUpdaterService getWorldUpdaterService() {
return worldUpdaterService;
public WorldUpdater getWorldUpdater() {
return worldUpdater;
}

public Optional<Bytes> getRevertReason() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.hyperledger.besu.evm.code.CodeV0;
import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.frame.WorldUpdaterService;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.hyperledger.besu.evm.processor.AbstractMessageProcessor;
import org.hyperledger.besu.evm.tracing.OperationTracer;
Expand Down Expand Up @@ -337,8 +336,7 @@ public TransactionProcessingResult processTransaction(
transaction.getPayload(), transaction.isContractCreation());
final long accessListGas =
gasCalculator.accessListGasCost(accessListEntries.size(), accessListStorageCount);
final long setCodeGas =
gasCalculator.setCodeListGasCost(transaction.setCodeTransactionPayloadSize());
final long setCodeGas = gasCalculator.setCodeListGasCost(transaction.authorizationListSize());
final long gasAvailable =
transaction.getGasLimit() - intrinsicGas - accessListGas - setCodeGas;
LOG.trace(
Expand All @@ -359,15 +357,12 @@ public TransactionProcessingResult processTransaction(
contextVariablesBuilder.put(KEY_PRIVATE_METADATA_UPDATER, privateMetadataUpdater);
}

final WorldUpdaterService worldUpdaterService =
new WorldUpdaterService(worldUpdater.updater());

operationTracer.traceStartTransaction(worldUpdater, transaction);

final MessageFrame.Builder commonMessageFrameBuilder =
MessageFrame.builder()
.maxStackSize(maxStackSize)
.worldUpdaterService(worldUpdaterService)
.worldUpdater(worldUpdater.updater())
.initialGas(gasAvailable)
.originator(senderAddress)
.gasPrice(transactionGasPrice)
Expand All @@ -387,8 +382,8 @@ public TransactionProcessingResult processTransaction(
commonMessageFrameBuilder.versionedHashes(
Optional.of(transaction.getVersionedHashes().get().stream().toList()));
} else if (transaction.getAuthorizationList().isPresent()) {
setCodeTransactionProcessor.addContractToAuthority(worldUpdaterService, transaction);
addressList.addAll(worldUpdaterService.getAuthorities());
setCodeTransactionProcessor.addContractToAuthority(worldUpdater, transaction);
addressList.addAll(worldUpdater.getAuthorizedAccountService().getAuthorities());
} else {
commonMessageFrameBuilder.versionedHashes(Optional.empty());
}
Expand All @@ -411,7 +406,7 @@ public TransactionProcessingResult processTransaction(
} else {
@SuppressWarnings("OptionalGetWithoutIsPresent") // isContractCall tests isPresent
final Address to = transaction.getTo().get();
final Optional<Account> maybeContract = Optional.ofNullable(worldUpdaterService.get(to));
final Optional<Account> maybeContract = Optional.ofNullable(worldState.get(to));
initialFrame =
commonMessageFrameBuilder
.type(MessageFrame.Type.MESSAGE_CALL)
Expand Down Expand Up @@ -477,7 +472,7 @@ public TransactionProcessingResult processTransaction(
final long gasUsedByTransaction = transaction.getGasLimit() - initialFrame.getRemainingGas();

// update the coinbase
final var coinbase = worldUpdaterService.getOrCreate(miningBeneficiary);
final var coinbase = worldState.getOrCreate(miningBeneficiary);
final long usedGas = transaction.getGasLimit() - refundedGas;
final CoinbaseFeePriceCalculator coinbaseCalculator;
if (blockHeader.getBaseFee().isPresent()) {
Expand All @@ -500,7 +495,7 @@ public TransactionProcessingResult processTransaction(
coinbaseCalculator.price(usedGas, transactionGasPrice, blockHeader.getBaseFee());

coinbase.incrementBalance(coinbaseWeiDelta);
worldUpdaterService.resetAuthorities();
worldUpdater.getAuthorizedAccountService().resetAuthorities();

operationTracer.traceEndTransaction(
worldUpdater,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private ValidationResult<TransactionInvalidReason> validateCostAndFee(
gasCalculator.transactionIntrinsicGasCost(
transaction.getPayload(), transaction.isContractCreation())
+ (transaction.getAccessList().map(gasCalculator::accessListGasCost).orElse(0L))
+ gasCalculator.setCodeListGasCost(transaction.setCodeTransactionPayloadSize());
+ gasCalculator.setCodeListGasCost(transaction.authorizationListSize());
if (Long.compareUnsigned(intrinsicGasCost, transaction.getGasLimit()) > 0) {
return ValidationResult.invalid(
TransactionInvalidReason.INTRINSIC_GAS_EXCEEDS_GAS_LIMIT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
import org.hyperledger.besu.evm.account.AccountState;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.frame.WorldUpdaterService;
import org.hyperledger.besu.evm.worldstate.AuthorizedAccountService;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;

import java.math.BigInteger;
import java.util.Optional;
Expand All @@ -48,7 +49,7 @@ public SetCodeTransactionProcessor(final BigInteger chainId) {
}

public void addContractToAuthority(
final WorldUpdaterService worldUpdaterService, final Transaction transaction) {
final WorldUpdater worldUpdater, final Transaction transaction) {

transaction
.getAuthorizationList()
Expand All @@ -67,7 +68,7 @@ public void addContractToAuthority(
}

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

Expand All @@ -76,12 +77,13 @@ public void addContractToAuthority(
return;
}

if (worldUpdaterService.hasAuthorization(authorityAddress)) {
final AuthorizedAccountService service =
worldUpdater.getAuthorizedAccountService();
if (service.hasAuthorization(authorityAddress)) {
return;
}

worldUpdaterService.addAuthorizedAccount(
authorityAddress, payload.address());
service.addAuthorizedAccount(authorityAddress, payload.address());
}));
}

Expand Down
Loading

0 comments on commit 239387e

Please sign in to comment.