-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement RPC for line counting (#451)
Signed-off-by: [email protected]
- Loading branch information
delehef
authored
Dec 5, 2023
1 parent
da61945
commit 5561663
Showing
48 changed files
with
411 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
arithmetization/src/main/java/net/consensys/linea/rpc/counters/Counters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright Consensys Software Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package net.consensys.linea.rpc.counters; | ||
|
||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
/** FileTrace represents an execution trace. */ | ||
public record Counters( | ||
@JsonProperty("tracesEngineVersion") String tracesEngineVersion, | ||
@JsonProperty("blockNumber") long blockNumber, | ||
@JsonProperty("tracesCounters") Map<String, Integer> traceCountersByModule) {} |
56 changes: 56 additions & 0 deletions
56
arithmetization/src/main/java/net/consensys/linea/rpc/counters/CountersRequestParams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright Consensys Software Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package net.consensys.linea.rpc.counters; | ||
|
||
import java.security.InvalidParameterException; | ||
|
||
import net.consensys.linea.zktracer.ZkTracer; | ||
|
||
/** Holds needed parameters for sending an execution trace generation request. */ | ||
@SuppressWarnings("unused") | ||
public record CountersRequestParams(long blockNumber, String runtimeVersion) { | ||
private static final int EXPECTED_PARAMS_SIZE = 2; | ||
|
||
/** | ||
* Parses a list of params to a {@link CountersRequestParams} object. | ||
* | ||
* @param params an array of parameters. | ||
* @return a parsed {@link CountersRequestParams} object.. | ||
*/ | ||
public static CountersRequestParams createTraceParams(final Object[] params) { | ||
// validate params size | ||
if (params.length != EXPECTED_PARAMS_SIZE) { | ||
throw new InvalidParameterException( | ||
String.format("Expected %d parameters but got %d", EXPECTED_PARAMS_SIZE, params.length)); | ||
} | ||
|
||
long blockNumber = Long.parseLong(params[0].toString()); | ||
String version = params[1].toString(); | ||
|
||
if (!version.equals(getTracerRuntime())) { | ||
throw new InvalidParameterException( | ||
String.format( | ||
"INVALID_TRACES_VERSION: Runtime version is %s, requesting version %s", | ||
getTracerRuntime(), version)); | ||
} | ||
|
||
return new CountersRequestParams(blockNumber, version); | ||
} | ||
|
||
private static String getTracerRuntime() { | ||
return ZkTracer.class.getPackage().getSpecificationVersion(); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
arithmetization/src/main/java/net/consensys/linea/rpc/counters/RollupGenerateCountersV0.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright Consensys Software Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package net.consensys.linea.rpc.counters; | ||
|
||
import java.util.Map; | ||
|
||
import com.google.common.base.Stopwatch; | ||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.consensys.linea.zktracer.ZkTracer; | ||
import org.hyperledger.besu.plugin.BesuContext; | ||
import org.hyperledger.besu.plugin.services.TraceService; | ||
import org.hyperledger.besu.plugin.services.exception.PluginRpcEndpointException; | ||
import org.hyperledger.besu.plugin.services.rpc.PluginRpcRequest; | ||
|
||
/** Responsible for trace counters generation. */ | ||
@Slf4j | ||
public class RollupGenerateCountersV0 { | ||
private static final int CACHE_SIZE = 10_000; | ||
static final Cache<Long, Map<String, Integer>> cache = | ||
CacheBuilder.newBuilder().maximumSize(CACHE_SIZE).build(); | ||
|
||
private final BesuContext besuContext; | ||
private TraceService traceService; | ||
|
||
public RollupGenerateCountersV0(final BesuContext besuContext) { | ||
this.besuContext = besuContext; | ||
} | ||
|
||
public String getNamespace() { | ||
return "rollup"; | ||
} | ||
|
||
public String getName() { | ||
return "getTracesCountersByBlockNumberV0"; | ||
} | ||
|
||
/** | ||
* Handles execution traces generation logic. | ||
* | ||
* @param request holds parameters of the RPC request. | ||
* @return an execution file trace. | ||
*/ | ||
public Counters execute(final PluginRpcRequest request) { | ||
if (traceService == null) { | ||
traceService = initTraceService(); | ||
} | ||
|
||
try { | ||
final Stopwatch sw = Stopwatch.createStarted(); | ||
final CountersRequestParams params = | ||
CountersRequestParams.createTraceParams(request.getParams()); | ||
final long requestedBlockNumber = params.blockNumber(); | ||
|
||
final Counters r = | ||
new Counters( | ||
params.runtimeVersion(), | ||
requestedBlockNumber, | ||
cache | ||
.asMap() | ||
.computeIfAbsent( | ||
requestedBlockNumber, | ||
blockNumber -> { | ||
final ZkTracer tracer = new ZkTracer(); | ||
traceService.trace( | ||
blockNumber, | ||
blockNumber, | ||
worldStateBeforeTracing -> tracer.traceStartConflation(1), | ||
worldStateAfterTracing -> tracer.traceEndConflation(), | ||
tracer); | ||
|
||
return tracer.getModulesLineCount(); | ||
})); | ||
log.info("counters for {} returned in {}", requestedBlockNumber, sw); | ||
return r; | ||
} catch (Exception ex) { | ||
throw new PluginRpcEndpointException(ex.getMessage()); | ||
} | ||
} | ||
|
||
private TraceService initTraceService() { | ||
return besuContext | ||
.getService(TraceService.class) | ||
.orElseThrow( | ||
() -> | ||
new RuntimeException( | ||
"Unable to find trace service. Please ensure TraceService is registered.")); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...zation/src/main/java/net/consensys/linea/rpc/counters/RollupRpcEndpointServicePlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright Consensys Software Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package net.consensys.linea.rpc.counters; | ||
|
||
import java.util.Optional; | ||
|
||
import com.google.auto.service.AutoService; | ||
import net.consensys.linea.LineaRequiredPlugin; | ||
import net.consensys.linea.zktracer.opcode.OpCodes; | ||
import org.hyperledger.besu.plugin.BesuContext; | ||
import org.hyperledger.besu.plugin.BesuPlugin; | ||
import org.hyperledger.besu.plugin.services.RpcEndpointService; | ||
|
||
/** Plugin with RPC endpoints. */ | ||
@AutoService(BesuPlugin.class) | ||
public class RollupRpcEndpointServicePlugin extends LineaRequiredPlugin { | ||
@Override | ||
public void doRegister(final BesuContext context) { | ||
RollupGenerateCountersV0 method = new RollupGenerateCountersV0(context); | ||
|
||
Optional<RpcEndpointService> service = context.getService(RpcEndpointService.class); | ||
createAndRegister( | ||
method, | ||
service.orElseThrow( | ||
() -> | ||
new RuntimeException("Failed to obtain RpcEndpointService from the BesuContext."))); | ||
} | ||
|
||
private void createAndRegister( | ||
final RollupGenerateCountersV0 method, final RpcEndpointService rpcEndpointService) { | ||
rpcEndpointService.registerRPCEndpoint( | ||
method.getNamespace(), method.getName(), method::execute); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
OpCodes.load(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.