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