Skip to content

Commit

Permalink
try another approach for blind/unblind
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Sep 10, 2024
1 parent 1afa6e1 commit 21b2421
Show file tree
Hide file tree
Showing 22 changed files with 853 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,8 @@ void shouldHandleBlindedBeaconBlocks() throws Exception {
assertThat(request.getResponseBody()).isEqualTo(blockContainerAndMetaData);
assertThat(request.getResponseHeaders(HEADER_CONSENSUS_VERSION))
.isEqualTo(Version.fromMilestone(blockContainerAndMetaData.specMilestone()).name());
final boolean expectedHeaderExecutionPayloadBlinded;
if (specMilestone.isGreaterThanOrEqualTo(SpecMilestone.EIP7732)) {
// no blind/unblind concept in ePBS
expectedHeaderExecutionPayloadBlinded = false;
} else {
expectedHeaderExecutionPayloadBlinded = true;
}
assertThat(request.getResponseHeaders(HEADER_EXECUTION_PAYLOAD_BLINDED))
.isEqualTo(Boolean.toString(expectedHeaderExecutionPayloadBlinded));
.isEqualTo(Boolean.toString(true));
assertThat(request.getResponseHeaders(HEADER_EXECUTION_PAYLOAD_VALUE))
.isEqualTo(blockContainerAndMetaData.executionPayloadValue().toDecimalString());
assertThat(request.getResponseHeaders(HEADER_CONSENSUS_BLOCK_VALUE))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import tech.pegasys.teku.api.schema.eip7732.BeaconBlockBodyEip7732;
import tech.pegasys.teku.api.schema.eip7732.BeaconBlockEip7732;
import tech.pegasys.teku.api.schema.eip7732.BeaconStateEip7732;
import tech.pegasys.teku.api.schema.eip7732.BlindedBeaconBlockBodyEip7732;
import tech.pegasys.teku.api.schema.eip7732.BlindedBlockEip7732;
import tech.pegasys.teku.api.schema.electra.BeaconBlockBodyElectra;
import tech.pegasys.teku.api.schema.electra.BeaconBlockElectra;
import tech.pegasys.teku.api.schema.electra.BeaconStateElectra;
Expand Down Expand Up @@ -134,12 +136,12 @@ public BeaconBlock getBlindedBlock(
block.getStateRoot(),
getBlindedBlockBodyElectra(block.getBody()));
case EIP7732 ->
new BeaconBlockEip7732(
new BlindedBlockEip7732(
block.getSlot(),
block.getProposerIndex(),
block.getParentRoot(),
block.getStateRoot(),
getBeaconBlockBodyEip7732(block.getBody()));
getBlindedBlockBodyEip7732(block.getBody()));
};
}

Expand Down Expand Up @@ -269,6 +271,13 @@ private BlindedBeaconBlockBodyElectra getBlindedBlockBodyElectra(
.BlindedBeaconBlockBodyElectra.required(body));
}

private BlindedBeaconBlockBodyEip7732 getBlindedBlockBodyEip7732(
final tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBody body) {
return new BlindedBeaconBlockBodyEip7732(
tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.eip7732
.BlindedBeaconBlockBodyEip7732.required(body));
}

