Skip to content

Commit

Permalink
Implement TopicInfoQuery (#441)
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Walworth <[email protected]>
  • Loading branch information
rwalworth authored Jul 26, 2023
1 parent a1dddff commit b47b48d
Show file tree
Hide file tree
Showing 15 changed files with 816 additions and 2 deletions.
2 changes: 2 additions & 0 deletions sdk/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ add_library(${PROJECT_NAME} STATIC
src/TopicCreateTransaction.cc
src/TopicDeleteTransaction.cc
src/TopicId.cc
src/TopicInfo.cc
src/TopicInfoQuery.cc
src/Transaction.cc
src/TransactionId.cc
src/TransactionReceipt.cc
Expand Down
133 changes: 133 additions & 0 deletions sdk/main/include/TopicInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*-
*
* Hedera C++ SDK
*
* Copyright (C) 2020 - 2022 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef HEDERA_SDK_CPP_TOPIC_INFO_H_
#define HEDERA_SDK_CPP_TOPIC_INFO_H_

#include "AccountId.h"
#include "Key.h"
#include "LedgerId.h"
#include "TopicId.h"

#include <chrono>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <vector>

namespace proto
{
class ConsensusGetTopicInfoResponse;
}

namespace Hedera
{
/**
* Response from a Hedera network when the client sends an TopicInfoQuery.
*/
class TopicInfo
{
public:
/**
* Construct a TopicInfo object from a ConsensusGetTopicInfoResponse protobuf object.
*
* @param proto The ConsensusGetTopicInfoResponse protobuf object from which to construct a TopicInfo object.
* @return The constructed TopicInfo object.
*/
[[nodiscard]] static TopicInfo fromProtobuf(const proto::ConsensusGetTopicInfoResponse& proto);

/**
* Construct a TopicInfo object from a byte array.
*
* @param bytes The byte array representing a TopicInfo object.
* @return The constructed TopicInfo object.
*/
[[nodiscard]] static TopicInfo fromBytes(const std::vector<std::byte>& bytes);

/**
* Construct a ConsensusGetTopicInfoResponse protobuf object from this TopicInfo object.
*
* @return A pointer to the created ConsensusGetTopicInfoResponse protobuf object.
*/
[[nodiscard]] std::unique_ptr<proto::ConsensusGetTopicInfoResponse> toProtobuf() const;

/**
* Construct a representative byte array from this TopicInfo object.
*
* @return The byte array representing this TopicInfo object.
*/
[[nodiscard]] std::vector<std::byte> toBytes() const;

/**
* The ID of the topic.
*/
TopicId mTopicId;

/**
* The publicly visible memo for the new topic.
*/
std::string mMemo;

/**
* The SHA384 running hash of [previousRunningHash, topicId, consensusTimestamp, sequenceNumber, message].
*/
std::vector<std::byte> mRunningHash;

/**
* The sequence number (which starts at one for the first message) of messages on the topic.
*/
uint64_t mSequenceNumber = 0ULL;

/**
* The timestamp at which the topic will expire.
*/
std::chrono::system_clock::time_point mExpirationTime = std::chrono::system_clock::now();

/**
* The key used for access control to update or delete the topic. Nullptr if there is no admin key for the topic.
*/
std::shared_ptr<Key> mAdminKey = nullptr;

/**
* The key that must sign to submit a message to the new topic (via a TopicMessageSubmitTransaction). Nullptr if there
* is no submit key for the topic.
*/
std::shared_ptr<Key> mSubmitKey = nullptr;

/**
* The amount of time by which to attempt to extend the topic's lifetime automatically at its expiration time.
*/
std::optional<std::chrono::duration<double>> mAutoRenewPeriod;

/**
* The ID of the account that should be charged to extend the lifetime of the new topic at its expiration time.
*/
std::optional<AccountId> mAutoRenewAccountId;

/**
* The ID of the ledger from which this response was returned.
*/
LedgerId mLedgerId;
};

} // namespace Hedera

#endif // HEDERA_SDK_CPP_TOPIC_INFO_H_
106 changes: 106 additions & 0 deletions sdk/main/include/TopicInfoQuery.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*-
*
* Hedera C++ SDK
*
* Copyright (C) 2020 - 2022 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef HEDERA_SDK_CPP_TOPIC_INFO_QUERY_H_
#define HEDERA_SDK_CPP_TOPIC_INFO_QUERY_H_

#include "Query.h"
#include "TopicId.h"

namespace Hedera
{
class TopicInfo;
class TransactionRecord;
}

namespace Hedera
{
/**
* A query that gets information about a topic instance.
*/
class TopicInfoQuery : public Query<TopicInfoQuery, TopicInfo>
{
public:
/**
* Set the ID of the topic of which to request the info.
*
* @param topic The ID of the topic of which to request the info.
* @return A reference to this TopicInfoQuery object with the newly-set topic ID.
*/
TopicInfoQuery& setTopicId(const TopicId& topicId);

/**
* Get the ID of the topic of which this query is currently configured to get the info.
*
* @return The ID of the topic for which this query is meant.
*/
[[nodiscard]] inline TopicId getTopicId() const { return mTopicId; }

private:
/**
* Derived from Executable. Construct a Query protobuf object from this TopicInfoQuery object.
*
* @param client The Client trying to construct this TopicInfoQuery.
* @param node The Node to which this TopicInfoQuery will be sent.
* @return A Query protobuf object filled with this TopicInfoQuery object's data.
*/
[[nodiscard]] proto::Query makeRequest(const Client& client,
const std::shared_ptr<internal::Node>& node) const override;

/**
* Derived from Executable. Construct a TopicInfo object from a Response protobuf object.
*
* @param response The Response protobuf object from which to construct a TopicInfo object.
* @return A TopicInfo object filled with the Response protobuf object's data.
*/
[[nodiscard]] TopicInfo mapResponse(const proto::Response& response) const override;

/**
* Derived from Executable. Get the status response code for a submitted TopicInfoQuery from a Response protobuf
* object.
*
* @param response The Response protobuf object from which to grab the TopicInfoQuery status response code.
* @return The TopicInfoQuery status response code of the input Response protobuf object.
*/
[[nodiscard]] Status mapResponseStatus(const proto::Response& response) const override;

/**
* Derived from Executable. Submit this TopicInfoQuery to a Node.
*
* @param client The Client submitting this TopicInfoQuery.
* @param deadline The deadline for submitting this TopicInfoQuery.
* @param node Pointer to the Node to which this TopicInfoQuery should be submitted.
* @param response Pointer to the Response protobuf object that gRPC should populate with the response information
* from the gRPC server.
* @return The gRPC status of the submission.
*/
[[nodiscard]] grpc::Status submitRequest(const Client& client,
const std::chrono::system_clock::time_point& deadline,
const std::shared_ptr<internal::Node>& node,
proto::Response* response) const override;

/**
* The ID of the topic of which this query should get the info.
*/
TopicId mTopicId;
};

} // namespace Hedera

#endif // HEDERA_SDK_CPP_TOPIC_INFO_QUERY_H_
1 change: 1 addition & 0 deletions sdk/main/include/TransferTransaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ class TransferTransaction : public Transaction<TransferTransaction>
friend class FileInfoQuery;
friend class TokenInfoQuery;
friend class TokenNftInfoQuery;
friend class TopicInfoQuery;
friend class TransactionRecordQuery;

/**
Expand Down
3 changes: 3 additions & 0 deletions sdk/main/src/Executable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
#include "TokenWipeTransaction.h"
#include "TopicCreateTransaction.h"
#include "TopicDeleteTransaction.h"
#include "TopicInfo.h"
#include "TopicInfoQuery.h"
#include "TransactionReceipt.h"
#include "TransactionReceiptQuery.h"
#include "TransactionRecord.h"
Expand Down Expand Up @@ -403,6 +405,7 @@ template class Executable<TokenUpdateTransaction, proto::Transaction, proto::Tra
template class Executable<TokenWipeTransaction, proto::Transaction, proto::TransactionResponse, TransactionResponse>;
template class Executable<TopicCreateTransaction, proto::Transaction, proto::TransactionResponse, TransactionResponse>;
template class Executable<TopicDeleteTransaction, proto::Transaction, proto::TransactionResponse, TransactionResponse>;
template class Executable<TopicInfoQuery, proto::Query, proto::Response, TopicInfo>;
template class Executable<TransactionReceiptQuery, proto::Query, proto::Response, TransactionReceipt>;
template class Executable<TransactionRecordQuery, proto::Query, proto::Response, TransactionRecord>;
template class Executable<TransferTransaction, proto::Transaction, proto::TransactionResponse, TransactionResponse>;
Expand Down
3 changes: 3 additions & 0 deletions sdk/main/src/Query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "TokenInfoQuery.h"
#include "TokenNftInfo.h"
#include "TokenNftInfoQuery.h"
#include "TopicInfo.h"
#include "TopicInfoQuery.h"
#include "TransactionReceipt.h"
#include "TransactionReceiptQuery.h"
#include "TransactionRecord.h"
Expand All @@ -51,6 +53,7 @@ template class Query<FileContentsQuery, FileContents>;
template class Query<FileInfoQuery, FileInfo>;
template class Query<TokenInfoQuery, TokenInfo>;
template class Query<TokenNftInfoQuery, TokenNftInfo>;
template class Query<TopicInfoQuery, TopicInfo>;
template class Query<TransactionReceiptQuery, TransactionReceipt>;
template class Query<TransactionRecordQuery, TransactionRecord>;

Expand Down
Loading

0 comments on commit b47b48d

Please sign in to comment.