Skip to content

Commit 1154c81

Browse files
authored
API fixes for Lotus remote worker (#578)
* Futon Miner Api sync with lotus-worker Signed-off-by: elestrias <[email protected]>
1 parent 84ff73b commit 1154c81

File tree

25 files changed

+328
-89
lines changed

25 files changed

+328
-89
lines changed

core/api/common_api.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@ namespace fc::api {
3636
const std::string &)
3737

3838
API_METHOD(Version, jwt::kReadPermission, VersionResult)
39+
40+
API_METHOD(Session, jwt::kReadPermission, std::string)
3941
};
4042

4143
template <typename A, typename F>
4244
void visitCommon(A &&a, const F &f) {
4345
f(a.AuthNew);
4446
f(a.AuthVerify);
47+
f(a.Session);
4548
f(a.Version);
4649
}
4750
} // namespace fc::api

core/api/full_node/make.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ namespace fc::api {
10301030
}
10311031
return result;
10321032
};
1033-
// TODO(artyom-yurin): FIL-165 implement method
1033+
10341034
api->StateSectorPartition =
10351035
[=](const Address &address,
10361036
SectorNumber sector,

core/api/rpc/json.hpp

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ namespace fc::api {
7878
using sector_storage::stores::AcquireMode;
7979
using sector_storage::stores::LocalPath;
8080
using sector_storage::stores::PathType;
81+
using sector_storage::stores::SectorStorageInfo;
8182
using sector_storage::stores::StorageConfig;
8283
using sector_storage::stores::StorageInfo;
8384
using vm::actor::builtin::types::market::DealProposal;
@@ -91,6 +92,8 @@ namespace fc::api {
9192
ModularVerificationParameter;
9293
using vm::runtime::ExecutionResult;
9394
using base64 = cppcodec::base64_rfc4648;
95+
using common::fromString;
96+
using common::toString;
9497

9598
struct Codec {
9699
rapidjson::MemoryPoolAllocator<> &allocator;
@@ -258,19 +261,23 @@ namespace fc::api {
258261
}
259262

260263
ENCODE(PathType) {
261-
return encode(common::to_int(v));
264+
return encode(toString(v).value());
262265
}
263266

264267
DECODE(PathType) {
265-
decodeEnum(v, j);
268+
std::string temp;
269+
decode(temp, j);
270+
v = fromString<PathType>(temp).value();
266271
}
267272

268273
ENCODE(AcquireMode) {
269-
return encode(common::to_int(v));
274+
return encode(toString(v));
270275
}
271276

272277
DECODE(AcquireMode) {
273-
decodeEnum(v, j);
278+
std::string temp;
279+
decode(temp, j);
280+
v = fromString<AcquireMode>(temp).value();
274281
}
275282

276283
ENCODE(NetworkVersion) {
@@ -869,7 +876,7 @@ namespace fc::api {
869876
Get(j, "Locked", v.locked);
870877
}
871878

872-
ENCODE(StorageInfo) {
879+
ENCODE(SectorStorageInfo) {
873880
Value j{rapidjson::kObjectType};
874881
Set(j, "ID", v.id);
875882
Set(j, "URLs", v.urls);
@@ -880,7 +887,7 @@ namespace fc::api {
880887
return j;
881888
}
882889

883-
DECODE(StorageInfo) {
890+
DECODE(SectorStorageInfo) {
884891
Get(j, "ID", v.id);
885892
Get(j, "URLs", v.urls);
886893
Get(j, "Weight", v.weight);
@@ -889,6 +896,24 @@ namespace fc::api {
889896
Get(j, "Primary", v.is_primary);
890897
}
891898

899+
ENCODE(StorageInfo) {
900+
Value j{rapidjson::kObjectType};
901+
Set(j, "ID", v.id);
902+
Set(j, "URLs", v.urls);
903+
Set(j, "Weight", v.weight);
904+
Set(j, "CanSeal", v.can_seal);
905+
Set(j, "CanStore", v.can_store);
906+
return j;
907+
}
908+
909+
DECODE(StorageInfo) {
910+
Get(j, "ID", v.id);
911+
Get(j, "URLs", v.urls);
912+
Get(j, "Weight", v.weight);
913+
Get(j, "CanSeal", v.can_seal);
914+
Get(j, "CanStore", v.can_store);
915+
}
916+
892917
ENCODE(StorageParticipantBalance) {
893918
Value j{rapidjson::kObjectType};
894919
Set(j, "Locked", v.locked);
@@ -1348,13 +1373,11 @@ namespace fc::api {
13481373
ENCODE(HealthReport) {
13491374
Value j{rapidjson::kObjectType};
13501375
Set(j, "Stat", v.stat);
1351-
Set(j, "Error", v.error);
13521376
return j;
13531377
}
13541378

13551379
DECODE(HealthReport) {
13561380
decode(v.stat, Get(j, "Stat"));
1357-
decode(v.error, Get(j, "Error"));
13581381
}
13591382

13601383
ENCODE(libp2p::multi::Multiaddress) {
@@ -2058,13 +2081,15 @@ namespace fc::api {
20582081
Value j{rapidjson::kObjectType};
20592082
Set(j, "Capacity", v.capacity);
20602083
Set(j, "Available", v.available);
2084+
Set(j, "FSAvailable", v.fs_available);
20612085
Set(j, "Reserved", v.reserved);
20622086
return j;
20632087
}
20642088

20652089
DECODE(FsStat) {
20662090
decode(v.capacity, Get(j, "Capacity"));
20672091
decode(v.available, Get(j, "Available"));
2092+
decode(v.fs_available, Get(j, "FSAvailable"));
20682093
decode(v.reserved, Get(j, "Reserved"));
20692094
}
20702095

@@ -2084,7 +2109,7 @@ namespace fc::api {
20842109
Value j{rapidjson::kObjectType};
20852110
Set(j, "MemPhysical", v.physical_memory);
20862111
Set(j, "MemSwap", v.swap_memory);
2087-
Set(j, "MemReserved", v.reserved_memory);
2112+
Set(j, "MemUsed", v.reserved_memory);
20882113
Set(j, "CPUs", v.cpus);
20892114
Set(j, "GPUs", v.gpus);
20902115
return j;
@@ -2093,7 +2118,7 @@ namespace fc::api {
20932118
DECODE(WorkerResources) {
20942119
Get(j, "MemPhysical", v.physical_memory);
20952120
Get(j, "MemSwap", v.swap_memory);
2096-
Get(j, "MemReserved", v.reserved_memory);
2121+
Get(j, "MemUsed", v.reserved_memory);
20972122
Get(j, "CPUs", v.cpus);
20982123
Get(j, "GPUs", v.gpus);
20992124
}
@@ -2189,6 +2214,21 @@ namespace fc::api {
21892214
}
21902215
return j;
21912216
}
2217+
ENCODE(std::set<TaskType>) {
2218+
Value j{rapidjson::kObjectType};
2219+
j.MemberReserve(v.size(), allocator);
2220+
for (auto &pair : v) {
2221+
std::map<std::string, std::string> value;
2222+
Set(j, pair, value);
2223+
}
2224+
return j;
2225+
}
2226+
2227+
DECODE(std::set<TaskType>) {
2228+
for (auto it = j.MemberBegin(); it != j.MemberEnd(); ++it) {
2229+
v.emplace(TaskType(AsString(it->name)));
2230+
}
2231+
}
21922232

21932233
template <typename T>
21942234
DECODE(std::set<T>) {

core/api/setup_common.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
#pragma once
77

8+
#include <boost/uuid/random_generator.hpp>
9+
#include <boost/uuid/uuid_io.hpp>
10+
811
#include "api/common_api.hpp"
912
#include "api/utils.hpp"
1013
#include "api/version.hpp"
@@ -16,6 +19,7 @@ namespace fc::api {
1619
using ::fc::ApiAlgorithm;
1720
using primitives::jwt::kPermissionKey;
1821
using primitives::jwt::kTokenType;
22+
namespace uuids = boost::uuids;
1923

2024
void fillAuthApi(const std::shared_ptr<CommonApi> &api,
2125
const std::shared_ptr<ApiAlgorithm> &secret_algorithm,
@@ -60,5 +64,10 @@ namespace fc::api {
6064

6165
return std::move(perms);
6266
};
67+
68+
api->Session = []() {
69+
const static std::string uuid = uuids::to_string(uuids::random_generator()());
70+
return uuid;
71+
};
6372
}
6473
} // namespace fc::api

core/api/storage_miner/storage_api.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
#include "api/storage_miner/storage_api.hpp"
77

88
#include "api/storage_miner/return_api.hpp"
9+
#include "common/outcome.hpp"
10+
#include "common/uri_parser/uri_parser.hpp"
911
#include "miner/miner_version.hpp"
1012
#include "sector_storage/impl/remote_worker.hpp"
1113

1214
namespace fc::api {
15+
using common::HttpUri;
1316
using miner::kMinerVersion;
1417
using sector_storage::RemoteWorker;
1518

@@ -166,32 +169,35 @@ namespace fc::api {
166169
return sector_index->storageDropSector(storage_id, sector, file_type);
167170
};
168171

169-
api->StorageFindSector =
170-
[=](const SectorId &sector,
171-
const SectorFileType &file_type,
172-
boost::optional<SectorSize> fetch_sector_size) {
173-
return sector_index->storageFindSector(
174-
sector, file_type, fetch_sector_size);
175-
};
172+
api->StorageFindSector = [=](const SectorId &sector,
173+
const SectorFileType &file_type,
174+
const SectorSize &fetch_sector_size,
175+
bool allow_fetch) {
176+
boost::optional<SectorSize> sector_size;
177+
if (allow_fetch) {
178+
sector_size = fetch_sector_size;
179+
}
180+
return sector_index->storageFindSector(sector, file_type, sector_size);
181+
};
176182

177183
api->StorageBestAlloc = [=](const SectorFileType &allocate,
178184
SectorSize sector_size,
179-
bool sealing_mode) {
185+
const std::string &sealing_mode) {
180186
return sector_index->storageBestAlloc(
181-
allocate, sector_size, sealing_mode);
187+
allocate, sector_size, (sealing_mode == "sealing"));
182188
};
183189

184190
makeReturnApi(api, sector_scheduler);
185191

186-
api->WorkerConnect =
187-
[=, self{api}](const std::string &address) -> outcome::result<void> {
188-
OUTCOME_TRY(maddress, libp2p::multi::Multiaddress::create(address));
189-
OUTCOME_TRY(worker,
190-
RemoteWorker::connectRemoteWorker(*io, self, maddress));
191-
192-
spdlog::info("Connected to a remote worker at {}", address);
192+
api->WorkerConnect = [=, self{api}](auto &&cb,
193+
const std::string &address) -> void {
194+
io->post([=] {
195+
OUTCOME_CB(auto worker,
196+
RemoteWorker::connectRemoteWorker(*io, api, address));
197+
spdlog::info("Connected to a remote worker at {}", address);
193198

194-
return sector_manager->addWorker(std::move(worker));
199+
OUTCOME_CB1(sector_manager->addWorker(std::move(worker)));
200+
});
195201
};
196202

197203
api->Version = [] {

core/api/storage_miner/storage_api.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ namespace fc::api {
5959
using sector_storage::stores::HealthReport;
6060
using sector_storage::stores::SectorIndex;
6161
using StorageInfo_ = sector_storage::stores::StorageInfo;
62+
using sector_storage::stores::SectorStorageInfo;
6263

6364
const static common::Logger kStorageApiLogger =
6465
common::createLogger("Storage API");
@@ -92,7 +93,7 @@ namespace fc::api {
9293
ChainEpoch early = {};
9394
};
9495

95-
constexpr ApiVersion kMinerApiVersion = makeApiVersion(1, 0, 0);
96+
constexpr ApiVersion kMinerApiVersion = makeApiVersion(1, 3, 0);
9697

9798
/**
9899
* Storage miner node low-level interface API.
@@ -172,16 +173,17 @@ namespace fc::api {
172173
const SectorFileType &)
173174
API_METHOD(StorageFindSector,
174175
jwt::kAdminPermission,
175-
std::vector<StorageInfo_>,
176+
std::vector<SectorStorageInfo>,
176177
const SectorId &,
177178
const SectorFileType &,
178-
boost::optional<SectorSize>)
179+
const SectorSize &,
180+
bool)
179181
API_METHOD(StorageBestAlloc,
180182
jwt::kAdminPermission,
181183
std::vector<StorageInfo_>,
182184
const SectorFileType &,
183185
SectorSize,
184-
bool)
186+
const std::string &)
185187

186188
API_METHOD(ReturnAddPiece,
187189
jwt::kAdminPermission,

0 commit comments

Comments
 (0)