Search in sources :

Example 1 with DepositData

use of tech.pegasys.teku.spec.datastructures.operations.DepositData in project teku by ConsenSys.

the class MockStartBeaconStateGenerator method createInitialBeaconState.

public BeaconState createInitialBeaconState(final UInt64 genesisTime, final List<DepositData> initialDepositData, final Optional<ExecutionPayloadHeader> payloadHeader) {
    final List<DepositWithIndex> deposits = new ArrayList<>();
    for (int index = 0; index < initialDepositData.size(); index++) {
        final DepositData data = initialDepositData.get(index);
        DepositWithIndex deposit = new DepositWithIndex(data, UInt64.valueOf(index));
        deposits.add(deposit);
    }
    final BeaconState initialState = spec.initializeBeaconStateFromEth1(BLOCK_HASH, genesisTime, deposits, payloadHeader);
    return initialState.updated(state -> state.setGenesis_time(genesisTime));
}
Also used : DepositData(tech.pegasys.teku.spec.datastructures.operations.DepositData) DepositWithIndex(tech.pegasys.teku.spec.datastructures.operations.DepositWithIndex) ArrayList(java.util.ArrayList) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)

Example 2 with DepositData

use of tech.pegasys.teku.spec.datastructures.operations.DepositData in project teku by ConsenSys.

the class DataStructureUtil method randomDepositData.

public DepositData randomDepositData() {
    BLSKeyPair keyPair = BLSTestUtil.randomKeyPair(nextSeed());
    BLSPublicKey pubkey = keyPair.getPublicKey();
    Bytes32 withdrawal_credentials = randomBytes32();
    DepositMessage proof_of_possession_data = new DepositMessage(pubkey, withdrawal_credentials, getMaxEffectiveBalance());
    final Bytes32 domain = computeDomain();
    final Bytes signing_root = getSigningRoot(proof_of_possession_data, domain);
    BLSSignature proof_of_possession = BLS.sign(keyPair.getSecretKey(), signing_root);
    return new DepositData(proof_of_possession_data, proof_of_possession);
}
Also used : DepositData(tech.pegasys.teku.spec.datastructures.operations.DepositData) DepositMessage(tech.pegasys.teku.spec.datastructures.operations.DepositMessage) Bytes(org.apache.tuweni.bytes.Bytes) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) BLSKeyPair(tech.pegasys.teku.bls.BLSKeyPair) Bytes32(org.apache.tuweni.bytes.Bytes32) BLSSignature(tech.pegasys.teku.bls.BLSSignature)

Example 3 with DepositData

use of tech.pegasys.teku.spec.datastructures.operations.DepositData in project teku by ConsenSys.

the class DataStructureUtil method newDeposits.

public List<DepositWithIndex> newDeposits(int numDeposits) {
    List<DepositWithIndex> deposits = new ArrayList<>();
    final DepositGenerator depositGenerator = new DepositGenerator(spec);
    for (int i = 0; i < numDeposits; i++) {
        BLSKeyPair keypair = BLSTestUtil.randomKeyPair(i);
        DepositData depositData = depositGenerator.createDepositData(keypair, getMaxEffectiveBalance(), keypair.getPublicKey());
        DepositWithIndex deposit = new DepositWithIndex(Deposit.SSZ_SCHEMA.getProofSchema().getDefault(), depositData, UInt64.valueOf(i));
        deposits.add(deposit);
    }
    return deposits;
}
Also used : DepositData(tech.pegasys.teku.spec.datastructures.operations.DepositData) DepositGenerator(tech.pegasys.teku.spec.datastructures.util.DepositGenerator) DepositWithIndex(tech.pegasys.teku.spec.datastructures.operations.DepositWithIndex) ArrayList(java.util.ArrayList) BLSKeyPair(tech.pegasys.teku.bls.BLSKeyPair) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) AnchorPoint(tech.pegasys.teku.spec.datastructures.state.AnchorPoint)

Example 4 with DepositData

use of tech.pegasys.teku.spec.datastructures.operations.DepositData in project teku by ConsenSys.

the class GenesisGeneratorTest method shouldActivateToppedUpValidator.

