Search in sources :

Example 6 with TopicMessageSubmitTransaction

use of com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction in project hedera-sdk-java by hashgraph.

the class ConsensusPubSubWithSubmitKeyExample method publishMessagesToTopic.

/**
 * Publish a list of messages to a topic, signing each transaction with the topic's submitKey.
 */
private void publishMessagesToTopic() throws TimeoutException, InterruptedException, PrecheckStatusException, ReceiptStatusException {
    Random r = new Random();
    for (int i = 0; i < messagesToPublish; i++) {
        String message = "random message " + r.nextLong();
        System.out.println("Publishing message: " + message);
        new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage(message).freezeWith(client).sign(submitKey).execute(client).transactionId.getReceipt(client);
        Thread.sleep(millisBetweenMessages);
    }
    Thread.sleep(10000);
}
Also used : Random(java.util.Random) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction)

Example 7 with TopicMessageSubmitTransaction

use of com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction in project hedera-sdk-java by hashgraph.

the class ConsensusPubSubChunkedExample method main.

public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException, InvalidProtocolBufferException {
    Client client = Client.forName(HEDERA_NETWORK);
    // Defaults the operator account ID and key such that all generated transactions will be paid for
    // by this account and be signed by this key
    client.setOperator(OPERATOR_ID, OPERATOR_KEY);
    // generate a submit key to use with the topic
    PrivateKey submitKey = PrivateKey.generateED25519();
    // make a new topic ID to use
    TopicId newTopicId = new TopicCreateTransaction().setTopicMemo("hedera-sdk-java/ConsensusPubSubChunkedExample").setSubmitKey(submitKey).execute(client).getReceipt(client).topicId;
    assert newTopicId != null;
    System.out.println("for topic " + newTopicId);
    // Let's wait a bit
    System.out.println("wait 10s to propagate to the mirror ...");
    Thread.sleep(10000);
    // setup a mirror client to print out messages as we receive them
    new TopicMessageQuery().setTopicId(newTopicId).subscribe(client, topicMessage -> {
        System.out.println("at " + topicMessage.consensusTimestamp + " ( seq = " + topicMessage.sequenceNumber + " ) received topic message of " + topicMessage.contents.length + " bytes");
    });
    // get a large file to send
    String bigContents = readResources("large_message.txt");
    System.out.println("about to prepare a transaction to send a message of " + bigContents.length() + " bytes");
    // prepare a message send transaction that requires a submit key from "somewhere else"
    @Var Transaction<?> transaction = new TopicMessageSubmitTransaction().setMaxChunks(// this is 10 by default
    15).setTopicId(newTopicId).setMessage(bigContents).signWithOperator(client);
    // serialize to bytes so we can be signed "somewhere else" by the submit key
    byte[] transactionBytes = transaction.toBytes();
    // now pretend we sent those bytes across the network
    // parse them into a transaction so we can sign as the submit key
    transaction = Transaction.fromBytes(transactionBytes);
    // view out the message size from the parsed transaction
    // this can be useful to display what we are about to sign
    long transactionMessageSize = ((TopicMessageSubmitTransaction) transaction).getMessage().size();
    System.out.println("about to send a transaction with a message of " + transactionMessageSize + " bytes");
    // sign with that submit key
    transaction.sign(submitKey);
    // now actually submit the transaction
    // get the receipt to ensure there were no errors
    transaction.execute(client).getReceipt(client);
    // noinspection InfiniteLoopStatement
    while (true) {
        System.out.println("waiting ...");
        // noinspection BusyWait
        Thread.sleep(2500);
    }
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) Var(com.google.errorprone.annotations.Var) TopicId(com.hedera.hashgraph.sdk.TopicId) Client(com.hedera.hashgraph.sdk.Client) TopicMessageQuery(com.hedera.hashgraph.sdk.TopicMessageQuery) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction)

Example 8 with TopicMessageSubmitTransaction

use of com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction in project hedera-sdk-java by hashgraph.

the class ConsensusPubSubExample method main.

