diff --git a/src/tck/include/file/FileService.h b/src/tck/include/file/FileService.h index df1ec90ed..14574d92a 100644 --- a/src/tck/include/file/FileService.h +++ b/src/tck/include/file/FileService.h @@ -9,10 +9,21 @@ namespace Hiero::TCK::FileService /** * Forward declarations. */ +struct AppendFileParams; struct CreateFileParams; struct DeleteFileParams; +struct GetFileContentsParams; +struct GetFileInfoParams; struct UpdateFileParams; +/** + * The parameters to use to append a file. + * + * @param params The parameters use to append a file. + * @return A JSON response containing the status of the appended file. + */ +nlohmann::json appendFile(const AppendFileParams& params); + /** * Create a file. * @@ -22,13 +33,29 @@ struct UpdateFileParams; nlohmann::json createFile(const CreateFileParams& params); /** - * delete a file. + * Delete a file. * * @param params The parameters to use to delete a file. * @return A JSON response containing the status of the file deletion. */ nlohmann::json deleteFile(const DeleteFileParams& params); +/** + * Get file contents. + * + * @param params The parameters to use to get the content of file. + * @return A JSON response containing the file contents. + */ +nlohmann::json getFileContents(const GetFileContentsParams& params); + +/** + * Get file info. + * + * @param params The parameters to use to get file info. + * @return A JSON response containing the file info. + */ +nlohmann::json getFileInfo(const GetFileInfoParams& params); + /** * Update a file. * diff --git a/src/tck/include/file/params/AppendFileParams.h b/src/tck/include/file/params/AppendFileParams.h new file mode 100644 index 000000000..c0fc453b2 --- /dev/null +++ b/src/tck/include/file/params/AppendFileParams.h @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: Apache-2.0 +#ifndef HIERO_TCK_CPP_APPEND_FILE_PARAMS_H_ +#define HIERO_TCK_CPP_APPEND_FILE_PARAMS_H_ + +#include "common/CommonTransactionParams.h" +#include "json/JsonUtils.h" + +#include +#include +#include + +namespace Hiero::TCK::FileService +{ +/** + * Struct to hold the arguments for an `appendFile` JSON-RPC method call. + */ +struct AppendFileParams +{ + /** + * The ID of the file to append to. + */ + std::optional mFileId; + + /** + * The contents of the file. + */ + std::string mContents; + + /** + * Maximum number of chunks allowed for this transaction + */ + std::optional mMaxChunks; + + /** + * Size of each chunk in bytes + */ + std::optional mChunkSize; + + /** + * Any parameters common to all transaction types. + */ + std::optional mCommonTxParams; +}; + +} // namespace Hiero::TCK::FileService + +namespace nlohmann +{ +/** + * JSON serializer template specialisation required to convert AppendFileParams argument Properly + */ +template<> +struct [[maybe_unused]] adl_serializer +{ + /** + * Convert a Json Object to a AppendFileParams + * + * @param jsonFrom The JSON object with which to fill the AppendFileParams + * @param params The AppendFileParams to fill with the JSON object. + */ + static void from_json(const json& jsonFrom, Hiero::TCK::FileService::AppendFileParams& params) + { + params.mFileId = Hiero::TCK::getOptionalJsonParameter(jsonFrom, "fileId"); + + params.mContents = Hiero::TCK::getRequiredJsonParameter(jsonFrom, "contents"); + + params.mMaxChunks = Hiero::TCK::getOptionalJsonParameter(jsonFrom, "maxChunks"); + + params.mChunkSize = Hiero::TCK::getOptionalJsonParameter(jsonFrom, "chunkSize"); + + params.mCommonTxParams = + Hiero::TCK::getOptionalJsonParameter(jsonFrom, "commonTransactionParams"); + } +}; + +} // namespace nlohmann + +#endif // HIERO_TCK_CPP_APPEND_FILE_PARAMS_H_ diff --git a/src/tck/include/file/params/GetFileContentsParams.h b/src/tck/include/file/params/GetFileContentsParams.h new file mode 100644 index 000000000..f48ff257d --- /dev/null +++ b/src/tck/include/file/params/GetFileContentsParams.h @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: Apache-2.0 +#ifndef HIERO_TCK_CPP_GET_FILE_CONTENTS_PARAMS_H_ +#define HIERO_TCK_CPP_GET_FILE_CONTENTS_PARAMS_H_ + +#include "json/JsonUtils.h" + +#include +#include +#include + +namespace Hiero::TCK::FileService +{ +/** + * Struct to hold the arguments for a `getFileContents` JSON-RPC method call. + */ +struct GetFileContentsParams +{ + /** + * The ID of the file to query. + */ + std::string mFileId; + + /** + * Explicit payment amount for the query in tinybars. + */ + std::optional mQueryPayment; + + /** + * Maximum payment amount for the query in tinybars. + */ + std::optional mMaxQueryPayment; +}; + +} // namespace Hiero::TCK::FileService + +namespace nlohmann +{ +/** + * JSON serializer template specialisation required to convert GetFileContentsParams arguments properly + */ +template<> +struct [[maybe_unused]] adl_serializer +{ + /** + * Convert a Json Object to a GetFileContentsParams + * + * @param jsonFrom The JSON object with which to fill the GetFileContentsParams + * @param params The GetFileContentsParams to fill with the JSON object. + */ + static void from_json(const json& jsonFrom, Hiero::TCK::FileService::GetFileContentsParams& params) + { + params.mFileId = Hiero::TCK::getRequiredJsonParameter(jsonFrom, "fileId"); + + params.mQueryPayment = Hiero::TCK::getOptionalJsonParameter(jsonFrom, "queryPayment"); + + params.mMaxQueryPayment = Hiero::TCK::getOptionalJsonParameter(jsonFrom, "maxQueryPayment"); + } +}; + +} // namespace nlohmann + +#endif // HIERO_TCK_CPP_GET_FILE_CONTENTS_PARAMS_H_ diff --git a/src/tck/include/file/params/GetFileInfoParams.h b/src/tck/include/file/params/GetFileInfoParams.h new file mode 100644 index 000000000..eec5f297b --- /dev/null +++ b/src/tck/include/file/params/GetFileInfoParams.h @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: Apache-2.0 +#ifndef HIERO_TCK_CPP_GET_FILE_INFO_PARAMS_H_ +#define HIERO_TCK_CPP_GET_FILE_INFO_PARAMS_H_ + +#include "json/JsonUtils.h" + +#include +#include +#include + +namespace Hiero::TCK::FileService +{ +/** + * Struct to hold the arguments for a `getFileInfo` JSON-RPC method call. + */ +struct GetFileInfoParams +{ + /** + * The ID of the file to query. + */ + std::optional mFileId; +}; + +} // namespace Hiero::TCK::FileService + +namespace nlohmann +{ +/** + * JSON serializer template specialization required to convert GetFileInfoParams arguments properly. + */ +template<> +struct [[maybe_unused]] adl_serializer +{ + static void from_json(const json& jsonFrom, Hiero::TCK::FileService::GetFileInfoParams& params) + { + params.mFileId = Hiero::TCK::getOptionalJsonParameter(jsonFrom, "fileId"); + } +}; + +} // namespace nlohmann + +#endif // HIERO_TCK_CPP_GET_FILE_INFO_PARAMS_H_ diff --git a/src/tck/src/TckServer.cc b/src/tck/src/TckServer.cc index d833d9152..b66af7e02 100644 --- a/src/tck/src/TckServer.cc +++ b/src/tck/src/TckServer.cc @@ -13,8 +13,11 @@ #include "account/params/TransferCryptoParams.h" #include "account/params/UpdateAccountParams.h" #include "file/FileService.h" +#include "file/params/AppendFileParams.h" #include "file/params/CreateFileParams.h" #include "file/params/DeleteFileParams.h" +#include "file/params/GetFileContentsParams.h" +#include "file/params/GetFileInfoParams.h" #include "file/params/UpdateFileParams.h" #include "key/KeyService.h" #include "key/params/GenerateKeyParams.h" @@ -95,8 +98,11 @@ TckServer::TckServer(int port) mJsonRpcParser.addMethod("wipeToken", getHandle(TokenService::wipeToken)); // Add the FileService functions. + mJsonRpcParser.addMethod("appendFile", getHandle(FileService::appendFile)); mJsonRpcParser.addMethod("createFile", getHandle(FileService::createFile)); mJsonRpcParser.addMethod("deleteFile", getHandle(FileService::deleteFile)); + mJsonRpcParser.addMethod("getFileContents", getHandle(FileService::getFileContents)); + mJsonRpcParser.addMethod("getFileInfo", getHandle(FileService::getFileInfo)); mJsonRpcParser.addMethod("updateFile", getHandle(FileService::updateFile)); setupHttpHandler(); @@ -207,10 +213,16 @@ template TckServer::MethodHandle TckServer::getHandle( nlohmann::json (*method)(const TokenService::ClaimAirdropParams&)); +template TckServer::MethodHandle TckServer::getHandle( + nlohmann::json (*method)(const FileService::AppendFileParams&)); template TckServer::MethodHandle TckServer::getHandle( nlohmann::json (*method)(const FileService::CreateFileParams&)); template TckServer::MethodHandle TckServer::getHandle( nlohmann::json (*method)(const FileService::DeleteFileParams&)); +template TckServer::MethodHandle TckServer::getHandle( + nlohmann::json (*method)(const FileService::GetFileContentsParams&)); +template TckServer::MethodHandle TckServer::getHandle( + nlohmann::json (*method)(const FileService::GetFileInfoParams&)); template TckServer::MethodHandle TckServer::getHandle( nlohmann::json (*method)(const FileService::UpdateFileParams&)); diff --git a/src/tck/src/file/FileService.cc b/src/tck/src/file/FileService.cc index e9d3ac16c..64f217ef7 100644 --- a/src/tck/src/file/FileService.cc +++ b/src/tck/src/file/FileService.cc @@ -1,17 +1,25 @@ // SPDX-License-Identifier: Apache-2.0 #include "file/FileService.h" +#include "file/params/AppendFileParams.h" #include "file/params/CreateFileParams.h" #include "file/params/DeleteFileParams.h" +#include "file/params/GetFileContentsParams.h" +#include "file/params/GetFileInfoParams.h" #include "file/params/UpdateFileParams.h" #include "key/KeyService.h" #include "sdk/SdkClient.h" #include "json/JsonErrorType.h" #include "json/JsonRpcException.h" +#include +#include #include #include #include +#include +#include #include +#include #include #include #include @@ -19,6 +27,7 @@ #include #include #include +#include #include #include @@ -31,6 +40,41 @@ namespace Hiero::TCK::FileService { +//----- +nlohmann::json appendFile(const AppendFileParams& params) +{ + FileAppendTransaction fileAppendTransaction; + fileAppendTransaction.setGrpcDeadline(SdkClient::DEFAULT_TCK_REQUEST_TIMEOUT); + + if (params.mFileId.has_value()) + { + fileAppendTransaction.setFileId(FileId::fromString(params.mFileId.value())); + } + + fileAppendTransaction.setContents(params.mContents); + + if (params.mMaxChunks.has_value()) + { + fileAppendTransaction.setMaxChunks(static_cast(params.mMaxChunks.value())); + } + + if (params.mChunkSize.has_value()) + { + fileAppendTransaction.setChunkSize(static_cast(params.mChunkSize.value())); + } + + if (params.mCommonTxParams.has_value()) + { + params.mCommonTxParams->fillOutTransaction(fileAppendTransaction, SdkClient::getClient()); + } + + return { + {"status", + gStatusToString.at( + fileAppendTransaction.execute(SdkClient::getClient()).getReceipt(SdkClient::getClient()).mStatus)} + }; +} + //----- nlohmann::json createFile(const CreateFileParams& params) { @@ -101,6 +145,73 @@ nlohmann::json deleteFile(const DeleteFileParams& params) }; } +//----- +nlohmann::json getFileContents(const GetFileContentsParams& params) +{ + FileContentsQuery query; + query.setGrpcDeadline(SdkClient::DEFAULT_TCK_REQUEST_TIMEOUT); + + query.setFileId(FileId::fromString(params.mFileId)); + + // Parsing the tinybar string to int64_t and set the query payment + if (params.mQueryPayment.has_value()) + { + query.setQueryPayment(Hbar::fromTinybars(std::stoll(params.mQueryPayment.value()))); + } + + // Parsing the tinybar string to int64_t and set the max query payment + if (params.mMaxQueryPayment.has_value()) + { + query.setMaxQueryPayment(Hbar::fromTinybars(std::stoll(params.mMaxQueryPayment.value()))); + } + + // Execute + const std::vector contents = query.execute(SdkClient::getClient()); + + return { + {"contents", internal::Utilities::byteVectorToString(contents)} + }; +} + +//----- +nlohmann::json getFileInfo(const GetFileInfoParams& params) +{ + FileInfoQuery query; + query.setGrpcDeadline(SdkClient::DEFAULT_TCK_REQUEST_TIMEOUT); + + if (params.mFileId.has_value()) + { + query.setFileId(FileId::fromString(params.mFileId.value())); + } + + const FileInfo info = query.execute(SdkClient::getClient()); + + nlohmann::json response; + + response["fileId"] = info.mFileId.toString(); + response["size"] = std::to_string(info.mSize); + response["isDeleted"] = info.mIsDeleted; + + auto expirySeconds = + std::chrono::duration_cast(info.mExpirationTime.time_since_epoch()).count(); + response["expirationTime"] = std::to_string(expirySeconds); + + response["memo"] = info.mMemo; + response["ledgerId"] = info.mLedgerId.toString(); + + // Handle the keys + response["keys"] = nlohmann::json::array(); + if (info.mAdminKeys && !info.mAdminKeys->empty()) + { + auto protoKeyList = info.mAdminKeys->toProtobufKey(); + std::string protoBytes; + protoKeyList->SerializeToString(&protoBytes); + response["keys"].push_back( + internal::HexConverter::bytesToHex(std::vector(protoBytes.begin(), protoBytes.end()))); + } + return response; +} + //----- nlohmann::json updateFile(const UpdateFileParams& params) {