Skip to content

Commit

Permalink
Storage market protocols versions 1.1.0 (#505)
Browse files Browse the repository at this point in the history
* storage market protocols versions 1.1.1 added
* respect cbor map order

Signed-off-by: Alexey Chernyshov <[email protected]>
  • Loading branch information
Alexey-N-Chernyshov authored Dec 4, 2021
1 parent 5fcc3bb commit 0ec0155
Show file tree
Hide file tree
Showing 32 changed files with 1,779 additions and 754 deletions.
6 changes: 3 additions & 3 deletions core/api/full_node/node_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "markets/retrieval/types.hpp"
#include "markets/storage/ask_protocol.hpp"
#include "markets/storage/client/import_manager/import_manager.hpp"
#include "markets/storage/deal_protocol.hpp"
#include "markets/storage/mk_protocol.hpp"
#include "primitives/block/block.hpp"
#include "primitives/chain_epoch/chain_epoch.hpp"
#include "primitives/tipset/tipset.hpp"
Expand All @@ -36,7 +36,7 @@ namespace fc::api {
using libp2p::peer::PeerId;
using markets::retrieval::RetrievalPeer;
using markets::storage::DataRef;
using markets::storage::SignedStorageAsk;
using markets::storage::SignedStorageAskV1_1_0;
using markets::storage::StorageDeal;
using markets::storage::StorageDealStatus;
using markets::storage::client::import_manager::Import;
Expand Down Expand Up @@ -408,7 +408,7 @@ namespace fc::api {
*/
API_METHOD(ClientQueryAsk,
jwt::kReadPermission,
SignedStorageAsk,
SignedStorageAskV1_1_0,
const std::string &,
const Address &)

Expand Down
4 changes: 2 additions & 2 deletions core/api/rpc/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1363,14 +1363,14 @@ namespace fc::api {
decode(v.channel_message, Get(j, "ChannelMessage"));
}

ENCODE(SignedStorageAsk) {
ENCODE(SignedStorageAskV1_1_0) {
Value j{rapidjson::kObjectType};
Set(j, "Ask", v.ask);
Set(j, "Signature", v.signature);
return j;
}

DECODE(SignedStorageAsk) {
DECODE(SignedStorageAskV1_1_0) {
Get(j, "Ask", v.ask);
Get(j, "Signature", v.signature);
}
Expand Down
2 changes: 1 addition & 1 deletion core/api/storage_miner/storage_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace fc::api {
return storage_market_provider->importDataForDeal(proposal, path);
};

api->MarketGetAsk = [=]() -> outcome::result<SignedStorageAsk> {
api->MarketGetAsk = [=]() -> outcome::result<SignedStorageAskV1_1_0> {
return stored_ask->getAsk(actor);
};

Expand Down
6 changes: 3 additions & 3 deletions core/api/storage_miner/storage_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ namespace fc::api {
using boost::asio::io_context;
using markets::retrieval::RetrievalAsk;
using markets::retrieval::provider::RetrievalProvider;
using markets::storage::MinerDeal;
using markets::storage::SignedStorageAsk;
using markets::storage::SignedStorageAskV1_1_0;
using markets::storage::provider::MinerDeal;
using markets::storage::provider::StorageProvider;
using markets::storage::provider::StoredAsk;
using miner::Miner;
Expand Down Expand Up @@ -91,7 +91,7 @@ namespace fc::api {
const CID &,
const std::string &)

API_METHOD(MarketGetAsk, jwt::kReadPermission, SignedStorageAsk)
API_METHOD(MarketGetAsk, jwt::kReadPermission, SignedStorageAskV1_1_0)
API_METHOD(MarketGetRetrievalAsk, jwt::kReadPermission, RetrievalAsk)
API_METHOD(MarketSetAsk,
jwt::kAdminPermission,
Expand Down
201 changes: 190 additions & 11 deletions core/markets/storage/ask_protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#pragma once

#include <libp2p/peer/protocol.hpp>
#include "codec/cbor/cbor_codec.hpp"
#include "codec/cbor/streams_annotation.hpp"
#include "crypto/signature/signature.hpp"
#include "primitives/address/address.hpp"
Expand All @@ -15,29 +16,39 @@
#include "primitives/types.hpp"

namespace fc::markets::storage {

using codec::cbor::CborDecodeStream;
using codec::cbor::CborEncodeStream;
using crypto::signature::Signature;
using primitives::ChainEpoch;
using primitives::TokenAmount;
using primitives::address::Address;
using primitives::piece::PaddedPieceSize;

const libp2p::peer::Protocol kAskProtocolId0 = "/fil/storage/ask/1.0.1";
const libp2p::peer::Protocol kAskProtocolId = "/fil/storage/ask/1.1.1";
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_0 = "/fil/storage/ask/1.1.0";

/**
* StorageAsk defines the parameters by which a miner will choose to accept or
* reject a deal.
*/
struct StorageAsk {
// Price per GiB / Epoch
TokenAmount price;
TokenAmount verified_price;
PaddedPieceSize min_piece_size;
PaddedPieceSize max_piece_size;
Address miner;
ChainEpoch timestamp;
ChainEpoch expiry;
uint64_t seq_no;
ChainEpoch timestamp{};
ChainEpoch expiry{};
uint64_t seq_no{};
};

CBOR_TUPLE(StorageAsk,
/** StorageAsk used in V1.0.1 */
struct StorageAskV1_0_1 : public StorageAsk {};

CBOR_TUPLE(StorageAskV1_0_1,
price,
verified_price,
min_piece_size,
Expand All @@ -47,12 +58,101 @@ namespace fc::markets::storage {
expiry,
seq_no)

/** StorageAsk used in V1.1.0. Cbores with field names. */
struct StorageAskV1_1_0 : public StorageAsk {};

inline CBOR2_ENCODE(StorageAskV1_1_0) {
auto m{CborEncodeStream::orderedMap()};
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;
}

inline CBOR2_DECODE(StorageAskV1_1_0) {
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;
}

struct SignedStorageAsk {
virtual ~SignedStorageAsk() = default;

StorageAsk ask;
Signature signature;

/** Returns response digset */
virtual outcome::result<Bytes> getDigest() const = 0;
};

CBOR_TUPLE(SignedStorageAsk, ask, signature)
/** SignedStorageAsk used in V1.0.1 */
struct SignedStorageAskV1_0_1 : public SignedStorageAsk {
SignedStorageAskV1_0_1() = default;
explicit SignedStorageAskV1_0_1(StorageAsk ask) {
this->ask = std::move(ask);
}
SignedStorageAskV1_0_1(StorageAsk ask, Signature signature) {
this->ask = std::move(ask);
this->signature = std::move(signature);
}

outcome::result<Bytes> getDigest() const override {
return codec::cbor::encode(StorageAskV1_0_1{this->ask});
};
};

inline CBOR2_ENCODE(SignedStorageAskV1_0_1) {
return s << (CborEncodeStream::list()
<< StorageAskV1_0_1{v.ask} << v.signature);
}
inline CBOR2_DECODE(SignedStorageAskV1_0_1) {
auto cbor_list{s.list()};
v.ask = cbor_list.get<StorageAskV1_0_1>();
cbor_list >> v.signature;
return s;
}

/** SignedStorageAsk used in V1.1.0 with named fields. */
struct SignedStorageAskV1_1_0 : public SignedStorageAsk {
SignedStorageAskV1_1_0() = default;
explicit SignedStorageAskV1_1_0(StorageAsk ask) {
this->ask = std::move(ask);
}
SignedStorageAskV1_1_0(StorageAsk ask, Signature signature) {
this->ask = std::move(ask);
this->signature = std::move(signature);
}

outcome::result<Bytes> getDigest() const override {
return codec::cbor::encode(StorageAskV1_1_0{this->ask});
};
};

inline CBOR2_ENCODE(SignedStorageAskV1_1_0) {
auto m{CborEncodeStream::orderedMap()};
m["Ask"] << StorageAskV1_1_0{v.ask};
m["Signature"] << v.signature;
return s << m;
}

inline CBOR2_DECODE(SignedStorageAskV1_1_0) {
auto m{s.map()};
v.ask = CborDecodeStream::named(m, "Ask").get<StorageAskV1_1_0>();
CborDecodeStream::named(m, "Signature") >> v.signature;
return s;
}

/**
* AskRequest is a request for current ask parameters for a given miner
Expand All @@ -61,16 +161,95 @@ namespace fc::markets::storage {
Address miner;
};

CBOR_TUPLE(AskRequest, miner)
/** AskRequest used in V1.0.1 */
struct AskRequestV1_0_1 : public AskRequest {};

CBOR_TUPLE(AskRequestV1_0_1, miner)

/** AskRequest used in V1.1.0. Cbores with field names. */
struct AskRequestV1_1_0 : public AskRequest {};

inline CBOR2_ENCODE(AskRequestV1_1_0) {
auto m{CborEncodeStream::orderedMap()};
m["Miner"] << v.miner;
return s << m;
}

inline CBOR2_DECODE(AskRequestV1_1_0) {
auto m{s.map()};
CborDecodeStream::named(m, "Miner") >> v.miner;
return s;
}

/**
* AskResponse is the response sent over the network in response to an ask
* request
*/
struct AskResponse {
SignedStorageAsk ask;
virtual ~AskResponse() = default;

virtual const SignedStorageAsk &ask() const = 0;
};

struct AskResponseV1_0_1 : public AskResponse {
AskResponseV1_0_1() = default;

explicit AskResponseV1_0_1(SignedStorageAskV1_0_1 ask)
: ask_(std::move(ask)) {}

const SignedStorageAsk &ask() const override {
return ask_;
}

private:
friend CborEncodeStream &operator<<(CborEncodeStream &,
const AskResponseV1_0_1 &);
friend CborDecodeStream &operator>>(CborDecodeStream &,
AskResponseV1_0_1 &);

SignedStorageAskV1_0_1 ask_;
};

CBOR_TUPLE(AskResponse, ask)
inline CBOR2_ENCODE(AskResponseV1_0_1) {
auto cbor_list = CborEncodeStream::list();
cbor_list << v.ask_;
return s << cbor_list;
}
inline CBOR2_DECODE(AskResponseV1_0_1) {
auto cbor_list{s.list()};
cbor_list >> v.ask_;
return s;
}

struct AskResponseV1_1_0 : public AskResponse {
AskResponseV1_1_0() = default;

explicit AskResponseV1_1_0(SignedStorageAskV1_1_0 ask)
: ask_(std::move(ask)) {}

const SignedStorageAsk &ask() const override {
return ask_;
}

private:
friend CborEncodeStream &operator<<(CborEncodeStream &,
const AskResponseV1_1_0 &);
friend CborDecodeStream &operator>>(CborDecodeStream &,
AskResponseV1_1_0 &);

SignedStorageAskV1_1_0 ask_;
};

inline CBOR2_ENCODE(AskResponseV1_1_0) {
auto m{CborEncodeStream::orderedMap()};
m["Ask"] << v.ask_;
return s << m;
}

inline CBOR2_DECODE(AskResponseV1_1_0) {
auto m{s.map()};
CborDecodeStream::named(m, "Ask") >> v.ask_;
return s;
}

} // namespace fc::markets::storage
28 changes: 28 additions & 0 deletions core/markets/storage/client/client_deal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include "vm/actor/builtin/types/market/deal.hpp"

namespace fc::markets::storage::client {
using vm::actor::builtin::types::market::ClientDealProposal;

/** Internal state of a deal on the client side. */
struct ClientDeal {
ClientDealProposal client_deal_proposal;
CID proposal_cid;
boost::optional<CID> add_funds_cid;
StorageDealStatus state;
PeerInfo miner;
Address miner_worker;
DealId deal_id;
DataRef data_ref;
bool is_fast_retrieval;
std::string message;
CID publish_message;
};

} // namespace fc::markets::storage::client
Loading

0 comments on commit 0ec0155

Please sign in to comment.