Search in sources :

Example 1 with GossipEncoding

use of tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding in project teku by ConsenSys.

the class BlockPropagationIntegrationTest method shouldFetchUnknownAncestorsOfPropagatedBlock.

@Test
public void shouldFetchUnknownAncestorsOfPropagatedBlock() throws Exception {
    final GossipEncoding gossipEncoding = GossipEncoding.SSZ_SNAPPY;
    UInt64 currentSlot = SpecConfig.GENESIS_SLOT;
    // Setup node 1
    SyncingNodeManager node1 = SyncingNodeManager.create(asyncRunner, networkFactory, validatorKeys, c -> c.rpcEncoding(rpcEncoding).gossipEncoding(gossipEncoding));
    node1.chainUtil().setSlot(currentSlot);
    // Add some blocks to node1, which node 2 will need to fetch
    final List<SignedBeaconBlock> blocksToFetch = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        currentSlot = currentSlot.plus(UInt64.ONE);
        final SignedBeaconBlock block = node1.chainUtil().createAndImportBlockAtSlot(currentSlot);
        blocksToFetch.add(block);
    }
    // Setup node 2
    SyncingNodeManager node2 = SyncingNodeManager.create(asyncRunner, networkFactory, validatorKeys, c -> c.rpcEncoding(rpcEncoding).gossipEncoding(gossipEncoding));
    // Connect networks
    Waiter.waitFor(node1.connect(node2));
    // Wait for connections to get set up
    Waiter.waitFor(() -> {
        assertThat(node1.network().getPeerCount()).isEqualTo(1);
        assertThat(node2.network().getPeerCount()).isEqualTo(1);
    });
    // Wait for subscriptions to complete (jvm-libp2p does this asynchronously)
    Thread.sleep(2000);
    // Update slot so that blocks can be imported
    currentSlot = currentSlot.plus(UInt64.ONE);
    node1.setSlot(currentSlot);
    node2.setSlot(currentSlot);
    // Propagate new block
    final SignedBeaconBlock newBlock = node1.chainUtil().createBlockAtSlot(currentSlot);
    node1.gossipBlock(newBlock);
    // Verify that node2 fetches required blocks in response
    Waiter.waitFor(() -> {
        for (SignedBeaconBlock block : blocksToFetch) {
            final Bytes32 blockRoot = block.getMessage().hashTreeRoot();
            assertThatSafeFuture(node2.recentChainData().retrieveBlockByRoot(blockRoot)).isCompletedWithNonEmptyOptional();
        }
        // Last block should be imported as well
        final Bytes32 newBlockRoot = newBlock.getMessage().hashTreeRoot();
        assertThatSafeFuture(node2.recentChainData().retrieveBlockByRoot(newBlockRoot)).isCompletedWithNonEmptyOptional();
    });
}
Also used : GossipEncoding(tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding) ArrayList(java.util.ArrayList) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Bytes32(org.apache.tuweni.bytes.Bytes32) Test(org.junit.jupiter.api.Test)

Example 2 with GossipEncoding

use of tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding in project teku by ConsenSys.

the class Eth2P2PNetworkBuilder method buildNetwork.

