Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ public class TrieLogLayerConverter {

private static final Logger LOG = LoggerFactory.getLogger(TrieLogLayerConverter.class);

final WorldStateStorage worldStateStorage;
final WorldStateStorage headWorldStateStorage;

public TrieLogLayerConverter(final WorldStateStorage worldStateStorage) {
this.worldStateStorage = worldStateStorage;
public TrieLogLayerConverter(final WorldStateStorage headWorldStateStorage) {
this.headWorldStateStorage = headWorldStateStorage;
}

public TrieLogLayer decodeTrieLog(final RLPInput input) {
return decodeTrieLog(input, headWorldStateStorage);
}

public TrieLogLayer decodeTrieLog(final RLPInput input, WorldStateStorage wss) {

TrieLogLayer trieLogLayer = new TrieLogLayer();

Expand Down Expand Up @@ -80,12 +84,12 @@ public TrieLogLayer decodeTrieLog(final RLPInput input) {
if (input.nextIsNull()) {
input.skipNext();
maybeAccountIndex =
worldStateStorage
wss
.getFlatLeaf(WRAP_ACCOUNT.apply(accountKey.accountHash()))
.map(FlattenedLeaf::leafIndex);
} else {
input.enterList();
final PriorAccount priorAccount = preparePriorTrieLogAccount(accountKey, input);
final PriorAccount priorAccount = preparePriorTrieLogAccount(accountKey, input, wss);
maybeAccountIndex = priorAccount.index;
final ZkAccount newAccountValue =
TrieLogLayer.nullOrValue(
Expand Down Expand Up @@ -122,7 +126,7 @@ public TrieLogLayer decodeTrieLog(final RLPInput input) {
maybeAccountIndex
.flatMap(
index -> {
return new StorageTrieRepositoryWrapper(index, worldStateStorage, null)
return new StorageTrieRepositoryWrapper(index, wss, null)
.getFlatLeaf(storageSlotKey.slotHash())
.map(FlattenedLeaf::leafValue)
.map(UInt256::fromBytes);
Expand Down Expand Up @@ -180,12 +184,12 @@ public TrieLogLayer decodeTrieLog(final RLPInput input) {

record PriorAccount(ZkAccount account, Bytes32 evmStorageRoot, Optional<Long> index) {}

public PriorAccount preparePriorTrieLogAccount(final AccountKey accountKey, final RLPInput in) {
public PriorAccount preparePriorTrieLogAccount(final AccountKey accountKey, final RLPInput in, final WorldStateStorage wss) {

final ZkAccount oldAccountValue;

final Optional<FlattenedLeaf> flatLeaf =
worldStateStorage.getFlatLeaf(WRAP_ACCOUNT.apply(accountKey.accountHash()));
wss.getFlatLeaf(WRAP_ACCOUNT.apply(accountKey.accountHash()));


if (in.nextIsNull() && flatLeaf.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public CompletableFuture<String> simulateTransaction(

// Create params with returnTrieLog=true
final SimulateV1Request.SimulateV1Params params =
new SimulateV1Request.SimulateV1Params(List.of(blockStateCall), true, true);
new SimulateV1Request.SimulateV1Params(List.of(blockStateCall), true, true, true);

// Specify the parent block number as the block parameter for simulation context
final String blockParameter = "0x" + Long.toHexString(parentBlockNumber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,18 @@ public static class SimulateV1Params {
@JsonProperty("returnTrieLog")
private final boolean returnTrieLog;

@JsonProperty("traceBlockImport")
private final boolean traceBlockImport;

public SimulateV1Params(
final List<BlockStateCall> blockStateCalls,
final boolean validation,
final boolean returnTrieLog) {
final boolean returnTrieLog,
final boolean traceBlockImport) {
this.blockStateCalls = blockStateCalls;
this.validation = validation;
this.returnTrieLog = returnTrieLog;
this.traceBlockImport = traceBlockImport;
}

public List<BlockStateCall> getBlockStateCalls() {
Expand All @@ -62,6 +67,10 @@ public boolean isValidation() {
public boolean isReturnTrieLog() {
return returnTrieLog;
}

public boolean isTraceBlockImport() {
return traceBlockImport;
}
}

public static class BlockStateCall {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
}

// do not have it in cache
if (worldStateArchive.getCachedWorldState(parentBlockNumber).isEmpty()) {
final var optWorldState = worldStateArchive.getCachedWorldState(parentBlockNumber);
if (optWorldState.isEmpty()) {
return new ShomeiJsonRpcErrorResponse(
requestContext.getRequest().getId(),
RpcErrorType.INVALID_PARAMS,
Expand All @@ -90,7 +91,8 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {

// Decode the trielog
final TrieLogLayer trieLogLayer =
worldStateArchive.getTrieLogLayerConverter().decodeTrieLog(RLP.input(trieLogBytes));
worldStateArchive.getTrieLogLayerConverter().decodeTrieLog(
RLP.input(trieLogBytes), optWorldState.get().getZkEvmWorldStateStorage());
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why we need to pass the worldstatestorage and not use the one we are passing in the constructor here https://github.com/Consensys/shomei/pull/165/changes#diff-4f07a7333cca9e86a5e0c552d5e21a36b4af014b7eec54b7071a127d70b79375R51

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because the trielog being generated needs to be against its parent, not necessarily the head worldstate. Like if we are trying to generate a trielog for a forced transaction that is n blocks back, we cannot use the head worldstate to validate prior values.


// Apply the virtual trielog and generate the trace
// This generates a trace without persisting the state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void shouldReturnValidResponseWhenSimulationSucceeds() throws Exception {

// Mock the trielog layer converter
when(worldStateArchive.getTrieLogLayerConverter()).thenReturn(trieLogLayerConverter);
when(trieLogLayerConverter.decodeTrieLog(any())).thenReturn(trieLogLayer);
when(trieLogLayerConverter.decodeTrieLog(any(), any())).thenReturn(trieLogLayer);

// Mock virtual trace generation
final List<List<Trace>> mockTraces = List.of(List.of());
Expand Down
Loading