diff --git a/sdk/main/CMakeLists.txt b/sdk/main/CMakeLists.txt index 41e9060c5..f46745aad 100644 --- a/sdk/main/CMakeLists.txt +++ b/sdk/main/CMakeLists.txt @@ -66,6 +66,8 @@ add_library(${PROJECT_NAME} STATIC src/LedgerId.cc src/Mnemonic.cc src/MnemonicBIP39.cc + src/NetworkVersionInfo.cc + src/NetworkVersionInfoQuery.cc src/NftId.cc src/PrivateKey.cc src/PublicKey.cc @@ -76,6 +78,7 @@ add_library(${PROJECT_NAME} STATIC src/ScheduleInfo.cc src/ScheduleInfoQuery.cc src/ScheduleSignTransaction.cc + src/SemanticVersion.cc src/StakingInfo.cc src/Status.cc src/SubscriptionHandle.cc diff --git a/sdk/main/include/NetworkVersionInfo.h b/sdk/main/include/NetworkVersionInfo.h new file mode 100644 index 000000000..3a1e9ffc0 --- /dev/null +++ b/sdk/main/include/NetworkVersionInfo.h @@ -0,0 +1,95 @@ +/*- + * + * 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_NETWORK_VERSION_INFO_H_ +#define HEDERA_SDK_CPP_NETWORK_VERSION_INFO_H_ + +#include "SemanticVersion.h" + +#include +#include +#include + +namespace proto +{ +class NetworkGetVersionInfoResponse; +} + +namespace Hedera +{ +/** + * Contains information about the network's version. + */ +class NetworkVersionInfo +{ +public: + NetworkVersionInfo() = default; + + /** + * Construct from a protobuf schema SemanticVersion and a Hedera services SemanticVersion. + * + * @param hapi The SemanticVersion of the protobuf schema. + * @param hedera The SemanticVersion of the Hedera services. + */ + NetworkVersionInfo(const SemanticVersion& hapi, const SemanticVersion& hedera); + + /** + * Construct a NetworkVersionInfo object from a NetworkGetVersionInfoResponse protobuf object. + * + * @param proto The NetworkGetVersionInfoResponse protobuf object from which to construct a NetworkVersionInfo object. + * @return The constructed NetworkVersionInfo object. + */ + [[nodiscard]] static NetworkVersionInfo fromProtobuf(const proto::NetworkGetVersionInfoResponse& proto); + + /** + * Construct a NetworkVersionInfo object from a byte array. + * + * @param bytes The byte array from which to construct a NetworkVersionInfo object. + * @return The constructed NetworkVersionInfo object. + */ + [[nodiscard]] static NetworkVersionInfo fromBytes(const std::vector& bytes); + + /** + * Construct a NetworkGetVersionInfoResponse protobuf object from this NetworkVersionInfo object. + * + * @return A pointer to the created NetworkVersionInfo protobuf object. + */ + [[nodiscard]] std::unique_ptr toProtobuf() const; + + /** + * Construct a representative byte array from this NetworkVersionInfo object. + * + * @return The byte array representing this NetworkVersionInfo object. + */ + [[nodiscard]] std::vector toBytes() const; + + /** + * The version of the protobuf schema in use by the network. + */ + SemanticVersion mProtobufVersion; + + /** + * The version of the Hedera services in use by the network. + */ + SemanticVersion mServicesVersion; +}; + +} // namespace Hedera + +#endif // HEDERA_SDK_CPP_NETWORK_VERSION_INFO_H_ diff --git a/sdk/main/include/NetworkVersionInfoQuery.h b/sdk/main/include/NetworkVersionInfoQuery.h new file mode 100644 index 000000000..b8bbcbb21 --- /dev/null +++ b/sdk/main/include/NetworkVersionInfoQuery.h @@ -0,0 +1,83 @@ +/*- + * + * 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_NETWORK_VERSION_INFO_QUERY_H_ +#define HEDERA_SDK_CPP_NETWORK_VERSION_INFO_QUERY_H_ + +#include "Query.h" + +namespace Hedera +{ +class NetworkVersionInfo; +} + +namespace Hedera +{ +/** + * Get the deployed versions of Hedera Services and the HAPI proto in semantic version format. + */ +class NetworkVersionInfoQuery : public Query +{ +private: + /** + * Derived from Executable. Construct a Query protobuf object from this NetworkVersionInfoQuery object. + * + * @param client The Client trying to construct this NetworkVersionInfoQuery. + * @param node The Node to which this NetworkVersionInfoQuery will be sent. + * @return A Query protobuf object filled with this NetworkVersionInfoQuery object's data. + */ + [[nodiscard]] proto::Query makeRequest(const Client& client, + const std::shared_ptr& node) const override; + + /** + * Derived from Executable. Construct a NetworkVersionInfo object from a Response protobuf object. + * + * @param response The Response protobuf object from which to construct a NetworkVersionInfo object. + * @return A NetworkVersionInfo object filled with the Response protobuf object's data. + */ + [[nodiscard]] NetworkVersionInfo mapResponse(const proto::Response& response) const override; + + /** + * Derived from Executable. Get the status response code for a submitted NetworkVersionInfoQuery from a Response + * protobuf object. + * + * @param response The Response protobuf object from which to grab the NetworkVersionInfoQuery status response code. + * @return The NetworkVersionInfoQuery status response code of the input Response protobuf object. + */ + [[nodiscard]] Status mapResponseStatus(const proto::Response& response) const override; + + /** + * Derived from Executable. Submit this NetworkVersionInfoQuery to a Node. + * + * @param client The Client submitting this NetworkVersionInfoQuery. + * @param deadline The deadline for submitting this NetworkVersionInfoQuery. + * @param node Pointer to the Node to which this NetworkVersionInfoQuery 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& node, + proto::Response* response) const override; +}; + +} // namespace Hedera + +#endif // HEDERA_SDK_CPP_NETWORK_VERSION_INFO_QUERY_H_ diff --git a/sdk/main/include/SemanticVersion.h b/sdk/main/include/SemanticVersion.h new file mode 100644 index 000000000..0d9533a2f --- /dev/null +++ b/sdk/main/include/SemanticVersion.h @@ -0,0 +1,131 @@ +/*- + * + * 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_SEMANTIC_VERSION_H_ +#define HEDERA_SDK_CPP_SEMANTIC_VERSION_H_ + +#include +#include +#include +#include +#include + +namespace proto +{ +class SemanticVersion; +} + +namespace Hedera +{ +/** + * Hedera follows semantic versioning for both the HAPI protobufs and the Services software. This type allows the + * getVersionInfo query in the NetworkService to return the deployed versions of both protobufs and software on the node + * answering the query. + */ +class SemanticVersion +{ +public: + SemanticVersion() = default; + + /** + * Construct from a major, minor, and patch number. + * + * @param major The major number. + * @param minor The minor number. + * @param patch The patch number. + * @param pre The pre-release version number. + * @param build The build metadata. + */ + SemanticVersion(int major, int minor, int patch, std::string_view pre = "", std::string_view build = ""); + + /** + * Compare this SemanticVersion to another SemanticVersion and determine if they represent the same semantic version. + * + * @param other The other SemanticVersion with which to compare this SemanticVersion. + * @return \c TRUE if this SemanticVersion is the same as the input SemanticVersion, otherwise \c FALSE. + */ + bool operator==(const SemanticVersion& other) const; + + /** + * Construct a SemanticVersion object from a SemanticVersion protobuf object. + * + * @param proto The SemanticVersion protobuf object from which to construct a SemanticVersion object. + * @return The constructed SemanticVersion object. + */ + [[nodiscard]] static SemanticVersion fromProtobuf(const proto::SemanticVersion& proto); + + /** + * Construct a SemanticVersion object from a byte array. + * + * @param bytes The byte array representing a SemanticVersion object. + * @return The constructed SemanticVersion object. + */ + [[nodiscard]] static SemanticVersion fromBytes(const std::vector& bytes); + + /** + * Construct a SemanticVersion protobuf object from this SemanticVersion object. + * + * @return A pointer to the created SemanticVersion protobuf object. + */ + [[nodiscard]] std::unique_ptr toProtobuf() const; + + /** + * Construct a representative byte array from this SemanticVersion object. + * + * @return The byte array representing this SemanticVersion object. + */ + [[nodiscard]] std::vector toBytes() const; + + /** + * Construct a string representation of this SemanticVersion object. + * + * @return The string representation of this SemanticVersion object. + */ + [[nodiscard]] std::string toString() const; + + /** + * Major number. Increases with incompatible API changes. + */ + int mMajor = 0; + + /** + * Minor number. Increases with backwards-compatible new functionality. + */ + int mMinor = 0; + + /** + * Patch number. Increases with backwards-compatible bug fixes. + */ + int mPatch = 0; + + /** + * Pre-release version. This may be denoted by appending a hyphen and a series of dot-separated identifiers. + */ + std::string mPre; + + /** + * Build metadata. This may be denoted by appending a plus sign and a series of dot-separated identifiers immediately + * following the patch or pre-release version. + */ + std::string mBuild; +}; + +} // namespace Hedera + +#endif // HEDERA_SDK_CPP_SEMANTIC_VERSION_H_ diff --git a/sdk/main/include/TransferTransaction.h b/sdk/main/include/TransferTransaction.h index 1384aa926..bc0d25a27 100644 --- a/sdk/main/include/TransferTransaction.h +++ b/sdk/main/include/TransferTransaction.h @@ -203,6 +203,7 @@ class TransferTransaction : public Transaction friend class ContractInfoQuery; friend class FileContentsQuery; friend class FileInfoQuery; + friend class NetworkVersionInfoQuery; friend class ScheduleInfoQuery; friend class TokenInfoQuery; friend class TokenNftInfoQuery; diff --git a/sdk/main/include/impl/Node.h b/sdk/main/include/impl/Node.h index 0a68dbb0b..316a908ee 100644 --- a/sdk/main/include/impl/Node.h +++ b/sdk/main/include/impl/Node.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -202,6 +203,11 @@ class Node */ std::unique_ptr mFreezeStub = nullptr; + /** + * Pointer to the gRPC stub used to communicate with the network service living on the remote node. + */ + std::unique_ptr mNetworkStub = nullptr; + /** * Pointer to the gRPC stub used to communicate with the schedule service living on the remote node. */ diff --git a/sdk/main/src/Executable.cc b/sdk/main/src/Executable.cc index 9a3588643..12fbcd353 100644 --- a/sdk/main/src/Executable.cc +++ b/sdk/main/src/Executable.cc @@ -48,6 +48,8 @@ #include "FileInfoQuery.h" #include "FileUpdateTransaction.h" #include "FreezeTransaction.h" +#include "NetworkVersionInfo.h" +#include "NetworkVersionInfoQuery.h" #include "ScheduleCreateTransaction.h" #include "ScheduleDeleteTransaction.h" #include "ScheduleInfo.h" @@ -378,6 +380,7 @@ template class Executable; template class Executable; template class Executable; +template class Executable; template class Executable + +namespace Hedera +{ +//----- +NetworkVersionInfo::NetworkVersionInfo(const SemanticVersion& hapi, const SemanticVersion& hedera) + : mProtobufVersion(hapi) + , mServicesVersion(hedera) +{ +} + +//----- +NetworkVersionInfo NetworkVersionInfo::fromProtobuf(const proto::NetworkGetVersionInfoResponse& proto) +{ + return NetworkVersionInfo(SemanticVersion::fromProtobuf(proto.hapiprotoversion()), + SemanticVersion::fromProtobuf(proto.hederaservicesversion())); +} + +//----- +NetworkVersionInfo NetworkVersionInfo::fromBytes(const std::vector& bytes) +{ + proto::NetworkGetVersionInfoResponse networkGetVersionInfoResponse; + networkGetVersionInfoResponse.ParseFromArray(bytes.data(), static_cast(bytes.size())); + return fromProtobuf(networkGetVersionInfoResponse); +} + +//----- +std::unique_ptr NetworkVersionInfo::toProtobuf() const +{ + auto proto = std::make_unique(); + proto->set_allocated_hapiprotoversion(mProtobufVersion.toProtobuf().release()); + proto->set_allocated_hederaservicesversion(mServicesVersion.toProtobuf().release()); + return proto; +} + +//----- +std::vector NetworkVersionInfo::toBytes() const +{ + return internal::Utilities::stringToByteVector(toProtobuf()->SerializeAsString()); +} + +} // namespace Hedera diff --git a/sdk/main/src/NetworkVersionInfoQuery.cc b/sdk/main/src/NetworkVersionInfoQuery.cc new file mode 100644 index 000000000..123a2d815 --- /dev/null +++ b/sdk/main/src/NetworkVersionInfoQuery.cc @@ -0,0 +1,78 @@ +/*- + * + * 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. + * + */ +#include "NetworkVersionInfoQuery.h" +#include "Client.h" +#include "NetworkVersionInfo.h" +#include "Status.h" +#include "TransferTransaction.h" +#include "impl/Node.h" + +#include +#include +#include +#include + +namespace Hedera +{ +//----- +proto::Query NetworkVersionInfoQuery::makeRequest(const Client& client, + const std::shared_ptr& node) const +{ + proto::Query query; + proto::NetworkGetVersionInfoQuery* getNetworkVersionInfoQuery = query.mutable_networkgetversioninfo(); + + proto::QueryHeader* header = getNetworkVersionInfoQuery->mutable_header(); + header->set_responsetype(proto::ANSWER_ONLY); + + TransferTransaction tx = TransferTransaction() + .setTransactionId(TransactionId::generate(*client.getOperatorAccountId())) + .setNodeAccountIds({ node->getAccountId() }) + .setMaxTransactionFee(Hbar(1LL)) + .addHbarTransfer(*client.getOperatorAccountId(), Hbar(-1LL)) + .addHbarTransfer(node->getAccountId(), Hbar(1LL)); + tx.onSelectNode(node); + header->set_allocated_payment(new proto::Transaction(tx.makeRequest(client, node))); + + return query; +} + +//----- +NetworkVersionInfo NetworkVersionInfoQuery::mapResponse(const proto::Response& response) const +{ + return NetworkVersionInfo::fromProtobuf(response.networkgetversioninfo()); +} + +//----- +Status NetworkVersionInfoQuery::mapResponseStatus(const proto::Response& response) const +{ + return gProtobufResponseCodeToStatus.at(response.networkgetversioninfo().header().nodetransactionprecheckcode()); +} + +//----- +grpc::Status NetworkVersionInfoQuery::submitRequest(const Client& client, + const std::chrono::system_clock::time_point& deadline, + const std::shared_ptr& node, + proto::Response* response) const +{ + return node->submitQuery( + proto::Query::QueryCase::kNetworkGetVersionInfo, makeRequest(client, node), deadline, response); +} + +} // namespace Hedera diff --git a/sdk/main/src/Query.cc b/sdk/main/src/Query.cc index 0d31d4b06..320af651c 100644 --- a/sdk/main/src/Query.cc +++ b/sdk/main/src/Query.cc @@ -29,6 +29,8 @@ #include "FileContentsQuery.h" #include "FileInfo.h" #include "FileInfoQuery.h" +#include "NetworkVersionInfo.h" +#include "NetworkVersionInfoQuery.h" #include "ScheduleInfo.h" #include "ScheduleInfoQuery.h" #include "TokenInfo.h" @@ -53,6 +55,7 @@ template class Query; template class Query; template class Query; template class Query; +template class Query; template class Query; template class Query; template class Query; diff --git a/sdk/main/src/SemanticVersion.cc b/sdk/main/src/SemanticVersion.cc new file mode 100644 index 000000000..b718c0b60 --- /dev/null +++ b/sdk/main/src/SemanticVersion.cc @@ -0,0 +1,94 @@ +/*- + * + * 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. + * + */ +#include "SemanticVersion.h" +#include "impl/Utilities.h" + +#include + +namespace Hedera +{ +//----- +SemanticVersion::SemanticVersion(int major, int minor, int patch, std::string_view pre, std::string_view build) + : mMajor(major) + , mMinor(minor) + , mPatch(patch) + , mPre(pre) + , mBuild(build) +{ +} + +//----- +bool SemanticVersion::operator==(const SemanticVersion& other) const +{ + return (mMajor == other.mMajor) && (mMinor == other.mMinor) && (mPatch == other.mPatch) && (mPre == other.mPre) && + (mBuild == other.mBuild); +} + +//----- +SemanticVersion SemanticVersion::fromProtobuf(const proto::SemanticVersion& proto) +{ + return { proto.major(), proto.minor(), proto.patch(), proto.pre(), proto.build() }; +} + +//----- +SemanticVersion SemanticVersion::fromBytes(const std::vector& bytes) +{ + proto::SemanticVersion semanticVersion; + semanticVersion.ParseFromArray(bytes.data(), static_cast(bytes.size())); + return fromProtobuf(semanticVersion); +} + +//----- +std::unique_ptr SemanticVersion::toProtobuf() const +{ + auto proto = std::make_unique(); + proto->set_major(mMajor); + proto->set_minor(mMinor); + proto->set_patch(mPatch); + proto->set_pre(mPre); + proto->set_build(mBuild); + return proto; +} + +//----- +std::vector SemanticVersion::toBytes() const +{ + return internal::Utilities::stringToByteVector(toProtobuf()->SerializeAsString()); +} + +//----- +std::string SemanticVersion::toString() const +{ + std::string str = std::to_string(mMajor) + '.' + std::to_string(mMinor) + '.' + std::to_string(mPatch); + + if (!mPre.empty()) + { + str += '-' + mPre; + } + + if (!mBuild.empty()) + { + str += '+' + mBuild; + } + + return str; +} + +} // namespace Hedera diff --git a/sdk/main/src/impl/Node.cc b/sdk/main/src/impl/Node.cc index 79e2ac23a..f9e844192 100644 --- a/sdk/main/src/impl/Node.cc +++ b/sdk/main/src/impl/Node.cc @@ -76,24 +76,29 @@ void Node::shutdown() mCryptoStub.reset(); } - if (mScheduleStub) + if (mFileStub) { - mScheduleStub.reset(); + mFileStub.reset(); } - if (mSmartContractStub) + if (mFreezeStub) { - mSmartContractStub.reset(); + mFreezeStub.reset(); } - if (mFileStub) + if (mNetworkStub) { - mFileStub.reset(); + mNetworkStub.reset(); } - if (mFreezeStub) + if (mScheduleStub) { - mFreezeStub.reset(); + mScheduleStub.reset(); + } + + if (mSmartContractStub) + { + mSmartContractStub.reset(); } if (mTokenStub) @@ -147,6 +152,8 @@ grpc::Status Node::submitQuery(proto::Query::QueryCase funcEnum, return mFileStub->getFileContent(&context, query, response); case proto::Query::QueryCase::kFileGetInfo: return mFileStub->getFileInfo(&context, query, response); + case proto::Query::QueryCase::kNetworkGetVersionInfo: + return mNetworkStub->getVersionInfo(&context, query, response); case proto::Query::QueryCase::kScheduleGetInfo: return mScheduleStub->getScheduleInfo(&context, query, response); case proto::Query::QueryCase::kTokenGetInfo: @@ -380,6 +387,7 @@ bool Node::initializeChannel(const std::chrono::system_clock::time_point& deadli mCryptoStub = proto::CryptoService::NewStub(mChannel); mFileStub = proto::FileService::NewStub(mChannel); mFreezeStub = proto::FreezeService::NewStub(mChannel); + mNetworkStub = proto::NetworkService::NewStub(mChannel); mScheduleStub = proto::ScheduleService::NewStub(mChannel); mSmartContractStub = proto::SmartContractService::NewStub(mChannel); mTokenStub = proto::TokenService::NewStub(mChannel); diff --git a/sdk/tests/integration/CMakeLists.txt b/sdk/tests/integration/CMakeLists.txt index ed470324f..a52e1cb9f 100644 --- a/sdk/tests/integration/CMakeLists.txt +++ b/sdk/tests/integration/CMakeLists.txt @@ -26,6 +26,7 @@ add_executable(${TEST_PROJECT_NAME} FileUpdateTransactionIntegrationTests.cc FreezeTransactionIntegrationTest.cc JSONIntegrationTest.cc + NetworkVersionInfoQueryIntegrationTests.cc ScheduleCreateTransactionIntegrationTests.cc ScheduleDeleteTransactionIntegrationTests.cc ScheduleInfoQueryIntegrationTests.cc diff --git a/sdk/tests/integration/NetworkVersionInfoQueryIntegrationTests.cc b/sdk/tests/integration/NetworkVersionInfoQueryIntegrationTests.cc new file mode 100644 index 000000000..48e017a91 --- /dev/null +++ b/sdk/tests/integration/NetworkVersionInfoQueryIntegrationTests.cc @@ -0,0 +1,38 @@ +/*- + * + * 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. + * + */ +#include "BaseIntegrationTest.h" +#include "NetworkVersionInfo.h" +#include "NetworkVersionInfoQuery.h" +#include "TransactionReceipt.h" + +#include + +using namespace Hedera; + +class NetworkVersionInfoQueryIntegrationTest : public BaseIntegrationTest +{ +}; + +//----- +TEST_F(NetworkVersionInfoQueryIntegrationTest, ExecuteNetworkVersionInfoQuery) +{ + // Given / When / Then + EXPECT_NO_THROW(const NetworkVersionInfo networkVersionInfo = NetworkVersionInfoQuery().execute(getTestClient())); +} diff --git a/sdk/tests/unit/CMakeLists.txt b/sdk/tests/unit/CMakeLists.txt index 086f95553..53e32d79f 100644 --- a/sdk/tests/unit/CMakeLists.txt +++ b/sdk/tests/unit/CMakeLists.txt @@ -59,6 +59,7 @@ add_executable(${TEST_PROJECT_NAME} HbarTransferTest.cc KeyListTest.cc LedgerIdTest.cc + NetworkVersionInfoUnitTests.cc NftIdTest.cc NodeAddressTest.cc ScheduleCreateTransactionUnitTests.cc @@ -67,6 +68,7 @@ add_executable(${TEST_PROJECT_NAME} ScheduleInfoQueryUnitTests.cc ScheduleInfoUnitTests.cc ScheduleSignTransactionUnitTests.cc + SemanticVersionUnitTests.cc StakingInfoTest.cc SystemDeleteTransactionUnitTests.cc SystemUndeleteTransactionUnitTests.cc diff --git a/sdk/tests/unit/NetworkVersionInfoUnitTests.cc b/sdk/tests/unit/NetworkVersionInfoUnitTests.cc new file mode 100644 index 000000000..a99715f1d --- /dev/null +++ b/sdk/tests/unit/NetworkVersionInfoUnitTests.cc @@ -0,0 +1,118 @@ +/*- + * + * 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. + * + */ +#include "NetworkVersionInfo.h" +#include "SemanticVersion.h" +#include "impl/Utilities.h" + +#include +#include + +using namespace Hedera; + +class NetworkVersionInfoTest : public ::testing::Test +{ +protected: + [[nodiscard]] inline const SemanticVersion& getTestHapiVersion() const { return mTestHapiVersion; } + [[nodiscard]] inline const SemanticVersion& getTestServicesVersion() const { return mTestServicesVersion; } + +private: + const SemanticVersion mTestHapiVersion = SemanticVersion(1, 2, 3); + const SemanticVersion mTestServicesVersion = SemanticVersion(4, 5, 6); +}; + +//----- +TEST_F(NetworkVersionInfoTest, ConstructWithValues) +{ + // Given / When + const NetworkVersionInfo networkVersionInfo(getTestHapiVersion(), getTestServicesVersion()); + + // Then + EXPECT_EQ(networkVersionInfo.mProtobufVersion, getTestHapiVersion()); + EXPECT_EQ(networkVersionInfo.mServicesVersion, getTestServicesVersion()); +} + +//----- +TEST_F(NetworkVersionInfoTest, FromProtobuf) +{ + // Given + proto::NetworkGetVersionInfoResponse networkGetVersionInfoResponse; + networkGetVersionInfoResponse.set_allocated_hapiprotoversion(getTestHapiVersion().toProtobuf().release()); + networkGetVersionInfoResponse.set_allocated_hederaservicesversion(getTestServicesVersion().toProtobuf().release()); + + // When + const NetworkVersionInfo networkVersionInfo = NetworkVersionInfo::fromProtobuf(networkGetVersionInfoResponse); + + // Then + EXPECT_EQ(networkVersionInfo.mProtobufVersion, getTestHapiVersion()); + EXPECT_EQ(networkVersionInfo.mServicesVersion, getTestServicesVersion()); +} + +//----- +TEST_F(NetworkVersionInfoTest, FromBytes) +{ + // Given + proto::NetworkGetVersionInfoResponse networkGetVersionInfoResponse; + networkGetVersionInfoResponse.set_allocated_hapiprotoversion(getTestHapiVersion().toProtobuf().release()); + networkGetVersionInfoResponse.set_allocated_hederaservicesversion(getTestServicesVersion().toProtobuf().release()); + + // When + const NetworkVersionInfo networkVersionInfo = NetworkVersionInfo::fromBytes( + internal::Utilities::stringToByteVector(networkGetVersionInfoResponse.SerializeAsString())); + + // Then + EXPECT_EQ(networkVersionInfo.mProtobufVersion, getTestHapiVersion()); + EXPECT_EQ(networkVersionInfo.mServicesVersion, getTestServicesVersion()); +} + +//----- +TEST_F(NetworkVersionInfoTest, ToProtobuf) +{ + // Given + const NetworkVersionInfo networkVersionInfo(getTestHapiVersion(), getTestServicesVersion()); + + // When + const std::unique_ptr protoNetworkVersionInfo = networkVersionInfo.toProtobuf(); + + // Then + EXPECT_EQ(protoNetworkVersionInfo->hapiprotoversion().major(), getTestHapiVersion().mMajor); + EXPECT_EQ(protoNetworkVersionInfo->hapiprotoversion().minor(), getTestHapiVersion().mMinor); + EXPECT_EQ(protoNetworkVersionInfo->hapiprotoversion().patch(), getTestHapiVersion().mPatch); + EXPECT_EQ(protoNetworkVersionInfo->hapiprotoversion().pre(), getTestHapiVersion().mPre); + EXPECT_EQ(protoNetworkVersionInfo->hapiprotoversion().build(), getTestHapiVersion().mBuild); + + EXPECT_EQ(protoNetworkVersionInfo->hederaservicesversion().major(), getTestServicesVersion().mMajor); + EXPECT_EQ(protoNetworkVersionInfo->hederaservicesversion().minor(), getTestServicesVersion().mMinor); + EXPECT_EQ(protoNetworkVersionInfo->hederaservicesversion().patch(), getTestServicesVersion().mPatch); + EXPECT_EQ(protoNetworkVersionInfo->hederaservicesversion().pre(), getTestServicesVersion().mPre); + EXPECT_EQ(protoNetworkVersionInfo->hederaservicesversion().build(), getTestServicesVersion().mBuild); +} + +//----- +TEST_F(NetworkVersionInfoTest, ToBytes) +{ + // Given + const NetworkVersionInfo networkVersionInfo(getTestHapiVersion(), getTestServicesVersion()); + + // When + const std::vector bytes = networkVersionInfo.toBytes(); + + // Then + EXPECT_EQ(bytes, internal::Utilities::stringToByteVector(networkVersionInfo.toProtobuf()->SerializeAsString())); +} diff --git a/sdk/tests/unit/SemanticVersionUnitTests.cc b/sdk/tests/unit/SemanticVersionUnitTests.cc new file mode 100644 index 000000000..e538df703 --- /dev/null +++ b/sdk/tests/unit/SemanticVersionUnitTests.cc @@ -0,0 +1,136 @@ +/*- + * + * 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. + * + */ +#include "SemanticVersion.h" +#include "impl/Utilities.h" + +#include +#include +#include + +using namespace Hedera; + +class SemanticVersionTest : public ::testing::Test +{ +protected: + [[nodiscard]] inline const int& getTestMajor() const { return mTestMajor; } + [[nodiscard]] inline const int& getTestMinor() const { return mTestMinor; } + [[nodiscard]] inline const int& getTestPatch() const { return mTestPatch; } + [[nodiscard]] inline const std::string& getTestPrerelease() const { return mTestPrerelease; } + [[nodiscard]] inline const std::string& getTestBuildMetadata() const { return mTestBuildMetadata; } + +private: + const int mTestMajor = 1; + const int mTestMinor = 2; + const int mTestPatch = 3; + const std::string mTestPrerelease = "test pre-release"; + const std::string mTestBuildMetadata = "test build metadata"; +}; + +//----- +TEST_F(SemanticVersionTest, ConstructWithValues) +{ + // Given / When + const SemanticVersion semanticVersion( + getTestMajor(), getTestMinor(), getTestPatch(), getTestPrerelease(), getTestBuildMetadata()); + + // Then + EXPECT_EQ(semanticVersion.mMajor, getTestMajor()); + EXPECT_EQ(semanticVersion.mMinor, getTestMinor()); + EXPECT_EQ(semanticVersion.mPatch, getTestPatch()); + EXPECT_EQ(semanticVersion.mPre, getTestPrerelease()); + EXPECT_EQ(semanticVersion.mBuild, getTestBuildMetadata()); +} + +//----- +TEST_F(SemanticVersionTest, FromProtobuf) +{ + // Given + proto::SemanticVersion protoSemanticVersion; + protoSemanticVersion.set_major(getTestMajor()); + protoSemanticVersion.set_minor(getTestMinor()); + protoSemanticVersion.set_patch(getTestPatch()); + protoSemanticVersion.set_pre(getTestPrerelease()); + protoSemanticVersion.set_build(getTestBuildMetadata()); + + // When + const SemanticVersion semanticVersion = SemanticVersion::fromProtobuf(protoSemanticVersion); + + // Then + EXPECT_EQ(semanticVersion.mMajor, getTestMajor()); + EXPECT_EQ(semanticVersion.mMinor, getTestMinor()); + EXPECT_EQ(semanticVersion.mPatch, getTestPatch()); + EXPECT_EQ(semanticVersion.mPre, getTestPrerelease()); + EXPECT_EQ(semanticVersion.mBuild, getTestBuildMetadata()); +} + +//----- +TEST_F(SemanticVersionTest, FromBytes) +{ + // Given + proto::SemanticVersion protoSemanticVersion; + protoSemanticVersion.set_major(getTestMajor()); + protoSemanticVersion.set_minor(getTestMinor()); + protoSemanticVersion.set_patch(getTestPatch()); + protoSemanticVersion.set_pre(getTestPrerelease()); + protoSemanticVersion.set_build(getTestBuildMetadata()); + + // When + const SemanticVersion semanticVersion = + SemanticVersion::fromBytes(internal::Utilities::stringToByteVector(protoSemanticVersion.SerializeAsString())); + + // Then + EXPECT_EQ(semanticVersion.mMajor, getTestMajor()); + EXPECT_EQ(semanticVersion.mMinor, getTestMinor()); + EXPECT_EQ(semanticVersion.mPatch, getTestPatch()); + EXPECT_EQ(semanticVersion.mPre, getTestPrerelease()); + EXPECT_EQ(semanticVersion.mBuild, getTestBuildMetadata()); +} + +//----- +TEST_F(SemanticVersionTest, ToProtobuf) +{ + // Given + const SemanticVersion semanticVersion( + getTestMajor(), getTestMinor(), getTestPatch(), getTestPrerelease(), getTestBuildMetadata()); + + // When + const std::unique_ptr protoSemanticVersion = semanticVersion.toProtobuf(); + + // Then + EXPECT_EQ(protoSemanticVersion->major(), getTestMajor()); + EXPECT_EQ(protoSemanticVersion->minor(), getTestMinor()); + EXPECT_EQ(protoSemanticVersion->patch(), getTestPatch()); + EXPECT_EQ(protoSemanticVersion->pre(), getTestPrerelease()); + EXPECT_EQ(protoSemanticVersion->build(), getTestBuildMetadata()); +} + +//----- +TEST_F(SemanticVersionTest, ToBytes) +{ + // Given + const SemanticVersion semanticVersion( + getTestMajor(), getTestMinor(), getTestPatch(), getTestPrerelease(), getTestBuildMetadata()); + + // When + const std::vector bytes = semanticVersion.toBytes(); + + // Then + EXPECT_EQ(bytes, internal::Utilities::stringToByteVector(semanticVersion.toProtobuf()->SerializeAsString())); +}