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