protected DiscoveryNetwork<?> buildNetwork(final GossipEncoding gossipEncoding, final SubnetSubscriptionService syncCommitteeSubnetService) {
    final ReputationManager reputationManager = new ReputationManager(metricsSystem, timeProvider, Constants.REPUTATION_MANAGER_CAPACITY);
    PreparedGossipMessageFactory defaultMessageFactory = gossipEncoding.createPreparedGossipMessageFactory(recentChainData::getMilestoneByForkDigest);
    final GossipTopicFilter gossipTopicsFilter = new Eth2GossipTopicFilter(recentChainData, gossipEncoding, spec);
    final NetworkConfig networkConfig = config.getNetworkConfig();
    final DiscoveryConfig discoConfig = config.getDiscoveryConfig();
    final P2PNetwork<Peer> p2pNetwork = createLibP2PNetworkBuilder().asyncRunner(asyncRunner).metricsSystem(metricsSystem).config(networkConfig).privateKeyProvider(new LibP2PPrivateKeyLoader(keyValueStore, networkConfig.getPrivateKeySource())).reputationManager(reputationManager).rpcMethods(rpcMethods).peerHandlers(peerHandlers).preparedGossipMessageFactory(defaultMessageFactory).gossipTopicFilter(gossipTopicsFilter).build();
    final AttestationSubnetTopicProvider attestationSubnetTopicProvider = new AttestationSubnetTopicProvider(recentChainData, gossipEncoding);
    final SyncCommitteeSubnetTopicProvider syncCommitteeSubnetTopicProvider = new SyncCommitteeSubnetTopicProvider(recentChainData, gossipEncoding);
    final TargetPeerRange targetPeerRange = new TargetPeerRange(discoConfig.getMinPeers(), discoConfig.getMaxPeers(), discoConfig.getMinRandomlySelectedPeers());
    final SchemaDefinitionsSupplier currentSchemaDefinitions = () -> recentChainData.getCurrentSpec().getSchemaDefinitions();
    return createDiscoveryNetworkBuilder().metricsSystem(metricsSystem).asyncRunner(asyncRunner).kvStore(keyValueStore).p2pNetwork(p2pNetwork).peerSelectionStrategy(new Eth2PeerSelectionStrategy(targetPeerRange, network -> PeerSubnetSubscriptions.create(currentSchemaDefinitions, network, attestationSubnetTopicProvider, syncCommitteeSubnetTopicProvider, syncCommitteeSubnetService, config.getTargetSubnetSubscriberCount()), reputationManager, Collections::shuffle)).discoveryConfig(discoConfig).p2pConfig(networkConfig).spec(config.getSpec()).currentSchemaDefinitionsSupplier(currentSchemaDefinitions).build();
}
Also used : PreparedGossipMessageFactory(tech.pegasys.teku.networking.p2p.gossip.PreparedGossipMessageFactory) Eth2PeerSelectionStrategy(tech.pegasys.teku.networking.eth2.peers.Eth2PeerSelectionStrategy) Peer(tech.pegasys.teku.networking.p2p.peer.Peer) NetworkConfig(tech.pegasys.teku.networking.p2p.network.config.NetworkConfig) AttestationSubnetTopicProvider(tech.pegasys.teku.networking.eth2.gossip.subnets.AttestationSubnetTopicProvider) TargetPeerRange(tech.pegasys.teku.networking.p2p.connection.TargetPeerRange) LibP2PPrivateKeyLoader(tech.pegasys.teku.networking.p2p.libp2p.LibP2PPrivateKeyLoader) Eth2GossipTopicFilter(tech.pegasys.teku.networking.eth2.gossip.topics.Eth2GossipTopicFilter) GossipTopicFilter(tech.pegasys.teku.networking.p2p.libp2p.gossip.GossipTopicFilter) Eth2GossipTopicFilter(tech.pegasys.teku.networking.eth2.gossip.topics.Eth2GossipTopicFilter) SchemaDefinitionsSupplier(tech.pegasys.teku.spec.schemas.SchemaDefinitionsSupplier) SyncCommitteeSubnetTopicProvider(tech.pegasys.teku.networking.eth2.gossip.subnets.SyncCommitteeSubnetTopicProvider) ReputationManager(tech.pegasys.teku.networking.p2p.reputation.ReputationManager) DiscoveryConfig(tech.pegasys.teku.networking.p2p.discovery.DiscoveryConfig) Collections(java.util.Collections)

Example 3 with GossipEncoding

use of tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding in project teku by ConsenSys.

the class GossipForkSubscriptionsPhase0 method addGossipManagers.

protected void addGossipManagers(final ForkInfo forkInfo) {
    AttestationSubnetSubscriptions attestationSubnetSubscriptions = new AttestationSubnetSubscriptions(spec, asyncRunner, discoveryNetwork, gossipEncoding, recentChainData, attestationProcessor, forkInfo, getMessageMaxSize());
    blockGossipManager = new BlockGossipManager(recentChainData, spec, asyncRunner, discoveryNetwork, gossipEncoding, forkInfo, blockProcessor, getMessageMaxSize());
    addGossipManager(blockGossipManager);
    attestationGossipManager = new AttestationGossipManager(metricsSystem, attestationSubnetSubscriptions);
    addGossipManager(attestationGossipManager);
    aggregateGossipManager = new AggregateGossipManager(spec, recentChainData, asyncRunner, discoveryNetwork, gossipEncoding, forkInfo, aggregateProcessor, getMessageMaxSize());
    addGossipManager(aggregateGossipManager);
    voluntaryExitGossipManager = new VoluntaryExitGossipManager(recentChainData, asyncRunner, discoveryNetwork, gossipEncoding, forkInfo, voluntaryExitProcessor, getMessageMaxSize());
    addGossipManager(voluntaryExitGossipManager);
    proposerSlashingGossipManager = new ProposerSlashingGossipManager(recentChainData, asyncRunner, discoveryNetwork, gossipEncoding, forkInfo, proposerSlashingProcessor, getMessageMaxSize());
    addGossipManager(proposerSlashingGossipManager);
    attesterSlashingGossipManager = new AttesterSlashingGossipManager(spec, recentChainData, asyncRunner, discoveryNetwork, gossipEncoding, forkInfo, attesterSlashingProcessor, getMessageMaxSize());
    addGossipManager(attesterSlashingGossipManager);
}
Also used : BlockGossipManager(tech.pegasys.teku.networking.eth2.gossip.BlockGossipManager) AttestationSubnetSubscriptions(tech.pegasys.teku.networking.eth2.gossip.subnets.AttestationSubnetSubscriptions) VoluntaryExitGossipManager(tech.pegasys.teku.networking.eth2.gossip.VoluntaryExitGossipManager) AttestationGossipManager(tech.pegasys.teku.networking.eth2.gossip.AttestationGossipManager) AttesterSlashingGossipManager(tech.pegasys.teku.networking.eth2.gossip.AttesterSlashingGossipManager) AggregateGossipManager(tech.pegasys.teku.networking.eth2.gossip.AggregateGossipManager) ProposerSlashingGossipManager(tech.pegasys.teku.networking.eth2.gossip.ProposerSlashingGossipManager)