@Test
public void shouldActivateToppedUpValidator() {
    MockStartDepositGenerator mockStartDepositGenerator = new MockStartDepositGenerator(spec, new DepositGenerator(spec, true));
    DepositData PARTIAL_DEPOSIT_DATA = mockStartDepositGenerator.createDeposits(VALIDATOR_KEYS.subList(0, 1), UInt64.valueOf(1000000000L)).get(0);
    DepositData TOP_UP_DEPOSIT_DATA = mockStartDepositGenerator.createDeposits(VALIDATOR_KEYS.subList(0, 1), UInt64.valueOf(31000000000L)).get(0);
    List<DepositData> INITIAL_DEPOSIT_DATA = List.of(PARTIAL_DEPOSIT_DATA, TOP_UP_DEPOSIT_DATA);
    List<Deposit> INITIAL_DEPOSITS = IntStream.range(0, INITIAL_DEPOSIT_DATA.size()).mapToObj(index -> {
        final DepositData data = INITIAL_DEPOSIT_DATA.get(index);
        return new DepositWithIndex(data, UInt64.valueOf(index));
    }).collect(toList());
    genesisGenerator.updateCandidateState(Bytes32.ZERO, UInt64.ZERO, INITIAL_DEPOSITS);
    final BeaconState state = genesisGenerator.getGenesisState();
    Assertions.<Integer>assertThat(spec.getActiveValidatorIndices(state, GENESIS_EPOCH)).hasSize(1);
    assertThat(genesisGenerator.getActiveValidatorCount()).isEqualTo(1);
}
Also used : DepositData(tech.pegasys.teku.spec.datastructures.operations.DepositData) IntStream(java.util.stream.IntStream) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BLSKeyPair(tech.pegasys.teku.bls.BLSKeyPair) SpecVersion(tech.pegasys.teku.spec.SpecVersion) ArrayList(java.util.ArrayList) ExecutionPayloadHeader(tech.pegasys.teku.spec.datastructures.execution.ExecutionPayloadHeader) MockStartDepositGenerator(tech.pegasys.teku.spec.datastructures.interop.MockStartDepositGenerator) Assertions(org.assertj.core.api.Assertions) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Spec(tech.pegasys.teku.spec.Spec) Bytes32(org.apache.tuweni.bytes.Bytes32) Validator(tech.pegasys.teku.spec.datastructures.state.Validator) BeaconStateBellatrix(tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.bellatrix.BeaconStateBellatrix) BLSSignature(tech.pegasys.teku.bls.BLSSignature) GENESIS_EPOCH(tech.pegasys.teku.spec.config.SpecConfig.GENESIS_EPOCH) Deposit(tech.pegasys.teku.spec.datastructures.operations.Deposit) Test(org.junit.jupiter.api.Test) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) DepositWithIndex(tech.pegasys.teku.spec.datastructures.operations.DepositWithIndex) TestSpecFactory(tech.pegasys.teku.spec.TestSpecFactory) DepositGenerator(tech.pegasys.teku.spec.datastructures.util.DepositGenerator) DataStructureUtil(tech.pegasys.teku.spec.util.DataStructureUtil) Optional(java.util.Optional) Collections(java.util.Collections) BLSKeyGenerator(tech.pegasys.teku.bls.BLSKeyGenerator) DepositData(tech.pegasys.teku.spec.datastructures.operations.DepositData) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) Deposit(tech.pegasys.teku.spec.datastructures.operations.Deposit) MockStartDepositGenerator(tech.pegasys.teku.spec.datastructures.interop.MockStartDepositGenerator) DepositGenerator(tech.pegasys.teku.spec.datastructures.util.DepositGenerator) MockStartDepositGenerator(tech.pegasys.teku.spec.datastructures.interop.MockStartDepositGenerator) DepositWithIndex(tech.pegasys.teku.spec.datastructures.operations.DepositWithIndex) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) Test(org.junit.jupiter.api.Test)

Example 5 with DepositData

use of tech.pegasys.teku.spec.datastructures.operations.DepositData in project teku by ConsenSys.

the class GenesisGeneratorTest method shouldIgnoreInvalidDeposits.

@Test
public void shouldIgnoreInvalidDeposits() {
    List<Deposit> deposits = new ArrayList<>(initialDeposits);
    // Add an invalid deposit at the start with the same key as a later, valid deposit
    final int expectedIndex = 3;
    final DepositData validData = deposits.get(expectedIndex).getData();
    final DepositData invalidData = new DepositData(validData.getPubkey(), validData.getWithdrawal_credentials(), validData.getAmount(), BLSSignature.empty());
    deposits.add(0, new Deposit(invalidData));
    genesisGenerator.updateCandidateState(Bytes32.ZERO, UInt64.ZERO, deposits);
    final BeaconState state = genesisGenerator.getGenesisState();
    // All deposits were processed
    assertThat(state.getEth1_deposit_index()).isEqualTo(UInt64.valueOf(deposits.size()));
    // But one didn't result in a new validator
    assertThat(state.getValidators()).hasSize(deposits.size() - 1);
    assertThat(genesisGenerator.getActiveValidatorCount()).isEqualTo(deposits.size() - 1);
    // And the validator with an invalid deposit should wind up at index 3, not 0 because their
    // first deposit was completely ignored
    final Validator validator = state.getValidators().get(expectedIndex);
    assertThat(validator.getPubkeyBytes()).isEqualTo(validData.getPubkey().toBytesCompressed());
    assertThat(genesisSpec.predicates().isActiveValidator(validator, GENESIS_EPOCH)).isTrue();
}
Also used : DepositData(tech.pegasys.teku.spec.datastructures.operations.DepositData) Deposit(tech.pegasys.teku.spec.datastructures.operations.Deposit) ArrayList(java.util.ArrayList) Validator(tech.pegasys.teku.spec.datastructures.state.Validator) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) Test(org.junit.jupiter.api.Test)

Aggregations

DepositData (tech.pegasys.teku.spec.datastructures.operations.DepositData)17 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)11 Test (org.junit.jupiter.api.Test)8 Bytes32 (org.apache.tuweni.bytes.Bytes32)7 DepositWithIndex (tech.pegasys.teku.spec.datastructures.operations.DepositWithIndex)7 ArrayList (java.util.ArrayList)6 BLSPublicKey (tech.pegasys.teku.bls.BLSPublicKey)6 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)6 BLSSignature (tech.pegasys.teku.bls.BLSSignature)5 Validator (tech.pegasys.teku.spec.datastructures.state.Validator)5 BLSKeyPair (tech.pegasys.teku.bls.BLSKeyPair)4 MockStartDepositGenerator (tech.pegasys.teku.spec.datastructures.interop.MockStartDepositGenerator)4 DepositMessage (tech.pegasys.teku.spec.datastructures.operations.DepositMessage)4 DepositGenerator (tech.pegasys.teku.spec.datastructures.util.DepositGenerator)4 SpecVersion (tech.pegasys.teku.spec.SpecVersion)3 Deposit (tech.pegasys.teku.spec.datastructures.operations.Deposit)3 List (java.util.List)2 Bytes (org.apache.tuweni.bytes.Bytes)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)2