Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented decoding and hash rules for execution requests #8668

Merged
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 @@ -20,9 +20,12 @@
import tech.pegasys.teku.infrastructure.ssz.tree.TreeNode;
import tech.pegasys.teku.spec.datastructures.type.SszPublicKey;

// https://eips.ethereum.org/EIPS/eip-7251
public class ConsolidationRequest
extends Container3<ConsolidationRequest, SszByteVector, SszPublicKey, SszPublicKey> {

public static final byte REQUEST_TYPE = 0x2;

public static final ConsolidationRequestSchema SSZ_SCHEMA = new ConsolidationRequestSchema();

protected ConsolidationRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
import tech.pegasys.teku.spec.datastructures.type.SszPublicKey;
import tech.pegasys.teku.spec.datastructures.type.SszSignature;

// https://eips.ethereum.org/EIPS/eip-6110
public class DepositRequest
extends Container5<
DepositRequest, SszPublicKey, SszBytes32, SszUInt64, SszSignature, SszUInt64> {

public static final byte REQUEST_TYPE = 0x0;

DepositRequest(
final DepositRequestSchema schema,
final BLSPublicKey pubkey,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright Consensys Software Inc., 2024
*
* 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.
*/

package tech.pegasys.teku.spec.datastructures.execution.versions.electra;

import com.google.common.annotations.VisibleForTesting;
import java.util.List;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.infrastructure.crypto.Hash;
import tech.pegasys.teku.infrastructure.ssz.SszList;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionRequestsBuilder;

/*
Implement the rules for decoding and hashing execution requests according to https://eips.ethereum.org/EIPS/eip-7685
*/
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;

public ExecutionRequestsDataCodec(final ExecutionRequestsSchema executionRequestsSchema) {
this.executionRequestsSchema = executionRequestsSchema;
}

public ExecutionRequests decode(final List<Bytes> executionRequestData) {
if (executionRequestData.size() != EXPECTED_REQUEST_DATA_ELEMENTS) {
throw new IllegalArgumentException(
"Invalid number of execution request data elements: expected "
+ EXPECTED_REQUEST_DATA_ELEMENTS
+ ", received "
+ executionRequestData.size());
}

final ExecutionRequestsBuilder executionRequestsBuilder =
new ExecutionRequestsBuilderElectra(executionRequestsSchema);

for (int index = 0; index < executionRequestData.size(); index++) {
// The request type is implicitly defined as the index of the element in executionRequestData
switch ((byte) index) {
case DepositRequest.REQUEST_TYPE ->
executionRequestsBuilder.deposits(
executionRequestsSchema
.getDepositRequestsSchema()
.sszDeserialize(executionRequestData.get(index))
.asList());
case WithdrawalRequest.REQUEST_TYPE ->
executionRequestsBuilder.withdrawals(
executionRequestsSchema
.getWithdrawalRequestsSchema()
.sszDeserialize(executionRequestData.get(index))
.asList());
case ConsolidationRequest.REQUEST_TYPE ->
executionRequestsBuilder.consolidations(
executionRequestsSchema
.getConsolidationRequestsSchema()
.sszDeserialize(executionRequestData.get(index))
.asList());
default -> throw new IllegalArgumentException("Invalid execution request type: " + index);
}
}

return executionRequestsBuilder.build();
}

@VisibleForTesting
List<Bytes> encodeWithTypePrefix(final ExecutionRequests executionRequests) {
final SszList<DepositRequest> depositRequestsSszList =
executionRequestsSchema
.getDepositRequestsSchema()
.createFromElements(executionRequests.getDeposits());
final SszList<WithdrawalRequest> withdrawalRequestsSszList =
executionRequestsSchema
.getWithdrawalRequestsSchema()
.createFromElements(executionRequests.getWithdrawals());
final SszList<ConsolidationRequest> consolidationRequestsSszList =
executionRequestsSchema
.getConsolidationRequestsSchema()
.createFromElements(executionRequests.getConsolidations());

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

public Bytes32 hash(final ExecutionRequests executionRequests) {
final Bytes sortedEncodedRequests =
encodeWithTypePrefix(executionRequests).stream()
.map(Hash::sha256)
.map(Bytes.class::cast)
.reduce(Bytes.EMPTY, Bytes::concatenate);
return Hash.sha256(sortedEncodedRequests);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.datastructures.type.SszPublicKey;

// https://eips.ethereum.org/EIPS/eip-7002
public class WithdrawalRequest
extends Container3<WithdrawalRequest, SszByteVector, SszPublicKey, SszUInt64> {

public static final byte REQUEST_TYPE = 0x1;

public static final WithdrawalRequestSchema SSZ_SCHEMA = new WithdrawalRequestSchema();

protected WithdrawalRequest(
Expand Down
Loading