Search in sources :

Example 1 with SszList

use of tech.pegasys.teku.infrastructure.ssz.SszList 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 SszList

use of tech.pegasys.teku.infrastructure.ssz.SszList in project teku by ConsenSys.

the class BeaconStateUtil method getAttestersTotalEffectiveBalance.

public UInt64 getAttestersTotalEffectiveBalance(final BeaconState state, final UInt64 slot) {
    beaconStateAccessors.validateStateForCommitteeQuery(state, slot);
    return BeaconStateCache.getTransitionCaches(state).getAttestersTotalBalance().get(slot, p -> {
        final SszList<Validator> validators = state.getValidators();
        final UInt64 committeeCount = beaconStateAccessors.getCommitteeCountPerSlot(state, miscHelpers.computeEpochAtSlot(slot));
        return UInt64.range(UInt64.ZERO, committeeCount).flatMap(committee -> streamEffectiveBalancesForCommittee(state, slot, committee)).reduce(UInt64.ZERO, UInt64::plus);
    });
}
Also used : MiscHelpers(tech.pegasys.teku.spec.logic.common.helpers.MiscHelpers) ATTESTATION_SUBNET_COUNT(tech.pegasys.teku.spec.config.Constants.ATTESTATION_SUBNET_COUNT) SpecConfig(tech.pegasys.teku.spec.config.SpecConfig) ReadOnlyForkChoiceStrategy(tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyForkChoiceStrategy) SchemaDefinitions(tech.pegasys.teku.spec.schemas.SchemaDefinitions) SszList(tech.pegasys.teku.infrastructure.ssz.SszList) BeaconStateAccessors(tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors) BeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.BeaconBlock) MinimalBeaconBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.MinimalBeaconBlockSummary) Collectors.toUnmodifiableList(java.util.stream.Collectors.toUnmodifiableList) Predicates(tech.pegasys.teku.spec.logic.common.helpers.Predicates) List(java.util.List) SszBitvector(tech.pegasys.teku.infrastructure.ssz.collections.SszBitvector) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) Optional(java.util.Optional) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) BeaconStateCache(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateCache) LogManager(org.apache.logging.log4j.LogManager) Bytes32(org.apache.tuweni.bytes.Bytes32) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) Validator(tech.pegasys.teku.spec.datastructures.state.Validator) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Validator(tech.pegasys.teku.spec.datastructures.state.Validator)

Example 3 with SszList

use of tech.pegasys.teku.infrastructure.ssz.SszList in project teku by ConsenSys.

the class AbstractValidatorStatusFactory method createValidatorStatuses.

@Override
public ValidatorStatuses createValidatorStatuses(final BeaconState state) {
    final SszList<Validator> validators = state.getValidators();
    final UInt64 currentEpoch = beaconStateAccessors.getCurrentEpoch(state);
    final UInt64 previousEpoch = beaconStateAccessors.getPreviousEpoch(state);
    final List<ValidatorStatus> statuses = validators.stream().map(validator -> createValidatorStatus(validator, previousEpoch, currentEpoch)).collect(Collectors.toCollection(() -> new ArrayList<>(validators.size())));
    processParticipation(statuses, state, previousEpoch, currentEpoch);
    final TotalBalances totalBalances = createTotalBalances(statuses);
    BeaconStateCache.getTransitionCaches(state).setLatestTotalBalances(totalBalances);
    BeaconStateCache.getTransitionCaches(state).getTotalActiveBalance().get(currentEpoch, __ -> totalBalances.getCurrentEpochActiveValidators());
    return new ValidatorStatuses(statuses, totalBalances);
}
Also used : SpecConfig(tech.pegasys.teku.spec.config.SpecConfig) SszList(tech.pegasys.teku.infrastructure.ssz.SszList) BeaconStateAccessors(tech.pegasys.teku.spec.logic.common.helpers.BeaconStateAccessors) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Predicates(tech.pegasys.teku.spec.logic.common.helpers.Predicates) List(java.util.List) AttestationUtil(tech.pegasys.teku.spec.logic.common.util.AttestationUtil) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) BeaconStateCache(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconStateCache) BeaconStateUtil(tech.pegasys.teku.spec.logic.common.util.BeaconStateUtil) Bytes32(org.apache.tuweni.bytes.Bytes32) Validator(tech.pegasys.teku.spec.datastructures.state.Validator) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) ArrayList(java.util.ArrayList) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Validator(tech.pegasys.teku.spec.datastructures.state.Validator)

