Skip to content

Commit

Permalink
Fix ExecutionRequestsDataCodec handling of missing request data
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassaldanha committed Dec 3, 2024
1 parent c0263ca commit 31397fa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,18 @@ public ExecutionRequests decode(final List<Bytes> executionRequests) {
if (request.isEmpty()) {
throw new IllegalArgumentException("Execution request data must not be empty");
}

final byte requestType = request.get(0);
if (requestType <= previousRequestType) {
throw new IllegalArgumentException(
"Execution requests are not in strictly ascending order");
}

final Bytes requestData = request.slice(1);
if (requestData.isEmpty()) {
throw new IllegalArgumentException("Empty data for request type " + requestType);
}

switch (requestType) {
case DepositRequest.REQUEST_TYPE ->
executionRequestsBuilder.deposits(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ public void decodeExecutionRequestsDataWithOneRequestMissing() {

@Test
public void decodeExecutionRequestsDataWithInvalidRequestType() {
final Bytes invalidRequestType = Bytes.of(9);
final Bytes invalidTypeEncodedList = Bytes.concatenate(invalidRequestType, Bytes.random(10));
final List<Bytes> invalidExecutionRequestsData =
List.of(depositRequestListEncoded, withdrawalRequestsListEncoded, Bytes.of(9));
List.of(depositRequestListEncoded, withdrawalRequestsListEncoded, invalidTypeEncodedList);

assertThatThrownBy(() -> codec.decode(invalidExecutionRequestsData))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Invalid execution request type: 9");
.hasMessage("Invalid execution request type: " + invalidRequestType.toInt());
}

@Test
Expand Down Expand Up @@ -126,6 +128,29 @@ public void decodeExecutionRequestDataWithRepeatedRequestsOfSameType() {
.hasMessage("Execution requests are not in strictly ascending order");
}

@Test
public void decodeExecutionRequestsDataWithEmptyRequestData() {
// Element containing only the type by but no data
final List<Bytes> invalidEmptyRequestsData = List.of(DepositRequest.REQUEST_TYPE_PREFIX);

assertThatThrownBy(() -> codec.decode(invalidEmptyRequestsData))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Empty data for request type 0");
}

@Test
public void decodeExecutionRequestsDataWithOneInvalidEmptyRequestData() {
final List<Bytes> invalidExecutionRequestsData =
List.of(
depositRequestListEncoded,
withdrawalRequestsListEncoded,
ConsolidationRequest.REQUEST_TYPE_PREFIX);

assertThatThrownBy(() -> codec.decode(invalidExecutionRequestsData))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Empty data for request type 2");
}

@Test
public void encodeExecutionRequests() {
final ExecutionRequests executionRequests =
Expand Down

0 comments on commit 31397fa

Please sign in to comment.