Search in sources :

Example 1 with TopicInfoQuery

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

the class TopicInfoIntegrationTest method getCostInsufficientTxFeeQueryTopicInfo.

@Test
@DisplayName("Can get cost for topic info query")
void getCostInsufficientTxFeeQueryTopicInfo() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    var response = new TopicCreateTransaction().setAdminKey(testEnv.operatorKey).setTopicMemo("[e2e::TopicCreateTransaction]").execute(testEnv.client);
    var topicId = Objects.requireNonNull(response.getReceipt(testEnv.client).topicId);
    var infoQuery = new TopicInfoQuery().setTopicId(topicId);
    var cost = infoQuery.getCost(testEnv.client);
    assertThat(cost).isNotNull();
    assertThatExceptionOfType(PrecheckStatusException.class).isThrownBy(() -> {
        infoQuery.setQueryPayment(Hbar.fromTinybars(1)).execute(testEnv.client);
    }).satisfies(error -> assertThat(error.status.toString()).isEqualTo("INSUFFICIENT_TX_FEE"));
    new TopicDeleteTransaction().setTopicId(topicId).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) TopicInfoQuery(com.hedera.hashgraph.sdk.TopicInfoQuery) TopicDeleteTransaction(com.hedera.hashgraph.sdk.TopicDeleteTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 2 with TopicInfoQuery

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

the class TopicMessageIntegrationTest method canReceiveATopicMessage.

@Test
@DisplayName("Can receive a topic message")
void canReceiveATopicMessage() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    var response = new TopicCreateTransaction().setAdminKey(testEnv.operatorKey).setTopicMemo("[e2e::TopicCreateTransaction]").execute(testEnv.client);
    var topicId = Objects.requireNonNull(response.getReceipt(testEnv.client).topicId);
    var info = new TopicInfoQuery().setTopicId(topicId).execute(testEnv.client);
    assertThat(info.topicId).isEqualTo(topicId);
    assertThat(info.topicMemo).isEqualTo("[e2e::TopicCreateTransaction]");
    assertThat(info.sequenceNumber).isEqualTo(0);
    assertThat(info.adminKey).isEqualTo(testEnv.operatorKey);
    Thread.sleep(3000);
    var receivedMessage = new boolean[] { false };
    var start = Instant.now();
    var handle = new TopicMessageQuery().setTopicId(topicId).setStartTime(Instant.EPOCH).subscribe(testEnv.client, (message) -> {
        receivedMessage[0] = new String(message.contents, StandardCharsets.UTF_8).equals("Hello, from HCS!");
    });
    Thread.sleep(3000);
    new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage("Hello, from HCS!").execute(testEnv.client).getReceipt(testEnv.client);
    while (!receivedMessage[0]) {
        if (Duration.between(start, Instant.now()).compareTo(Duration.ofSeconds(60)) > 0) {
            throw new Exception("TopicMessage was not received in 60 seconds or less");
        }
        Thread.sleep(2000);
    }
    handle.unsubscribe();
    new TopicDeleteTransaction().setTopicId(topicId).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) TopicInfoQuery(com.hedera.hashgraph.sdk.TopicInfoQuery) TopicMessageQuery(com.hedera.hashgraph.sdk.TopicMessageQuery) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction) TopicDeleteTransaction(com.hedera.hashgraph.sdk.TopicDeleteTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 3 with TopicInfoQuery

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

the class TopicMessageIntegrationTest method canReceiveALargeTopicMessage.

@Test
@DisplayName("Can receive a large topic message")
void canReceiveALargeTopicMessage() throws Exception {
    var testEnv = new IntegrationTestEnv(2);
    var response = new TopicCreateTransaction().setAdminKey(testEnv.operatorKey).setTopicMemo("[e2e::TopicCreateTransaction]").execute(testEnv.client);
    var topicId = Objects.requireNonNull(response.getReceipt(testEnv.client).topicId);
    Thread.sleep(5000);
    var info = new TopicInfoQuery().setTopicId(topicId).execute(testEnv.client);
    assertThat(info.topicId).isEqualTo(topicId);
    assertThat(info.topicMemo).isEqualTo("[e2e::TopicCreateTransaction]");
    assertThat(info.sequenceNumber).isEqualTo(0);
    assertThat(info.adminKey).isEqualTo(testEnv.operatorKey);
    var receivedMessage = new boolean[] { false };
    var start = Instant.now();
    var handle = new TopicMessageQuery().setTopicId(topicId).setStartTime(Instant.EPOCH).subscribe(testEnv.client, (message) -> {
        receivedMessage[0] = new String(message.contents, StandardCharsets.UTF_8).equals(Contents.BIG_CONTENTS);
    });
    new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage(Contents.BIG_CONTENTS).execute(testEnv.client).getReceipt(testEnv.client);
    while (!receivedMessage[0]) {
        if (Duration.between(start, Instant.now()).compareTo(Duration.ofSeconds(60)) > 0) {
            throw new Exception("TopicMessage was not received in 60 seconds or less");
        }
        Thread.sleep(1000);
    }
    handle.unsubscribe();
    new TopicDeleteTransaction().setTopicId(topicId).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) TopicInfoQuery(com.hedera.hashgraph.sdk.TopicInfoQuery) TopicMessageQuery(com.hedera.hashgraph.sdk.TopicMessageQuery) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction) TopicDeleteTransaction(com.hedera.hashgraph.sdk.TopicDeleteTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 4 with TopicInfoQuery

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

the class TopicMessageSubmitIntegrationTest method canSubmitALargeTopicMessage.

@Test
@DisplayName("Can submit a large topic message")
void canSubmitALargeTopicMessage() {
    // Skip if using PreviewNet
    Assumptions.assumeTrue(!System.getProperty("HEDERA_NETWORK").equals("previewnet"));
    assertThatNoException().isThrownBy(() -> {
        var testEnv = new IntegrationTestEnv(2);
        var response = new TopicCreateTransaction().setAdminKey(testEnv.operatorKey).setTopicMemo("[e2e::TopicCreateTransaction]").execute(testEnv.client);
        var topicId = Objects.requireNonNull(response.getReceipt(testEnv.client).topicId);
        Thread.sleep(5000);
        @Var var info = new TopicInfoQuery().setTopicId(topicId).execute(testEnv.client);
        assertThat(info.topicId).isEqualTo(topicId);
        assertThat(info.topicMemo).isEqualTo("[e2e::TopicCreateTransaction]");
        assertThat(info.sequenceNumber).isEqualTo(0);
        assertThat(info.adminKey).isEqualTo(testEnv.operatorKey);
        var responses = new TopicMessageSubmitTransaction().setTopicId(topicId).setMaxChunks(15).setMessage(Contents.BIG_CONTENTS).executeAll(testEnv.client);
        for (var resp : responses) {
            resp.getReceipt(testEnv.client);
        }
        info = new TopicInfoQuery().setTopicId(topicId).execute(testEnv.client);
        assertThat(info.topicId).isEqualTo(topicId);
        assertThat(info.topicMemo).isEqualTo("[e2e::TopicCreateTransaction]");
        assertThat(info.sequenceNumber).isEqualTo(14);
        assertThat(info.adminKey).isEqualTo(testEnv.operatorKey);
        new TopicDeleteTransaction().setTopicId(topicId).execute(testEnv.client).getReceipt(testEnv.client);
        testEnv.close();
    });
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) TopicInfoQuery(com.hedera.hashgraph.sdk.TopicInfoQuery) Var(com.google.errorprone.annotations.Var) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction) TopicDeleteTransaction(com.hedera.hashgraph.sdk.TopicDeleteTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 5 with TopicInfoQuery

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

the class TopicWithAdminKeyExample method updateTopicAdminKeyAndMemo.

private void updateTopicAdminKeyAndMemo() throws TimeoutException, PrecheckStatusException, ReceiptStatusException {
    // Generate the new keys that are part of the adminKey's thresholdKey.
    // 4 ED25519 keys part of a 3-of-4 threshold key.
    PrivateKey[] newAdminKeys = new PrivateKey[4];
    J8Arrays.setAll(newAdminKeys, i -> PrivateKey.generate());
    KeyList thresholdKey = KeyList.withThreshold(3);
    Collections.addAll(thresholdKey, newAdminKeys);
    Transaction<?> transaction = new TopicUpdateTransaction().setTopicId(topicId).setTopicMemo("updated demo topic").setAdminKey(thresholdKey).freezeWith(hapiClient);
    // Sign with the initial adminKey. 2 of the 3 keys already part of the topic's adminKey.
    J8Arrays.stream(initialAdminKeys, 0, 2).forEach(k -> {
        System.out.println("Signing ConsensusTopicUpdateTransaction with initial admin key " + k);
        transaction.sign(k);
    });
    // Sign with the new adminKey. 3 of 4 keys already part of the topic's adminKey.
    J8Arrays.stream(newAdminKeys, 0, 3).forEach(k -> {
        System.out.println("Signing ConsensusTopicUpdateTransaction with new admin key " + k);
        transaction.sign(k);
    });
    TransactionResponse transactionResponse = transaction.execute(hapiClient);
    // Retrieve results post-consensus.
    transactionResponse.getReceipt(hapiClient);
    System.out.println("Updated topic " + topicId + " with 3-of-4 threshold key as adminKey");
    TopicInfo topicInfo = new TopicInfoQuery().setTopicId(topicId).execute(hapiClient);
    System.out.println(topicInfo);
}
Also used : TopicUpdateTransaction(com.hedera.hashgraph.sdk.TopicUpdateTransaction) TopicInfoQuery(com.hedera.hashgraph.sdk.TopicInfoQuery) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) KeyList(com.hedera.hashgraph.sdk.KeyList) TopicInfo(com.hedera.hashgraph.sdk.TopicInfo)

Aggregations

TopicInfoQuery (com.hedera.hashgraph.sdk.TopicInfoQuery)11 TopicCreateTransaction (com.hedera.hashgraph.sdk.TopicCreateTransaction)10 TopicDeleteTransaction (com.hedera.hashgraph.sdk.TopicDeleteTransaction)10 DisplayName (org.junit.jupiter.api.DisplayName)10 Test (org.junit.jupiter.api.Test)10 TopicMessageSubmitTransaction (com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction)4 Var (com.google.errorprone.annotations.Var)2 TopicMessageQuery (com.hedera.hashgraph.sdk.TopicMessageQuery)2 TopicUpdateTransaction (com.hedera.hashgraph.sdk.TopicUpdateTransaction)2 Hbar (com.hedera.hashgraph.sdk.Hbar)1 KeyList (com.hedera.hashgraph.sdk.KeyList)1 MaxQueryPaymentExceededException (com.hedera.hashgraph.sdk.MaxQueryPaymentExceededException)1 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)1 TopicInfo (com.hedera.hashgraph.sdk.TopicInfo)1 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)1