Search in sources :

Example 1 with AttestationGenerator

use of tech.pegasys.teku.core.AttestationGenerator in project teku by ConsenSys.

the class DefaultPerformanceTrackerTest method shouldDisplayIncorrectHeadBlockRoot.

@Test
void shouldDisplayIncorrectHeadBlockRoot() {
    chainUpdater.updateBestBlock(chainUpdater.advanceChainUntil(1));
    ChainBuilder chainBuilderFork = chainBuilder.fork();
    ChainUpdater chainUpdaterFork = new ChainUpdater(storageSystem.recentChainData(), chainBuilderFork);
    chainUpdater.updateBestBlock(chainUpdater.advanceChainUntil(9));
    ChainBuilder.BlockOptions block1Options = ChainBuilder.BlockOptions.create();
    Attestation attestation1 = createAttestation(10, 9);
    block1Options.addAttestation(attestation1);
    SignedBlockAndState blockAndState1 = chainBuilder.generateBlockAtSlot(10, block1Options);
    chainUpdater.saveBlock(blockAndState1);
    chainUpdater.updateBestBlock(blockAndState1);
    SignedBlockAndState blockAndState = chainUpdaterFork.advanceChainUntil(8);
    ChainBuilder.BlockOptions block2Options = ChainBuilder.BlockOptions.create();
    AttestationGenerator attestationGenerator = new AttestationGenerator(spec, chainBuilder.getValidatorKeys());
    Attestation attestation2 = attestationGenerator.validAttestation(blockAndState.toUnsigned(), UInt64.valueOf(9));
    block2Options.addAttestation(attestation2);
    SignedBlockAndState blockAndState2 = chainBuilder.generateBlockAtSlot(11, block2Options);
    chainUpdater.saveBlock(blockAndState2);
    chainUpdater.updateBestBlock(blockAndState2);
    performanceTracker.saveProducedAttestation(attestation1);
    performanceTracker.saveProducedAttestation(attestation2);
    when(validatorTracker.getNumberOfValidatorsForEpoch(any())).thenReturn(2);
    UInt64 slot = spec.computeStartSlotAtEpoch(ATTESTATION_INCLUSION_RANGE.plus(1));
    performanceTracker.onSlot(slot);
    UInt64 attestationEpoch = spec.computeEpochAtSlot(slot).minus(ATTESTATION_INCLUSION_RANGE);
    AttestationPerformance expectedAttestationPerformance = new AttestationPerformance(attestationEpoch, 2, 2, 2, 2, 1, 1.5, 2, 1);
    verify(log).performance(expectedAttestationPerformance.toString());
}
Also used : ChainBuilder(tech.pegasys.teku.core.ChainBuilder) AttestationGenerator(tech.pegasys.teku.core.AttestationGenerator) ChainUpdater(tech.pegasys.teku.storage.client.ChainUpdater) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) Test(org.junit.jupiter.api.Test)

Example 2 with AttestationGenerator

use of tech.pegasys.teku.core.AttestationGenerator in project teku by ConsenSys.

the class Generator method generateBlocks.

