Skip to content

Commit 39b3b6f

Browse files
authored
Serialization vectors (#152)
Signed-off-by: turuslan <[email protected]>
1 parent 7924a20 commit 39b3b6f

38 files changed

+359
-188
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
[submodule "deps/filecoin-ffi"]
55
path = deps/filecoin-ffi
66
url = https://github.com/filecoin-project/filecoin-ffi.git
7+
[submodule "test/core/serialization/serialization_vectors"]
8+
path = test/core/serialization/serialization_vectors
9+
url = https://github.com/filecoin-project/serialization-vectors.git

core/api/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ target_link_libraries(api
1010
address
1111
chain_store
1212
cid
13+
message
1314
)
1415

1516
add_library(rpc

core/api/rpc/json.hpp

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "api/api.hpp"
1313
#include "api/rpc/json_errors.hpp"
14+
#include "common/enum.hpp"
1415
#include "primitives/address/address_codec.hpp"
1516

1617
#define COMMA ,
@@ -26,6 +27,7 @@ namespace fc::api {
2627
using crypto::signature::Signature;
2728
using primitives::BigInt;
2829
using primitives::block::BlockHeader;
30+
using primitives::sector::PoStProof;
2931
using primitives::ticket::EPostProof;
3032
using primitives::ticket::EPostTicket;
3133
using primitives::ticket::Ticket;
@@ -35,6 +37,8 @@ namespace fc::api {
3537
using vm::actor::builtin::payment_channel::Merge;
3638
using vm::actor::builtin::payment_channel::ModularVerificationParameter;
3739
using base64 = cppcodec::base64_rfc4648;
40+
using SignatureType = crypto::signature::Type;
41+
using primitives::sector::RegisteredProof;
3842

3943
struct Request {
4044
uint64_t id;
@@ -82,7 +86,10 @@ namespace fc::api {
8286
Set(j, key, encode(v));
8387
}
8488

85-
static auto decodeBase64(const Value &j) {
89+
static std::vector<uint8_t> decodeBase64(const Value &j) {
90+
if (j.IsNull()) {
91+
return {};
92+
}
8693
return base64::decode(AsString(j));
8794
}
8895

@@ -229,16 +236,16 @@ namespace fc::api {
229236
}
230237

231238
ENCODE(Signature) {
232-
const char *type;
239+
uint64_t type;
233240
gsl::span<const uint8_t> data;
234241
visit_in_place(
235242
v,
236243
[&](const BlsSignature &bls) {
237-
type = "bls";
244+
type = SignatureType::BLS;
238245
data = gsl::make_span(bls);
239246
},
240247
[&](const Secp256k1Signature &secp) {
241-
type = "secp256k1";
248+
type = SignatureType::SECP256K1;
242249
data = gsl::make_span(secp);
243250
});
244251
Value j{rapidjson::kObjectType};
@@ -248,11 +255,12 @@ namespace fc::api {
248255
}
249256

250257
DECODE(Signature) {
251-
auto type = AsString(Get(j, "Type"));
258+
uint64_t type;
259+
decode(type, Get(j, "Type"));
252260
auto &data = Get(j, "Data");
253-
if (type == "bls") {
261+
if (type == SignatureType::BLS) {
254262
v = decode<BlsSignature>(data);
255-
} else if (type == "secp256k1") {
263+
} else if (type == SignatureType::SECP256K1) {
256264
v = decode<Secp256k1Signature>(data);
257265
} else {
258266
outcome::raise(JsonError::WRONG_ENUM);
@@ -273,16 +281,30 @@ namespace fc::api {
273281
decode(v.challenge_index, Get(j, "ChallengeIndex"));
274282
}
275283

284+
ENCODE(PoStProof) {
285+
Value j{rapidjson::kObjectType};
286+
Set(j, "RegisteredProof", common::to_int(v.registered_proof));
287+
Set(j, "ProofBytes", gsl::make_span(v.proof));
288+
return j;
289+
}
290+
291+
DECODE(PoStProof) {
292+
std::underlying_type_t<RegisteredProof> registered_proof;
293+
decode(registered_proof, Get(j, "RegisteredProof"));
294+
v.registered_proof = RegisteredProof{registered_proof};
295+
decode(v.proof, Get(j, "ProofBytes"));
296+
}
297+
276298
ENCODE(EPostProof) {
277299
Value j{rapidjson::kObjectType};
278-
Set(j, "Proof", gsl::make_span(v.proof));
300+
Set(j, "Proofs", v.proofs);
279301
Set(j, "PostRand", gsl::make_span(v.post_rand));
280302
Set(j, "Candidates", v.candidates);
281303
return j;
282304
}
283305

284306
DECODE(EPostProof) {
285-
decode(v.proof, Get(j, "Proof"));
307+
decode(v.proofs, Get(j, "Proofs"));
286308
decode(v.post_rand, Get(j, "PostRand"));
287309
decode(v.candidates, Get(j, "Candidates"));
288310
}

core/primitives/sector/sector.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ namespace fc::primitives::sector {
8989
Proof proof;
9090
};
9191

92+
inline bool operator==(const PoStProof &lhs, const PoStProof &rhs) {
93+
return lhs.registered_proof == rhs.registered_proof
94+
&& lhs.proof == rhs.proof;
95+
}
96+
9297
struct PrivatePoStCandidateProof {
9398
RegisteredProof registered_proof;
9499
Buffer externalized;

core/primitives/ticket/epost_ticket.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace fc::primitives::ticket {
1212
}
1313

1414
bool operator==(const EPostProof &lhs, const EPostProof &rhs) {
15-
return lhs.proof == rhs.proof && lhs.post_rand == rhs.post_rand
15+
return lhs.proofs == rhs.proofs && lhs.post_rand == rhs.post_rand
1616
&& lhs.candidates == rhs.candidates;
1717
}
1818
} // namespace fc::primitives::ticket

core/primitives/ticket/epost_ticket.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "common/blob.hpp"
1010
#include "common/buffer.hpp"
1111
#include "crypto/vrf/vrf_types.hpp"
12+
#include "primitives/sector/sector.hpp"
1213

1314
namespace fc::primitives::ticket {
1415
/**
@@ -27,8 +28,8 @@ namespace fc::primitives::ticket {
2728
* @struct EPostProof proof of space/time
2829
*/
2930
struct EPostProof {
30-
common::Buffer proof; ///< number of bytes is not known beforehand
31-
PostRandomness post_rand; ///< randomness
31+
std::vector<sector::PoStProof> proofs;
32+
PostRandomness post_rand; ///< randomness
3233
std::vector<EPostTicket> candidates; ///< candidates
3334
};
3435

core/primitives/ticket/epost_ticket_codec.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace fc::primitives::ticket {
4242
return s;
4343
}
4444

45-
CBOR_ENCODE_TUPLE(EPostProof, proof, post_rand, candidates)
45+
CBOR_ENCODE_TUPLE(EPostProof, proofs, post_rand, candidates)
4646

4747
/**
4848
* @brief cbor-decodes EPostProof instance
@@ -52,10 +52,8 @@ namespace fc::primitives::ticket {
5252
* @return stream reference
5353
*/
5454
CBOR_DECODE(EPostProof, epp) {
55-
std::vector<uint8_t> proof;
5655
std::vector<uint8_t> rand;
57-
s.list() >> proof >> rand >> epp.candidates;
58-
epp.proof = common::Buffer(std::move(proof));
56+
s.list() >> epp.proofs >> rand >> epp.candidates;
5957
if (rand.size() != epp.post_rand.size()) {
6058
outcome::raise(EPoSTTicketCodecError::INVALID_POST_RAND_LENGTH);
6159
}

core/primitives/types.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ namespace fc::primitives {
2727

2828
using DealId = uint64_t;
2929

30+
using GasAmount = int64_t;
31+
3032
struct SectorStorageWeightDesc {
3133
SectorSize sector_size;
3234
EpochDuration duration;

core/vm/message/message.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "primitives/address/address.hpp"
1616
#include "primitives/address/address_codec.hpp"
1717
#include "primitives/big_int.hpp"
18+
#include "primitives/types.hpp"
1819
#include "vm/actor/actor.hpp"
1920

2021
namespace fc::vm::message {
@@ -32,6 +33,7 @@ namespace fc::vm::message {
3233
using actor::MethodParams;
3334
using crypto::signature::Signature;
3435
using primitives::BigInt;
36+
using primitives::GasAmount;
3537
using primitives::address::Address;
3638

3739
/**
@@ -45,7 +47,7 @@ namespace fc::vm::message {
4547
uint64_t nonce,
4648
BigInt value,
4749
BigInt gasPrice,
48-
BigInt gasLimit,
50+
GasAmount gasLimit,
4951
MethodNumber method,
5052
MethodParams params)
5153
: to{std::move(to)},
@@ -65,7 +67,7 @@ namespace fc::vm::message {
6567
BigInt value{};
6668

6769
BigInt gasPrice{};
68-
BigInt gasLimit{};
70+
GasAmount gasLimit{};
6971

7072
MethodNumber method{};
7173
MethodParams params{};

core/vm/runtime/env.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define FILECOIN_CORE_VM_RUNTIME_ENV_HPP
88

99
#include "crypto/randomness/randomness_provider.hpp"
10+
#include "primitives/types.hpp"
1011
#include "vm/actor/invoker.hpp"
1112
#include "vm/indices/indices.hpp"
1213
#include "vm/state/state_tree.hpp"
@@ -35,7 +36,7 @@ namespace fc::vm::runtime {
3536
outcome::result<MessageReceipt> applyMessage(
3637
const UnsignedMessage &message);
3738

38-
outcome::result<InvocationOutput> send(BigInt &gas_used,
39+
outcome::result<InvocationOutput> send(GasAmount &gas_used,
3940
const Address &origin,
4041
const UnsignedMessage &message);
4142

0 commit comments

Comments
 (0)