Skip to content

Commit

Permalink
Add consensus block value to block V3
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-aouadi committed Oct 4, 2023
1 parent 87d6a3d commit 139e44c
Show file tree
Hide file tree
Showing 24 changed files with 199 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.junit.jupiter.api.AfterEach;
import tech.pegasys.teku.api.DataProvider;
import tech.pegasys.teku.api.ExecutionClientDataProvider;
import tech.pegasys.teku.api.RewardCalculator;
import tech.pegasys.teku.beacon.sync.SyncService;
import tech.pegasys.teku.bls.BLSKeyGenerator;
import tech.pegasys.teku.bls.BLSKeyPair;
Expand Down Expand Up @@ -134,6 +135,8 @@ public abstract class AbstractDataBackedRestAPIIntegrationTest {
protected final ExecutionLayerBlockProductionManager executionLayerBlockProductionManager =
mock(ExecutionLayerBlockProductionManager.class);

protected final RewardCalculator rewardCalculator = mock(RewardCalculator.class);

protected OperationPool<SignedBlsToExecutionChange> blsToExecutionChangePool;

protected final SignedBlsToExecutionChangeValidator validator =
Expand Down Expand Up @@ -241,6 +244,7 @@ private void setupAndStartRestAPI(BeaconRestApiConfig config) {
.syncCommitteeContributionPool(syncCommitteeContributionPool)
.proposersDataManager(proposersDataManager)
.executionLayerBlockProductionManager(executionLayerBlockProductionManager)
.rewardCalculator(rewardCalculator)
.build();

beaconRestApi =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static tech.pegasys.teku.infrastructure.http.HttpStatusCodes.SC_OK;
import static tech.pegasys.teku.infrastructure.http.RestApiConstants.HEADER_CONSENSUS_BLOCK_VALUE;
import static tech.pegasys.teku.infrastructure.http.RestApiConstants.HEADER_CONSENSUS_VERSION;
import static tech.pegasys.teku.infrastructure.http.RestApiConstants.HEADER_EXECUTION_PAYLOAD_BLINDED;
import static tech.pegasys.teku.infrastructure.http.RestApiConstants.HEADER_EXECUTION_PAYLOAD_VALUE;
Expand All @@ -39,6 +40,7 @@
import org.apache.tuweni.units.bigints.UInt256;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestTemplate;
import tech.pegasys.teku.api.migrated.BlockRewardData;
import tech.pegasys.teku.beaconrestapi.AbstractDataBackedRestAPIIntegrationTest;
import tech.pegasys.teku.beaconrestapi.handlers.v3.validator.GetNewBlockV3;
import tech.pegasys.teku.bls.BLSSignature;
Expand All @@ -48,6 +50,7 @@
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.TestSpecContext;
import tech.pegasys.teku.spec.TestSpecInvocationContextProvider;
import tech.pegasys.teku.spec.constants.EthConstants;
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock;
import tech.pegasys.teku.spec.datastructures.blocks.versions.deneb.BlindedBlockContents;
import tech.pegasys.teku.spec.datastructures.blocks.versions.deneb.BlockContents;
Expand All @@ -61,22 +64,17 @@ public class GetNewBlockV3IntegrationTest extends AbstractDataBackedRestAPIInteg
private DataStructureUtil dataStructureUtil;
private SpecMilestone specMilestone;
private final UInt256 executionPayloadValue = UInt256.valueOf(12345);
private final UInt256 consensusBlockValue = UInt256.valueOf(123);

private final String consensusBlockValueWei =
EthConstants.GWEI_TO_WEI.multiply(consensusBlockValue).toDecimalString();

@BeforeEach
void setup(TestSpecInvocationContextProvider.SpecContext specContext) {
spec = specContext.getSpec();
specMilestone = specContext.getSpecMilestone();
startRestAPIAtGenesis(specMilestone);
dataStructureUtil = new DataStructureUtil(spec);
}

@TestTemplate
void shouldGetUnBlindedBeaconBlockAsJson() throws IOException {
assumeThat(specMilestone).isLessThan(DENEB);
final BeaconBlock beaconBlock = dataStructureUtil.randomBeaconBlock(ONE);
final BLSSignature signature = beaconBlock.getBlock().getBody().getRandaoReveal();
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any()))
.thenReturn(SafeFuture.completedFuture(Optional.of(beaconBlock)));
when(executionLayerBlockProductionManager.getCachedPayloadResult(UInt64.ONE))
.thenReturn(
Optional.of(
Expand All @@ -86,14 +84,20 @@ void shouldGetUnBlindedBeaconBlockAsJson() throws IOException {
Optional.empty(),
Optional.empty(),
Optional.of(SafeFuture.completedFuture(executionPayloadValue)))));
final BlockRewardData blockRewardDataMock = mock(BlockRewardData.class);
when(blockRewardDataMock.getTotal()).thenReturn(consensusBlockValue.toLong());
when(rewardCalculator.getBlockRewardData(any(), any())).thenReturn(blockRewardDataMock);
}

