Skip to content

Commit

Permalink
named ask protocol
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey-N-Chernyshov <[email protected]>
  • Loading branch information
Alexey-N-Chernyshov committed Nov 22, 2021
1 parent dd89f21 commit 8a288f5
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 27 deletions.
1 change: 1 addition & 0 deletions core/codec/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ add_library(cbor_types
target_link_libraries(cbor_types
address
signature
piece
)
73 changes: 73 additions & 0 deletions core/codec/cbor/cbor_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,82 @@

#include "codec/cbor/cbor_codec.hpp"
#include "markets/retrieval/protocols/retrieval_protocol.hpp"
#include "markets/storage/ask_protocol.hpp"
#include "primitives/address/address_codec.hpp"
#include "storage/ipfs/graphsync/extension.hpp"

namespace fc::markets::storage {
using codec::cbor::CborDecodeStream;
using codec::cbor::CborEncodeStream;

CBOR2_ENCODE(StorageAsk::Named) {
auto m{CborEncodeStream::map()};
m["Price"] << v.price;
m["VerifiedPrice"] << v.verified_price;
m["MinPieceSize"] << v.min_piece_size;
m["MaxPieceSize"] << v.max_piece_size;
m["Miner"] << v.miner;
m["Timestamp"] << v.timestamp;
m["Expiry"] << v.expiry;
m["SeqNo"] << v.seq_no;
return s << m;
}

CBOR2_DECODE(StorageAsk::Named) {
auto m{s.map()};
CborDecodeStream::named(m, "Price") >> v.price;
CborDecodeStream::named(m, "VerifiedPrice") >> v.verified_price;
CborDecodeStream::named(m, "MinPieceSize") >> v.min_piece_size;
CborDecodeStream::named(m, "MaxPieceSize") >> v.max_piece_size;
CborDecodeStream::named(m, "Miner") >> v.miner;
CborDecodeStream::named(m, "Timestamp") >> v.timestamp;
CborDecodeStream::named(m, "Expiry") >> v.expiry;
CborDecodeStream::named(m, "SeqNo") >> v.seq_no;
return s;
}

CBOR2_ENCODE(SignedStorageAsk::Named) {
auto m{CborEncodeStream::map()};
m["Ask"] << static_cast<const StorageAsk::Named &>(v.ask);
m["Signature"] << v.signature;
return s << m;
}

CBOR2_DECODE(SignedStorageAsk::Named) {
auto m{s.map()};
CborDecodeStream::named(m, "Ask")
>> *(static_cast<StorageAsk::Named *>(&v.ask));
CborDecodeStream::named(m, "Signature") >> v.signature;
return s;
}

CBOR2_ENCODE(AskRequest::Named) {
auto m{CborEncodeStream::map()};
m["Miner"] << v.miner;
return s << m;
}

CBOR2_DECODE(AskRequest::Named) {
auto m{s.map()};

CborDecodeStream::named(m, "Miner") >> v.miner;
return s;
}

CBOR2_ENCODE(AskResponse::Named) {
auto m{CborEncodeStream::map()};
m["Ask"] << static_cast<const SignedStorageAsk::Named &>(v.ask);
return s << m;
}

CBOR2_DECODE(AskResponse::Named) {
auto m{s.map()};
CborDecodeStream::named(m, "Ask")
>> *(static_cast<SignedStorageAsk::Named *>(&v.ask));
return s;
}
} // namespace fc::markets::storage

namespace fc::markets::retrieval {
using codec::cbor::CborDecodeStream;
using codec::cbor::CborEncodeStream;
Expand Down
21 changes: 20 additions & 1 deletion core/markets/storage/ask_protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@
#include "primitives/types.hpp"

namespace fc::markets::storage {

using crypto::signature::Signature;
using primitives::ChainEpoch;
using primitives::TokenAmount;
using primitives::address::Address;
using primitives::piece::PaddedPieceSize;

const libp2p::peer::Protocol kAskProtocolId_v1_0_1 = "/fil/storage/ask/1.0.1";

/** Protocol 1.1.1 uses named cbor */
const libp2p::peer::Protocol kAskProtocolId_v1_1_1 = "/fil/storage/ask/1.1.1";

struct StorageAsk {
struct Named;

// Price per GiB / Epoch
TokenAmount price;
TokenAmount verified_price;
Expand All @@ -37,6 +40,8 @@ namespace fc::markets::storage {
uint64_t seq_no;
};

struct StorageAsk::Named : StorageAsk {};

CBOR_TUPLE(StorageAsk,
price,
verified_price,
Expand All @@ -48,29 +53,43 @@ namespace fc::markets::storage {
seq_no)

struct SignedStorageAsk {
struct Named;

StorageAsk ask;
Signature signature;
};

struct SignedStorageAsk::Named : SignedStorageAsk {};

CBOR_TUPLE(SignedStorageAsk, ask, signature)

/**
* AskRequest is a request for current ask parameters for a given miner
*/
struct AskRequest {
struct Named;

Address miner;
};

/** Named ask request for named cbor */
struct AskRequest::Named : AskRequest {};

CBOR_TUPLE(AskRequest, miner)

/**
* AskResponse is the response sent over the network in response to an ask
* request
*/
struct AskResponse {
struct Named;

SignedStorageAsk ask;
};

/** Named ask response for named cbor */
struct AskResponse::Named : AskResponse {};

CBOR_TUPLE(AskResponse, ask)

} // namespace fc::markets::storage
27 changes: 3 additions & 24 deletions core/markets/storage/provider/impl/provider_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ namespace fc::markets::storage::provider {
OUTCOME_TRY(
filestore_->createDirectories(kStorageMarketImportDir.string()));

setAskHandlers();
setAskHandler<AskRequest, AskResponse>(kAskProtocolId_v1_0_1);
setAskHandler<AskRequest::Named, AskResponse::Named>(kAskProtocolId_v1_1_1);

setDealStatusHandlers();

auto handle = [&](auto &&protocol) {
Expand Down Expand Up @@ -739,29 +741,6 @@ namespace fc::markets::storage::provider {
}
}

void StorageProviderImpl::setAskHandlers() {
auto handle{[&](auto &&protocol) {
host_->setProtocolHandler(
protocol, [stored_ask{weaken(stored_ask_)}](auto _stream) {
auto stream{std::make_shared<common::libp2p::CborStream>(_stream)};
stream->template read<AskRequest>([stored_ask,
stream](auto _request) {
if (_request) {
if (auto asker{stored_ask.lock()}) {
if (auto _ask{asker->getAsk(_request.value().miner)}) {
return stream->write(AskResponse{_ask.value()},
[stream](auto) { stream->close(); });
}
}
}
stream->stream()->reset();
});
});
}};
handle(kAskProtocolId_v1_0_1);
handle(kAskProtocolId_v1_1_1);
}

outcome::result<ProviderDealState>
StorageProviderImpl::prepareDealStateResponse(
const DealStatusRequest &request) const {
Expand Down
22 changes: 20 additions & 2 deletions core/markets/storage/provider/impl/provider_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ namespace fc::markets::storage::provider {
using fc::storage::filestore::FileStore;
using fc::storage::piece::PieceStorage;
using libp2p::Host;
using vm::actor::builtin::types::market::deal_info_manager::DealInfoManager;
using pieceio::PieceIO;
using primitives::BigInt;
using primitives::EpochDuration;
using primitives::GasAmount;
using primitives::sector::RegisteredSealProof;
using sectorblocks::SectorBlocks;
using vm::actor::builtin::types::market::deal_info_manager::DealInfoManager;
using ProviderTransition =
fsm::Transition<ProviderEvent, void, StorageDealStatus, MinerDeal>;
using ProviderFSM =
Expand Down Expand Up @@ -83,7 +83,25 @@ namespace fc::markets::storage::provider {
outcome::result<Signature> sign(const Bytes &input) const;

private:
void setAskHandlers();
template <typename AskRequestType, typename AskResponseType>
inline void setAskHandler(const std::string &protocol) {
host_->setProtocolHandler(
protocol, [stored_ask{weaken(stored_ask_)}](auto _stream) {
auto stream{std::make_shared<common::libp2p::CborStream>(_stream)};
stream->template read<AskRequestType>([stored_ask,
stream](auto request) {
if (request) {
if (auto asker{stored_ask.lock()}) {
if (auto ask{asker->getAsk(request.value().miner)}) {
return stream->write(AskResponseType{ask.value()},
[stream](auto) { stream->close(); });
}
}
}
stream->stream()->reset();
});
});
}

outcome::result<ProviderDealState> prepareDealStateResponse(
const DealStatusRequest &request) const;
Expand Down

0 comments on commit 8a288f5

Please sign in to comment.