Skip to content

Commit

Permalink
fix: add validations to block number params of trace generation and l…
Browse files Browse the repository at this point in the history
…ine counting JSON-RPC endpoints (#1191)


Signed-off-by: Tsvetan Dimitrov <[email protected]>
  • Loading branch information
powerslider authored Sep 16, 2024
1 parent 91fbcff commit b6dacf6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@

/** Holds needed parameters for sending an execution trace generation request. */
public record CountersRequestParams(long blockNumber, String expectedTracesEngineVersion) {
public void validateTracerVersion() {
public void validate() {
if (!expectedTracesEngineVersion.equals(getTracerRuntime())) {
throw new InvalidParameterException(
String.format(
"INVALID_TRACES_VERSION: Runtime version is %s, requesting version %s",
getTracerRuntime(), expectedTracesEngineVersion));
}

if (blockNumber < 0) {
throw new InvalidParameterException(
"INVALID_BLOCK_NUMBER: blockNumber: %d cannot be a negative number"
.formatted(blockNumber));
}
}

private static String getTracerRuntime() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Counters execute(final PluginRpcRequest request) {
final CountersRequestParams params =
CONVERTER.fromJson(CONVERTER.toJson(rawParams[0]), CountersRequestParams.class);

params.validateTracerVersion();
params.validate();

final long requestedBlockNumber = params.blockNumber();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public TraceFile execute(final PluginRpcRequest request) {
TraceRequestParams params =
CONVERTER.fromJson(CONVERTER.toJson(rawParams[0]), TraceRequestParams.class);

params.validateTracerVersion();
params.validate();

final long fromBlock = params.startBlockNumber();
final long toBlock = params.endBlockNumber();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,29 @@
public record TraceRequestParams(
long startBlockNumber, long endBlockNumber, String expectedTracesEngineVersion) {

public void validateTracerVersion() {
public void validate() {
if (!expectedTracesEngineVersion.equals(getTracerRuntime())) {
throw new InvalidParameterException(
String.format(
"INVALID_TRACES_VERSION: Runtime version is %s, requesting version %s",
getTracerRuntime(), expectedTracesEngineVersion));
"INVALID_TRACES_VERSION: Runtime version is %s, requesting version %s"
.formatted(getTracerRuntime(), expectedTracesEngineVersion));
}

if (startBlockNumber < 0) {
throw new InvalidParameterException(
"INVALID_START_BLOCK_NUMBER: startBlockNumber: %d cannot be a negative number"
.formatted(startBlockNumber));
}

if (endBlockNumber < 0) {
throw new InvalidParameterException(
"INVALID_END_BLOCK_NUMBER: endBlockNumber: %d cannot be a negative number"
.formatted(endBlockNumber));
}

if (endBlockNumber - startBlockNumber < 0) {
throw new InvalidParameterException(
"INVALID_END_BLOCK_NUMBER: endBlockNumber: %d cannot be less than startBlockNumber: %d"
.formatted(endBlockNumber, startBlockNumber));
}
}

Expand Down
2 changes: 1 addition & 1 deletion linea-constraints

0 comments on commit b6dacf6

Please sign in to comment.