Search in sources :

Example 11 with ValidationResult

use of io.libp2p.core.pubsub.ValidationResult in project teku by ConsenSys.

the class ProposerSlashingTopicHandlerTest method handleMessage_rejectedSlashing.

@Test
public void handleMessage_rejectedSlashing() {
    final ProposerSlashing slashing = dataStructureUtil.randomProposerSlashing();
    when(processor.process(slashing)).thenReturn(SafeFuture.completedFuture(InternalValidationResult.reject("Nope")));
    Bytes serialized = gossipEncoding.encode(slashing);
    final SafeFuture<ValidationResult> result = topicHandler.handleMessage(topicHandler.prepareMessage(serialized));
    asyncRunner.executeQueuedActions();
    assertThat(result).isCompletedWithValue(ValidationResult.Invalid);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) ProposerSlashing(tech.pegasys.teku.spec.datastructures.operations.ProposerSlashing) ValidationResult(io.libp2p.core.pubsub.ValidationResult) InternalValidationResult(tech.pegasys.teku.statetransition.validation.InternalValidationResult) Test(org.junit.jupiter.api.Test)

Example 12 with ValidationResult

use of io.libp2p.core.pubsub.ValidationResult in project teku by ConsenSys.

the class VoluntaryExitTopicHandlerTest method handleMessage_invalidSSZ.

@Test
public void handleMessage_invalidSSZ() {
    Bytes serialized = Bytes.fromHexString("0x1234");
    final ValidationResult result = topicHandler.handleMessage(topicHandler.prepareMessage(serialized)).join();
    assertThat(result).isEqualTo(ValidationResult.Invalid);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) ValidationResult(io.libp2p.core.pubsub.ValidationResult) InternalValidationResult(tech.pegasys.teku.statetransition.validation.InternalValidationResult) Test(org.junit.jupiter.api.Test)

Example 13 with ValidationResult

use of io.libp2p.core.pubsub.ValidationResult in project teku by ConsenSys.

the class VoluntaryExitTopicHandlerTest method handleMessage_validExit.

@Test
public void handleMessage_validExit() {
    final SignedVoluntaryExit exit = exitGenerator.withEpoch(getBestState(), 3, 3);
    when(processor.process(exit)).thenReturn(SafeFuture.completedFuture(InternalValidationResult.ACCEPT));
    Bytes serialized = gossipEncoding.encode(exit);
    final SafeFuture<ValidationResult> result = topicHandler.handleMessage(topicHandler.prepareMessage(serialized));
    asyncRunner.executeQueuedActions();
    assertThat(result).isCompletedWithValue(ValidationResult.Valid);
}
Also used : SignedVoluntaryExit(tech.pegasys.teku.spec.datastructures.operations.SignedVoluntaryExit) Bytes(org.apache.tuweni.bytes.Bytes) ValidationResult(io.libp2p.core.pubsub.ValidationResult) InternalValidationResult(tech.pegasys.teku.statetransition.validation.InternalValidationResult) Test(org.junit.jupiter.api.Test)

Example 14 with ValidationResult

use of io.libp2p.core.pubsub.ValidationResult in project teku by ConsenSys.

the class VoluntaryExitTopicHandlerTest method handleMessage_ignoredExit.

@Test
public void handleMessage_ignoredExit() {
    final SignedVoluntaryExit exit = exitGenerator.withEpoch(getBestState(), 3, 3);
    when(processor.process(exit)).thenReturn(SafeFuture.completedFuture(InternalValidationResult.IGNORE));
    Bytes serialized = gossipEncoding.encode(exit);
    final SafeFuture<ValidationResult> result = topicHandler.handleMessage(topicHandler.prepareMessage(serialized));
    asyncRunner.executeQueuedActions();
    assertThat(result).isCompletedWithValue(ValidationResult.Ignore);
}
Also used : SignedVoluntaryExit(tech.pegasys.teku.spec.datastructures.operations.SignedVoluntaryExit) Bytes(org.apache.tuweni.bytes.Bytes) ValidationResult(io.libp2p.core.pubsub.ValidationResult) InternalValidationResult(tech.pegasys.teku.statetransition.validation.InternalValidationResult) Test(org.junit.jupiter.api.Test)

Example 15 with ValidationResult