Example 4 with SszList

use of tech.pegasys.teku.infrastructure.ssz.SszList 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)

Example 5 with SszList

use of tech.pegasys.teku.infrastructure.ssz.SszList in project teku by ConsenSys.

the class AggregatingAttestationPool method getAttestationsForBlock.

public synchronized SszList<Attestation> getAttestationsForBlock(final BeaconState stateAtBlockSlot, final AttestationForkChecker forkChecker, final AttestationWorthinessChecker worthinessChecker) {
    final UInt64 currentEpoch = spec.getCurrentEpoch(stateAtBlockSlot);
    final int previousEpochLimit = spec.getPreviousEpochAttestationCapacity(stateAtBlockSlot);
    final SszListSchema<Attestation, ?> attestationsSchema = spec.atSlot(stateAtBlockSlot.getSlot()).getSchemaDefinitions().getBeaconBlockBodySchema().getAttestationsSchema();
    final AtomicInteger prevEpochCount = new AtomicInteger(0);
    return dataHashBySlot.descendingMap().values().stream().flatMap(Collection::stream).map(attestationGroupByDataHash::get).filter(Objects::nonNull).filter(group -> isValid(stateAtBlockSlot, group.getAttestationData())).filter(forkChecker::areAttestationsFromCorrectFork).filter(group -> worthinessChecker.areAttestationsWorthy(group.getAttestationData())).flatMap(MatchingDataAttestationGroup::stream).limit(attestationsSchema.getMaxLength()).map(ValidateableAttestation::getAttestation).filter(att -> {
        if (spec.computeEpochAtSlot(att.getData().getSlot()).isLessThan(currentEpoch)) {
            final int currentCount = prevEpochCount.getAndIncrement();
            return currentCount < previousEpochLimit;
        }
        return true;
    }).collect(attestationsSchema.collector());
}
Also used : SszList(tech.pegasys.teku.infrastructure.ssz.SszList) HashMap(java.util.HashMap) Bytes(org.apache.tuweni.bytes.Bytes) HashSet(java.util.HashSet) SettableGauge(tech.pegasys.teku.infrastructure.metrics.SettableGauge) AttestationData(tech.pegasys.teku.spec.datastructures.operations.AttestationData) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) SszListSchema(tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Spec(tech.pegasys.teku.spec.Spec) Bytes32(org.apache.tuweni.bytes.Bytes32) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) SlotEventsChannel(tech.pegasys.teku.ethereum.events.SlotEventsChannel) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) NavigableMap(java.util.NavigableMap) AttestationWorthinessChecker(tech.pegasys.teku.spec.logic.common.statetransition.attestation.AttestationWorthinessChecker) Objects(java.util.Objects) Stream(java.util.stream.Stream) TekuMetricCategory(tech.pegasys.teku.infrastructure.metrics.TekuMetricCategory) TreeMap(java.util.TreeMap) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) Optional(java.util.Optional) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Objects(java.util.Objects) Collection(java.util.Collection) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation)

Aggregations

Bytes32 (org.apache.tuweni.bytes.Bytes32)5 SszList (tech.pegasys.teku.infrastructure.ssz.SszList)5 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)5 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)5 List (java.util.List)3 SszListSchema (tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema)3 Spec (tech.pegasys.teku.spec.Spec)3 SpecConfig (tech.pegasys.teku.spec.config.SpecConfig)3 Validator (tech.pegasys.teku.spec.datastructures.state.Validator)3 ArrayList (java.util.ArrayList)2 NavigableMap (java.util.NavigableMap)2 Optional (java.util.Optional)2 TreeMap (java.util.TreeMap)2 Stream (java.util.stream.Stream)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 Bytes (org.apache.tuweni.bytes.Bytes)2 MetricsSystem (org.hyperledger.besu.plugin.services.MetricsSystem)2 SszBytes32Vector (tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector)2 Eth1Data (tech.pegasys.teku.spec.datastructures.blocks.Eth1Data)2