Search in sources :

Example 1 with Deposit

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

the class DepositProvider method getDepositsWithProof.

/**
 * @param fromDepositIndex inclusive
 * @param toDepositIndex exclusive
 * @param eth1DepositCount number of deposits in the merkle tree according to Eth1Data in state
 * @return
 */
private SszList<Deposit> getDepositsWithProof(UInt64 fromDepositIndex, UInt64 toDepositIndex, UInt64 eth1DepositCount, long maxDeposits) {
    final AtomicReference<UInt64> expectedDepositIndex = new AtomicReference<>(fromDepositIndex);
    SszListSchema<Deposit, ?> depositsSchema = depositsSchemaCache.get(maxDeposits);
    return depositNavigableMap.subMap(fromDepositIndex, true, toDepositIndex, false).values().stream().map(deposit -> {
        if (!deposit.getIndex().equals(expectedDepositIndex.get())) {
            throw MissingDepositsException.missingRange(expectedDepositIndex.get(), deposit.getIndex());
        }
        expectedDepositIndex.set(deposit.getIndex().plus(ONE));
        SszBytes32Vector proof = Deposit.SSZ_SCHEMA.getProofSchema().of(depositMerkleTree.getProofWithViewBoundary(deposit.getIndex().intValue(), eth1DepositCount.intValue()));
        return new DepositWithIndex(proof, deposit.getData(), deposit.getIndex());
    }).collect(depositsSchema.collector());
}
Also used : SszBytes32Vector(tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector) SszList(tech.pegasys.teku.infrastructure.ssz.SszList) MinGenesisTimeBlockEvent(tech.pegasys.teku.ethereum.pow.api.MinGenesisTimeBlockEvent) ONE(tech.pegasys.teku.infrastructure.unsigned.UInt64.ONE) AtomicReference(java.util.concurrent.atomic.AtomicReference) Eth1Data(tech.pegasys.teku.spec.datastructures.blocks.Eth1Data) SszListSchema(tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) MerkleTree(tech.pegasys.teku.spec.datastructures.util.MerkleTree) DepositUtil(tech.pegasys.teku.spec.datastructures.util.DepositUtil) DepositsFromBlockEvent(tech.pegasys.teku.ethereum.pow.api.DepositsFromBlockEvent) Spec(tech.pegasys.teku.spec.Spec) FinalizedCheckpointChannel(tech.pegasys.teku.storage.api.FinalizedCheckpointChannel) Bytes32(org.apache.tuweni.bytes.Bytes32) NavigableMap(java.util.NavigableMap) Deposit(tech.pegasys.teku.spec.datastructures.operations.Deposit) DepositWithIndex(tech.pegasys.teku.spec.datastructures.operations.DepositWithIndex) Logger(org.apache.logging.log4j.Logger) TekuMetricCategory(tech.pegasys.teku.infrastructure.metrics.TekuMetricCategory) TreeMap(java.util.TreeMap) RecentChainData(tech.pegasys.teku.storage.client.RecentChainData) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) LogManager(org.apache.logging.log4j.LogManager) Eth1EventsChannel(tech.pegasys.teku.pow.api.Eth1EventsChannel) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) OptimizedMerkleTree(tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree) Counter(org.hyperledger.besu.plugin.services.metrics.Counter) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) Deposit(tech.pegasys.teku.spec.datastructures.operations.Deposit) SszBytes32Vector(tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector) DepositWithIndex(tech.pegasys.teku.spec.datastructures.operations.DepositWithIndex) AtomicReference(java.util.concurrent.atomic.AtomicReference) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64)

Example 2 with Deposit

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

the class DepositProviderTest method mockDepositsFromEth1Block.

private void mockDepositsFromEth1Block(int startIndex, int n) {
    allSeenDepositsList.subList(startIndex, n).stream().map(depositUtil::convertDepositEventToOperationDeposit).map(Deposit::getData).map(DepositData::hashTreeRoot).forEachOrdered(depositMerkleTree::add);
    DepositsFromBlockEvent depositsFromBlockEvent = mock(DepositsFromBlockEvent.class);
    when(depositsFromBlockEvent.getDeposits()).thenReturn(allSeenDepositsList.subList(startIndex, startIndex + n));
    when(depositsFromBlockEvent.getBlockHash()).thenReturn(Bytes32.ZERO);
    depositProvider.onDepositsFromBlock(depositsFromBlockEvent);
}
Also used : Deposit(tech.pegasys.teku.spec.datastructures.operations.Deposit) DepositsFromBlockEvent(tech.pegasys.teku.ethereum.pow.api.DepositsFromBlockEvent)

