Skip to content

Commit 22f99ed

Browse files
authored
Feature/network api (#492)
* network_api structure Signed-off-by: elestrias <[email protected]> * Net api separation Signed-off-by: elestrias <[email protected]> * net api disconnect Signed-off-by: elestrias <[email protected]> * netApi disconnect fixes * logger upper level translation * lambd capture fixes * pr fixes * includes fixes Signed-off-by: elestrias <[email protected]> * fix blocksync Signed-off-by: elestrias <[email protected]>
1 parent 51c352b commit 22f99ed

File tree

10 files changed

+120
-57
lines changed

10 files changed

+120
-57
lines changed

core/api/common_api.hpp

-18
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,13 @@ namespace fc::api {
3535
std::vector<Permission>,
3636
const std::string &)
3737

38-
/**
39-
* Returns listen addresses.
40-
*/
41-
API_METHOD(NetAddrsListen, jwt::kReadPermission, PeerInfo)
42-
43-
/**
44-
* Initiates the connection to the peer.
45-
*/
46-
API_METHOD(NetConnect, jwt::kWritePermission, void, const PeerInfo &)
47-
48-
/**
49-
* Returns all peers connected to the this host.
50-
*/
51-
API_METHOD(NetPeers, jwt::kReadPermission, std::vector<PeerInfo>)
52-
5338
API_METHOD(Version, jwt::kReadPermission, VersionResult)
5439
};
5540

5641
template <typename A, typename F>
5742
void visitCommon(A &&a, const F &f) {
5843
f(a.AuthNew);
5944
f(a.AuthVerify);
60-
f(a.NetAddrsListen);
61-
f(a.NetConnect);
62-
f(a.NetPeers);
6345
f(a.Version);
6446
}
6547
} // namespace fc::api

