Skip to content

Commit

Permalink
[Merge] Adopt some recent Merge spec PRs (#4408)
Browse files Browse the repository at this point in the history
* Replace consts in all configs with TERMINAL_TOTAL_DIFFICULTY
* Remove TransitionStore and targetSecondsToMerge, minAnchorPowBlockDifficulty constants
* Adopt merge PR https://github.com/ethereum/consensus-specs/pull/2522/files
* Adopt merge PR ethereum/consensus-specs#2595
  • Loading branch information
Nashatyrev authored Sep 28, 2021
1 parent 8381c48 commit 24c942b
Show file tree
Hide file tree
Showing 29 changed files with 180 additions and 379 deletions.
9 changes: 1 addition & 8 deletions ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import tech.pegasys.teku.spec.datastructures.forkchoice.MutableStore;
import tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyForkChoiceStrategy;
import tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyStore;
import tech.pegasys.teku.spec.datastructures.forkchoice.TransitionStore;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationData;
import tech.pegasys.teku.spec.datastructures.operations.AttesterSlashing;
Expand Down Expand Up @@ -481,16 +480,10 @@ public BlockImportResult onBlock(
final BeaconState blockSlotState,
final IndexedAttestationCache indexedAttestationCache) {
SpecVersion specVersion = atBlock(signedBlock);
Optional<TransitionStore> maybeTransitionStore = specVersion.getTransitionStore();
return specVersion
.getForkChoiceUtil()
.onBlock(
executionEngineChannel,
store,
signedBlock,
blockSlotState,
indexedAttestationCache,
maybeTransitionStore);
executionEngineChannel, store, signedBlock, blockSlotState, indexedAttestationCache);
}