use of io.libp2p.core.pubsub.ValidationResult 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);
}
Also used : MAX_SUBSCIPTIONS_PER_MESSAGE(tech.pegasys.teku.networking.p2p.libp2p.config.LibP2PParamsFactory.MAX_SUBSCIPTIONS_PER_MESSAGE) PubsubApi(io.libp2p.core.pubsub.PubsubApi) LoggingHandler(io.netty.handler.logging.LoggingHandler) GossipConfig(tech.pegasys.teku.networking.p2p.gossip.config.GossipConfig) NULL_SEQNO_GENERATOR(tech.pegasys.teku.networking.p2p.libp2p.gossip.LibP2PGossipNetwork.NULL_SEQNO_GENERATOR) GossipParams(io.libp2p.pubsub.gossip.GossipParams) GossipScoreParams(io.libp2p.pubsub.gossip.GossipScoreParams) Bytes(org.apache.tuweni.bytes.Bytes) SeenCache(io.libp2p.pubsub.SeenCache) STRICT_FIELDS_VALIDATOR(tech.pegasys.teku.networking.p2p.libp2p.gossip.LibP2PGossipNetwork.STRICT_FIELDS_VALIDATOR) MaxCountTopicSubscriptionFilter(io.libp2p.pubsub.MaxCountTopicSubscriptionFilter) Gossip(io.libp2p.pubsub.gossip.Gossip) PreparedGossipMessageFactory(tech.pegasys.teku.networking.p2p.gossip.PreparedGossipMessageFactory) LibP2PParamsFactory(tech.pegasys.teku.networking.p2p.libp2p.config.LibP2PParamsFactory) TTLSeenCache(io.libp2p.pubsub.TTLSeenCache) FastIdSeenCache(io.libp2p.pubsub.FastIdSeenCache) TopicSubscriptionFilter(io.libp2p.pubsub.TopicSubscriptionFilter) ValidationResult(io.libp2p.core.pubsub.ValidationResult) PubsubPublisherApi(io.libp2p.core.pubsub.PubsubPublisherApi) PubsubProtocol(io.libp2p.pubsub.PubsubProtocol) Hash(tech.pegasys.teku.infrastructure.crypto.Hash) LogLevel(io.netty.handler.logging.LogLevel) GossipRouter(io.libp2p.pubsub.gossip.GossipRouter) PreparedGossipMessage(tech.pegasys.teku.networking.p2p.gossip.PreparedGossipMessage) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) ChannelHandler(io.netty.channel.ChannelHandler) PubsubApiKt(io.libp2p.core.pubsub.PubsubApiKt) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) NotNull(org.jetbrains.annotations.NotNull) GossipScoreParams(io.libp2p.pubsub.gossip.GossipScoreParams) LoggingHandler(io.netty.handler.logging.LoggingHandler) Optional(java.util.Optional) PreparedGossipMessage(tech.pegasys.teku.networking.p2p.gossip.PreparedGossipMessage) GossipRouter(io.libp2p.pubsub.gossip.GossipRouter) MaxCountTopicSubscriptionFilter(io.libp2p.pubsub.MaxCountTopicSubscriptionFilter) SeenCache(io.libp2p.pubsub.SeenCache) TTLSeenCache(io.libp2p.pubsub.TTLSeenCache) FastIdSeenCache(io.libp2p.pubsub.FastIdSeenCache) FastIdSeenCache(io.libp2p.pubsub.FastIdSeenCache) MaxCountTopicSubscriptionFilter(io.libp2p.pubsub.MaxCountTopicSubscriptionFilter) TopicSubscriptionFilter(io.libp2p.pubsub.TopicSubscriptionFilter) ValidationResult(io.libp2p.core.pubsub.ValidationResult) Bytes(org.apache.tuweni.bytes.Bytes) Gossip(io.libp2p.pubsub.gossip.Gossip) GossipParams(io.libp2p.pubsub.gossip.GossipParams) PubsubApi(io.libp2p.core.pubsub.PubsubApi)

Aggregations

ValidationResult (io.libp2p.core.pubsub.ValidationResult)36 Test (org.junit.jupiter.api.Test)34 InternalValidationResult (tech.pegasys.teku.statetransition.validation.InternalValidationResult)29 Bytes (org.apache.tuweni.bytes.Bytes)28 ValidateableAttestation (tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation)8 MockMessageApi (tech.pegasys.teku.network.p2p.jvmlibp2p.MockMessageApi)6 AttestationGenerator (tech.pegasys.teku.core.AttestationGenerator)4 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)4 StateAndBlockSummary (tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary)4 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)3 CompletionException (java.util.concurrent.CompletionException)2 DecodingException (tech.pegasys.teku.networking.eth2.gossip.encoding.DecodingException)2 AttesterSlashing (tech.pegasys.teku.spec.datastructures.operations.AttesterSlashing)2 ProposerSlashing (tech.pegasys.teku.spec.datastructures.operations.ProposerSlashing)2 SignedVoluntaryExit (tech.pegasys.teku.spec.datastructures.operations.SignedVoluntaryExit)2 Preconditions (com.google.common.base.Preconditions)1 PubsubApi (io.libp2p.core.pubsub.PubsubApi)1 PubsubApiKt (io.libp2p.core.pubsub.PubsubApiKt)1 PubsubPublisherApi (io.libp2p.core.pubsub.PubsubPublisherApi)1 FastIdSeenCache (io.libp2p.pubsub.FastIdSeenCache)1