@TestTemplate
void shouldGetUnBlindedBeaconBlockAsJson() throws IOException {
assumeThat(specMilestone).isLessThan(DENEB);
final BeaconBlock beaconBlock = dataStructureUtil.randomBeaconBlock(ONE);
final BLSSignature signature = beaconBlock.getBlock().getBody().getRandaoReveal();
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any()))
.thenReturn(SafeFuture.completedFuture(Optional.of(beaconBlock)));
Response response = get(signature, ContentTypes.JSON);
assertThat(response.code()).isEqualTo(SC_OK);
assertThat(response.header(HEADER_CONSENSUS_VERSION))
.isEqualTo(specMilestone.name().toLowerCase(Locale.ROOT));
assertThat(response.header(HEADER_EXECUTION_PAYLOAD_BLINDED))
.isEqualTo(Boolean.toString(false));
assertThat(response.header(HEADER_EXECUTION_PAYLOAD_VALUE))
.isEqualTo(executionPayloadValue.toDecimalString());
assertResponseWithHeaders(response, false);
final String body = response.body().string();
assertThat(body).isEqualTo(getExpectedBlockAsJson(specMilestone, false, false));
}
Expand All @@ -105,23 +109,8 @@ void shouldGetUnblindedBeaconBlockAsSsz() throws IOException {
final BLSSignature signature = beaconBlock.getBlock().getBody().getRandaoReveal();
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any()))
.thenReturn(SafeFuture.completedFuture(Optional.of(beaconBlock)));
when(executionLayerBlockProductionManager.getCachedPayloadResult(UInt64.ONE))
.thenReturn(
Optional.of(
new ExecutionPayloadResult(
mock(ExecutionPayloadContext.class),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.of(SafeFuture.completedFuture(executionPayloadValue)))));
Response response = get(signature, ContentTypes.OCTET_STREAM);
assertThat(response.code()).isEqualTo(SC_OK);
assertThat(response.header(HEADER_CONSENSUS_VERSION))
.isEqualTo(specMilestone.name().toLowerCase(Locale.ROOT));
assertThat(response.header(HEADER_EXECUTION_PAYLOAD_BLINDED))
.isEqualTo(Boolean.toString(false));
assertThat(response.header(HEADER_EXECUTION_PAYLOAD_VALUE))
.isEqualTo(executionPayloadValue.toDecimalString());
assertResponseWithHeaders(response, false);
final BeaconBlock result =
spec.getGenesisSchemaDefinitions()
.getBeaconBlockSchema()
Expand All @@ -136,22 +125,8 @@ void shouldGetBlindedBeaconBlockAsJson() throws IOException {
final BLSSignature signature = blindedBeaconBlock.getBlock().getBody().getRandaoReveal();
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any()))
.thenReturn(SafeFuture.completedFuture(Optional.of(blindedBeaconBlock)));
when(executionLayerBlockProductionManager.getCachedPayloadResult(UInt64.ONE))
.thenReturn(
Optional.of(
new ExecutionPayloadResult(
mock(ExecutionPayloadContext.class),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.of(SafeFuture.completedFuture(executionPayloadValue)))));
Response response = get(signature, ContentTypes.JSON);
assertThat(response.code()).isEqualTo(SC_OK);
assertThat(response.header(HEADER_CONSENSUS_VERSION))
.isEqualTo(specMilestone.name().toLowerCase(Locale.ROOT));
assertThat(response.header(HEADER_EXECUTION_PAYLOAD_BLINDED)).isEqualTo(Boolean.toString(true));
assertThat(response.header(HEADER_EXECUTION_PAYLOAD_VALUE))
.isEqualTo(executionPayloadValue.toDecimalString());
assertResponseWithHeaders(response, true);
final String body = response.body().string();
assertThat(body).isEqualTo(getExpectedBlockAsJson(specMilestone, true, false));
}
Expand All @@ -163,22 +138,8 @@ void shouldGetBlindedBeaconBlockAsSsz() throws IOException {
final BLSSignature signature = blindedBeaconBlock.getBlock().getBody().getRandaoReveal();
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any()))
.thenReturn(SafeFuture.completedFuture(Optional.of(blindedBeaconBlock)));
when(executionLayerBlockProductionManager.getCachedPayloadResult(UInt64.ONE))
.thenReturn(
Optional.of(
new ExecutionPayloadResult(
mock(ExecutionPayloadContext.class),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.of(SafeFuture.completedFuture(executionPayloadValue)))));
Response response = get(signature, ContentTypes.OCTET_STREAM);
assertThat(response.code()).isEqualTo(SC_OK);
assertThat(response.header(HEADER_CONSENSUS_VERSION))
.isEqualTo(specMilestone.name().toLowerCase(Locale.ROOT));
assertThat(response.header(HEADER_EXECUTION_PAYLOAD_BLINDED)).isEqualTo(Boolean.toString(true));
assertThat(response.header(HEADER_EXECUTION_PAYLOAD_VALUE))
.isEqualTo(executionPayloadValue.toDecimalString());
assertResponseWithHeaders(response, true);
final BeaconBlock result =
spec.getGenesisSchemaDefinitions()
.getBlindedBeaconBlockSchema()
Expand All @@ -193,23 +154,8 @@ void shouldGetUnBlindedBlockContentAsJsonPostDenebAsJson() throws IOException {
final BLSSignature signature = blockContents.getBlock().getBody().getRandaoReveal();
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any()))
.thenReturn(SafeFuture.completedFuture(Optional.of(blockContents)));
when(executionLayerBlockProductionManager.getCachedPayloadResult(UInt64.ONE))
.thenReturn(
Optional.of(
new ExecutionPayloadResult(
mock(ExecutionPayloadContext.class),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.of(SafeFuture.completedFuture(executionPayloadValue)))));
Response response = get(signature, ContentTypes.JSON);
assertThat(response.code()).isEqualTo(SC_OK);
assertThat(response.header(HEADER_CONSENSUS_VERSION))
.isEqualTo(specMilestone.name().toLowerCase(Locale.ROOT));
assertThat(response.header(HEADER_EXECUTION_PAYLOAD_BLINDED))
.isEqualTo(Boolean.toString(false));
assertThat(response.header(HEADER_EXECUTION_PAYLOAD_VALUE))
.isEqualTo(executionPayloadValue.toDecimalString());
assertResponseWithHeaders(response, false);
final String body = response.body().string();
assertThat(body).isEqualTo(getExpectedBlockAsJson(specMilestone, false, true));
}
Expand All @@ -221,23 +167,8 @@ void shouldGetUnBlindedBlockContentAsJsonPostDenebAsSsz() throws IOException {
final BLSSignature signature = blockContents.getBlock().getBody().getRandaoReveal();
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any()))
.thenReturn(SafeFuture.completedFuture(Optional.of(blockContents)));
when(executionLayerBlockProductionManager.getCachedPayloadResult(UInt64.ONE))
.thenReturn(
Optional.of(
new ExecutionPayloadResult(
mock(ExecutionPayloadContext.class),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.of(SafeFuture.completedFuture(executionPayloadValue)))));
Response response = get(signature, ContentTypes.OCTET_STREAM);
assertThat(response.code()).isEqualTo(SC_OK);
assertThat(response.header(HEADER_CONSENSUS_VERSION))
.isEqualTo(specMilestone.name().toLowerCase(Locale.ROOT));
assertThat(response.header(HEADER_EXECUTION_PAYLOAD_BLINDED))
.isEqualTo(Boolean.toString(false));
assertThat(response.header(HEADER_EXECUTION_PAYLOAD_VALUE))
.isEqualTo(executionPayloadValue.toDecimalString());
assertResponseWithHeaders(response, false);
final BlockContents result =
(BlockContents)
spec.getGenesisSchemaDefinitions()
Expand All @@ -254,22 +185,8 @@ void shouldGetBlindedBlockContentAsJsonPostDenebAsJson() throws IOException {
final BLSSignature signature = blindedBlockContents.getBlock().getBody().getRandaoReveal();
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any()))
.thenReturn(SafeFuture.completedFuture(Optional.of(blindedBlockContents)));
when(executionLayerBlockProductionManager.getCachedPayloadResult(UInt64.ONE))
.thenReturn(
Optional.of(
new ExecutionPayloadResult(
mock(ExecutionPayloadContext.class),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.of(SafeFuture.completedFuture(executionPayloadValue)))));
Response response = get(signature, ContentTypes.JSON);
assertThat(response.code()).isEqualTo(SC_OK);
assertThat(response.header(HEADER_CONSENSUS_VERSION))
.isEqualTo(specMilestone.name().toLowerCase(Locale.ROOT));
assertThat(response.header(HEADER_EXECUTION_PAYLOAD_BLINDED)).isEqualTo(Boolean.toString(true));
assertThat(response.header(HEADER_EXECUTION_PAYLOAD_VALUE))
.isEqualTo(executionPayloadValue.toDecimalString());
assertResponseWithHeaders(response, true);
final String body = response.body().string();
assertThat(body).isEqualTo(getExpectedBlockAsJson(specMilestone, true, true));
}
Expand All @@ -282,22 +199,8 @@ void shouldGetBlindedBlockContentAsJsonPostDenebAsSsz() throws IOException {
final BLSSignature signature = blindedBlockContents.getBlock().getBody().getRandaoReveal();
when(validatorApiChannel.createUnsignedBlock(eq(UInt64.ONE), eq(signature), any()))
.thenReturn(SafeFuture.completedFuture(Optional.of(blindedBlockContents)));
when(executionLayerBlockProductionManager.getCachedPayloadResult(UInt64.ONE))
.thenReturn(
Optional.of(
new ExecutionPayloadResult(
mock(ExecutionPayloadContext.class),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.of(SafeFuture.completedFuture(executionPayloadValue)))));
Response response = get(signature, ContentTypes.OCTET_STREAM);
assertThat(response.code()).isEqualTo(SC_OK);
assertThat(response.header(HEADER_CONSENSUS_VERSION))
.isEqualTo(specMilestone.name().toLowerCase(Locale.ROOT));
assertThat(response.header(HEADER_EXECUTION_PAYLOAD_BLINDED)).isEqualTo(Boolean.toString(true));
assertThat(response.header(HEADER_EXECUTION_PAYLOAD_VALUE))
.isEqualTo(executionPayloadValue.toDecimalString());
assertResponseWithHeaders(response, true);
final BlindedBlockContents result =
(BlindedBlockContents)
spec.getGenesisSchemaDefinitions()
Expand Down Expand Up @@ -325,4 +228,15 @@ private String getExpectedBlockAsJson(
return Resources.toString(
Resources.getResource(GetNewBlockV3IntegrationTest.class, fileName), UTF_8);
}

private void assertResponseWithHeaders(Response response, boolean blinded) {
assertThat(response.code()).isEqualTo(SC_OK);
assertThat(response.header(HEADER_CONSENSUS_VERSION))
.isEqualTo(specMilestone.name().toLowerCase(Locale.ROOT));
assertThat(response.header(HEADER_EXECUTION_PAYLOAD_BLINDED))
.isEqualTo(Boolean.toString(blinded));
assertThat(response.header(HEADER_EXECUTION_PAYLOAD_VALUE))
.isEqualTo(executionPayloadValue.toDecimalString());
assertThat(response.header(HEADER_CONSENSUS_BLOCK_VALUE)).isEqualTo(consensusBlockValueWei);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title" : "ProduceBlockV3Response",
"type" : "object",
"required" : [ "version", "execution_payload_blinded", "execution_payload_value", "data" ],
"required" : [ "version", "execution_payload_blinded", "execution_payload_value", "consensus_block_value", "data" ],
"properties" : {
"version" : {
"type" : "string",
Expand All @@ -16,6 +16,12 @@
"example" : "1",
"format" : "uint256"
},
"consensus_block_value" : {
"type" : "string",
"description" : "unsigned 256 bit integer",
"example" : "1",
"format" : "uint256"
},
"data" : {
"title" : "Block",
"type" : "object",
Expand Down
Loading

0 comments on commit 139e44c

Please sign in to comment.