Skip to content

Commit 1250930

Browse files
authored
feat: Add FileUpdateTransaction JSON-RPC endpoint to TCK (#997)
Signed-off-by: b-l-u-e <winnie.gitau282@gmail.com>
1 parent d51f293 commit 1250930

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

src/tck/include/file/FileService.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace Hiero::TCK::FileService
1010
* Forward declarations.
1111
*/
1212
struct CreateFileParams;
13+
struct UpdateFileParams;
1314

1415
/**
1516
* Create a file.
@@ -19,6 +20,14 @@ struct CreateFileParams;
1920
*/
2021
nlohmann::json createFile(const CreateFileParams& params);
2122

23+
/**
24+
* Update a file.
25+
*
26+
* @param params The parameters to use to update a file.
27+
* @return A JSON response containing the status of the file update.
28+
*/
29+
nlohmann::json updateFile(const UpdateFileParams& params);
30+
2231
} // namespace Hiero::TCK::FileService
2332

2433
#endif // HIERO_TCK_CPP_FILE_SERVICE_H_
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
#ifndef HIERO_TCK_CPP_UPDATE_FILE_PARAMS_H_
3+
#define HIERO_TCK_CPP_UPDATE_FILE_PARAMS_H_
4+
5+
#include "common/CommonTransactionParams.h"
6+
#include "json/JsonUtils.h"
7+
8+
#include <nlohmann/json.hpp>
9+
#include <optional>
10+
#include <string>
11+
#include <vector>
12+
13+
namespace Hiero::TCK::FileService
14+
{
15+
/**
16+
* Struct to hold the arguments for an `updateFile` JSON-RPC method call.
17+
*/
18+
struct UpdateFileParams
19+
{
20+
/**
21+
* The ID of the file to update.
22+
*/
23+
std::optional<std::string> mFileId;
24+
25+
/**
26+
* The keys that must sign when mutating the file.
27+
*/
28+
std::optional<std::vector<std::string>> mKeys;
29+
30+
/**
31+
* The new contents of the file.
32+
*/
33+
std::optional<std::string> mContents;
34+
35+
/**
36+
* The new memo for the file.
37+
*/
38+
std::optional<std::string> mFileMemo;
39+
40+
/**
41+
* The new time at which the file will expire.
42+
*/
43+
std::optional<std::string> mExpirationTime;
44+
45+
/**
46+
* Any parameters common to all transaction types.
47+
*/
48+
std::optional<CommonTransactionParams> mCommonTxParams;
49+
};
50+
51+
} // namespace Hiero::TCK::FileService
52+
53+
namespace nlohmann
54+
{
55+
/**
56+
* JSON serializer template specialization required to convert UpdateFileParams arguments properly.
57+
*/
58+
template<>
59+
struct [[maybe_unused]] adl_serializer<Hiero::TCK::FileService::UpdateFileParams>
60+
{
61+
static void from_json(const json& jsonFrom, Hiero::TCK::FileService::UpdateFileParams& params)
62+
{
63+
params.mFileId = Hiero::TCK::getOptionalJsonParameter<std::string>(jsonFrom, "fileId");
64+
params.mKeys = Hiero::TCK::getOptionalJsonParameter<std::vector<std::string>>(jsonFrom, "keys");
65+
params.mContents = Hiero::TCK::getOptionalJsonParameter<std::string>(jsonFrom, "contents");
66+
params.mFileMemo = Hiero::TCK::getOptionalJsonParameter<std::string>(jsonFrom, "fileMemo");
67+
params.mExpirationTime = Hiero::TCK::getOptionalJsonParameter<std::string>(jsonFrom, "expirationTime");
68+
params.mCommonTxParams =
69+
Hiero::TCK::getOptionalJsonParameter<Hiero::TCK::CommonTransactionParams>(jsonFrom, "commonTransactionParams");
70+
}
71+
};
72+
73+
} // namespace nlohmann
74+
75+
#endif // HIERO_TCK_CPP_UPDATE_FILE_PARAMS_H_

