Search in sources :

Example 1 with Eth1Data

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);
}
Also used : Eth1Data(tech.pegasys.teku.spec.datastructures.blocks.Eth1Data) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) Test(org.junit.jupiter.api.Test)

Example 2 with Eth1Data

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()));
}
Also used : Eth1Data(tech.pegasys.teku.spec.datastructures.blocks.Eth1Data)

Example 3 with Eth1Data

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());
}
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 4 with Eth1Data

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));
}
Also used : Eth1Data(tech.pegasys.teku.spec.datastructures.blocks.Eth1Data) HashSet(java.util.HashSet)

Example 5 with Eth1Data

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);
}
Also used : UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Eth1Data(tech.pegasys.teku.spec.datastructures.blocks.Eth1Data) HashMap(java.util.HashMap) NavigableMap(java.util.NavigableMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) Map(java.util.Map)

Aggregations

Eth1Data (tech.pegasys.teku.spec.datastructures.blocks.Eth1Data)30 Test (org.junit.jupiter.api.Test)19 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)14 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)9 Bytes32 (org.apache.tuweni.bytes.Bytes32)7 ChainBuilder (tech.pegasys.teku.core.ChainBuilder)4 HashSet (java.util.HashSet)3 NavigableMap (java.util.NavigableMap)3 SignedBlockAndState (tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState)3 Deposit (tech.pegasys.teku.spec.datastructures.operations.Deposit)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)2 MetricsSystem (org.hyperledger.besu.plugin.services.MetricsSystem)2 BLSPublicKey (tech.pegasys.teku.bls.BLSPublicKey)2 BlockOptions (tech.pegasys.teku.core.ChainBuilder.BlockOptions)2 DepositsFromBlockEvent (tech.pegasys.teku.ethereum.pow.api.DepositsFromBlockEvent)2 TekuMetricCategory (tech.pegasys.teku.infrastructure.metrics.TekuMetricCategory)2 SszList (tech.pegasys.teku.infrastructure.ssz.SszList)2 SszBytes32Vector (tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector)2