use of tech.pegasys.teku.networking.p2p.gossip.PreparedGossipMessageFactory 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.p2p.gossip.PreparedGossipMessageFactory in project teku by ConsenSys.
the class LibP2PGossipNetworkBuilder method createGossip.
protected Gossip createGossip(GossipConfig gossipConfig, boolean gossipLogsEnabled, PreparedGossipMessageFactory defaultMessageFactory, GossipTopicFilter gossipTopicFilter, GossipTopicHandlers topicHandlers) {
final GossipParams gossipParams = LibP2PParamsFactory.createGossipParams(gossipConfig);
final GossipScoreParams scoreParams = LibP2PParamsFactory.createGossipScoreParams(gossipConfig.getScoringConfig());
final TopicSubscriptionFilter subscriptionFilter = new MaxCountTopicSubscriptionFilter(MAX_SUBSCIPTIONS_PER_MESSAGE, MAX_SUBSCRIBED_TOPICS, gossipTopicFilter::isRelevantTopic);
GossipRouter router = new GossipRouter(gossipParams, scoreParams, PubsubProtocol.Gossip_V_1_1, subscriptionFilter) {
final SeenCache<Optional<ValidationResult>> seenCache = new TTLSeenCache<>(new FastIdSeenCache<>(msg -> Bytes.wrap(Hash.sha256(msg.getProtobufMessage().getData().toByteArray()))), gossipParams.getSeenTTL(), getCurTimeMillis());
@NotNull
@Override
protected SeenCache<Optional<ValidationResult>> getSeenMessages() {
return seenCache;
}
};
router.setMessageFactory(msg -> {
Preconditions.checkArgument(msg.getTopicIDsCount() == 1, "Unexpected number of topics for a single message: " + msg.getTopicIDsCount());
String topic = msg.getTopicIDs(0);
Bytes payload = Bytes.wrap(msg.getData().toByteArray());
PreparedGossipMessage preparedMessage = topicHandlers.getHandlerForTopic(topic).map(handler -> handler.prepareMessage(payload)).orElse(defaultMessageFactory.create(topic, payload));
return new PreparedPubsubMessage(msg, preparedMessage);
});
router.setMessageValidator(STRICT_FIELDS_VALIDATOR);
if (gossipLogsEnabled) {
if (debugGossipHandler != null) {
throw new IllegalStateException("Adding more than 1 gossip debug handlers is not implemented yet");
}
debugGossipHandler = new LoggingHandler("wire.gossip", LogLevel.DEBUG);
}
PubsubApi pubsubApi = PubsubApiKt.createPubsubApi(router);
return new Gossip(router, pubsubApi, debugGossipHandler);
}
Aggregations