Skip to content

Commit 0ec0155

Browse files
Storage market protocols versions 1.1.0 (#505)
* storage market protocols versions 1.1.1 added * respect cbor map order Signed-off-by: Alexey Chernyshov <[email protected]>
1 parent 5fcc3bb commit 0ec0155

32 files changed

+1779
-754
lines changed

core/api/full_node/node_api.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "markets/retrieval/types.hpp"
1515
#include "markets/storage/ask_protocol.hpp"
1616
#include "markets/storage/client/import_manager/import_manager.hpp"
17-
#include "markets/storage/deal_protocol.hpp"
17+
#include "markets/storage/mk_protocol.hpp"
1818
#include "primitives/block/block.hpp"
1919
#include "primitives/chain_epoch/chain_epoch.hpp"
2020
#include "primitives/tipset/tipset.hpp"
@@ -36,7 +36,7 @@ namespace fc::api {
3636
using libp2p::peer::PeerId;
3737
using markets::retrieval::RetrievalPeer;
3838
using markets::storage::DataRef;
39-
using markets::storage::SignedStorageAsk;
39+
using markets::storage::SignedStorageAskV1_1_0;
4040
using markets::storage::StorageDeal;
4141
using markets::storage::StorageDealStatus;
4242
using markets::storage::client::import_manager::Import;
@@ -408,7 +408,7 @@ namespace fc::api {
408408
*/
409409
API_METHOD(ClientQueryAsk,
410410
jwt::kReadPermission,
411-
SignedStorageAsk,
411+
SignedStorageAskV1_1_0,
412412
const std::string &,
413413
const Address &)
414414

core/api/rpc/json.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,14 +1363,14 @@ namespace fc::api {
13631363
decode(v.channel_message, Get(j, "ChannelMessage"));
13641364
}
13651365

1366-
ENCODE(SignedStorageAsk) {
1366+
ENCODE(SignedStorageAskV1_1_0) {
13671367
Value j{rapidjson::kObjectType};
13681368
Set(j, "Ask", v.ask);
13691369
Set(j, "Signature", v.signature);
13701370
return j;
13711371
}
13721372

1373-
DECODE(SignedStorageAsk) {
1373+
DECODE(SignedStorageAskV1_1_0) {
13741374
Get(j, "Ask", v.ask);
13751375
Get(j, "Signature", v.signature);
13761376
}

core/api/storage_miner/storage_api.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace fc::api {
4141
return storage_market_provider->importDataForDeal(proposal, path);
4242
};
4343

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

core/api/storage_miner/storage_api.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ 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;
29-
using markets::storage::SignedStorageAsk;
28+
using markets::storage::SignedStorageAskV1_1_0;
29+
using markets::storage::provider::MinerDeal;
3030
using markets::storage::provider::StorageProvider;
3131
using markets::storage::provider::StoredAsk;
3232
using miner::Miner;
@@ -91,7 +91,7 @@ namespace fc::api {
9191
const CID &,
9292
const std::string &)
9393

94-
API_METHOD(MarketGetAsk, jwt::kReadPermission, SignedStorageAsk)
94+
API_METHOD(MarketGetAsk, jwt::kReadPermission, SignedStorageAskV1_1_0)
9595
API_METHOD(MarketGetRetrievalAsk, jwt::kReadPermission, RetrievalAsk)
9696
API_METHOD(MarketSetAsk,
9797
jwt::kAdminPermission,

core/markets/storage/ask_protocol.hpp

Lines changed: 190 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#pragma once
77

88
#include <libp2p/peer/protocol.hpp>
9+
#include "codec/cbor/cbor_codec.hpp"
910
#include "codec/cbor/streams_annotation.hpp"
1011
#include "crypto/signature/signature.hpp"
1112
#include "primitives/address/address.hpp"
@@ -15,29 +16,39 @@
1516
#include "primitives/types.hpp"
1617

1718
namespace fc::markets::storage {
18-
19+
using codec::cbor::CborDecodeStream;
20+
using codec::cbor::CborEncodeStream;
1921
using crypto::signature::Signature;
2022
using primitives::ChainEpoch;
2123
using primitives::TokenAmount;
2224
using primitives::address::Address;
2325
using primitives::piece::PaddedPieceSize;
2426

25-
const libp2p::peer::Protocol kAskProtocolId0 = "/fil/storage/ask/1.0.1";
26-
const libp2p::peer::Protocol kAskProtocolId = "/fil/storage/ask/1.1.1";
27+
const libp2p::peer::Protocol kAskProtocolId_v1_0_1 = "/fil/storage/ask/1.0.1";
28+
29+
/** Protocol 1.1.1 uses named cbor */
30+
const libp2p::peer::Protocol kAskProtocolId_v1_1_0 = "/fil/storage/ask/1.1.0";
2731

32+
/**
33+
* StorageAsk defines the parameters by which a miner will choose to accept or
34+
* reject a deal.
35+
*/
2836
struct StorageAsk {
2937
// Price per GiB / Epoch
3038
TokenAmount price;
3139
TokenAmount verified_price;
3240
PaddedPieceSize min_piece_size;
3341
PaddedPieceSize max_piece_size;
3442
Address miner;
35-
ChainEpoch timestamp;
36-
ChainEpoch expiry;
37-
uint64_t seq_no;
43+
ChainEpoch timestamp{};
44+
ChainEpoch expiry{};
45+
uint64_t seq_no{};
3846
};
3947

40-
CBOR_TUPLE(StorageAsk,
48+
/** StorageAsk used in V1.0.1 */
49+
struct StorageAskV1_0_1 : public StorageAsk {};
50+
51+
CBOR_TUPLE(StorageAskV1_0_1,
4152
price,
4253
verified_price,
4354
min_piece_size,
@@ -47,12 +58,101 @@ namespace fc::markets::storage {
4758
expiry,
4859
seq_no)
4960

61+
/** StorageAsk used in V1.1.0. Cbores with field names. */
62+
struct StorageAskV1_1_0 : public StorageAsk {};
63+
64+
inline CBOR2_ENCODE(StorageAskV1_1_0) {
65+
auto m{CborEncodeStream::orderedMap()};
66+
m["Price"] << v.price;
67+
m["VerifiedPrice"] << v.verified_price;
68+
m["MinPieceSize"] << v.min_piece_size;
69+
m["MaxPieceSize"] << v.max_piece_size;
70+
m["Miner"] << v.miner;
71+
m["Timestamp"] << v.timestamp;
72+
m["Expiry"] << v.expiry;
73+
m["SeqNo"] << v.seq_no;
74+
return s << m;
75+
}
76+
77+
inline CBOR2_DECODE(StorageAskV1_1_0) {
78+
auto m{s.map()};
79+
CborDecodeStream::named(m, "Price") >> v.price;
80+
CborDecodeStream::named(m, "VerifiedPrice") >> v.verified_price;
81+
CborDecodeStream::named(m, "MinPieceSize") >> v.min_piece_size;
82+
CborDecodeStream::named(m, "MaxPieceSize") >> v.max_piece_size;
83+
CborDecodeStream::named(m, "Miner") >> v.miner;
84+
CborDecodeStream::named(m, "Timestamp") >> v.timestamp;
85+
CborDecodeStream::named(m, "Expiry") >> v.expiry;
86+
CborDecodeStream::named(m, "SeqNo") >> v.seq_no;
87+
return s;
88+
}
89+
5090
struct SignedStorageAsk {
91+
virtual ~SignedStorageAsk() = default;
92+
5193
StorageAsk ask;
5294
Signature signature;
95+
96+
/** Returns response digset */
97+
virtual outcome::result<Bytes> getDigest() const = 0;
5398
};
5499

55-
CBOR_TUPLE(SignedStorageAsk, ask, signature)
100+
/** SignedStorageAsk used in V1.0.1 */
101+
struct SignedStorageAskV1_0_1 : public SignedStorageAsk {
102+
SignedStorageAskV1_0_1() = default;
103+
explicit SignedStorageAskV1_0_1(StorageAsk ask) {
104+
this->ask = std::move(ask);
105+
}
106+
SignedStorageAskV1_0_1(StorageAsk ask, Signature signature) {
107+
this->ask = std::move(ask);
108+
this->signature = std::move(signature);
109+
}
110+
111+
outcome::result<Bytes> getDigest() const override {
112+
return codec::cbor::encode(StorageAskV1_0_1{this->ask});
113+
};
114+
};
115+
116+
inline CBOR2_ENCODE(SignedStorageAskV1_0_1) {
117+
return s << (CborEncodeStream::list()
118+
<< StorageAskV1_0_1{v.ask} << v.signature);
119+
}
120+
inline CBOR2_DECODE(SignedStorageAskV1_0_1) {
121+
auto cbor_list{s.list()};
122+
v.ask = cbor_list.get<StorageAskV1_0_1>();
123+
cbor_list >> v.signature;
124+
return s;
125+
}
126+
127+
/** SignedStorageAsk used in V1.1.0 with named fields. */
128+
struct SignedStorageAskV1_1_0 : public SignedStorageAsk {
129+
SignedStorageAskV1_1_0() = default;
130+
explicit SignedStorageAskV1_1_0(StorageAsk ask) {
131+
this->ask = std::move(ask);
132+
}
133+
SignedStorageAskV1_1_0(StorageAsk ask, Signature signature) {
134+
this->ask = std::move(ask);
135+
this->signature = std::move(signature);
136+
}
137+
138+
outcome::result<Bytes> getDigest() const override {
139+
return codec::cbor::encode(StorageAskV1_1_0{this->ask});
140+
};
141+
};
142+
143+
inline CBOR2_ENCODE(SignedStorageAskV1_1_0) {
144+
auto m{CborEncodeStream::orderedMap()};
145+
m["Ask"] << StorageAskV1_1_0{v.ask};
146+
m["Signature"] << v.signature;
147+
return s << m;
148+
}
149+
150+
inline CBOR2_DECODE(SignedStorageAskV1_1_0) {
151+
auto m{s.map()};
152+
v.ask = CborDecodeStream::named(m, "Ask").get<StorageAskV1_1_0>();
153+
CborDecodeStream::named(m, "Signature") >> v.signature;
154+
return s;
155+
}
56156

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

64-
CBOR_TUPLE(AskRequest, miner)
164+
/** AskRequest used in V1.0.1 */
165+
struct AskRequestV1_0_1 : public AskRequest {};
166+
167+
CBOR_TUPLE(AskRequestV1_0_1, miner)
168+
169+
/** AskRequest used in V1.1.0. Cbores with field names. */
170+
struct AskRequestV1_1_0 : public AskRequest {};
171+
172+
inline CBOR2_ENCODE(AskRequestV1_1_0) {
173+
auto m{CborEncodeStream::orderedMap()};
174+
m["Miner"] << v.miner;
175+
return s << m;
176+
}
177+
178+
inline CBOR2_DECODE(AskRequestV1_1_0) {
179+
auto m{s.map()};
180+
CborDecodeStream::named(m, "Miner") >> v.miner;
181+
return s;
182+
}
65183

66184
/**
67185
* AskResponse is the response sent over the network in response to an ask
68186
* request
69187
*/
70188
struct AskResponse {
71-
SignedStorageAsk ask;
189+
virtual ~AskResponse() = default;
190+
191+
virtual const SignedStorageAsk &ask() const = 0;
192+
};
193+
194+
struct AskResponseV1_0_1 : public AskResponse {
195+
AskResponseV1_0_1() = default;
196+
197+
explicit AskResponseV1_0_1(SignedStorageAskV1_0_1 ask)
198+
: ask_(std::move(ask)) {}
199+
200+
const SignedStorageAsk &ask() const override {
201+
return ask_;
202+
}
203+
204+
private:
205+
friend CborEncodeStream &operator<<(CborEncodeStream &,
206+
const AskResponseV1_0_1 &);
207+
friend CborDecodeStream &operator>>(CborDecodeStream &,
208+
AskResponseV1_0_1 &);
209+
210+
SignedStorageAskV1_0_1 ask_;
72211
};
73212

74-
CBOR_TUPLE(AskResponse, ask)
213+
inline CBOR2_ENCODE(AskResponseV1_0_1) {
214+
auto cbor_list = CborEncodeStream::list();
215+
cbor_list << v.ask_;
216+
return s << cbor_list;
217+
}
218+
inline CBOR2_DECODE(AskResponseV1_0_1) {
219+
auto cbor_list{s.list()};
220+
cbor_list >> v.ask_;
221+
return s;
222+
}
223+
224+
struct AskResponseV1_1_0 : public AskResponse {
225+
AskResponseV1_1_0() = default;
226+
227+
explicit AskResponseV1_1_0(SignedStorageAskV1_1_0 ask)
228+
: ask_(std::move(ask)) {}
229+
230+
const SignedStorageAsk &ask() const override {
231+
return ask_;
232+
}
233+
234+
private:
235+
friend CborEncodeStream &operator<<(CborEncodeStream &,
236+
const AskResponseV1_1_0 &);
237+
friend CborDecodeStream &operator>>(CborDecodeStream &,
238+
AskResponseV1_1_0 &);
239+
240+
SignedStorageAskV1_1_0 ask_;
241+
};
242+
243+
inline CBOR2_ENCODE(AskResponseV1_1_0) {
244+
auto m{CborEncodeStream::orderedMap()};
245+
m["Ask"] << v.ask_;
246+
return s << m;
247+
}
248+
249+
inline CBOR2_DECODE(AskResponseV1_1_0) {
250+
auto m{s.map()};
251+
CborDecodeStream::named(m, "Ask") >> v.ask_;
252+
return s;
253+
}
75254

76255
} // namespace fc::markets::storage
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 "vm/actor/builtin/types/market/deal.hpp"
9+
10+
namespace fc::markets::storage::client {
11+
using vm::actor::builtin::types::market::ClientDealProposal;
12+
13+
/** Internal state of a deal on the client side. */
14+
struct ClientDeal {
15+
ClientDealProposal client_deal_proposal;
16+
CID proposal_cid;
17+
boost::optional<CID> add_funds_cid;
18+
StorageDealStatus state;
19+
PeerInfo miner;
20+
Address miner_worker;
21+
DealId deal_id;
22+
DataRef data_ref;
23+
bool is_fast_retrieval;
24+
std::string message;
25+
CID publish_message;
26+
};
27+
28+
} // namespace fc::markets::storage::client

0 commit comments

Comments
 (0)