public BeaconState getBeaconState(
final tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState state) {
final UInt64 slot = state.getSlot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import tech.pegasys.teku.api.schema.deneb.SignedBeaconBlockDeneb;
import tech.pegasys.teku.api.schema.deneb.SignedBlindedBeaconBlockDeneb;
import tech.pegasys.teku.api.schema.eip7732.SignedBeaconBlockEip7732;
import tech.pegasys.teku.api.schema.eip7732.SignedBlindedBeaconBlockEip7732;
import tech.pegasys.teku.api.schema.electra.SignedBeaconBlockElectra;
import tech.pegasys.teku.api.schema.electra.SignedBlindedBeaconBlockElectra;
import tech.pegasys.teku.api.schema.interfaces.SignedBlock;
Expand Down Expand Up @@ -69,7 +70,7 @@ public static SignedBeaconBlock create(
() ->
beaconBlock
.toBlindedVersionEip7732()
.map(__ -> new SignedBeaconBlockEip7732(internalBlock)),
.map(__ -> new SignedBlindedBeaconBlockEip7732(internalBlock)),
() ->
beaconBlock
.toVersionEip7732()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Copyright Consensys Software Inc., 2022
*
* 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.api.schema.eip7732;

import static com.google.common.base.Preconditions.checkNotNull;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.api.schema.Attestation;
import tech.pegasys.teku.api.schema.AttesterSlashing;
import tech.pegasys.teku.api.schema.BLSSignature;
import tech.pegasys.teku.api.schema.Deposit;
import tech.pegasys.teku.api.schema.Eth1Data;
import tech.pegasys.teku.api.schema.ProposerSlashing;
import tech.pegasys.teku.api.schema.SignedVoluntaryExit;
import tech.pegasys.teku.api.schema.altair.BeaconBlockBodyAltair;
import tech.pegasys.teku.api.schema.altair.SyncAggregate;
import tech.pegasys.teku.api.schema.capella.SignedBlsToExecutionChange;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema;
import tech.pegasys.teku.spec.SpecVersion;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBody;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.eip7732.BeaconBlockBodySchemaEip7732;

public class BlindedBeaconBlockBodyEip7732 extends BeaconBlockBodyAltair {

@JsonProperty("bls_to_execution_changes")
public final List<SignedBlsToExecutionChange> blsToExecutionChanges;

@JsonProperty("signed_execution_payload_header")
public final SignedExecutionPayloadHeader signedExecutionPayloadHeader;

@JsonProperty("payload_attestations")
public final List<PayloadAttestation> payloadAttestations;

@JsonCreator
public BlindedBeaconBlockBodyEip7732(
@JsonProperty("randao_reveal") final BLSSignature randaoReveal,
@JsonProperty("eth1_data") final Eth1Data eth1Data,
@JsonProperty("graffiti") final Bytes32 graffiti,
@JsonProperty("proposer_slashings") final List<ProposerSlashing> proposerSlashings,
@JsonProperty("attester_slashings") final List<AttesterSlashing> attesterSlashings,
@JsonProperty("attestations") final List<Attestation> attestations,
@JsonProperty("deposits") final List<Deposit> deposits,
@JsonProperty("voluntary_exits") final List<SignedVoluntaryExit> voluntaryExits,
@JsonProperty("sync_aggregate") final SyncAggregate syncAggregate,
@JsonProperty("bls_to_execution_changes")
final List<SignedBlsToExecutionChange> blsToExecutionChanges,
@JsonProperty("signed_execution_payload_header")
final SignedExecutionPayloadHeader signedExecutionPayloadHeader,
@JsonProperty("payload_attestations") final List<PayloadAttestation> payloadAttestations) {
super(
randaoReveal,
eth1Data,
graffiti,
proposerSlashings,
attesterSlashings,
attestations,
deposits,
voluntaryExits,
syncAggregate);
checkNotNull(blsToExecutionChanges, "BlsToExecutionChanges is required for Eip7732 blocks");
this.blsToExecutionChanges = blsToExecutionChanges;
checkNotNull(
signedExecutionPayloadHeader,
"SignedExecutionPayloadHeader is required for Eip7732 blocks");
this.signedExecutionPayloadHeader = signedExecutionPayloadHeader;
checkNotNull(payloadAttestations, "PayloadAttestations is required for Eip7732 blocks");
this.payloadAttestations = payloadAttestations;
}

public BlindedBeaconBlockBodyEip7732(
final tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.eip7732
.BlindedBeaconBlockBodyEip7732
message) {
super(message);
checkNotNull(
message.getBlsToExecutionChanges(),
"BlsToExecutionChanges are required for Eip7732 blocks");
this.blsToExecutionChanges =
message.getBlsToExecutionChanges().stream().map(SignedBlsToExecutionChange::new).toList();
checkNotNull(
message.getSignedExecutionPayloadHeader(),
"SignedExecutionPayloadHeader are required for Eip7732 blocks");
this.signedExecutionPayloadHeader =
new SignedExecutionPayloadHeader(message.getSignedExecutionPayloadHeader());
checkNotNull(
message.getPayloadAttestations(), "PayloadAttestations are required for Eip7732 blocks");
this.payloadAttestations =
message.getPayloadAttestations().stream().map(PayloadAttestation::new).toList();
}

@Override
public BeaconBlockBodySchemaEip7732<?> getBeaconBlockBodySchema(final SpecVersion spec) {
return (BeaconBlockBodySchemaEip7732<?>)
spec.getSchemaDefinitions().getBlindedBeaconBlockBodySchema();
}

@Override
public boolean isBlinded() {
return true;
}

@Override
public BeaconBlockBody asInternalBeaconBlockBody(final SpecVersion spec) {
final SszListSchema<
tech.pegasys.teku.spec.datastructures.operations.SignedBlsToExecutionChange, ?>
blsToExecutionChangesSchema =
getBeaconBlockBodySchema(spec).getBlsToExecutionChangesSchema();
return super.asInternalBeaconBlockBody(
spec,
builder -> {
builder.blsToExecutionChanges(
this.blsToExecutionChanges.stream()
.map(b -> b.asInternalSignedBlsToExecutionChange(spec))
.collect(blsToExecutionChangesSchema.collector()));
builder.signedExecutionPayloadHeader(
signedExecutionPayloadHeader.asInternalSignedExecutionPayloadHeader(spec));
final SszListSchema<
tech.pegasys.teku.spec.datastructures.operations.PayloadAttestation, ?>
payloadAttestationsSchema =
getBeaconBlockBodySchema(spec).getPayloadAttestationsSchema();
builder.payloadAttestations(
this.payloadAttestations.stream()
.map(pa -> pa.asInternalPayloadAttestation(spec))
.collect(payloadAttestationsSchema.collector()));
return SafeFuture.COMPLETE;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright Consensys Software Inc., 2022
*
* 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.api.schema.eip7732;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.api.schema.altair.BeaconBlockAltair;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecVersion;
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockSchema;

public class BlindedBlockEip7732 extends BeaconBlockAltair {

public BlindedBlockEip7732(final BeaconBlock message) {
super(
message.getSlot(),
message.getProposerIndex(),
message.getParentRoot(),
message.getStateRoot(),
new BlindedBeaconBlockBodyEip7732(
message.getBody().toBlindedVersionEip7732().orElseThrow()));
}

@Override
public BeaconBlockSchema getBeaconBlockSchema(final SpecVersion spec) {
return spec.getSchemaDefinitions().getBlindedBeaconBlockSchema();
}

@Override
public BeaconBlock asInternalBeaconBlock(final Spec spec) {
final SpecVersion specVersion = spec.atSlot(slot);
return getBeaconBlockSchema(specVersion)
.create(
slot,
proposer_index,
parent_root,
state_root,
body.asInternalBeaconBlockBody(specVersion));
}

@JsonProperty("body")
@Override
public BlindedBeaconBlockBodyEip7732 getBody() {
return (BlindedBeaconBlockBodyEip7732) body;
}

@JsonCreator
public BlindedBlockEip7732(
@JsonProperty("slot") final UInt64 slot,
@JsonProperty("proposer_index") final UInt64 proposerIndex,
@JsonProperty("parent_root") final Bytes32 parentRoot,
@JsonProperty("state_root") final Bytes32 stateRoot,
@JsonProperty("body") final BlindedBeaconBlockBodyEip7732 body) {
super(slot, proposerIndex, parentRoot, stateRoot, body);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright Consensys Software Inc., 2022
*
* 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.api.schema.eip7732;

import static com.google.common.base.Preconditions.checkArgument;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import tech.pegasys.teku.api.schema.BLSSignature;
import tech.pegasys.teku.api.schema.SignedBeaconBlock;
import tech.pegasys.teku.api.schema.interfaces.SignedBlock;

public class SignedBlindedBeaconBlockEip7732 extends SignedBeaconBlock implements SignedBlock {
private final BlindedBlockEip7732 message;

public SignedBlindedBeaconBlockEip7732(
final tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock internalBlock) {
super(internalBlock);
checkArgument(
internalBlock.getMessage().getBody().isBlinded(), "requires a signed blinded beacon block");
this.message = new BlindedBlockEip7732(internalBlock.getMessage());
}

@Override
public BlindedBlockEip7732 getMessage() {
return message;
}

@JsonCreator
public SignedBlindedBeaconBlockEip7732(
@JsonProperty("message") final BlindedBlockEip7732 message,
@JsonProperty("signature") final BLSSignature signature) {
super(message, signature);
this.message = message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ private TreeNode getBlindedTree() {
}

public SignedBeaconBlock unblind(
final SchemaDefinitions schemaDefinitions, final ExecutionPayload payload) {
final SchemaDefinitions schemaDefinitions, final Optional<ExecutionPayload> payload) {
if (!isBlinded()) {
return this;
}

final TreeNode unblindedTree = getUnblindedTree(payload);
if (payload.isEmpty()) {
return this;
}
final TreeNode unblindedTree = getUnblindedTree(payload.get());
return schemaDefinitions.getSignedBeaconBlockSchema().createFromBackingNode(unblindedTree);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.deneb.BeaconBlockBodyDeneb;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.deneb.BlindedBeaconBlockBodyDeneb;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.eip7732.BeaconBlockBodyEip7732;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.eip7732.BlindedBeaconBlockBodyEip7732;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.electra.BeaconBlockBodyElectra;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.electra.BlindedBeaconBlockBodyElectra;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload;
Expand Down Expand Up @@ -145,7 +146,7 @@ default Optional<BlindedBeaconBlockBodyElectra> toBlindedVersionElectra() {
return Optional.empty();
}

default Optional<BeaconBlockBodyEip7732> toBlindedVersionEip7732() {
default Optional<BlindedBeaconBlockBodyEip7732> toBlindedVersionEip7732() {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttesterSlashing;
import tech.pegasys.teku.spec.datastructures.operations.Deposit;
import tech.pegasys.teku.spec.datastructures.operations.PayloadAttestation;
import tech.pegasys.teku.spec.datastructures.operations.ProposerSlashing;
import tech.pegasys.teku.spec.datastructures.operations.SignedVoluntaryExit;

Expand All @@ -51,6 +52,10 @@ SafeFuture<? extends BeaconBlockBody> createBlockBody(

SszListSchema<SignedVoluntaryExit, ?> getVoluntaryExitsSchema();

default SszListSchema<PayloadAttestation, ?> getPayloadAttestationsSchema() {
throw new UnsupportedOperationException("PayloadAttestations not supported until EIP-7732");
}

default Optional<BeaconBlockBodySchemaAltair<?>> toVersionAltair() {
return Optional.empty();
}
Expand Down
Loading

0 comments on commit 21b2421

Please sign in to comment.