Skip to content

Commit 557e083

Browse files
Refactor BlockProcessor and EpochProcessor (#8856)
1 parent d5528fa commit 557e083

File tree

8 files changed

+132
-231
lines changed

8 files changed

+132
-231
lines changed

ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/common/block/AbstractBlockProcessor.java

Lines changed: 6 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package tech.pegasys.teku.spec.logic.common.block;
1515

1616
import static com.google.common.base.Preconditions.checkArgument;
17-
import static tech.pegasys.teku.spec.config.SpecConfig.FAR_FUTURE_EPOCH;
1817

1918
import com.google.common.annotations.VisibleForTesting;
2019
import it.unimi.dsi.fastutil.ints.IntList;
@@ -54,7 +53,6 @@
5453
import tech.pegasys.teku.spec.datastructures.operations.AttesterSlashing;
5554
import tech.pegasys.teku.spec.datastructures.operations.Deposit;
5655
import tech.pegasys.teku.spec.datastructures.operations.DepositData;
57-
import tech.pegasys.teku.spec.datastructures.operations.DepositMessage;
5856
import tech.pegasys.teku.spec.datastructures.operations.IndexedAttestation;
5957
import tech.pegasys.teku.spec.datastructures.operations.ProposerSlashing;
6058
import tech.pegasys.teku.spec.datastructures.operations.SignedVoluntaryExit;
@@ -682,7 +680,7 @@ private boolean batchVerifyDepositSignatures(final SszList<Deposit> deposits) {
682680
final BLSPublicKey pubkey = deposit.getData().getPubkey();
683681
publicKeys.add(List.of(pubkey));
684682
messages.add(
685-
computeDepositSigningRoot(
683+
miscHelpers.computeDepositSigningRoot(
686684
pubkey,
687685
deposit.getData().getWithdrawalCredentials(),
688686
deposit.getData().getAmount()));
@@ -760,34 +758,18 @@ public void applyDeposit(
760758
// Verify the deposit signature (proof of possession) which is not checked by the deposit
761759
// contract
762760
if (signatureAlreadyVerified
763-
|| isValidDepositSignature(pubkey, withdrawalCredentials, amount, signature)) {
764-
addValidatorToRegistry(state, pubkey, withdrawalCredentials, amount);
761+
|| miscHelpers.isValidDepositSignature(
762+
pubkey, withdrawalCredentials, amount, signature)) {
763+
beaconStateMutators.addValidatorToRegistry(state, pubkey, withdrawalCredentials, amount);
765764
} else {
766765
handleInvalidDeposit(pubkey, maybePubkeyToIndexMap);
767766
}
768767
} else {
769-
applyDepositToValidatorIndex(
770-
state,
771-
withdrawalCredentials,
772-
signatureAlreadyVerified,
773-
existingIndex.get(),
774-
amount,
775-
pubkey,
776-
signature);
768+
// Increase balance by deposit amount
769+
beaconStateMutators.increaseBalance(state, existingIndex.get(), amount);
777770
}
778771
}
779772

780-
protected void applyDepositToValidatorIndex(
781-
final MutableBeaconState state,
782-
final Bytes32 withdrawalCredentials,
783-
final boolean signatureAlreadyVerified,
784-
final int validatorIndex,
785-
final UInt64 amount,
786-
final BLSPublicKey pubkey,
787-
final BLSSignature signature) {
788-
beaconStateMutators.increaseBalance(state, validatorIndex, amount);
789-
}
790-
791773
protected void handleInvalidDeposit(
792774
final BLSPublicKey pubkey,
793775
final Optional<Object2IntMap<BLSPublicKey>> maybePubkeyToIndexMap) {
@@ -799,55 +781,6 @@ protected void handleInvalidDeposit(
799781
});
800782
}
801783

802-
/** is_valid_deposit_signature */
803-
protected boolean isValidDepositSignature(
804-
final BLSPublicKey pubkey,
805-
final Bytes32 withdrawalCredentials,
806-
final UInt64 amount,
807-
final BLSSignature signature) {
808-
try {
809-
return depositSignatureVerifier.verify(
810-
pubkey, computeDepositSigningRoot(pubkey, withdrawalCredentials, amount), signature);
811-
} catch (final BlsException e) {
812-
return false;
813-
}
814-
}
815-
816-
private Bytes computeDepositSigningRoot(
817-
final BLSPublicKey pubkey, final Bytes32 withdrawalCredentials, final UInt64 amount) {
818-
final Bytes32 domain = miscHelpers.computeDomain(Domain.DEPOSIT);
819-
final DepositMessage depositMessage = new DepositMessage(pubkey, withdrawalCredentials, amount);
820-
return miscHelpers.computeSigningRoot(depositMessage, domain);
821-
}
822-
823-
protected void addValidatorToRegistry(
824-
final MutableBeaconState state,
825-
final BLSPublicKey pubkey,
826-
final Bytes32 withdrawalCredentials,
827-
final UInt64 amount) {
828-
final Validator validator = getValidatorFromDeposit(pubkey, withdrawalCredentials, amount);
829-
LOG.debug("Adding new validator with index {} to state", state.getValidators().size());
830-
state.getValidators().append(validator);
831-
state.getBalances().appendElement(amount);
832-
}
833-
834-
protected Validator getValidatorFromDeposit(
835-
final BLSPublicKey pubkey, final Bytes32 withdrawalCredentials, final UInt64 amount) {
836-
final UInt64 effectiveBalance =
837-
amount
838-
.minus(amount.mod(specConfig.getEffectiveBalanceIncrement()))
839-
.min(specConfig.getMaxEffectiveBalance());
840-
return new Validator(
841-
pubkey,
842-
withdrawalCredentials,
843-
effectiveBalance,
844-
false,
845-
FAR_FUTURE_EPOCH,
846-
FAR_FUTURE_EPOCH,
847-
FAR_FUTURE_EPOCH,
848-
FAR_FUTURE_EPOCH);
849-
}
850-
851784
@Override
852785
public void processVoluntaryExits(
853786
final MutableBeaconState state,
@@ -905,13 +838,6 @@ protected BlockValidationResult verifyVoluntaryExits(
905838
return BlockValidationResult.SUCCESSFUL;
906839
}
907840

908-
protected void processWithdrawalRequests(
909-
final MutableBeaconState state,
910-
final BeaconBlockBody beaconBlockBody,
911-
final Supplier<ValidatorExitContext> validatorExitContextSupplier) {
912-
// No WithdrawalRequests until Electra
913-
}
914-
915841
@Override
916842
public void processDepositRequests(
917843
final MutableBeaconState state, final List<DepositRequest> depositRequests) {
@@ -952,13 +878,6 @@ protected void safelyProcess(final BlockProcessingAction action) throws BlockPro
952878
}
953879
}
954880

955-
protected void assertCondition(final boolean condition, final String errorMessage)
956-
throws BlockProcessingException {
957-
if (!condition) {
958-
throw new BlockProcessingException(errorMessage);
959-
}
960-
}
961-
962881
public interface IndexedAttestationProvider {
963882

964883
IndexedAttestation getIndexedAttestation(final Attestation attestation);

ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/common/helpers/BeaconStateMutators.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121
import java.util.function.Supplier;
22+
import org.apache.logging.log4j.LogManager;
23+
import org.apache.logging.log4j.Logger;
24+
import org.apache.tuweni.bytes.Bytes32;
25+
import tech.pegasys.teku.bls.BLSPublicKey;
2226
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
2327
import tech.pegasys.teku.spec.config.SpecConfig;
2428
import tech.pegasys.teku.spec.datastructures.state.Validator;
@@ -27,6 +31,8 @@
2731
import tech.pegasys.teku.spec.datastructures.state.beaconstate.MutableBeaconState;
2832

2933
public class BeaconStateMutators {
34+
private static final Logger LOG = LogManager.getLogger();
35+
3036
protected final SpecConfig specConfig;
3137
protected final MiscHelpers miscHelpers;
3238
private final BeaconStateAccessors beaconStateAccessors;
@@ -142,6 +148,22 @@ public Supplier<ValidatorExitContext> createValidatorExitContextSupplier(
142148
return Suppliers.memoize(() -> createValidatorExitContext(state));
143149
}
144150

151+
/**
152+
* <a
153+
* href="https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#deposits">add_validator_to_registry</a>
154+
*/
155+
public void addValidatorToRegistry(
156+
final MutableBeaconState state,
157+
final BLSPublicKey pubkey,
158+
final Bytes32 withdrawalCredentials,
159+
final UInt64 amount) {
160+
final Validator validator =
161+
miscHelpers.getValidatorFromDeposit(pubkey, withdrawalCredentials, amount);
162+
LOG.debug("Adding new validator with index {} to state", state.getValidators().size());
163+
state.getValidators().append(validator);
164+
state.getBalances().appendElement(amount);
165+
}
166+
145167
/**
146168
* This function implements an optimized version of exitQueueEpoch and exitQueueChurn calculation,
147169
* compared to the `initiate_validator_exit` reference implementation.

ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/common/helpers/MiscHelpers.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
import static com.google.common.base.Preconditions.checkArgument;
1717
import static tech.pegasys.teku.infrastructure.crypto.Hash.getSha256Instance;
18+
import static tech.pegasys.teku.spec.config.SpecConfig.FAR_FUTURE_EPOCH;
19+
import static tech.pegasys.teku.spec.logic.common.block.AbstractBlockProcessor.depositSignatureVerifier;
1820
import static tech.pegasys.teku.spec.logic.common.helpers.MathHelpers.bytesToUInt64;
1921
import static tech.pegasys.teku.spec.logic.common.helpers.MathHelpers.uint64ToBytes;
2022
import static tech.pegasys.teku.spec.logic.common.helpers.MathHelpers.uintTo4Bytes;
@@ -27,6 +29,9 @@
2729
import org.apache.tuweni.bytes.Bytes;
2830
import org.apache.tuweni.bytes.Bytes32;
2931
import org.apache.tuweni.units.bigints.UInt256;
32+
import tech.pegasys.teku.bls.BLSPublicKey;
33+
import tech.pegasys.teku.bls.BLSSignature;
34+
import tech.pegasys.teku.bls.impl.BlsException;
3035
import tech.pegasys.teku.infrastructure.bytes.Bytes4;
3136
import tech.pegasys.teku.infrastructure.crypto.Hash;
3237
import tech.pegasys.teku.infrastructure.crypto.Sha256;
@@ -37,10 +42,12 @@
3742
import tech.pegasys.teku.kzg.KZG;
3843
import tech.pegasys.teku.kzg.KZGCommitment;
3944
import tech.pegasys.teku.spec.config.SpecConfig;
45+
import tech.pegasys.teku.spec.constants.Domain;
4046
import tech.pegasys.teku.spec.constants.NetworkConstants;
4147
import tech.pegasys.teku.spec.datastructures.blobs.versions.deneb.BlobSidecar;
4248
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock;
4349
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock;
50+
import tech.pegasys.teku.spec.datastructures.operations.DepositMessage;
4451
import tech.pegasys.teku.spec.datastructures.state.ForkData;
4552
import tech.pegasys.teku.spec.datastructures.state.SigningData;
4653
import tech.pegasys.teku.spec.datastructures.state.Validator;
@@ -326,6 +333,13 @@ public Bytes32 computeSigningRoot(final Bytes bytes, final Bytes32 domain) {
326333
return domainWrappedObject.hashTreeRoot();
327334
}
328335

336+
public Bytes computeDepositSigningRoot(
337+
final BLSPublicKey pubkey, final Bytes32 withdrawalCredentials, final UInt64 amount) {
338+
final Bytes32 domain = computeDomain(Domain.DEPOSIT);
339+
final DepositMessage depositMessage = new DepositMessage(pubkey, withdrawalCredentials, amount);
340+
return computeSigningRoot(depositMessage, domain);
341+
}
342+
329343
public Bytes4 computeForkDigest(
330344
final Bytes4 currentVersion, final Bytes32 genesisValidatorsRoot) {
331345
return new Bytes4(computeForkDataRoot(currentVersion, genesisValidatorsRoot).slice(0, 4));
@@ -400,6 +414,38 @@ public boolean isFormerDepositMechanismDisabled(final BeaconState state) {
400414
return false;
401415
}
402416

417+
/** is_valid_deposit_signature */
418+
public boolean isValidDepositSignature(
419+
final BLSPublicKey pubkey,
420+
final Bytes32 withdrawalCredentials,
421+
final UInt64 amount,
422+
final BLSSignature signature) {
423+
try {
424+
return depositSignatureVerifier.verify(
425+
pubkey, computeDepositSigningRoot(pubkey, withdrawalCredentials, amount), signature);
426+
} catch (final BlsException e) {
427+
return false;
428+
}
429+
}
430+
431+
/** get_validator_from_deposit */
432+
public Validator getValidatorFromDeposit(
433+
final BLSPublicKey pubkey, final Bytes32 withdrawalCredentials, final UInt64 amount) {
434+
final UInt64 effectiveBalance =
435+
amount
436+
.minus(amount.mod(specConfig.getEffectiveBalanceIncrement()))
437+
.min(specConfig.getMaxEffectiveBalance());
438+
return new Validator(
439+
pubkey,
440+
withdrawalCredentials,
441+
effectiveBalance,
442+
false,
443+
FAR_FUTURE_EPOCH,
444+
FAR_FUTURE_EPOCH,
445+
FAR_FUTURE_EPOCH,
446+
FAR_FUTURE_EPOCH);
447+
}
448+
403449
public Optional<MiscHelpersDeneb> toVersionDeneb() {
404450
return Optional.empty();
405451
}

ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/altair/block/BlockProcessorAltair.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -213,20 +213,6 @@ public Optional<UInt64> processAttestationProposerReward(
213213
return Optional.empty();
214214
}
215215

216-
@Override
217-
protected void addValidatorToRegistry(
218-
final MutableBeaconState state,
219-
final BLSPublicKey pubkey,
220-
final Bytes32 withdrawalCredentials,
221-
final UInt64 amount) {
222-
super.addValidatorToRegistry(state, pubkey, withdrawalCredentials, amount);
223-
final MutableBeaconStateAltair stateAltair = MutableBeaconStateAltair.required(state);
224-
225-
stateAltair.getPreviousEpochParticipation().append(SszByte.ZERO);
226-
stateAltair.getCurrentEpochParticipation().append(SszByte.ZERO);
227-
stateAltair.getInactivityScores().append(SszUInt64.ZERO);
228-
}
229-
230216
@Override
231217
public void processSyncAggregate(
232218
final MutableBeaconState baseState,

ethereum/spec/src/main/java/tech/pegasys/teku/spec/logic/versions/altair/helpers/BeaconStateMutatorsAltair.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
1717
import static tech.pegasys.teku.spec.constants.IncentivizationWeights.WEIGHT_DENOMINATOR;
1818

1919
import java.util.function.Supplier;
20+
import org.apache.tuweni.bytes.Bytes32;
21+
import tech.pegasys.teku.bls.BLSPublicKey;
22+
import tech.pegasys.teku.infrastructure.ssz.primitive.SszByte;
23+
import tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64;
2024
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
2125
import tech.pegasys.teku.spec.config.SpecConfigAltair;
2226
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateCache;
2327
import tech.pegasys.teku.spec.datastructures.state.beaconstate.MutableBeaconState;
28+
import tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.MutableBeaconStateAltair;
2429
import tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors;
2530
import tech.pegasys.teku.spec.logic.common.helpers.BeaconStateMutators;
2631
import tech.pegasys.teku.spec.logic.common.helpers.MiscHelpers;
@@ -36,6 +41,20 @@ public BeaconStateMutatorsAltair(
3641
this.specConfigAltair = specConfig;
3742
}
3843

44+
@Override
45+
public void addValidatorToRegistry(
46+
final MutableBeaconState state,
47+
final BLSPublicKey pubkey,
48+
final Bytes32 withdrawalCredentials,
49+
final UInt64 amount) {
50+
super.addValidatorToRegistry(state, pubkey, withdrawalCredentials, amount);
51+
final MutableBeaconStateAltair stateAltair = MutableBeaconStateAltair.required(state);
52+
53+
stateAltair.getPreviousEpochParticipation().append(SszByte.ZERO);
54+
stateAltair.getCurrentEpochParticipation().append(SszByte.ZERO);
55+
stateAltair.getInactivityScores().append(SszUInt64.ZERO);
56+
}
57+
3958
@Override
4059
protected UInt64 calculateProposerReward(final UInt64 whistleblowerReward) {
4160
return whistleblowerReward.times(PROPOSER_WEIGHT).dividedBy(WEIGHT_DENOMINATOR);

0 commit comments

Comments
 (0)