Example 4 with GossipEncoding

use of tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding in project teku by ConsenSys.

the class Eth2GossipTopicFilter method computeRelevantTopics.

private Set<String> computeRelevantTopics(final RecentChainData recentChainData, final GossipEncoding gossipEncoding) {
    final ForkInfo forkInfo = recentChainData.getCurrentForkInfo().orElseThrow();
    final Bytes4 forkDigest = forkInfo.getForkDigest(spec);
    final Set<String> topics = getAllTopics(gossipEncoding, forkDigest);
    spec.getForkSchedule().getForks().stream().filter(fork -> fork.getEpoch().isGreaterThanOrEqualTo(forkInfo.getFork().getEpoch())).map(futureFork -> spec.atEpoch(futureFork.getEpoch()).miscHelpers().computeForkDigest(futureFork.getCurrent_version(), forkInfo.getGenesisValidatorsRoot())).forEach(futureForkDigest -> topics.addAll(getAllTopics(gossipEncoding, futureForkDigest)));
    return topics;
}
Also used : Logger(org.apache.logging.log4j.Logger) RecentChainData(tech.pegasys.teku.storage.client.RecentChainData) GossipTopicFilter(tech.pegasys.teku.networking.p2p.libp2p.gossip.GossipTopicFilter) Suppliers(com.google.common.base.Suppliers) Set(java.util.Set) ForkInfo(tech.pegasys.teku.spec.datastructures.state.ForkInfo) GossipTopics.getAllTopics(tech.pegasys.teku.networking.eth2.gossip.topics.GossipTopics.getAllTopics) Bytes4(tech.pegasys.teku.infrastructure.bytes.Bytes4) Spec(tech.pegasys.teku.spec.Spec) Supplier(java.util.function.Supplier) LogManager(org.apache.logging.log4j.LogManager) GossipEncoding(tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding) ForkInfo(tech.pegasys.teku.spec.datastructures.state.ForkInfo) Bytes4(tech.pegasys.teku.infrastructure.bytes.Bytes4)

Example 5 with GossipEncoding

use of tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding in project teku by ConsenSys.

the class GossipMessageHandlerIntegrationTest method shouldNotGossipAttestationsWhenPeerDeregistersFromTopic.