public boolean blockDescendsFromLatestFinalizedBlock(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,25 +679,19 @@ public class MergeBuilder {
private UInt64 mergeForkEpoch;

// Transition
private UInt64 targetSecondsToMerge;
private UInt256 minAnchorPowBlockDifficulty;
private UInt256 terminalTotalDifficulty;

private MergeBuilder() {}

SpecConfigMerge build(final SpecConfigAltair specConfig) {
return new SpecConfigMerge(
specConfig,
mergeForkVersion,
mergeForkEpoch,
targetSecondsToMerge,
minAnchorPowBlockDifficulty);
specConfig, mergeForkVersion, mergeForkEpoch, terminalTotalDifficulty);
}

void validate() {
validateConstant("mergeForkVersion", mergeForkVersion);
validateConstant("mergeForkEpoch", mergeForkEpoch);
validateConstant("targetSecondsToMerge", targetSecondsToMerge);
validateConstant("minAnchorPowBlockDifficulty", minAnchorPowBlockDifficulty);
validateConstant("terminalTotalDifficulty", terminalTotalDifficulty);
}

public MergeBuilder mergeForkVersion(Bytes4 mergeForkVersion) {
Expand All @@ -712,13 +706,8 @@ public MergeBuilder mergeForkEpoch(UInt64 mergeForkEpoch) {
return this;
}

public MergeBuilder targetSecondsToMerge(UInt64 targetSecondsToMerge) {
this.targetSecondsToMerge = targetSecondsToMerge;
return this;
}

public MergeBuilder minAnchorPowBlockDifficulty(UInt256 minAnchorPowBlockDifficulty) {
this.minAnchorPowBlockDifficulty = minAnchorPowBlockDifficulty;
public MergeBuilder terminalTotalDifficulty(UInt256 terminalTotalDifficulty) {
this.terminalTotalDifficulty = terminalTotalDifficulty;
return this;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,17 @@ public class SpecConfigMerge extends DelegatingSpecConfigAltair {
private final UInt64 mergeForkEpoch;

// Transition
private final UInt64 targetSecondsToMerge;
private final UInt256 minAnchorPowBlockDifficulty;
private final UInt256 terminalTotalDifficulty;

public SpecConfigMerge(
SpecConfigAltair specConfig,
Bytes4 mergeForkVersion,
UInt64 mergeForkEpoch,
UInt64 targetSecondsToMerge,
UInt256 minAnchorPowBlockDifficulty) {
UInt256 terminalTotalDifficulty) {
super(specConfig);
this.mergeForkVersion = mergeForkVersion;
this.mergeForkEpoch = mergeForkEpoch;
this.targetSecondsToMerge = targetSecondsToMerge;
this.minAnchorPowBlockDifficulty = minAnchorPowBlockDifficulty;
this.terminalTotalDifficulty = terminalTotalDifficulty;
}

public static SpecConfigMerge required(final SpecConfig specConfig) {
Expand Down Expand Up @@ -73,12 +70,8 @@ public UInt64 getMergeForkEpoch() {
return mergeForkEpoch;
}

public UInt64 getTargetSecondsToMerge() {
return targetSecondsToMerge;
}

public UInt256 getMinAnchorPowBlockDifficulty() {
return minAnchorPowBlockDifficulty;
public UInt256 getTerminalTotalDifficulty() {
return terminalTotalDifficulty;
}

@Override
Expand All @@ -97,13 +90,11 @@ public boolean equals(Object o) {
SpecConfigMerge that = (SpecConfigMerge) o;
return Objects.equals(mergeForkVersion, that.mergeForkVersion)
&& Objects.equals(mergeForkEpoch, that.mergeForkEpoch)
&& Objects.equals(targetSecondsToMerge, that.targetSecondsToMerge)
&& Objects.equals(minAnchorPowBlockDifficulty, that.minAnchorPowBlockDifficulty);
&& Objects.equals(terminalTotalDifficulty, that.terminalTotalDifficulty);
}

@Override
public int hashCode() {
return Objects.hash(
mergeForkVersion, mergeForkEpoch, targetSecondsToMerge, minAnchorPowBlockDifficulty);
return Objects.hash(mergeForkVersion, mergeForkEpoch, terminalTotalDifficulty);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
package tech.pegasys.teku.spec.logic;

import java.util.Optional;
import tech.pegasys.teku.spec.datastructures.forkchoice.TransitionStore;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.executionengine.ExecutionEngineChannel;
import tech.pegasys.teku.spec.logic.common.block.BlockProcessor;
import tech.pegasys.teku.spec.logic.common.forktransition.StateUpgrade;
import tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors;
Expand Down Expand Up @@ -48,12 +45,6 @@ public Optional<StateUpgrade<?>> getStateUpgrade() {
return specLogic.getStateUpgrade();
}

@Override
public void initializeTransitionStore(
ExecutionEngineChannel executionEngineChannel, BeaconState state) {
specLogic.initializeTransitionStore(executionEngineChannel, state);
}

@Override
public ValidatorsUtil getValidatorsUtil() {
return specLogic.getValidatorsUtil();
Expand Down Expand Up @@ -134,11 +125,6 @@ public Optional<MergeTransitionHelpers> getMergeTransitionHelpers() {
return specLogic.getMergeTransitionHelpers();
}

@Override
public Optional<TransitionStore> getTransitionStore() {
return specLogic.getTransitionStore();
}

@Override
public OperationSignatureVerifier operationSignatureVerifier() {
return specLogic.operationSignatureVerifier();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
package tech.pegasys.teku.spec.logic;

import java.util.Optional;
import tech.pegasys.teku.spec.datastructures.forkchoice.TransitionStore;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.executionengine.ExecutionEngineChannel;
import tech.pegasys.teku.spec.logic.common.block.BlockProcessor;
import tech.pegasys.teku.spec.logic.common.forktransition.StateUpgrade;
import tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors;
Expand All @@ -39,8 +36,6 @@
public interface SpecLogic {
Optional<StateUpgrade<?>> getStateUpgrade();

void initializeTransitionStore(ExecutionEngineChannel executionEngineChannel, BeaconState state);

ValidatorsUtil getValidatorsUtil();

BeaconStateUtil getBeaconStateUtil();
Expand Down Expand Up @@ -74,6 +69,4 @@ public interface SpecLogic {
Optional<ExecutionPayloadUtil> getExecutionPayloadUtil();

Optional<MergeTransitionHelpers> getMergeTransitionHelpers();

Optional<TransitionStore> getTransitionStore();
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public BeaconState processSlots(
.orElse(prevMilestoneState);
// Update spec
currentSpec = newSpec;
newSpec.initializeTransitionStore(executionEngineChannel, state);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@

import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
import tech.pegasys.teku.spec.config.SpecConfigMerge;
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock;
import tech.pegasys.teku.spec.datastructures.forkchoice.TransitionStore;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.executionengine.ExecutionEngineChannel;
import tech.pegasys.teku.spec.logic.versions.merge.helpers.MiscHelpersMerge;

public class MergeTransitionHelpers {

private final MiscHelpersMerge miscHelpers;
private final SpecConfigMerge specConfig;

public MergeTransitionHelpers(MiscHelpersMerge miscHelpers) {
public MergeTransitionHelpers(MiscHelpersMerge miscHelpers, SpecConfigMerge specConfig) {
this.miscHelpers = miscHelpers;
this.specConfig = specConfig;
}

public boolean isMergeComplete(BeaconState state) {
Expand All @@ -37,18 +39,20 @@ public boolean isMergeBlock(BeaconState state, BeaconBlock block) {
return miscHelpers.isMergeBlock(state, block);
}

public boolean isValidTerminalPowBlock(PowBlock powBlock, TransitionStore transitionStore) {
public boolean isValidTerminalPowBlock(PowBlock powBlock, PowBlock parentPowBlock) {
boolean isTotalDifficultyReached =
powBlock.totalDifficulty.compareTo(transitionStore.getTransitionTotalDifficulty()) >= 0;
return powBlock.isValid && isTotalDifficultyReached;
powBlock.totalDifficulty.compareTo(specConfig.getTerminalTotalDifficulty()) >= 0;
boolean isParentTotalDifficultyValid =
parentPowBlock.totalDifficulty.compareTo(specConfig.getTerminalTotalDifficulty()) < 0;
return isTotalDifficultyReached && isParentTotalDifficultyValid;
}

public PowBlock getPowBlock(ExecutionEngineChannel executionEngineChannel, Bytes32 blockHash) {
return executionEngineChannel
.getPowBlock(blockHash)
.join()
.map(PowBlock::new)
.orElse(new PowBlock(blockHash, false, false, UInt256.ZERO, UInt256.ZERO));
.orElse(new PowBlock(blockHash, Bytes32.ZERO, UInt256.ZERO, UInt256.ZERO));
}

public PowBlock getPowChainHead(ExecutionEngineChannel executionEngineChannel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,22 @@
public class PowBlock {

public final Bytes32 blockHash;
public final boolean isProcessed;
public final boolean isValid;
public final Bytes32 parentHash;
public final UInt256 totalDifficulty;
public final UInt256 difficulty;

public PowBlock(
Bytes32 blockHash,
boolean isProcessed,
boolean isValid,
UInt256 totalDifficulty,
UInt256 difficulty) {
Bytes32 blockHash, Bytes32 parentHash, UInt256 totalDifficulty, UInt256 difficulty) {
this.blockHash = blockHash;
this.isProcessed = isProcessed;
this.isValid = isValid;
this.parentHash = parentHash;
this.totalDifficulty = totalDifficulty;
this.difficulty = difficulty;
}

PowBlock(EthBlock.Block block) {
this(
Bytes32.fromHexString(block.getHash()),
true,
true,
Bytes32.fromHexString(block.getParentHash()),
UInt256.valueOf(block.getTotalDifficulty()),
UInt256.valueOf(block.getDifficulty()));
}
Expand Down
Loading

0 comments on commit 24c942b

Please sign in to comment.