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