@Disabled
@Test
public void generateBlocks() throws Exception {
    final Spec spec = TestSpecFactory.createMainnetAltair();
    AbstractBlockProcessor.BLS_VERIFY_DEPOSIT = false;
    System.out.println("Generating keypairs...");
    int validatorsCount = 32 * 1024;
    List<BLSKeyPair> validatorKeys = BlsKeyPairIO.createReaderForResource("/bls-key-pairs/bls-key-pairs-200k-seed-0.txt.gz").readAll(validatorsCount);
    System.out.println("Keypairs done.");
    RecentChainData localStorage = MemoryOnlyRecentChainData.create(spec);
    BeaconChainUtil localChain = BeaconChainUtil.builder().specProvider(spec).recentChainData(localStorage).validatorKeys(validatorKeys).signDeposits(false).build();
    localChain.initializeStorage();
    AttestationGenerator attestationGenerator = new AttestationGenerator(spec, validatorKeys);
    UInt64 currentSlot = localStorage.getHeadSlot();
    List<Attestation> attestations = Collections.emptyList();
    final int slotsPerEpoch = spec.getGenesisSpecConfig().getSlotsPerEpoch();
    String blocksFile = "blocks_epoch_" + slotsPerEpoch + "_validators_" + validatorsCount + ".ssz.gz";
    try (Writer writer = BlockIO.createFileWriter(blocksFile)) {
        for (int j = 0; j < 50; j++) {
            for (int i = 0; i < slotsPerEpoch; i++) {
                long s = System.currentTimeMillis();
                currentSlot = currentSlot.plus(UInt64.ONE);
                final SignedBeaconBlock block = localChain.createAndImportBlockAtSlotWithAttestations(currentSlot, AttestationGenerator.groupAndAggregateAttestations(attestations));
                writer.accept(block);
                final StateAndBlockSummary postState = localStorage.getStore().retrieveStateAndBlockSummary(block.getMessage().hashTreeRoot()).join().orElseThrow();
                attestations = UInt64.ONE.equals(currentSlot) ? Collections.emptyList() : attestationGenerator.getAttestationsForSlot(postState, currentSlot);
                System.out.println("Processed: " + currentSlot + ", " + getCommittees(spec, postState.getState()) + ", " + (System.currentTimeMillis() - s) + " ms");
            }
            Optional<BeaconState> bestState = localStorage.retrieveBlockState(localStorage.getBestBlockRoot().orElse(null)).join();
            System.out.println("Epoch done: " + bestState);
        }
    }
}
Also used : MemoryOnlyRecentChainData(tech.pegasys.teku.storage.client.MemoryOnlyRecentChainData) RecentChainData(tech.pegasys.teku.storage.client.RecentChainData) StateAndBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary) AttestationGenerator(tech.pegasys.teku.core.AttestationGenerator) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) BLSKeyPair(tech.pegasys.teku.bls.BLSKeyPair) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) BeaconChainUtil(tech.pegasys.teku.statetransition.BeaconChainUtil) Spec(tech.pegasys.teku.spec.Spec) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Writer(tech.pegasys.teku.benchmarks.gen.BlockIO.Writer) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 3 with AttestationGenerator

use of tech.pegasys.teku.core.AttestationGenerator in project teku by ConsenSys.

the class BlockImporterTest method importBlock_validAttestations.

@Test
public void importBlock_validAttestations() throws Exception {
    UInt64 currentSlot = UInt64.ONE;
    SignedBeaconBlock block1 = localChain.createAndImportBlockAtSlot(currentSlot);
    currentSlot = currentSlot.plus(UInt64.ONE);
    AttestationGenerator attestationGenerator = new AttestationGenerator(spec, validatorKeys);
    final StateAndBlockSummary stateAndBlock = recentChainData.getStore().retrieveStateAndBlockSummary(block1.getRoot()).join().orElseThrow();
    List<Attestation> attestations = attestationGenerator.getAttestationsForSlot(stateAndBlock, currentSlot);
    List<Attestation> aggregatedAttestations = AttestationGenerator.groupAndAggregateAttestations(attestations);
    currentSlot = currentSlot.plus(UInt64.ONE);
    localChain.createAndImportBlockAtSlotWithAttestations(currentSlot, aggregatedAttestations);
}
Also used : StateAndBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary) AttestationGenerator(tech.pegasys.teku.core.AttestationGenerator) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) Test(org.junit.jupiter.api.Test)

Example 4 with AttestationGenerator

use of tech.pegasys.teku.core.AttestationGenerator in project teku by ConsenSys.

the class BlockImporterTest method importBlock_attestationWithInvalidSignature.