Example 3 with Deposit

use of tech.pegasys.teku.spec.datastructures.operations.Deposit 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 4 with Deposit

use of tech.pegasys.teku.spec.datastructures.operations.Deposit 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)

Example 5 with Deposit

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

the class BlockProcessorTest method processDepositHelper.

private BeaconState processDepositHelper(BeaconState beaconState, DepositData depositData) throws BlockProcessingException {
    // Add the deposit to a Merkle tree so that we can get the root to put into the state Eth1 data
    MerkleTree depositMerkleTree = new OptimizedMerkleTree(specConfig.getDepositContractTreeDepth());
    depositMerkleTree.add(depositData.hashTreeRoot());
    beaconState = beaconState.updated(state -> state.setEth1_data(new Eth1Data(depositMerkleTree.getRoot(), UInt64.valueOf(1), Bytes32.ZERO)));
    SszListSchema<Deposit, ?> schema = SszListSchema.create(DepositWithIndex.SSZ_SCHEMA, specConfig.getMaxDeposits());
    SszBytes32Vector proof = Deposit.SSZ_SCHEMA.getProofSchema().of(depositMerkleTree.getProof(0));
    SszList<Deposit> deposits = schema.of(new DepositWithIndex(proof, depositData, UInt64.valueOf(0)));
    // Attempt to process deposit with above data.
    return beaconState.updated(state -> blockProcessor.processDeposits(state, deposits));
}
Also used : BouncyCastleExtension(org.apache.tuweni.junit.BouncyCastleExtension) SszBytes32Vector(tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SszList(tech.pegasys.teku.infrastructure.ssz.SszList) Bytes(org.apache.tuweni.bytes.Bytes) Fork(tech.pegasys.teku.spec.datastructures.state.Fork) SpecVersion(tech.pegasys.teku.spec.SpecVersion) ArrayList(java.util.ArrayList) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Eth1Data(tech.pegasys.teku.spec.datastructures.blocks.Eth1Data) SszListSchema(tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) MerkleTree(tech.pegasys.teku.spec.datastructures.util.MerkleTree) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Spec(tech.pegasys.teku.spec.Spec) Bytes32(org.apache.tuweni.bytes.Bytes32) BlockProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException) Validator(tech.pegasys.teku.spec.datastructures.state.Validator) BLSSignature(tech.pegasys.teku.bls.BLSSignature) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) SpecConfig(tech.pegasys.teku.spec.config.SpecConfig) Deposit(tech.pegasys.teku.spec.datastructures.operations.Deposit) Test(org.junit.jupiter.api.Test) List(java.util.List) DepositWithIndex(tech.pegasys.teku.spec.datastructures.operations.DepositWithIndex) DataStructureUtil(tech.pegasys.teku.spec.util.DataStructureUtil) Bytes48(org.apache.tuweni.bytes.Bytes48) DepositMessage(tech.pegasys.teku.spec.datastructures.operations.DepositMessage) DepositData(tech.pegasys.teku.spec.datastructures.operations.DepositData) OptimizedMerkleTree(tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) OptimizedMerkleTree(tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree) Deposit(tech.pegasys.teku.spec.datastructures.operations.Deposit) MerkleTree(tech.pegasys.teku.spec.datastructures.util.MerkleTree) OptimizedMerkleTree(tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree) SszBytes32Vector(tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector) DepositWithIndex(tech.pegasys.teku.spec.datastructures.operations.DepositWithIndex) Eth1Data(tech.pegasys.teku.spec.datastructures.blocks.Eth1Data)

Aggregations

Deposit (tech.pegasys.teku.spec.datastructures.operations.Deposit)15 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)10 Bytes32 (org.apache.tuweni.bytes.Bytes32)8 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)7 Test (org.junit.jupiter.api.Test)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Bytes (org.apache.tuweni.bytes.Bytes)4 Spec (tech.pegasys.teku.spec.Spec)4 Eth1Data (tech.pegasys.teku.spec.datastructures.blocks.Eth1Data)4 DepositData (tech.pegasys.teku.spec.datastructures.operations.DepositData)4 DepositWithIndex (tech.pegasys.teku.spec.datastructures.operations.DepositWithIndex)4 Validator (tech.pegasys.teku.spec.datastructures.state.Validator)4 Optional (java.util.Optional)3 IntStream (java.util.stream.IntStream)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 BLSSignature (tech.pegasys.teku.bls.BLSSignature)3 SszList (tech.pegasys.teku.infrastructure.ssz.SszList)3 BlockProcessingException (tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException)3 DataStructureUtil (tech.pegasys.teku.spec.util.DataStructureUtil)3