src/tck/src/TckServer.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "account/params/TransferCryptoParams.h"
1111
#include "account/params/UpdateAccountParams.h"
1212
#include "file/params/CreateFileParams.h"
13+
#include "file/params/UpdateFileParams.h"
1314
#include "key/params/GenerateKeyParams.h"
1415
#include "sdk/params/ResetParams.h"
1516
#include "sdk/params/SetupParams.h"
@@ -415,5 +416,7 @@ template TckServer::MethodHandle TckServer::getHandle<TokenService::WipeTokenPar
415416
nlohmann::json (*method)(const TokenService::WipeTokenParams&));
416417
template TckServer::MethodHandle TckServer::getHandle<FileService::CreateFileParams>(
417418
nlohmann::json (*method)(const FileService::CreateFileParams&));
419+
template TckServer::MethodHandle TckServer::getHandle<FileService::UpdateFileParams>(
420+
nlohmann::json (*method)(const FileService::UpdateFileParams&));
418421

419422
} // namespace Hiero::TCK

src/tck/src/file/FileService.cc

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
// SPDX-License-Identifier: Apache-2.0
22
#include "file/FileService.h"
33
#include "file/params/CreateFileParams.h"
4+
#include "file/params/UpdateFileParams.h"
45
#include "key/KeyService.h"
56
#include "sdk/SdkClient.h"
67
#include "json/JsonErrorType.h"
78
#include "json/JsonRpcException.h"
89

910
#include <FileCreateTransaction.h>
11+
#include <FileId.h>
12+
#include <FileUpdateTransaction.h>
1013
#include <Key.h>
1114
#include <KeyList.h>
1215
#include <PrivateKey.h>
@@ -73,4 +76,56 @@ nlohmann::json createFile(const CreateFileParams& params)
7376
};
7477
}
7578

79+
//-----
80+
nlohmann::json updateFile(const UpdateFileParams& params)
81+
{
82+
FileUpdateTransaction fileUpdateTransaction;
83+
fileUpdateTransaction.setGrpcDeadline(SdkClient::DEFAULT_TCK_REQUEST_TIMEOUT);
84+
85+
// Set the file ID (optional)
86+
if (params.mFileId.has_value())
87+
{
88+
fileUpdateTransaction.setFileId(FileId::fromString(params.mFileId.value()));
89+
}
90+
91+
if (params.mKeys.has_value())
92+
{
93+
std::vector<std::shared_ptr<Key>> keys;
94+
for (const std::string& keyString : params.mKeys.value())
95+
{
96+
keys.push_back(KeyService::getHieroKey(keyString));
97+
}
98+
fileUpdateTransaction.setKeys(keys);
99+
}
100+
101+
if (params.mContents.has_value())
102+
{
103+
fileUpdateTransaction.setContents(params.mContents.value());
104+
}
105+
106+
if (params.mFileMemo.has_value())
107+
{
108+
fileUpdateTransaction.setFileMemo(params.mFileMemo.value());
109+
}
110+
111+
if (params.mExpirationTime.has_value())
112+
{
113+
fileUpdateTransaction.setExpirationTime(
114+
std::chrono::system_clock::from_time_t(0) +
115+
std::chrono::seconds(Hiero::internal::EntityIdHelper::getNum<int64_t>(params.mExpirationTime.value())));
116+
}
117+
118+
if (params.mCommonTxParams.has_value())
119+
{
120+
params.mCommonTxParams->fillOutTransaction(fileUpdateTransaction, SdkClient::getClient());
121+
}
122+
123+
const TransactionReceipt txReceipt =
124+
fileUpdateTransaction.execute(SdkClient::getClient()).getReceipt(SdkClient::getClient());
125+
126+
return {
127+
{"status", gStatusToString.at(txReceipt.mStatus)}
128+
};
129+
}
130+
76131
} // namespace Hiero::TCK::FileService

src/tck/src/main.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ int main(int argc, char** argv)
5454

5555
// Add the FileService functions.
5656
tckServer.add("createFile", tckServer.getHandle(&FileService::createFile));
57+
tckServer.add("updateFile", tckServer.getHandle(&FileService::updateFile));
5758

5859
// Start listening for requests.
5960
tckServer.startServer();

0 commit comments

Comments
 (0)