core/api/full_node/make.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ namespace fc::api {
9292
using vm::toolchain::Toolchain;
9393
using vm::version::getNetworkVersion;
9494

95-
const static Logger logger = common::createLogger("Full node API");
96-
9795
// TODO (turuslan): reuse for block validation
9896
inline bool minerHasMinPower(const StoragePower &claim_qa,
9997
size_t min_power_miners) {
@@ -440,7 +438,7 @@ namespace fc::api {
440438
std::vector<QueryOffer> result;
441439
for (const auto &[peer, maybe_response] : all_calls) {
442440
if (maybe_response.has_error()) {
443-
logger->error("Error when query peer {}",
441+
kNodeApiLogger->error("Error when query peer {}",
444442
maybe_response.error().message());
445443
} else {
446444
result.emplace_back(maybe_response.value());
@@ -490,11 +488,11 @@ namespace fc::api {
490488
order.miner,
491489
[=](outcome::result<void> res) {
492490
if (res.has_error()) {
493-
logger->error("Error in ClientRetrieve {}",
491+
kNodeApiLogger->error("Error in ClientRetrieve {}",
494492
res.error().message());
495493
return cb(res.error());
496494
}
497-
logger->info("retrieval deal done");
495+
kNodeApiLogger->info("retrieval deal done");
498496
if (file_ref.is_car) {
499497
OUTCOME_CB1(storage::car::makeSelectiveCar(
500498
*markets_ipld, {{order.root, {}}}, file_ref.path));

core/api/full_node/make.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "api/full_node/node_api.hpp"
99
#include "blockchain/weight_calculator.hpp"
10+
#include "common/logger.hpp"
1011
#include "fwd.hpp"
1112
#include "markets/discovery/discovery.hpp"
1213
#include "markets/retrieval/client/retrieval_client.hpp"
@@ -32,6 +33,8 @@ namespace fc::api {
3233
using sync::PubSubGate;
3334
using vm::runtime::EnvironmentContext;
3435

36+
const static common::Logger kNodeApiLogger = common::createLogger("Full Node API");
37+
3538
outcome::result<IpldObject> getNode(const std::shared_ptr<Ipld> &ipld,
3639
const CID &root,
3740
gsl::span<const std::string> parts);

core/api/full_node/node_api.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#pragma once
77

88
#include "api/common_api.hpp"
9+
#include "api/network/network_api.hpp"
910
#include "common/libp2p/peer/cbor_peer_id.hpp"
1011
#include "const.hpp"
1112
#include "data_transfer/types.hpp"
@@ -294,7 +295,7 @@ namespace fc::api {
294295
* FullNode API is a low-level interface to the Filecoin network full node.
295296
* Provides the latest node API v2.0.0
296297
*/
297-
struct FullNodeApi : public CommonApi {
298+
struct FullNodeApi : public CommonApi, public NetworkApi {
298299
/**
299300
* @note long operation
300301
*/
@@ -805,6 +806,7 @@ namespace fc::api {
805806
template <typename A, typename F>
806807
void visit(const FullNodeApi &, A &&a, const F &f) {
807808
visitCommon(a, f);
809+
visitNet(a, f);
808810
f(a.BeaconGetEntry);
809811
f(a.ChainGetBlock);
810812
f(a.ChainGetBlockMessages);

core/api/network/network_api.hpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#pragma once
7+
8+
#include "api/common_api.hpp"
9+
10+
namespace fc::api {
11+
class NetworkApi{
12+
public:
13+
/**
14+
* Returns listen addresses.
15+
*/
16+
API_METHOD(NetAddrsListen, jwt::kReadPermission, PeerInfo)
17+
18+
/**
19+
* Initiates the connection to the peer.
20+
*/
21+
API_METHOD(NetConnect, jwt::kWritePermission, void, const PeerInfo &)
22+
23+
/**
24+
* Returns all peers connected to the this host.
25+
*/
26+
API_METHOD(NetPeers, jwt::kReadPermission, std::vector<PeerInfo>)
27+
28+
/**
29+
* Removes provided peer from list of connected.
30+
*/
31+
API_METHOD(NetDisconnect, jwt::kWritePermission, void, const PeerInfo &)
32+
};
33+
34+
template <typename A, typename F>
35+
void visitNet(A &&a, const F &f) {
36+
f(a.NetAddrsListen);
37+
f(a.NetConnect);
38+
f(a.NetPeers);
39+
f(a.NetDisconnect);
40+
}
41+
42+
}; // namespace fc::api

core/api/network/setup_net.hpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#pragma once
7+
8+
#include <libp2p/host/host.hpp>
9+
#include "api/network/network_api.hpp"
10+
11+
namespace fc::api {
12+
using libp2p::Host;
13+
14+
inline void fillNetApi(const std::shared_ptr<NetworkApi> &api,
15+
const PeerInfo &api_peer_info,
16+
const std::shared_ptr<Host> &host,
17+
const common::Logger &logger) {
18+
api->NetAddrsListen = [api_peer_info]() -> outcome::result<PeerInfo> {
19+
return api_peer_info;
20+
};
21+
22+
api->NetConnect = [=](auto &peer) {
23+
host->connect(peer);
24+
return outcome::success();
25+
};
26+
27+
api->NetPeers = [=]() -> outcome::result<std::vector<PeerInfo>> {
28+
const auto &peer_repository = host->getPeerRepository();
29+
const auto connections =
30+
host->getNetwork().getConnectionManager().getConnections();
31+
std::vector<PeerInfo> result;
32+
for (const auto &conncection : connections) {
33+
const auto remote = conncection->remotePeer();
34+
if (remote.has_error()) {
35+
logger->error("get remote peer error", remote.error().message());
36+
continue;
37+
}
38+
result.push_back(peer_repository.getPeerInfo(remote.value()));
39+
}
40+
return result;
41+
};
42+
43+
api->NetDisconnect = [=](const auto &peer) {
44+
host->disconnect(peer.id);
45+
return outcome::success();
46+
};
47+
}
48+
49+
} // namespace fc::api

core/api/storage_miner/storage_api.hpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
#include "api/common_api.hpp"
99
#include "api/full_node/node_api.hpp"
10+
#include "api/network/network_api.hpp"
1011
#include "api/version.hpp"
12+
#include "common/logger.hpp"
1113
#include "markets/retrieval/provider/retrieval_provider.hpp"
1214
#include "markets/retrieval/types.hpp"
1315
#include "markets/storage/ask_protocol.hpp"
@@ -53,6 +55,8 @@ namespace fc::api {
5355
using sector_storage::stores::SectorIndex;
5456
using StorageInfo_ = sector_storage::stores::StorageInfo;
5557

58+
const static common::Logger kStorageApiLogger = common::createLogger("Storage API");
59+
5660
struct PieceLocation {
5761
SectorNumber sector_number;
5862
PaddedPieceSize offset;
@@ -75,7 +79,7 @@ namespace fc::api {
7579
/**
7680
* Storage miner node low-level interface API.
7781
*/
78-
struct StorageMinerApi : public CommonApi {
82+
struct StorageMinerApi : public CommonApi, public NetworkApi {
7983
API_METHOD(ActorAddress, jwt::kReadPermission, Address)
8084

8185
API_METHOD(ActorSectorSize,
@@ -236,6 +240,7 @@ namespace fc::api {
236240
template <typename A, typename F>
237241
void visit(const StorageMinerApi &, A &&a, const F &f) {
238242
visitCommon(a, f);
243+
visitNet(a, f);
239244
f(a.ActorAddress);
240245
f(a.ActorSectorSize);
241246
f(a.PledgeSector);

core/miner/main/main.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,8 @@ namespace fc {
497497
storage_provider,
498498
retrieval_provider);
499499

500-
api::fillAuthApi(mapi, api_secret, log());
500+
api::fillAuthApi(mapi, api_secret, api::kStorageApiLogger);
501+
//TODO(@elestrias): [FIL-427] make net api configuration
501502

502503
std::map<std::string, std::shared_ptr<api::Rpc>> mrpc;
503504
mrpc.emplace(

core/node/main/builder.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <libp2p/protocol/kademlia/impl/validator_default.hpp>
2323

2424
#include "api/full_node/make.hpp"
25+
#include "api/network/setup_net.hpp"
2526
#include "api/impl/paych_get.hpp"
2627
#include "api/setup_common.hpp"
2728
#include "blockchain/block_validator/impl/block_validator_impl.hpp"
@@ -33,6 +34,7 @@
3334
#include "codec/json/json.hpp"
3435
#include "common/api_secret.hpp"
3536
#include "common/error_text.hpp"
37+
#include "common/libp2p/peer/peer_info_helper.hpp"
3638
#include "common/peer_key.hpp"
3739
#include "crypto/bls/impl/bls_provider_impl.hpp"
3840
#include "crypto/secp256k1/impl/secp256k1_provider_impl.hpp"
@@ -75,6 +77,7 @@
7577
#include "vm/state/impl/state_tree_impl.hpp"
7678

7779
namespace fc::node {
80+
using api::PeerInfo;
7881
using markets::discovery::DiscoveryImpl;
7982
using markets::pieceio::PieceIOImpl;
8083
using markets::retrieval::client::RetrievalClientImpl;
@@ -638,7 +641,13 @@ namespace fc::node {
638641
o.api,
639642
std::make_shared<storage::MapPrefix>("paych_maker/", o.kv_store)));
640643

641-
api::fillAuthApi(o.api, api_secret, log());
644+
api::fillAuthApi(o.api, api_secret, api::kNodeApiLogger);
645+
646+
const PeerInfo api_peer_info{
647+
o.host->getPeerInfo().id,
648+
nonZeroAddrs(o.host->getAddresses(), &config.localIp())};
649+
650+
api::fillNetApi(o.api, api_peer_info, o.host, api::kNodeApiLogger);
642651

643652
auto paych{
644653
std::make_shared<payment_channel_manager::PaymentChannelManagerImpl>(

core/node/main/main.cpp

+2-30
Original file line numberDiff line numberDiff line change
@@ -85,34 +85,6 @@ namespace fc {
8585
void startApi(const node::Config &config,
8686
NodeObjects &node_objects,
8787
const Metrics &metrics) {
88-
// Network API
89-
PeerInfo api_peer_info{
90-
node_objects.host->getPeerInfo().id,
91-
nonZeroAddrs(node_objects.host->getAddresses(), &config.localIp())};
92-
node_objects.api->NetAddrsListen =
93-
[api_peer_info]() -> outcome::result<PeerInfo> {
94-
return api_peer_info;
95-
};
96-
node_objects.api->NetConnect = [&](auto &peer) {
97-
node_objects.host->connect(peer);
98-
return outcome::success();
99-
};
100-
node_objects.api->NetPeers =
101-
[&]() -> outcome::result<std::vector<PeerInfo>> {
102-
const auto &peer_repository = node_objects.host->getPeerRepository();
103-
auto connections = node_objects.host->getNetwork()
104-
.getConnectionManager()
105-
.getConnections();
106-
std::vector<PeerInfo> result;
107-
for (const auto &conncection : connections) {
108-
const auto remote = conncection->remotePeer();
109-
if (remote.has_error())
110-
log()->error("get remote peer error", remote.error().message());
111-
result.push_back(peer_repository.getPeerInfo(remote.value()));
112-
}
113-
return result;
114-
};
115-
11688
// Market Client API
11789
node_objects.api->ClientImport =
11890
[&](auto &file_ref) -> outcome::result<ImportRes> {
@@ -123,7 +95,7 @@ namespace fc {
12395
return ImportRes{root, 0};
12496
};
12597

126-
node_objects.api->ClientListDeals = [api_peer_info, &node_objects]()
98+
node_objects.api->ClientListDeals = [&node_objects]()
12799
-> outcome::result<std::vector<StorageMarketDealInfo>> {
128100
std::vector<StorageMarketDealInfo> result;
129101
OUTCOME_TRY(local_deals,
@@ -145,7 +117,7 @@ namespace fc {
145117
{},
146118
deal.client_deal_proposal.proposal.verified,
147119
// TODO (a.chernyshov) actual ChannelId
148-
{api_peer_info.id, deal.miner.id, 0},
120+
{node_objects.host->getId(), deal.miner.id, 0},
149121
// TODO (a.chernyshov) actual data transfer
150122
{0, 0, deal.proposal_cid, true, true, "", "", deal.miner.id, 0}});
151123
}

0 commit comments

Comments
 (0)