-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ability to add deposit receipts in the stub (#8139)
- Loading branch information
1 parent
15f5a84
commit 508459f
Showing
7 changed files
with
114 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...um/spec/src/main/java/tech/pegasys/teku/spec/datastructures/util/DepositReceiptsUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2024 | ||
* | ||
* 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.spec.datastructures.util; | ||
|
||
import java.security.SecureRandom; | ||
import java.util.List; | ||
import java.util.stream.IntStream; | ||
import org.apache.tuweni.bytes.Bytes32; | ||
import tech.pegasys.teku.bls.BLS; | ||
import tech.pegasys.teku.bls.BLSKeyPair; | ||
import tech.pegasys.teku.bls.BLSPublicKey; | ||
import tech.pegasys.teku.bls.BLSSignature; | ||
import tech.pegasys.teku.infrastructure.unsigned.UInt64; | ||
import tech.pegasys.teku.spec.Spec; | ||
import tech.pegasys.teku.spec.constants.Domain; | ||
import tech.pegasys.teku.spec.datastructures.execution.versions.electra.DepositReceipt; | ||
import tech.pegasys.teku.spec.datastructures.operations.DepositMessage; | ||
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState; | ||
import tech.pegasys.teku.spec.logic.common.helpers.MiscHelpers; | ||
import tech.pegasys.teku.spec.schemas.SchemaDefinitionsElectra; | ||
|
||
public class DepositReceiptsUtil { | ||
|
||
private static final int MAX_NUMBER_OF_DEPOSITS_PER_BLOCK = 3; | ||
|
||
private final Spec spec; | ||
|
||
@SuppressWarnings("DoNotCreateSecureRandomDirectly") | ||
private final SecureRandom random = new SecureRandom(); | ||
|
||
public DepositReceiptsUtil(final Spec spec) { | ||
this.spec = spec; | ||
} | ||
|
||
public List<DepositReceipt> generateDepositReceipts(final BeaconState state) { | ||
final UInt64 nextDepositReceiptIndex = UInt64.valueOf(state.getValidators().size()); | ||
return IntStream.range(0, getNumberOfDepositReceiptsToGenerate()) | ||
.mapToObj(i -> createDepositReceipt(state.getSlot(), nextDepositReceiptIndex.plus(i))) | ||
.toList(); | ||
} | ||
|
||
private int getNumberOfDepositReceiptsToGenerate() { | ||
return random.nextInt(MAX_NUMBER_OF_DEPOSITS_PER_BLOCK + 1); | ||
} | ||
|
||
private DepositReceipt createDepositReceipt(final UInt64 slot, final UInt64 index) { | ||
final BLSKeyPair validatorKeyPair = BLSKeyPair.random(random); | ||
final BLSPublicKey publicKey = validatorKeyPair.getPublicKey(); | ||
final UInt64 depositAmount = UInt64.THIRTY_TWO_ETH; | ||
final DepositMessage depositMessage = | ||
new DepositMessage(publicKey, Bytes32.ZERO, depositAmount); | ||
final MiscHelpers miscHelpers = spec.atSlot(slot).miscHelpers(); | ||
final Bytes32 depositDomain = miscHelpers.computeDomain(Domain.DEPOSIT); | ||
final BLSSignature signature = | ||
BLS.sign( | ||
validatorKeyPair.getSecretKey(), | ||
miscHelpers.computeSigningRoot(depositMessage, depositDomain)); | ||
return SchemaDefinitionsElectra.required(spec.atSlot(slot).getSchemaDefinitions()) | ||
.getDepositReceiptSchema() | ||
.create(publicKey, Bytes32.ZERO, depositAmount, signature, index); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters