Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassaldanha committed Oct 8, 2024
1 parent 9a0f710 commit 68567dc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
public class ExecutionRequestsDataCodec {

private static final int EXPECTED_REQUEST_DATA_ELEMENTS = 3;
private static final Bytes DEPOSIT_REQUEST_PREFIX = Bytes.of(DepositRequest.REQUEST_TYPE);
private static final Bytes WITHDRAWAL_REQUEST_PREFIX = Bytes.of(WithdrawalRequest.REQUEST_TYPE);
private static final Bytes CONSOLIDATION_REQUEST_PREFIX =
Bytes.of(ConsolidationRequest.REQUEST_TYPE);

private final ExecutionRequestsSchema executionRequestsSchema;

Expand Down Expand Up @@ -75,7 +79,7 @@ public ExecutionRequests decode(final List<Bytes> executionRequestData) {
}

@VisibleForTesting
List<Bytes> encode(final ExecutionRequests executionRequests) {
List<Bytes> encodeWithTypePrefix(final ExecutionRequests executionRequests) {
final SszList<DepositRequest> depositRequestsSszList =
executionRequestsSchema
.getDepositRequestsSchema()
Expand All @@ -90,18 +94,15 @@ List<Bytes> encode(final ExecutionRequests executionRequests) {
.createFromElements(executionRequests.getConsolidations());

return List.of(
Bytes.concatenate(DEPOSIT_REQUEST_PREFIX, depositRequestsSszList.sszSerialize()),
Bytes.concatenate(WITHDRAWAL_REQUEST_PREFIX, withdrawalRequestsSszList.sszSerialize()),
Bytes.concatenate(
Bytes.of(DepositRequest.REQUEST_TYPE), depositRequestsSszList.sszSerialize()),
Bytes.concatenate(
Bytes.of(WithdrawalRequest.REQUEST_TYPE), withdrawalRequestsSszList.sszSerialize()),
Bytes.concatenate(
Bytes.of(ConsolidationRequest.REQUEST_TYPE),
consolidationRequestsSszList.sszSerialize()));
CONSOLIDATION_REQUEST_PREFIX, consolidationRequestsSszList.sszSerialize()));
}

public Bytes32 hash(final ExecutionRequests executionRequests) {
final Bytes sortedEncodedRequests =
encode(executionRequests).stream()
encodeWithTypePrefix(executionRequests).stream()
.map(Hash::sha256)
.map(Bytes.class::cast)
.reduce(Bytes.EMPTY, Bytes::concatenate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ public void decodeExecutionRequestDataWithZeroElements() {
}

@Test
public void encodeExecutionRequests() {
public void encodeWithTypePrefixExecutionRequests() {
final ExecutionRequests executionRequests =
new ExecutionRequestsBuilderElectra(executionRequestsSchema)
.deposits(List.of(depositRequest1, depositRequest2))
.withdrawals(List.of(withdrawalRequest1, withdrawalRequest2))
.consolidations(List.of(consolidationRequest1))
.build();

final List<Bytes> encodedRequests = codec.encode(executionRequests);
final List<Bytes> encodedRequests = codec.encodeWithTypePrefix(executionRequests);

assertThat(encodedRequests)
.containsExactly(
Expand All @@ -142,15 +142,15 @@ public void encodeExecutionRequests() {
}

@Test
public void encodeExecutionRequestsWithOneEmptyRequestList() {
public void encodeWithTypePrefixExecutionRequestsWithOneEmptyRequestList() {
final ExecutionRequests executionRequests =
new ExecutionRequestsBuilderElectra(executionRequestsSchema)
.deposits(List.of(depositRequest1, depositRequest2))
.withdrawals(List.of())
.consolidations(List.of(consolidationRequest1))
.build();

final List<Bytes> encodedRequests = codec.encode(executionRequests);
final List<Bytes> encodedRequests = codec.encodeWithTypePrefix(executionRequests);

assertThat(encodedRequests)
.containsExactly(
Expand All @@ -161,15 +161,15 @@ public void encodeExecutionRequestsWithOneEmptyRequestList() {
}

@Test
public void encodeExecutionRequestsWithAllEmptyRequestLists() {
public void encodeWithTypePrefixExecutionRequestsWithAllEmptyRequestLists() {
final ExecutionRequests executionRequests =
new ExecutionRequestsBuilderElectra(executionRequestsSchema)
.deposits(List.of())
.withdrawals(List.of())
.consolidations(List.of())
.build();

final List<Bytes> encodedRequests = codec.encode(executionRequests);
final List<Bytes> encodedRequests = codec.encodeWithTypePrefix(executionRequests);

assertThat(encodedRequests)
.containsExactly(
Expand Down

0 comments on commit 68567dc

Please sign in to comment.