Skip to content

Commit da2cabd

Browse files
authored
Miner deal fixes (#541)
* Fix StorageAsk Signed-off-by: ortyomka <[email protected]> * Fixes miner deals Signed-off-by: ortyomka <[email protected]>
1 parent 9dc996a commit da2cabd

38 files changed

+320
-178
lines changed

cmake/Hunter/config.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ hunter_config(libp2p
2424
)
2525

2626
hunter_config(jwt-cpp
27-
URL https://github.com/Thalhammer/jwt-cpp/archive/ac0424b115721e4066d2fb99f72ba0cd58759882.tar.gz
27+
URL https://github.com/soramitsu/fuhon-jwt-cpp/archive/ac0424b115721e4066d2fb99f72ba0cd58759882.tar.gz
2828
SHA1 92667cf319f7009234f98f27884db571c2eb1171
2929
CMAKE_ARGS JWT_BUILD_EXAMPLES=OFF
3030
)

core/api/rpc/json.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,40 @@ namespace fc::api {
555555
v = BigInt{AsString(j)};
556556
}
557557

558+
ENCODE(MinerDeal) {
559+
Value j{rapidjson::kObjectType};
560+
Set(j, "Proposal", v.client_deal_proposal.proposal);
561+
Set(j, "ClientSignature", v.client_deal_proposal.client_signature);
562+
Set(j, "ProposalCid", v.proposal_cid);
563+
Set(j, "AddFundsCid", v.add_funds_cid);
564+
Set(j, "PublishCid", v.publish_cid);
565+
Set(j, "Client", v.client);
566+
Set(j, "State", v.state);
567+
Set(j, "PiecePath", v.piece_path);
568+
Set(j, "MetadataPath", v.metadata_path);
569+
Set(j, "FastRetrieval", v.is_fast_retrieval);
570+
Set(j, "Message", v.message);
571+
Set(j, "Ref", v.ref);
572+
Set(j, "DealId", v.deal_id);
573+
return j;
574+
}
575+
576+
DECODE(MinerDeal) {
577+
Get(j, "Proposal", v.client_deal_proposal.proposal);
578+
Get(j, "ClientSignature", v.client_deal_proposal.client_signature);
579+
Get(j, "ProposalCid", v.proposal_cid);
580+
Get(j, "AddFundsCid", v.add_funds_cid);
581+
Get(j, "PublishCid", v.publish_cid);
582+
Get(j, "Client", v.client);
583+
Get(j, "State", v.state);
584+
Get(j, "PiecePath", v.piece_path);
585+
Get(j, "MetadataPath", v.metadata_path);
586+
Get(j, "FastRetrieval", v.is_fast_retrieval);
587+
Get(j, "Message", v.message);
588+
Get(j, "Ref", v.ref);
589+
Get(j, "DealId", v.deal_id);
590+
}
591+
558592
ENCODE(MinerInfo) {
559593
Value j{rapidjson::kObjectType};
560594
Set(j, "Owner", v.owner);

core/api/storage_miner/storage_api.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ namespace fc::api {
7070
return outcome::success();
7171
};
7272

73+
api->MarketListIncompleteDeals =
74+
[=]() -> outcome::result<std::vector<MinerDeal>> {
75+
return storage_market_provider->getLocalDeals();
76+
};
77+
7378
api->SectorsList = [=]() -> outcome::result<std::vector<SectorNumber>> {
7479
std::vector<SectorNumber> result;
7580
for (const auto &sector : miner->getSealing()->getListSectors()) {

core/api/storage_miner/storage_api.hpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ namespace fc::api {
2525
using boost::asio::io_context;
2626
using markets::retrieval::RetrievalAsk;
2727
using markets::retrieval::provider::RetrievalProvider;
28+
using markets::storage::MinerDeal;
2829
using markets::storage::SignedStorageAsk;
2930
using markets::storage::provider::StorageProvider;
3031
using markets::storage::provider::StoredAsk;
3132
using miner::Miner;
3233
using mining::types::DealInfo;
3334
using mining::types::DealSchedule;
35+
using mining::types::PieceLocation;
3436
using primitives::ChainEpoch;
3537
using primitives::DealId;
3638
using primitives::SectorNumber;
@@ -55,25 +57,14 @@ namespace fc::api {
5557
using sector_storage::stores::SectorIndex;
5658
using StorageInfo_ = sector_storage::stores::StorageInfo;
5759

58-
const static common::Logger kStorageApiLogger = common::createLogger("Storage API");
59-
60-
struct PieceLocation {
61-
SectorNumber sector_number;
62-
PaddedPieceSize offset;
63-
PaddedPieceSize length;
64-
};
65-
CBOR_TUPLE(PieceLocation, sector_number, offset, length)
60+
const static common::Logger kStorageApiLogger =
61+
common::createLogger("Storage API");
6662

6763
// TODO(ortyomka): [FIL-421] implement it
6864
struct ApiSectorInfo {
6965
mining::SealingState state = mining::SealingState::kStateUnknown;
7066
};
7167

72-
inline bool operator==(const PieceLocation &lhs, const PieceLocation &rhs) {
73-
return lhs.sector_number == rhs.sector_number && lhs.offset == rhs.offset
74-
&& lhs.length == rhs.length;
75-
}
76-
7768
constexpr ApiVersion kMinerApiVersion = makeApiVersion(1, 0, 0);
7869

7970
/**
@@ -116,6 +107,10 @@ namespace fc::api {
116107
void,
117108
const RetrievalAsk &)
118109

110+
API_METHOD(MarketListIncompleteDeals,
111+
jwt::kReadPermission,
112+
std::vector<MinerDeal>);
113+
119114
API_METHOD(SectorsList, jwt::kReadPermission, std::vector<SectorNumber>)
120115

121116
API_METHOD(
@@ -249,6 +244,7 @@ namespace fc::api {
249244
f(a.MarketGetRetrievalAsk);
250245
f(a.MarketSetAsk);
251246
f(a.MarketSetRetrievalAsk);
247+
f(a.MarketListIncompleteDeals);
252248
f(a.SectorsList);
253249
f(a.SectorsStatus);
254250
f(a.StorageAttach);

core/common/local_ip.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#pragma once
7+
8+
#include <boost/asio.hpp>
9+
10+
namespace fc::common {
11+
inline const std::string &localIp() {
12+
static const std::string ip{[] {
13+
using namespace boost::asio::ip;
14+
boost::asio::io_context io;
15+
tcp::resolver resolver{io};
16+
boost::system::error_code ec;
17+
tcp::resolver::iterator end;
18+
for (auto it{resolver.resolve(host_name(), "", ec)}; it != end; ++it) {
19+
auto addr{it->endpoint().address()};
20+
if (addr.is_v4()) {
21+
return addr.to_string();
22+
}
23+
}
24+
return std::string{"127.0.0.1"};
25+
}()};
26+
return ip;
27+
}
28+
} // namespace fc::common

core/markets/storage/deal_protocol.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <libp2p/peer/peer_info.hpp>
99
#include <libp2p/peer/protocol.hpp>
1010
#include "codec/cbor/streams_annotation.hpp"
11+
#include "common/libp2p/peer/cbor_peer_info.hpp"
1112
#include "crypto/signature/signature.hpp"
1213
#include "primitives/address/address.hpp"
1314
#include "primitives/cid/cid.hpp"
@@ -103,7 +104,7 @@ namespace fc::markets::storage {
103104
CID proposal_cid;
104105
boost::optional<CID> add_funds_cid;
105106
boost::optional<CID> publish_cid;
106-
PeerInfo client;
107+
PeerInfo client = codec::cbor::kDefaultT<PeerInfo>();
107108
StorageDealStatus state;
108109
Path piece_path;
109110
Path metadata_path;

core/markets/storage/provider/impl/provider_impl.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@ namespace fc::markets::storage::provider {
153153
return StorageMarketProviderError::kLocalDealNotFound;
154154
}
155155

156+
outcome::result<std::vector<MinerDeal>> StorageProviderImpl::getLocalDeals()
157+
const {
158+
std::vector<MinerDeal> deals;
159+
const auto fsm_deals = fsm_->list();
160+
deals.reserve(fsm_deals.size());
161+
for (const auto &it : fsm_deals) {
162+
deals.push_back(*it.first);
163+
}
164+
165+
return deals;
166+
}
167+
156168
outcome::result<void> StorageProviderImpl::importDataForDeal(
157169
const CID &proposal_cid, const boost::filesystem::path &path) {
158170
auto fsm_state_table = fsm_->list();
@@ -388,19 +400,19 @@ namespace fc::markets::storage::provider {
388400

389401
for (const auto &ref : piece_refs) {
390402
OUTCOME_TRY(sector_info,
391-
sector_blocks_->getMiner()->getSectorInfo(ref.sector_number));
403+
sector_blocks_->getMiner()->getSectorInfo(ref.sector));
392404

393405
if (sector_info->state == SealingState::kProving) {
394406
piece_location = ref;
395407
break;
396408
}
397409
}
398410

399-
if (!piece_location.has_value()) {
400-
return StorageProviderError::kNotFoundSector;
411+
if (piece_location.has_value()) {
412+
return piece_location.get();
401413
}
402414

403-
return piece_location.get();
415+
return StorageProviderError::kNotFoundSector;
404416
}
405417

406418
outcome::result<void> StorageProviderImpl::recordPieceInfo(
@@ -418,9 +430,9 @@ namespace fc::markets::storage::provider {
418430
OUTCOME_TRY(piece_storage_->addDealForPiece(
419431
deal->client_deal_proposal.proposal.piece_cid,
420432
DealInfo{.deal_id = deal->deal_id,
421-
.sector_id = piece_location.sector_number,
433+
.sector_id = piece_location.sector,
422434
.offset = piece_location.offset,
423-
.length = piece_location.length}));
435+
.length = piece_location.size}));
424436
return outcome::success();
425437
}
426438

@@ -667,14 +679,18 @@ namespace fc::markets::storage::provider {
667679
StorageDealStatus to) {
668680
// TODO(a.chernyshov): hand off
669681
auto &p{deal->client_deal_proposal.proposal};
670-
OUTCOME_EXCEPT(sector_blocks_->addPiece(
682+
auto maybe_piece_location = sector_blocks_->addPiece(
671683
p.piece_size.unpadded(),
672684
deal->piece_path,
673685
mining::types::DealInfo{deal->publish_cid,
674686
deal->deal_id,
675687
p,
676688
{p.start_epoch, p.end_epoch},
677-
deal->is_fast_retrieval}));
689+
deal->is_fast_retrieval});
690+
FSM_HALT_ON_ERROR(maybe_piece_location, "Unable to locate piece", deal);
691+
FSM_HALT_ON_ERROR(recordPieceInfo(deal, maybe_piece_location.value()),
692+
"Record piece failed",
693+
deal);
678694
FSM_SEND(deal, ProviderEvent::ProviderEventDealHandedOff);
679695
}
680696

@@ -697,11 +713,6 @@ namespace fc::markets::storage::provider {
697713
ProviderEvent event,
698714
StorageDealStatus from,
699715
StorageDealStatus to) {
700-
auto maybe_piece_location = locatePiece(deal);
701-
FSM_HALT_ON_ERROR(maybe_piece_location, "Unable to locate piece", deal);
702-
FSM_HALT_ON_ERROR(recordPieceInfo(deal, maybe_piece_location.value()),
703-
"Record piece failed",
704-
deal);
705716
// TODO(a.chernyshov): wait expiration
706717
}
707718

core/markets/storage/provider/impl/provider_impl.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ namespace fc::markets::storage::provider {
3232
using fc::storage::filestore::FileStore;
3333
using fc::storage::piece::PieceStorage;
3434
using libp2p::Host;
35-
using vm::actor::builtin::types::market::deal_info_manager::DealInfoManager;
3635
using pieceio::PieceIO;
3736
using primitives::BigInt;
3837
using primitives::EpochDuration;
3938
using primitives::GasAmount;
4039
using primitives::sector::RegisteredSealProof;
4140
using sectorblocks::SectorBlocks;
41+
using vm::actor::builtin::types::market::deal_info_manager::DealInfoManager;
4242
using ProviderTransition =
4343
fsm::Transition<ProviderEvent, void, StorageDealStatus, MinerDeal>;
4444
using ProviderFSM =
@@ -76,6 +76,9 @@ namespace fc::markets::storage::provider {
7676
auto getDeal(const CID &proposal_cid) const
7777
-> outcome::result<MinerDeal> override;
7878

79+
auto getLocalDeals() const
80+
-> outcome::result<std::vector<MinerDeal>> override;
81+
7982
auto importDataForDeal(const CID &proposal_cid,
8083
const boost::filesystem::path &path)
8184
-> outcome::result<void> override;

core/markets/storage/provider/impl/stored_ask.cpp

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,31 @@ namespace fc::markets::storage::provider {
1010
// Key to store last ask in datastore
1111
const Bytes kBestAskKey{codec::cbor::encode("latest-ask").value()};
1212

13+
outcome::result<std::shared_ptr<StoredAsk>> StoredAsk::newStoredAsk(
14+
std::shared_ptr<Datastore> datastore,
15+
std::shared_ptr<FullNodeApi> api,
16+
Address actor_address) {
17+
struct make_unique_enabler : StoredAsk {
18+
make_unique_enabler(std::shared_ptr<Datastore> datastore,
19+
std::shared_ptr<FullNodeApi> api,
20+
Address actor_address)
21+
: StoredAsk(
22+
std::move(datastore), std::move(api), std::move(actor_address)){};
23+
};
24+
25+
std::unique_ptr<StoredAsk> stored_ask =
26+
std::make_unique<make_unique_enabler>(
27+
std::move(datastore), std::move(api), std::move(actor_address));
28+
29+
OUTCOME_TRY(maybe_ask, stored_ask->tryLoadSignedAsk());
30+
31+
if (not maybe_ask.has_value()) {
32+
OUTCOME_TRY(stored_ask->addAsk(kDefaultPrice, kDefaultDuration));
33+
}
34+
35+
return std::move(stored_ask);
36+
}
37+
1338
StoredAsk::StoredAsk(std::shared_ptr<Datastore> datastore,
1439
std::shared_ptr<FullNodeApi> api,
1540
Address actor_address)
@@ -31,12 +56,21 @@ namespace fc::markets::storage::provider {
3156

3257
outcome::result<void> StoredAsk::addAsk(const TokenAmount &price,
3358
ChainEpoch duration) {
59+
auto min_size = kDefaultMinPieceSize;
60+
auto max_size = kDefaultMaxPieceSize;
61+
62+
if (last_signed_storage_ask_.has_value()) {
63+
auto &ask{(*last_signed_storage_ask_).ask};
64+
min_size = ask.min_piece_size;
65+
max_size = ask.max_piece_size;
66+
}
67+
3468
return addAsk(
3569
{
3670
.price = price,
3771
.verified_price = price,
38-
.min_piece_size = kDefaultMinPieceSize,
39-
.max_piece_size = kDefaultMaxPieceSize,
72+
.min_piece_size = min_size,
73+
.max_piece_size = max_size,
4074
.miner = actor_,
4175
},
4276
duration);
@@ -54,26 +88,18 @@ namespace fc::markets::storage::provider {
5488
}
5589

5690
outcome::result<SignedStorageAsk> StoredAsk::loadSignedAsk() {
57-
if (datastore_->contains(kBestAskKey)) {
58-
OUTCOME_TRY(ask_bytes, datastore_->get(kBestAskKey));
59-
OUTCOME_TRY(ask, codec::cbor::decode<SignedStorageAsk>(ask_bytes));
60-
return std::move(ask);
61-
}
91+
OUTCOME_TRY(ask_bytes, datastore_->get(kBestAskKey));
92+
OUTCOME_TRY(ask, codec::cbor::decode<SignedStorageAsk>(ask_bytes));
93+
return std::move(ask);
94+
}
6295

63-
// otherwise return default which 'not actively accepting deals'
64-
OUTCOME_TRY(chain_head, api_->ChainHead());
65-
ChainEpoch timestamp = chain_head->height();
66-
ChainEpoch expiry = chain_head->height() + kDefaultDuration;
67-
StorageAsk default_ask{.price = kDefaultPrice,
68-
.verified_price = kDefaultPrice,
69-
.min_piece_size = kDefaultMinPieceSize,
70-
.max_piece_size = kDefaultMaxPieceSize,
71-
.miner = actor_,
72-
.timestamp = timestamp,
73-
.expiry = expiry,
74-
.seq_no = 0};
75-
OUTCOME_TRY(signed_ask, signAsk(default_ask, *chain_head));
76-
return std::move(signed_ask);
96+
outcome::result<boost::optional<SignedStorageAsk>>
97+
StoredAsk::tryLoadSignedAsk() {
98+
if (not datastore_->contains(kBestAskKey)) {
99+
return boost::none;
100+
}
101+
OUTCOME_TRY(ask, loadSignedAsk());
102+
return std::move(ask);
77103
}
78104

79105
outcome::result<void> StoredAsk::saveSignedAsk(const SignedStorageAsk &ask) {

core/markets/storage/provider/provider.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ namespace fc::markets::storage::provider {
3434
virtual auto getDeal(const CID &proposal_cid) const
3535
-> outcome::result<MinerDeal> = 0;
3636

37+
virtual auto getLocalDeals() const
38+
-> outcome::result<std::vector<MinerDeal>> = 0;
39+
3740
/**
3841
* Imports data to proceed deal with 'manual' transfer type
3942
* @param proposal_cid - deal proposal CID

0 commit comments

Comments
 (0)