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());
}
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);
});
}
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);
}
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));
}
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());
}
Aggregations