@Test
public void shouldNotGossipAttestationsWhenPeerDeregistersFromTopic() throws Exception {
    final GossipEncoding gossipEncoding = GossipEncoding.SSZ_SNAPPY;
    List<ValidateableAttestation> node2attestations = new ArrayList<>();
    Subscribers<ProcessedAttestationListener> processedAttestationSubscribers = Subscribers.create(false);
    final Consumer<Eth2P2PNetworkBuilder> networkBuilder1 = b -> {
        b.gossipEncoding(gossipEncoding);
        b.gossipedAttestationProcessor((__) -> SafeFuture.completedFuture(InternalValidationResult.ACCEPT));
        b.processedAttestationSubscriptionProvider(processedAttestationSubscribers::subscribe);
    };
    final Consumer<Eth2P2PNetworkBuilder> networkBuilder2 = b -> {
        b.gossipEncoding(gossipEncoding);
        b.gossipedAttestationProcessor((attestation) -> {
            node2attestations.add(attestation);
            return SafeFuture.completedFuture(InternalValidationResult.ACCEPT);
        });
    };
    // Setup network 1
    final NodeManager node1 = createNodeManager(networkBuilder1);
    final Eth2P2PNetwork network1 = node1.network();
    // Setup network 2
    final NodeManager node2 = createNodeManager(networkBuilder2);
    final Eth2P2PNetwork network2 = node2.network();
    // Connect networks 1 -> 2
    waitFor(node1.connect(node2));
    // Wait for connections to get set up
    Waiter.waitFor(() -> {
        assertThat(network1.getPeerCount()).isEqualTo(1);
        assertThat(network2.getPeerCount()).isEqualTo(1);
    });
    // Propagate attestation from network 1
    AttestationGenerator attestationGenerator = new AttestationGenerator(spec, validatorKeys);
    final StateAndBlockSummary bestBlockAndState = getChainHead(node1);
    Attestation attestation = attestationGenerator.validAttestation(bestBlockAndState);
    final int subnetId = spec.computeSubnetForAttestation(bestBlockAndState.getState(), attestation);
    ValidateableAttestation validAttestation = ValidateableAttestation.fromNetwork(spec, attestation, subnetId);
    node1.network().subscribeToAttestationSubnetId(subnetId);
    node2.network().subscribeToAttestationSubnetId(subnetId);
    waitForTopicRegistration();
    processedAttestationSubscribers.forEach(s -> s.accept(validAttestation));
    waitForMessageToBeDelivered();
    assertThat(node2attestations.size()).isEqualTo(1);
    assertThat(node2attestations.get(0)).isEqualTo(validAttestation);
    node1.network().unsubscribeFromAttestationSubnetId(subnetId);
    node2.network().unsubscribeFromAttestationSubnetId(subnetId);
    waitForTopicDeregistration();
    processedAttestationSubscribers.forEach(s -> s.accept(validAttestation));
    ensureConditionRemainsMet(() -> {
        assertThat(node2attestations.size()).isEqualTo(1);
        assertThat(node2attestations.get(0)).isEqualTo(validAttestation);
    });
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) BLSKeyPair(tech.pegasys.teku.bls.BLSKeyPair) Subscribers(tech.pegasys.teku.infrastructure.subscribers.Subscribers) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StateAndBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary) ProcessedAttestationListener(tech.pegasys.teku.spec.datastructures.attestation.ProcessedAttestationListener) AttestationGenerator(tech.pegasys.teku.core.AttestationGenerator) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Spec(tech.pegasys.teku.spec.Spec) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) Waiter.ensureConditionRemainsMet(tech.pegasys.teku.infrastructure.async.Waiter.ensureConditionRemainsMet) Eth2P2PNetworkBuilder(tech.pegasys.teku.networking.eth2.Eth2P2PNetworkFactory.Eth2P2PNetworkBuilder) Set(java.util.Set) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) TestSpecFactory(tech.pegasys.teku.spec.TestSpecFactory) Waiter.waitFor(tech.pegasys.teku.infrastructure.async.Waiter.waitFor) Waiter(tech.pegasys.teku.infrastructure.async.Waiter) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) BLSKeyGenerator(tech.pegasys.teku.bls.BLSKeyGenerator) InternalValidationResult(tech.pegasys.teku.statetransition.validation.InternalValidationResult) SafeFutureAssert.safeJoin(tech.pegasys.teku.infrastructure.async.SafeFutureAssert.safeJoin) GossipEncoding(tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding) StateAndBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary) AttestationGenerator(tech.pegasys.teku.core.AttestationGenerator) Eth2P2PNetworkBuilder(tech.pegasys.teku.networking.eth2.Eth2P2PNetworkFactory.Eth2P2PNetworkBuilder) GossipEncoding(tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding) ArrayList(java.util.ArrayList) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) ProcessedAttestationListener(tech.pegasys.teku.spec.datastructures.attestation.ProcessedAttestationListener) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)13 GossipEncoding (tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding)11 Set (java.util.Set)9 HashSet (java.util.HashSet)8 List (java.util.List)8 Consumer (java.util.function.Consumer)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 AfterEach (org.junit.jupiter.api.AfterEach)8 BLSKeyGenerator (tech.pegasys.teku.bls.BLSKeyGenerator)8 BLSKeyPair (tech.pegasys.teku.bls.BLSKeyPair)8 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)8 Waiter (tech.pegasys.teku.infrastructure.async.Waiter)8 Waiter.waitFor (tech.pegasys.teku.infrastructure.async.Waiter.waitFor)8 Eth2P2PNetworkBuilder (tech.pegasys.teku.networking.eth2.Eth2P2PNetworkFactory.Eth2P2PNetworkBuilder)8 Spec (tech.pegasys.teku.spec.Spec)8 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)8 InternalValidationResult (tech.pegasys.teku.statetransition.validation.InternalValidationResult)8 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)7 TestSpecFactory (tech.pegasys.teku.spec.TestSpecFactory)7 ArrayList (java.util.ArrayList)6