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

[Altair] Remove empty syncAggregate creation in Altair block creation #4528

Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import tech.pegasys.teku.api.schema.Attestation;
import tech.pegasys.teku.api.schema.SignedBeaconBlock;
import tech.pegasys.teku.api.schema.SignedVoluntaryExit;
import tech.pegasys.teku.api.schema.altair.SignedBeaconBlockAltair;
import tech.pegasys.teku.infrastructure.async.StubAsyncRunner;
import tech.pegasys.teku.infrastructure.events.EventChannels;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
Expand Down Expand Up @@ -95,7 +96,7 @@ public class EventSubscriptionManagerTest {

private final SyncState sampleSyncState = SyncState.IN_SYNC;
private final SignedBeaconBlock sampleBlock =
new SignedBeaconBlock(data.randomSignedBeaconBlock(0));
new SignedBeaconBlockAltair(data.randomSignedBeaconBlock(0));
private final Attestation sampleAttestation = new Attestation(data.randomAttestation(0));
private final SignedVoluntaryExit sampleVoluntaryExit =
new SignedVoluntaryExit(data.randomSignedVoluntaryExit());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public SignedBeaconBlock(
public tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock asInternalSignedBeaconBlock(
final Spec spec) {
final tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock beaconBlock =
message.asInternalBeaconBlock(spec);
getMessage().asInternalBeaconBlock(spec);
return tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock.create(
spec, beaconBlock, signature.asInternalBLSSignature());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,32 @@
import tech.pegasys.teku.api.schema.BeaconBlock;
import tech.pegasys.teku.api.schema.interfaces.UnsignedBlock;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecVersion;
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsAltair;

public class BeaconBlockAltair extends BeaconBlock implements UnsignedBlock {
private final BeaconBlockBodyAltair body;

public BeaconBlockAltair(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock message) {
super(message);
this.body = new BeaconBlockBodyAltair(message.getBody().toVersionAltair().orElseThrow());
}

@Override
public tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock asInternalBeaconBlock(
final Spec spec) {
final SpecVersion specVersion = spec.atSlot(slot);
return SchemaDefinitionsAltair.required(specVersion.getSchemaDefinitions())
.getBeaconBlockSchema()
.create(
slot,
proposer_index,
parent_root,
state_root,
body.asInternalBeaconBlockBody(specVersion));
}

@JsonProperty("body")
@Override
public final BeaconBlockBodyAltair getBody() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
public class SignedBeaconBlockAltair extends SignedBeaconBlock implements SignedBlock {
private final BeaconBlockAltair message;

public SignedBeaconBlockAltair(
tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock internalBlock) {
super(internalBlock);
this.message = new BeaconBlockAltair(internalBlock.getMessage());
}

@Override
public BeaconBlockAltair getMessage() {
return message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.EpochProcessingException;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.SlotProcessingException;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.StateTransitionException;
import tech.pegasys.teku.spec.util.DataStructureUtil;
import tech.pegasys.teku.ssz.SszList;

public class BlockProposalTestUtil {
private final Spec spec;
private final DataStructureUtil dataStructureUtil;
private final BeaconBlockBodyLists blockBodyLists;

public BlockProposalTestUtil(final Spec spec) {
this.spec = spec;
this.dataStructureUtil = new DataStructureUtil(spec);
blockBodyLists = BeaconBlockBodyLists.ofSpec(spec);
}

Expand Down Expand Up @@ -79,7 +82,10 @@ public SignedBlockAndState createNewBlock(
.proposerSlashings(slashings)
.attesterSlashings(blockBodyLists.createAttesterSlashings())
.deposits(deposits)
.voluntaryExits(exits));
.voluntaryExits(exits)
.syncAggregate(
() ->
dataStructureUtil.emptySyncAggregateIfRequiredByState(blockSlotState)));

// Sign block and set block signature
final BeaconBlock block = newBlockAndState.getBlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ static BeaconBlockBodySchemaAltair<? extends BeaconBlockBodyAltair> create(
@Override
public BeaconBlockBody createBlockBody(final Consumer<BeaconBlockBodyBuilder> builderConsumer) {
final BeaconBlockBodyBuilderAltair builder = new BeaconBlockBodyBuilderAltair().schema(this);
// Provide a default empty sync aggregate
builder.syncAggregate(getSyncAggregateSchema()::createEmpty);
builderConsumer.accept(builder);
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ static BeaconBlockBodySchemaMergeImpl create(
@Override
public BeaconBlockBody createBlockBody(final Consumer<BeaconBlockBodyBuilder> builderConsumer) {
final BeaconBlockBodyBuilderMerge builder = new BeaconBlockBodyBuilderMerge().schema(this);
// Provide a default empty sync aggregate
builder.syncAggregate(getSyncAggregateSchema()::createEmpty);
builderConsumer.accept(builder);
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
import org.junit.jupiter.api.Test;
import tech.pegasys.teku.bls.BLSSignature;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.datastructures.blocks.Eth1Data;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBody;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBodyBuilder;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBodySchema;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttesterSlashing;
import tech.pegasys.teku.spec.datastructures.operations.Deposit;
Expand All @@ -42,37 +42,54 @@
import tech.pegasys.teku.ssz.SszList;

public abstract class AbstractBeaconBlockBodyTest<T extends BeaconBlockBody> {
protected final Spec spec = TestSpecFactory.createMinimalPhase0();
private final BeaconBlockBodyLists blockBodyLists = BeaconBlockBodyLists.ofSpec(spec);

protected final DataStructureUtil dataStructureUtil = new DataStructureUtil(spec);

protected BLSSignature randaoReveal = dataStructureUtil.randomSignature();
protected Eth1Data eth1Data = dataStructureUtil.randomEth1Data();
protected Bytes32 graffiti = dataStructureUtil.randomBytes32();
protected SszList<ProposerSlashing> proposerSlashings =
blockBodyLists.createProposerSlashings(
dataStructureUtil.randomProposerSlashing(),
dataStructureUtil.randomProposerSlashing(),
dataStructureUtil.randomProposerSlashing());
protected SszList<AttesterSlashing> attesterSlashings =
blockBodyLists.createAttesterSlashings(dataStructureUtil.randomAttesterSlashing());
protected SszList<Attestation> attestations =
blockBodyLists.createAttestations(
dataStructureUtil.randomAttestation(),
dataStructureUtil.randomAttestation(),
dataStructureUtil.randomAttestation());
protected SszList<Deposit> deposits =
blockBodyLists.createDeposits(dataStructureUtil.randomDeposits(2).toArray(new Deposit[0]));
protected SszList<SignedVoluntaryExit> voluntaryExits =
blockBodyLists.createVoluntaryExits(
dataStructureUtil.randomSignedVoluntaryExit(),
dataStructureUtil.randomSignedVoluntaryExit(),
dataStructureUtil.randomSignedVoluntaryExit());
protected ExecutionPayload executionPayload = dataStructureUtil.randomExecutionPayload();

private final T defaultBlockBody = createDefaultBlockBody();
BeaconBlockBodySchema<?> blockBodySchema = defaultBlockBody.getSchema();
protected Spec spec;
protected DataStructureUtil dataStructureUtil;

protected BLSSignature randaoReveal;
protected Eth1Data eth1Data;
protected Bytes32 graffiti;
protected SszList<ProposerSlashing> proposerSlashings;
protected SszList<AttesterSlashing> attesterSlashings;
protected SszList<Attestation> attestations;
protected SszList<Deposit> deposits;
protected SszList<SignedVoluntaryExit> voluntaryExits;

private T defaultBlockBody;
BeaconBlockBodySchema<?> blockBodySchema;

protected void setUpBaseClass(final SpecMilestone milestone, Runnable additionalSetup) {
spec = TestSpecFactory.createMinimal(milestone);
dataStructureUtil = new DataStructureUtil(spec);
BeaconBlockBodyLists blockBodyLists = BeaconBlockBodyLists.ofSpec(spec);

voluntaryExits =
blockBodyLists.createVoluntaryExits(
dataStructureUtil.randomSignedVoluntaryExit(),
dataStructureUtil.randomSignedVoluntaryExit(),
dataStructureUtil.randomSignedVoluntaryExit());
deposits =
blockBodyLists.createDeposits(dataStructureUtil.randomDeposits(2).toArray(new Deposit[0]));
attestations =
blockBodyLists.createAttestations(
dataStructureUtil.randomAttestation(),
dataStructureUtil.randomAttestation(),
dataStructureUtil.randomAttestation());
attesterSlashings =
blockBodyLists.createAttesterSlashings(dataStructureUtil.randomAttesterSlashing());
proposerSlashings =
blockBodyLists.createProposerSlashings(
dataStructureUtil.randomProposerSlashing(),
dataStructureUtil.randomProposerSlashing(),
dataStructureUtil.randomProposerSlashing());
graffiti = dataStructureUtil.randomBytes32();
eth1Data = dataStructureUtil.randomEth1Data();
randaoReveal = dataStructureUtil.randomSignature();

additionalSetup.run();

defaultBlockBody = createDefaultBlockBody();
blockBodySchema = defaultBlockBody.getSchema();
}

private T createBlockBody() {
return createBlockBody(createContentProvider());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,21 @@

package tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.altair;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBodyBuilder;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBodySchema;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.common.AbstractBeaconBlockBodyTest;

class BeaconBlockBodyAltairTest extends AbstractBeaconBlockBodyTest<BeaconBlockBodyAltair> {

@Test
void shouldCreateWithEmptySyncAggregate() {
// This won't always be true but until we can calculate the actual SyncAggregate, use the empty
// one to make the block valid

final BeaconBlockBodyAltair blockBody = createDefaultBlockBody();
final SyncAggregate emptySyncAggregate =
SyncAggregateSchema.create(
spec.getGenesisSpecConfig().toVersionAltair().orElseThrow().getSyncCommitteeSize())
.createEmpty();
assertThat(blockBody.getSyncAggregate()).isEqualTo(emptySyncAggregate);
protected SyncAggregate syncAggregate;

@BeforeEach
void setup() {
super.setUpBaseClass(
SpecMilestone.ALTAIR, () -> syncAggregate = dataStructureUtil.randomSyncAggregate());
}

@Override
Expand All @@ -46,4 +40,10 @@ protected BeaconBlockBodyAltair createBlockBody(
protected BeaconBlockBodySchema<? extends BeaconBlockBodyAltair> getBlockBodySchema() {
return BeaconBlockBodySchemaAltair.create(spec.getGenesisSpecConfig());
}

@Override
protected Consumer<BeaconBlockBodyBuilder> createContentProvider() {
return super.createContentProvider()
.andThen(builder -> builder.syncAggregate(() -> syncAggregate));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,28 @@

package tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.merge;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBodyBuilder;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBodySchema;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.common.AbstractBeaconBlockBodyTest;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.altair.SyncAggregate;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.altair.SyncAggregateSchema;
import tech.pegasys.teku.spec.datastructures.execution.ExecutionPayload;

class BeaconBlockBodyMergeTest extends AbstractBeaconBlockBodyTest<BeaconBlockBodyMerge> {

@Test
void shouldCreateWithEmptySyncAggregate() {
// This won't always be true but until we can calculate the actual SyncAggregate, use the empty
// one to make the block valid

final BeaconBlockBodyMerge blockBody = createDefaultBlockBody();
final SyncAggregate emptySyncAggregate =
SyncAggregateSchema.create(
spec.getGenesisSpecConfig().toVersionMerge().orElseThrow().getSyncCommitteeSize())
.createEmpty();
assertThat(blockBody.getSyncAggregate()).isEqualTo(emptySyncAggregate);
protected SyncAggregate syncAggregate;
protected ExecutionPayload executionPayload;

@BeforeEach
void setup() {
super.setUpBaseClass(
SpecMilestone.MERGE,
() -> {
syncAggregate = dataStructureUtil.randomSyncAggregate();
executionPayload = dataStructureUtil.randomExecutionPayload();
});
}

@Override
Expand All @@ -52,6 +51,10 @@ protected BeaconBlockBodySchema<? extends BeaconBlockBodyMerge> getBlockBodySche
@Override
protected Consumer<BeaconBlockBodyBuilder> createContentProvider() {
return super.createContentProvider()
.andThen(builder -> builder.executionPayload(() -> executionPayload));
.andThen(
builder ->
builder
.syncAggregate(() -> syncAggregate)
.executionPayload(() -> executionPayload));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@
package tech.pegasys.teku.spec.datastructures.blocks.blockbody.versions.phase0;

import java.util.function.Consumer;
import org.junit.jupiter.api.BeforeEach;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBodyBuilder;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.BeaconBlockBodySchema;
import tech.pegasys.teku.spec.datastructures.blocks.blockbody.common.AbstractBeaconBlockBodyTest;

public class BeaconBlockBodyPhase0Test extends AbstractBeaconBlockBodyTest<BeaconBlockBodyPhase0> {

@BeforeEach
void setup() {
super.setUpBaseClass(SpecMilestone.PHASE0, () -> {});
}

@Override
protected BeaconBlockBodyPhase0 createBlockBody(
final Consumer<BeaconBlockBodyBuilder> contentProvider) {
Expand Down
Loading