Search in sources :

Example 1 with TopicMessage

use of com.hedera.hashgraph.sdk.TopicMessage in project hedera-mirror-node by hashgraph.

the class GrpcClientSDK method subscribeToClient.

private Flux<SubscribeResponse> subscribeToClient(Client client, GrpcSubscription subscription) {
    Sinks.Many<TopicMessage> sink = Sinks.many().multicast().directBestEffort();
    TopicMessageQuery topicMessageQuery = subscription.getTopicMessageQuery();
    topicMessageQuery.setCompletionHandler(sink::tryEmitComplete);
    topicMessageQuery.setErrorHandler((throwable, topicMessage) -> sink.tryEmitError(throwable));
    // Disable since we use our own retry logic to capture errors
    topicMessageQuery.setMaxAttempts(0);
    SubscriptionHandle subscriptionHandle = topicMessageQuery.subscribe(client, sink::tryEmitNext);
    return sink.asFlux().publishOn(Schedulers.parallel()).doFinally(s -> subscriptionHandle.unsubscribe()).doOnComplete(subscription::onComplete).doOnError(subscription::onError).doOnNext(subscription::onNext).map(t -> toResponse(subscription, t));
}
Also used : Sinks(reactor.core.publisher.Sinks) TopicMessage(com.hedera.hashgraph.sdk.TopicMessage) SubscriptionHandle(com.hedera.hashgraph.sdk.SubscriptionHandle) TopicMessageQuery(com.hedera.hashgraph.sdk.TopicMessageQuery)

Example 2 with TopicMessage

use of com.hedera.hashgraph.sdk.TopicMessage in project hedera-mirror-node by hashgraph.

the class SubscriptionResponse method validateReceivedMessages.

public void validateReceivedMessages() throws Exception {
    int invalidMessages = 0;
    TopicMessage lastTopicMessage = null;
    for (MirrorHCSResponse mirrorHCSResponseResponse : mirrorHCSResponses) {
        TopicMessage topicMessage = mirrorHCSResponseResponse.getTopicMessage();
        Instant publishInstant = Instant.ofEpochMilli(Longs.fromByteArray(topicMessage.contents));
        long publishSeconds = publishInstant.getEpochSecond();
        long consensusSeconds = topicMessage.consensusTimestamp.getEpochSecond();
        long receiptSeconds = mirrorHCSResponseResponse.getReceivedInstant().getEpochSecond();
        long e2eSeconds = receiptSeconds - publishSeconds;
        long consensusToDelivery = receiptSeconds - consensusSeconds;
        log.trace("Observed message, e2eSeconds: {}s, consensusToDelivery: {}s, publish timestamp: {}, " + "consensus timestamp: {}, receipt time: {}, topic sequence number: {}", e2eSeconds, consensusToDelivery, publishInstant, topicMessage.consensusTimestamp, mirrorHCSResponseResponse.getReceivedInstant(), topicMessage.sequenceNumber);
        if (!validateResponse(lastTopicMessage, topicMessage)) {
            invalidMessages++;
        }
        lastTopicMessage = topicMessage;
    }
    if (invalidMessages > 0) {
        throw new Exception("Retrieved {} invalid messages in response");
    }
    log.info("{} messages were successfully validated", mirrorHCSResponses.size());
}
Also used : TopicMessage(com.hedera.hashgraph.sdk.TopicMessage) Instant(java.time.Instant)

Aggregations

TopicMessage (com.hedera.hashgraph.sdk.TopicMessage)2 SubscriptionHandle (com.hedera.hashgraph.sdk.SubscriptionHandle)1 TopicMessageQuery (com.hedera.hashgraph.sdk.TopicMessageQuery)1 Instant (java.time.Instant)1 Sinks (reactor.core.publisher.Sinks)1