@Test
public void importBlock_attestationWithInvalidSignature() throws Exception {
    UInt64 currentSlot = UInt64.ONE;
    SignedBeaconBlock block1 = localChain.createAndImportBlockAtSlot(currentSlot);
    currentSlot = currentSlot.plus(UInt64.ONE);
    AttestationGenerator attestationGenerator = new AttestationGenerator(spec, validatorKeys);
    final StateAndBlockSummary stateAndBlock = recentChainData.getStore().retrieveStateAndBlockSummary(block1.getRoot()).join().orElseThrow();
    List<Attestation> attestations = attestationGenerator.getAttestationsForSlot(stateAndBlock, currentSlot);
    List<Attestation> aggregatedAttestations = AttestationGenerator.groupAndAggregateAttestations(attestations);
    // make one attestation signature invalid
    int invalidAttIdx = aggregatedAttestations.size() / 2;
    Attestation att = aggregatedAttestations.get(invalidAttIdx);
    Attestation invalidAtt = attestationSchema.create(att.getAggregationBits(), att.getData(), BLSTestUtil.randomSignature(1));
    aggregatedAttestations.set(invalidAttIdx, invalidAtt);
    UInt64 currentSlotFinal = currentSlot.plus(UInt64.ONE);
    assertThatCode(() -> {
        localChain.createAndImportBlockAtSlotWithAttestations(currentSlotFinal, aggregatedAttestations);
    }).hasMessageContaining("signature");
}
Also used : StateAndBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary) AttestationGenerator(tech.pegasys.teku.core.AttestationGenerator) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) Test(org.junit.jupiter.api.Test)

Example 5 with AttestationGenerator

use of tech.pegasys.teku.core.AttestationGenerator in project teku by ConsenSys.

the class BlockImporterTest method importBlock_wrongChain.

@Test
public void importBlock_wrongChain() throws Exception {
    UInt64 currentSlot = recentChainData.getHeadSlot();
    for (int i = 0; i < 3; i++) {
        currentSlot = currentSlot.plus(UInt64.ONE);
        localChain.createAndImportBlockAtSlot(currentSlot);
    }
    // Update finalized epoch
    final StoreTransaction tx = recentChainData.startStoreTransaction();
    final Bytes32 finalizedRoot = recentChainData.getBestBlockRoot().orElseThrow();
    final UInt64 finalizedEpoch = UInt64.ONE;
    final Checkpoint finalized = new Checkpoint(finalizedEpoch, finalizedRoot);
    tx.setFinalizedCheckpoint(finalized);
    tx.commit().join();
    // Now create a new block that is not descendant from the finalized block
    AttestationGenerator attestationGenerator = new AttestationGenerator(spec, validatorKeys);
    final StateAndBlockSummary blockAndState = safeJoin(otherStorage.getChainHead().orElseThrow().asStateAndBlockSummary());
    final Attestation attestation = attestationGenerator.validAttestation(blockAndState);
    final SignedBeaconBlock block = otherChain.createAndImportBlockAtSlotWithAttestations(currentSlot, List.of(attestation));
    final BlockImportResult result = blockImporter.importBlock(block).get();
    assertImportFailed(result, FailureReason.UNKNOWN_PARENT);
}
Also used : Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) StateAndBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary) AttestationGenerator(tech.pegasys.teku.core.AttestationGenerator) StoreTransaction(tech.pegasys.teku.storage.store.UpdatableStore.StoreTransaction) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Bytes32(org.apache.tuweni.bytes.Bytes32) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) BlockImportResult(tech.pegasys.teku.spec.logic.common.statetransition.results.BlockImportResult) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) Test(org.junit.jupiter.api.Test)

Aggregations

AttestationGenerator (tech.pegasys.teku.core.AttestationGenerator)14 Test (org.junit.jupiter.api.Test)13 StateAndBlockSummary (tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary)12 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)9 Attestation (tech.pegasys.teku.spec.datastructures.operations.Attestation)9 ValidateableAttestation (tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation)7 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)7 InternalValidationResult (tech.pegasys.teku.statetransition.validation.InternalValidationResult)7 ValidationResult (io.libp2p.core.pubsub.ValidationResult)4 Bytes (org.apache.tuweni.bytes.Bytes)4 BLSKeyPair (tech.pegasys.teku.bls.BLSKeyPair)4 Spec (tech.pegasys.teku.spec.Spec)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Set (java.util.Set)3 Consumer (java.util.function.Consumer)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 AfterEach (org.junit.jupiter.api.AfterEach)3 BLSKeyGenerator (tech.pegasys.teku.bls.BLSKeyGenerator)3