use of tech.pegasys.teku.spec.datastructures.blocks.Eth1Data in project teku by ConsenSys.
the class Eth1DataCacheTest method smallestDistanceWinsIfNoMajority.
@Test
void smallestDistanceWinsIfNoMajority() {
Eth1Data eth1Data1 = createEth1Data(STATE_DEPOSIT_COUNT);
Eth1Data eth1Data2 = createEth1Data(STATE_DEPOSIT_COUNT);
BeaconState beaconState = createBeaconStateWithVotes(eth1Data1, eth1Data2);
eth1DataCache.onBlockWithDeposit(IN_RANGE_TIMESTAMP_1, eth1Data1);
eth1DataCache.onBlockWithDeposit(IN_RANGE_TIMESTAMP_2, eth1Data2);
assertThat(eth1DataCache.getEth1Vote(beaconState)).isEqualTo(eth1Data1);
}
use of tech.pegasys.teku.spec.datastructures.blocks.Eth1Data in project teku by ConsenSys.
the class DepositProvider method onDepositsFromBlock.
@Override
public synchronized void onDepositsFromBlock(DepositsFromBlockEvent event) {
event.getDeposits().stream().map(depositUtil::convertDepositEventToOperationDeposit).forEach(deposit -> {
if (!recentChainData.isPreGenesis()) {
LOG.debug("About to process deposit: {}", deposit.getIndex());
}
depositNavigableMap.put(deposit.getIndex(), deposit);
depositMerkleTree.add(deposit.getData().hashTreeRoot());
});
depositCounter.inc(event.getDeposits().size());
eth1DataCache.onBlockWithDeposit(event.getBlockTimestamp(), new Eth1Data(depositMerkleTree.getRoot(), UInt64.valueOf(depositMerkleTree.getNumberOfLeaves()), event.getBlockHash()));
}
use of tech.pegasys.teku.spec.datastructures.blocks.Eth1Data 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.spec.datastructures.blocks.Eth1Data in project teku by ConsenSys.
the class Eth1DataCache method updateMetrics.
public void updateMetrics(final BeaconState state) {
final Eth1Data currentEth1Data = state.getEth1_data();
// Avoid using .values() directly as it has O(n) lookup which gets expensive fast
final Set<Eth1Data> knownBlocks = new HashSet<>(getVotesToConsider(state.getSlot(), state.getGenesis_time(), currentEth1Data).values());
Map<Eth1Data, Eth1Vote> votes = countVotes(state);
currentPeriodVotesMax.set(eth1VotingPeriod.getTotalSlotsInVotingPeriod(state.getSlot()));
currentPeriodVotesTotal.set(state.getEth1_data_votes().size());
currentPeriodVotesUnknown.set(votes.keySet().stream().filter(votedBlock -> !knownBlocks.contains(votedBlock)).count());
currentPeriodVotesCurrent.set(votes.getOrDefault(currentEth1Data, new Eth1Vote(0)).getVoteCount());
currentPeriodVotesBest.set(votes.values().stream().max(Comparator.naturalOrder()).map(Eth1Vote::getVoteCount).orElse(0));
}
use of tech.pegasys.teku.spec.datastructures.blocks.Eth1Data in project teku by ConsenSys.
the class Eth1DataCache method onEth1Block.
public void onEth1Block(final Bytes32 blockHash, final UInt64 blockTimestamp) {
final Map.Entry<UInt64, Eth1Data> previousBlock = eth1ChainCache.floorEntry(blockTimestamp);
final Eth1Data data;
if (previousBlock == null) {
data = new Eth1Data(Eth1Data.EMPTY_DEPOSIT_ROOT, UInt64.ZERO, blockHash);
} else {
data = previousBlock.getValue().withBlockHash(blockHash);
}
eth1ChainCache.put(blockTimestamp, data);
prune(blockTimestamp);
}
Aggregations