Skip to content

Commit

Permalink
Rename accountOverride to stateOverride to match spec (#8111)
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel-Trintinalia <gabriel.trintinalia@consensys.net>
  • Loading branch information
Gabriel-Trintinalia authored Jan 13, 2025
1 parent 85f85da commit 4eb6fb0
Show file tree
Hide file tree
Showing 17 changed files with 157 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package org.hyperledger.besu.services;

import org.hyperledger.besu.datatypes.AccountOverrideMap;
import org.hyperledger.besu.datatypes.StateOverrideMap;
import org.hyperledger.besu.datatypes.Transaction;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.BlockHeader;
Expand Down Expand Up @@ -69,16 +69,16 @@ public BlockSimulatorServiceImpl(
* @param blockNumber the block number
* @param transactions the transactions to include in the block
* @param blockOverrides the blockSimulationOverride of the block
* @param accountOverrides state overrides of the block
* @param stateOverrides state overrides of the block
* @return the block context
*/
@Override
public PluginBlockSimulationResult simulate(
final long blockNumber,
final List<? extends Transaction> transactions,
final BlockOverrides blockOverrides,
final AccountOverrideMap accountOverrides) {
return processSimulation(blockNumber, transactions, blockOverrides, accountOverrides, false);
final StateOverrideMap stateOverrides) {
return processSimulation(blockNumber, transactions, blockOverrides, stateOverrides, false);
}

/**
Expand All @@ -88,7 +88,7 @@ public PluginBlockSimulationResult simulate(
* @param blockNumber the block number
* @param transactions the transactions to include in the block
* @param blockOverrides block overrides for the block
* @param accountOverrides state overrides of the block
* @param stateOverrides state overrides of the block
* @return the PluginBlockSimulationResult
*/
@Unstable
Expand All @@ -97,21 +97,21 @@ public PluginBlockSimulationResult simulateAndPersistWorldState(
final long blockNumber,
final List<? extends Transaction> transactions,
final BlockOverrides blockOverrides,
final AccountOverrideMap accountOverrides) {
return processSimulation(blockNumber, transactions, blockOverrides, accountOverrides, true);
final StateOverrideMap stateOverrides) {
return processSimulation(blockNumber, transactions, blockOverrides, stateOverrides, true);
}

private PluginBlockSimulationResult processSimulation(
final long blockNumber,
final List<? extends Transaction> transactions,
final BlockOverrides blockOverrides,
final AccountOverrideMap accountOverrides,
final StateOverrideMap stateOverrides,
final boolean persistWorldState) {
BlockHeader header = getBlockHeader(blockNumber);
List<CallParameter> callParameters =
transactions.stream().map(CallParameter::fromTransaction).toList();
BlockStateCall blockStateCall =
new BlockStateCall(callParameters, blockOverrides, accountOverrides, true);
new BlockStateCall(callParameters, blockOverrides, stateOverrides, true);
try (final MutableWorldState ws = getWorldState(header, persistWorldState)) {
List<BlockSimulationResult> results =
blockSimulator.process(header, List.of(blockStateCall), ws);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
*/
package org.hyperledger.besu.services;

import org.hyperledger.besu.datatypes.AccountOverrideMap;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.StateOverrideMap;
import org.hyperledger.besu.datatypes.Transaction;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams;
Expand Down Expand Up @@ -54,7 +54,7 @@ public void init(final Blockchain blockchain, final TransactionSimulator transac
@Override
public Optional<TransactionSimulationResult> simulate(
final Transaction transaction,
final Optional<AccountOverrideMap> maybeAccountOverrides,
final Optional<StateOverrideMap> maybeStateOverrides,
final Optional<Hash> maybeBlockHash,
final OperationTracer operationTracer,
final boolean isAllowExceedingBalance) {
Expand Down Expand Up @@ -89,7 +89,7 @@ public Optional<TransactionSimulationResult> simulate(
return transactionSimulator
.processOnPending(
callParameter,
maybeAccountOverrides,
maybeStateOverrides,
isAllowExceedingBalance
? TransactionValidationParams.transactionSimulatorAllowExceedingBalance()
: TransactionValidationParams.transactionSimulator(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@

/** Account Override parameter class */
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonDeserialize(builder = AccountOverride.Builder.class)
public class AccountOverride {
private static final Logger LOG = LoggerFactory.getLogger(AccountOverride.class);
@JsonDeserialize(builder = StateOverride.Builder.class)
public class StateOverride {
private static final Logger LOG = LoggerFactory.getLogger(StateOverride.class);

private final Optional<Wei> balance;
private final Optional<Long> nonce;
private final Optional<String> code;
private final Optional<Map<String, String>> stateDiff;

private AccountOverride(
private StateOverride(
final Optional<Wei> balance,
final Optional<Long> nonce,
final Optional<String> code,
Expand Down Expand Up @@ -144,8 +144,8 @@ public Builder withStateDiff(final Map<String, String> stateDiff) {
*
* @return account override
*/
public AccountOverride build() {
return new AccountOverride(balance, nonce, code, stateDiff);
public StateOverride build() {
return new StateOverride(balance, nonce, code, stateDiff);
}
}

Expand All @@ -172,11 +172,11 @@ public boolean equals(final Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
final AccountOverride accountOverride = (AccountOverride) o;
return balance.equals(accountOverride.balance)
&& nonce.equals(accountOverride.nonce)
&& code.equals(accountOverride.code)
&& stateDiff.equals(accountOverride.stateDiff);
final StateOverride stateOverride = (StateOverride) o;
return balance.equals(stateOverride.balance)
&& nonce.equals(stateOverride.nonce)
&& code.equals(stateOverride.code)
&& stateDiff.equals(stateOverride.stateDiff);
}

@Override
Expand All @@ -186,7 +186,7 @@ public int hashCode() {

@Override
public String toString() {
return "AccountOverride{"
return "StateOverride{"
+ "balance="
+ balance
+ ", nonce="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

/** Map of account overrides, indexed by address */
@JsonIgnoreProperties(ignoreUnknown = true)
public class AccountOverrideMap extends HashMap<Address, AccountOverride> {
public class StateOverrideMap extends HashMap<Address, StateOverride> {

/** Default constructor */
public AccountOverrideMap() {}
public StateOverrideMap() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonCallParameterUtil.validateAndGetCallParams;

import org.hyperledger.besu.datatypes.AccountOverrideMap;
import org.hyperledger.besu.datatypes.StateOverrideMap;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcErrorConverter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
Expand Down Expand Up @@ -76,7 +76,7 @@ protected abstract Object simulate(
protected Object pendingResult(final JsonRpcRequestContext requestContext) {
final JsonCallParameter jsonCallParameter = validateAndGetCallParams(requestContext);
final var validationParams = getTransactionValidationParams(jsonCallParameter);
final var maybeStateOverrides = getAddressAccountOverrideMap(requestContext);
final var maybeStateOverrides = getAddressStateOverrideMap(requestContext);
final var pendingBlockHeader = transactionSimulator.simulatePendingBlockHeader();
final TransactionSimulationFunction simulationFunction =
(cp, op) ->
Expand All @@ -103,7 +103,7 @@ private Object resultByBlockHeader(
final JsonCallParameter jsonCallParameter,
final BlockHeader blockHeader) {
final var validationParams = getTransactionValidationParams(jsonCallParameter);
final var maybeStateOverrides = getAddressAccountOverrideMap(requestContext);
final var maybeStateOverrides = getAddressStateOverrideMap(requestContext);
final TransactionSimulationFunction simulationFunction =
(cp, op) ->
transactionSimulator.process(
Expand Down Expand Up @@ -214,10 +214,10 @@ protected static TransactionValidationParams getTransactionValidationParams(
}

@VisibleForTesting
protected Optional<AccountOverrideMap> getAddressAccountOverrideMap(
protected Optional<StateOverrideMap> getAddressStateOverrideMap(
final JsonRpcRequestContext request) {
try {
return request.getOptionalParameter(2, AccountOverrideMap.class);
return request.getOptionalParameter(2, StateOverrideMap.class);
} catch (JsonRpcParameter.JsonRpcParameterException e) {
throw new InvalidJsonRpcRequestException(
"Invalid account overrides parameter (index 2)", RpcErrorType.INVALID_CALL_PARAMS, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType.BLOCK_NOT_FOUND;
import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType.INTERNAL_ERROR;

import org.hyperledger.besu.datatypes.AccountOverrideMap;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.StateOverrideMap;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcErrorConverter;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
Expand Down Expand Up @@ -86,7 +86,7 @@ protected Object resultByBlockHash(final JsonRpcRequestContext request, final Ha
protected Object resultByBlockHeader(
final JsonRpcRequestContext request, final BlockHeader header) {
JsonCallParameter callParams = JsonCallParameterUtil.validateAndGetCallParams(request);
Optional<AccountOverrideMap> maybeStateOverrides = getAddressAccountOverrideMap(request);
Optional<StateOverrideMap> maybeStateOverrides = getAddressStateOverrideMap(request);
// TODO implement for block overrides

return transactionSimulator
Expand Down Expand Up @@ -117,10 +117,10 @@ protected Object resultByBlockHeader(
}

@VisibleForTesting
protected Optional<AccountOverrideMap> getAddressAccountOverrideMap(
protected Optional<StateOverrideMap> getAddressStateOverrideMap(
final JsonRpcRequestContext request) {
try {
return request.getOptionalParameter(2, AccountOverrideMap.class);
return request.getOptionalParameter(2, StateOverrideMap.class);
} catch (JsonRpcParameterException e) {
throw new InvalidJsonRpcRequestException(
"Invalid account overrides parameter (index 2)", RpcErrorType.INVALID_CALL_PARAMS, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import org.hyperledger.besu.datatypes.AccountOverride;
import org.hyperledger.besu.datatypes.AccountOverrideMap;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.StateOverride;
import org.hyperledger.besu.datatypes.StateOverrideMap;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.datatypes.parameters.UnsignedLongParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest;
Expand Down Expand Up @@ -96,26 +96,26 @@ public void shouldReturnCorrectMethodName() {
}

@Test
public void noAccountOverrides() {
public void noStateOverrides() {
final JsonRpcRequestContext request = ethCallRequest(callParameter(), "latest");
Optional<AccountOverrideMap> overrideMap = method.getAddressAccountOverrideMap(request);
Optional<StateOverrideMap> overrideMap = method.getAddressStateOverrideMap(request);
assertThat(overrideMap.isPresent()).isFalse();
}

@Test
public void someAccountOverrides() {
AccountOverrideMap expectedOverrides = new AccountOverrideMap();
AccountOverride override =
new AccountOverride.Builder().withNonce(new UnsignedLongParameter("0x9e")).build();
public void someStateOverrides() {
StateOverrideMap expectedOverrides = new StateOverrideMap();
StateOverride override =
new StateOverride.Builder().withNonce(new UnsignedLongParameter("0x9e")).build();
final Address address = Address.fromHexString("0xd9c9cd5f6779558b6e0ed4e6acf6b1947e7fa1f3");
expectedOverrides.put(address, override);

final JsonRpcRequestContext request =
ethCallRequestWithStateOverrides(callParameter(), "latest", expectedOverrides);

Optional<AccountOverrideMap> maybeOverrideMap = method.getAddressAccountOverrideMap(request);
Optional<StateOverrideMap> maybeOverrideMap = method.getAddressStateOverrideMap(request);
assertThat(maybeOverrideMap.isPresent()).isTrue();
AccountOverrideMap overrideMap = maybeOverrideMap.get();
StateOverrideMap overrideMap = maybeOverrideMap.get();
assertThat(overrideMap.keySet()).hasSize(1);
assertThat(overrideMap.values()).hasSize(1);

Expand Down Expand Up @@ -498,7 +498,7 @@ private JsonRpcRequestContext ethCallRequest(
private JsonRpcRequestContext ethCallRequestWithStateOverrides(
final CallParameter callParameter,
final String blockNumberInHex,
final AccountOverrideMap overrides) {
final StateOverrideMap overrides) {
return new JsonRpcRequestContext(
new JsonRpcRequest(
"2.0", "eth_call", new Object[] {callParameter, blockNumberInHex, overrides}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.hyperledger.besu.datatypes.AccountOverride;
import org.hyperledger.besu.datatypes.AccountOverrideMap;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.StateOverride;
import org.hyperledger.besu.datatypes.StateOverrideMap;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.datatypes.parameters.UnsignedLongParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest;
Expand Down Expand Up @@ -104,19 +104,19 @@ public void shouldReturnCorrectMethodName() {
}

@Test
public void noAccountOverrides() {
public void noStateOverrides() {
final Wei gasPrice = Wei.of(1000);
final JsonRpcRequestContext request =
ethEstimateGasRequest(defaultLegacyTransactionCallParameter(gasPrice), "latest");
Optional<AccountOverrideMap> overrideMap = method.getAddressAccountOverrideMap(request);
Optional<StateOverrideMap> overrideMap = method.getAddressStateOverrideMap(request);
assertThat(overrideMap.isPresent()).isFalse();
}

@Test
public void someAccountOverrides() {
AccountOverrideMap expectedOverrides = new AccountOverrideMap();
AccountOverride override =
new AccountOverride.Builder().withNonce(new UnsignedLongParameter("0x9e")).build();
public void someStateOverrides() {
StateOverrideMap expectedOverrides = new StateOverrideMap();
StateOverride override =
new StateOverride.Builder().withNonce(new UnsignedLongParameter("0x9e")).build();
final Address address = Address.fromHexString("0xd9c9cd5f6779558b6e0ed4e6acf6b1947e7fa1f3");
expectedOverrides.put(address, override);

Expand All @@ -125,9 +125,9 @@ public void someAccountOverrides() {
ethEstimateGasRequestWithStateOverrides(
defaultLegacyTransactionCallParameter(gasPrice), "latest", expectedOverrides);

Optional<AccountOverrideMap> maybeOverrideMap = method.getAddressAccountOverrideMap(request);
Optional<StateOverrideMap> maybeOverrideMap = method.getAddressStateOverrideMap(request);
assertThat(maybeOverrideMap.isPresent()).isTrue();
AccountOverrideMap overrideMap = maybeOverrideMap.get();
StateOverrideMap overrideMap = maybeOverrideMap.get();
assertThat(overrideMap.keySet()).hasSize(1);
assertThat(overrideMap.values()).hasSize(1);

Expand Down Expand Up @@ -630,7 +630,7 @@ private JsonRpcRequestContext ethEstimateGasRequest(
private JsonRpcRequestContext ethEstimateGasRequestWithStateOverrides(
final CallParameter callParameter,
final String blockParam,
final AccountOverrideMap overrides) {
final StateOverrideMap overrides) {
return new JsonRpcRequestContext(
new JsonRpcRequest(
"2.0", "eth_estimateGas", new Object[] {callParameter, blockParam, overrides}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
*/
package org.hyperledger.besu.ethereum.transaction;

import org.hyperledger.besu.datatypes.AccountOverride;
import org.hyperledger.besu.datatypes.AccountOverrideMap;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.StateOverride;
import org.hyperledger.besu.datatypes.StateOverrideMap;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockBody;
Expand Down Expand Up @@ -141,7 +141,7 @@ private BlockSimulationResult processSingleBlockStateCall(

// Apply block header overrides and state overrides
BlockHeader blockHeader = applyBlockHeaderOverrides(header, newProtocolSpec, blockOverrides);
blockStateCall.getAccountOverrides().ifPresent(overrides -> applyStateOverrides(overrides, ws));
blockStateCall.getStateOverrideMap().ifPresent(overrides -> applyStateOverrides(overrides, ws));

// Override the mining beneficiary calculator if a fee recipient is specified, otherwise use the
// default
Expand Down Expand Up @@ -236,15 +236,15 @@ protected BlockSimulationResult finalizeBlock(
/**
* Applies state overrides to the world state.
*
* @param accountOverrideMap The AccountOverrideMap containing the state overrides.
* @param stateOverrideMap The StateOverrideMap containing the state overrides.
* @param ws The MutableWorldState to apply the overrides to.
*/
@VisibleForTesting
protected void applyStateOverrides(
final AccountOverrideMap accountOverrideMap, final MutableWorldState ws) {
final StateOverrideMap stateOverrideMap, final MutableWorldState ws) {
var updater = ws.updater();
for (Address accountToOverride : accountOverrideMap.keySet()) {
final AccountOverride override = accountOverrideMap.get(accountToOverride);
for (Address accountToOverride : stateOverrideMap.keySet()) {
final StateOverride override = stateOverrideMap.get(accountToOverride);
MutableAccount account = updater.getOrCreate(accountToOverride);
override.getNonce().ifPresent(account::setNonce);
if (override.getBalance().isPresent()) {
Expand Down
Loading

0 comments on commit 4eb6fb0

Please sign in to comment.