public static void main(String[] args) throws TimeoutException, InterruptedException, PrecheckStatusException, ReceiptStatusException {
    Client client = Client.forName(HEDERA_NETWORK);
    // Defaults the operator account ID and key such that all generated transactions will be paid for
    // by this account and be signed by this key
    client.setOperator(OPERATOR_ID, OPERATOR_KEY);
    TransactionResponse transactionResponse = new TopicCreateTransaction().execute(client);
    TransactionReceipt transactionReceipt = transactionResponse.getReceipt(client);
    TopicId topicId = Objects.requireNonNull(transactionReceipt.topicId);
    System.out.println("New topic created: " + topicId);
    Thread.sleep(5000);
    new TopicMessageQuery().setTopicId(topicId).subscribe(client, resp -> {
        String messageAsString = new String(resp.contents, StandardCharsets.UTF_8);
        System.out.println(resp.consensusTimestamp + " received topic message: " + messageAsString);
    });
    // noinspection InfiniteLoopStatement
    for (int i = 0; ; i++) {
        new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage("hello, HCS! " + i).execute(client).getReceipt(client);
        Thread.sleep(2500);
    }
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) TopicId(com.hedera.hashgraph.sdk.TopicId) Client(com.hedera.hashgraph.sdk.Client) TopicMessageQuery(com.hedera.hashgraph.sdk.TopicMessageQuery) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction)

Example 9 with TopicMessageSubmitTransaction

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

the class TopicClient method publishMessageToTopic.

public TransactionId publishMessageToTopic(TopicId topicId, byte[] message, KeyList submitKeys) {
    TopicMessageSubmitTransaction consensusMessageSubmitTransaction = new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage(message).setTransactionMemo(getMemo("Publish topic message"));
    TransactionId transactionId = executeTransaction(consensusMessageSubmitTransaction, submitKeys);
    TransactionRecord transactionRecord = getTransactionRecord(transactionId);
    // get only the 1st sequence number
    if (recordPublishInstants.size() == 0) {
        recordPublishInstants.put(0L, transactionRecord.consensusTimestamp);
    }
    if (log.isTraceEnabled()) {
        log.trace("Published message : '{}' to topicId : {} with consensusTimestamp: {}", new String(message, StandardCharsets.UTF_8), topicId, transactionRecord.consensusTimestamp);
    }
    return transactionId;
}
Also used : TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction) TransactionRecord(com.hedera.hashgraph.sdk.TransactionRecord) TransactionId(com.hedera.hashgraph.sdk.TransactionId)

Example 10 with TopicMessageSubmitTransaction

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

the class ConsensusSubmitMessageTransactionSupplierTest method createWithCustomMessageSize.

@Test
void createWithCustomMessageSize() {
    ConsensusSubmitMessageTransactionSupplier consensusSubmitMessageTransactionSupplier = new ConsensusSubmitMessageTransactionSupplier();
    consensusSubmitMessageTransactionSupplier.setMaxTransactionFee(1);
    consensusSubmitMessageTransactionSupplier.setMessageSize(14);
    consensusSubmitMessageTransactionSupplier.setTopicId(TOPIC_ID.toString());
    TopicMessageSubmitTransaction actual = consensusSubmitMessageTransactionSupplier.get();
    assertThat(actual).returns(ONE_TINYBAR, TopicMessageSubmitTransaction::getMaxTransactionFee).returns(TOPIC_ID, TopicMessageSubmitTransaction::getTopicId).satisfies(a -> assertThat(a.getMessage()).isNotNull()).extracting(a -> a.getMessage().toStringUtf8(), STRING).hasSize(14);
}
Also used : ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction) Hbar(com.hedera.hashgraph.sdk.Hbar) STRING(org.assertj.core.api.InstanceOfAssertFactories.STRING) AbstractTransactionSupplierTest(com.hedera.mirror.monitor.publish.transaction.AbstractTransactionSupplierTest) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction) Test(org.junit.jupiter.api.Test) AbstractTransactionSupplierTest(com.hedera.mirror.monitor.publish.transaction.AbstractTransactionSupplierTest)

Aggregations

TopicMessageSubmitTransaction (com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction)15 TopicCreateTransaction (com.hedera.hashgraph.sdk.TopicCreateTransaction)10 Test (org.junit.jupiter.api.Test)10 DisplayName (org.junit.jupiter.api.DisplayName)7 TopicDeleteTransaction (com.hedera.hashgraph.sdk.TopicDeleteTransaction)6 Var (com.google.errorprone.annotations.Var)4 TopicInfoQuery (com.hedera.hashgraph.sdk.TopicInfoQuery)4 TopicMessageQuery (com.hedera.hashgraph.sdk.TopicMessageQuery)4 ByteString (com.google.protobuf.ByteString)3 Client (com.hedera.hashgraph.sdk.Client)3 Hbar (com.hedera.hashgraph.sdk.Hbar)3 AbstractTransactionSupplierTest (com.hedera.mirror.monitor.publish.transaction.AbstractTransactionSupplierTest)3 TopicId (com.hedera.hashgraph.sdk.TopicId)2 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)2 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 STRING (org.assertj.core.api.InstanceOfAssertFactories.STRING)2 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)1 KeyList (com.hedera.hashgraph.sdk.KeyList)1 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)1