Search in sources :

Example 1 with PubsubMessage

use of io.libp2p.pubsub.PubsubMessage in project teku by ConsenSys.

the class GossipHandler method apply.

@Override
public SafeFuture<ValidationResult> apply(final MessageApi message) {
    messageCounter.inc();
    final int messageSize = message.getData().readableBytes();
    final int maxMessageSize = handler.getMaxMessageSize();
    if (messageSize > maxMessageSize) {
        LOG.trace("Rejecting gossip message of length {} which exceeds maximum size of {}", messageSize, maxMessageSize);
        return VALIDATION_FAILED;
    }
    byte[] arr = new byte[message.getData().readableBytes()];
    message.getData().slice().readBytes(arr);
    Bytes bytes = Bytes.wrap(arr);
    if (!processedMessages.add(bytes)) {
        // We've already seen this message, skip processing
        LOG.trace("Ignoring duplicate message for topic {}: {} bytes", topic, bytes.size());
        return VALIDATION_IGNORED;
    }
    LOG.trace("Received message for topic {}: {} bytes", topic, bytes.size());
    PubsubMessage pubsubMessage = message.getOriginalMessage();
    if (!(pubsubMessage instanceof PreparedPubsubMessage)) {
        throw new IllegalArgumentException("Don't know this PubsubMessage implementation: " + pubsubMessage.getClass());
    }
    PreparedPubsubMessage gossipPubsubMessage = (PreparedPubsubMessage) pubsubMessage;
    return handler.handleMessage(gossipPubsubMessage.getPreparedMessage());
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) PubsubMessage(io.libp2p.pubsub.PubsubMessage)

Example 2 with PubsubMessage

use of io.libp2p.pubsub.PubsubMessage in project teku by ConsenSys.

the class MockMessageApi method getOriginalMessage.

@NotNull
@Override
public PubsubMessage getOriginalMessage() {
    Message protoMessage = Message.newBuilder().addAllTopicIDs(getTopics().stream().map(Topic::getTopic).collect(Collectors.toList())).setData(ByteString.copyFrom(getData().nioBuffer())).build();
    PreparedGossipMessage preparedMessage = new PreparedGossipMessage() {

        @Override
        public Bytes getMessageId() {
            return Bytes.wrap(Hash.sha256(protoMessage.getData().toByteArray()));
        }

        @Override
        public DecodedMessageResult getDecodedMessage() {
            final Bytes decoded = Bytes.of(protoMessage.getData().toByteArray());
            return DecodedMessageResult.successful(decoded);
        }

        @Override
        public Bytes getOriginalMessage() {
            return Bytes.wrapByteBuf(data);
        }
    };
    return new PreparedPubsubMessage(protoMessage, preparedMessage);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) PubsubMessage(io.libp2p.pubsub.PubsubMessage) PreparedPubsubMessage(tech.pegasys.teku.networking.p2p.libp2p.gossip.PreparedPubsubMessage) Message(pubsub.pb.Rpc.Message) PreparedGossipMessage(tech.pegasys.teku.networking.p2p.gossip.PreparedGossipMessage) PreparedGossipMessage(tech.pegasys.teku.networking.p2p.gossip.PreparedGossipMessage) Topic(io.libp2p.core.pubsub.Topic) PreparedPubsubMessage(tech.pegasys.teku.networking.p2p.libp2p.gossip.PreparedPubsubMessage) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PubsubMessage (io.libp2p.pubsub.PubsubMessage)2 Bytes (org.apache.tuweni.bytes.Bytes)2 Topic (io.libp2p.core.pubsub.Topic)1 NotNull (org.jetbrains.annotations.NotNull)1 Message (pubsub.pb.Rpc.Message)1 PreparedGossipMessage (tech.pegasys.teku.networking.p2p.gossip.PreparedGossipMessage)1 PreparedPubsubMessage (tech.pegasys.teku.networking.p2p.libp2p.gossip.PreparedPubsubMessage)1