Skip to content

Commit 599716b

Browse files
authored
Refactor cbor tuple and chain epoch (#104)
Signed-off-by: turuslan <[email protected]>
1 parent bfb774c commit 599716b

23 files changed

+216
-577
lines changed

core/codec/cbor/cbor_decode_stream.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ namespace fc::codec::cbor {
2222
explicit CborDecodeStream(gsl::span<const uint8_t> data);
2323

2424
/** Decodes integer or bool */
25-
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
25+
template <
26+
typename T,
27+
typename = std::enable_if_t<std::is_integral_v<T> || std::is_enum_v<T>>>
2628
CborDecodeStream &operator>>(T &num) {
2729
if constexpr (std::is_same_v<T, bool>) {
2830
if (!cbor_value_is_boolean(&value_)) {

core/codec/cbor/cbor_encode_stream.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ namespace fc::codec::cbor {
2020
static constexpr auto is_cbor_encoder_stream = true;
2121

2222
/** Encodes integer or bool */
23-
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
23+
template <
24+
typename T,
25+
typename = std::enable_if_t<std::is_integral_v<T> || std::is_enum_v<T>>>
2426
CborEncodeStream &operator<<(T num) {
2527
addCount(1);
2628
std::array<uint8_t, 9> buffer{0};
2729
CborEncoder encoder;
2830
cbor_encoder_init(&encoder, buffer.data(), buffer.size(), 0);
29-
if (std::is_same_v<T, bool>) {
31+
if constexpr (std::is_same_v<T, bool>) {
3032
cbor_encode_boolean(&encoder, num);
31-
} else if (std::is_unsigned_v<T>) {
33+
} else if constexpr (std::is_unsigned_v<T>) {
3234
cbor_encode_uint(&encoder, static_cast<uint64_t>(num));
3335
} else {
3436
cbor_encode_int(&encoder, static_cast<int64_t>(num));

core/codec/cbor/streams_annotation.hpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,61 @@
1818
std::remove_reference_t<Stream>::is_cbor_decoder_stream>> \
1919
Stream &operator>>(Stream &&s, type &var)
2020

21+
#define _CBOR_TUPLE_1(op, m) op t.m
22+
#define _CBOR_TUPLE_2(op, m, ...) \
23+
_CBOR_TUPLE_1(op, m) _CBOR_TUPLE_1(op, __VA_ARGS__)
24+
#define _CBOR_TUPLE_3(op, m, ...) \
25+
_CBOR_TUPLE_1(op, m) _CBOR_TUPLE_2(op, __VA_ARGS__)
26+
#define _CBOR_TUPLE_4(op, m, ...) \
27+
_CBOR_TUPLE_1(op, m) _CBOR_TUPLE_3(op, __VA_ARGS__)
28+
#define _CBOR_TUPLE_5(op, m, ...) \
29+
_CBOR_TUPLE_1(op, m) _CBOR_TUPLE_4(op, __VA_ARGS__)
30+
#define _CBOR_TUPLE_6(op, m, ...) \
31+
_CBOR_TUPLE_1(op, m) _CBOR_TUPLE_5(op, __VA_ARGS__)
32+
#define _CBOR_TUPLE_7(op, m, ...) \
33+
_CBOR_TUPLE_1(op, m) _CBOR_TUPLE_6(op, __VA_ARGS__)
34+
#define _CBOR_TUPLE_8(op, m, ...) \
35+
_CBOR_TUPLE_1(op, m) _CBOR_TUPLE_7(op, __VA_ARGS__)
36+
#define _CBOR_TUPLE_9(op, m, ...) \
37+
_CBOR_TUPLE_1(op, m) _CBOR_TUPLE_8(op, __VA_ARGS__)
38+
#define _CBOR_TUPLE_10(op, m, ...) \
39+
_CBOR_TUPLE_1(op, m) _CBOR_TUPLE_9(op, __VA_ARGS__)
40+
#define _CBOR_TUPLE_11(op, m, ...) \
41+
_CBOR_TUPLE_1(op, m) _CBOR_TUPLE_10(op, __VA_ARGS__)
42+
#define _CBOR_TUPLE_12(op, m, ...) \
43+
_CBOR_TUPLE_1(op, m) _CBOR_TUPLE_11(op, __VA_ARGS__)
44+
#define _CBOR_TUPLE_13(op, m, ...) \
45+
_CBOR_TUPLE_1(op, m) _CBOR_TUPLE_12(op, __VA_ARGS__)
46+
#define _CBOR_TUPLE_V( \
47+
_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, f, ...) \
48+
f
49+
#define _CBOR_TUPLE(op, ...) \
50+
_CBOR_TUPLE_V(__VA_ARGS__, \
51+
_CBOR_TUPLE_13, \
52+
_CBOR_TUPLE_12, \
53+
_CBOR_TUPLE_11, \
54+
_CBOR_TUPLE_10, \
55+
_CBOR_TUPLE_9, \
56+
_CBOR_TUPLE_8, \
57+
_CBOR_TUPLE_7, \
58+
_CBOR_TUPLE_6, \
59+
_CBOR_TUPLE_5, \
60+
_CBOR_TUPLE_4, \
61+
_CBOR_TUPLE_3, \
62+
_CBOR_TUPLE_2, \
63+
_CBOR_TUPLE_1) \
64+
(op, __VA_ARGS__)
65+
66+
#define CBOR_ENCODE_TUPLE(T, ...) \
67+
CBOR_ENCODE(T, t) { \
68+
return s << (s.list() _CBOR_TUPLE(<<, __VA_ARGS__)); \
69+
}
70+
71+
#define CBOR_TUPLE(T, ...) \
72+
CBOR_ENCODE_TUPLE(T, __VA_ARGS__) \
73+
CBOR_DECODE(T, t) { \
74+
s.list() _CBOR_TUPLE(>>, __VA_ARGS__); \
75+
return s; \
76+
}
77+
2178
#endif // CPP_FILECOIN_STREAMS_ANNOTATION_HPP

core/crypto/randomness/impl/randomness_provider_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace fc::crypto::randomness {
2121
Serialization s,
2222
const ChainEpoch &index) {
2323
return deriveRandomnessInternal(
24-
static_cast<uint64_t>(tag), std::move(s), index.convert_to<int64_t>());
24+
static_cast<uint64_t>(tag), std::move(s), index);
2525
}
2626

2727
Randomness RandomnessProviderImpl::deriveRandomnessInternal(uint64_t tag,

core/primitives/address/address_codec.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,8 @@ namespace fc::primitives::address {
5353
CBOR_DECODE(Address, address) {
5454
std::vector<uint8_t> data{};
5555
s >> data;
56-
auto res = decode(data);
57-
if (res.has_error()) {
58-
outcome::raise(AddressError::INVALID_PAYLOAD);
59-
}
60-
address = std::move(res.value());
56+
OUTCOME_EXCEPT(decoded, decode(data));
57+
address = std::move(decoded);
6158
return s;
6259
}
6360

core/primitives/block/block.hpp

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,20 @@ namespace fc::primitives::block {
5757
&& lhs.fork_signaling == rhs.fork_signaling;
5858
}
5959

60-
CBOR_ENCODE(BlockHeader, block) {
61-
return s << (s.list() << block.miner << block.ticket << block.epost_proof
62-
<< block.parents << block.parent_weight
63-
<< block.height << block.parent_state_root
64-
<< block.parent_message_receipts << block.messages
65-
<< block.bls_aggregate << block.timestamp
66-
<< block.block_sig << block.fork_signaling);
67-
}
68-
69-
CBOR_DECODE(BlockHeader, block) {
70-
s.list() >> block.miner >> block.ticket >> block.epost_proof
71-
>> block.parents >> block.parent_weight >> block.height
72-
>> block.parent_state_root >> block.parent_message_receipts
73-
>> block.messages >> block.bls_aggregate >> block.timestamp
74-
>> block.block_sig >> block.fork_signaling;
75-
return s;
76-
}
60+
CBOR_TUPLE(BlockHeader,
61+
miner,
62+
ticket,
63+
epost_proof,
64+
parents,
65+
parent_weight,
66+
height,
67+
parent_state_root,
68+
parent_message_receipts,
69+
messages,
70+
bls_aggregate,
71+
timestamp,
72+
block_sig,
73+
fork_signaling)
7774

78-
CBOR_ENCODE(MsgMeta, meta) {
79-
return s << (s.list() << meta.bls_messages << meta.secpk_messages);
80-
}
81-
82-
CBOR_DECODE(MsgMeta, meta) {
83-
s.list() >> meta.bls_messages >> meta.secpk_messages;
84-
return s;
85-
}
75+
CBOR_TUPLE(MsgMeta, bls_messages, secpk_messages)
8676
} // namespace fc::primitives::block

core/primitives/chain_epoch/chain_epoch.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ namespace fc::primitives {
1414
* @brief epoch index type represents a round of a blockchain protocol, which
1515
* acts as a proxy for time within the VM
1616
*/
17-
using ChainEpoch = BigInt;
17+
using ChainEpoch = int64_t;
1818

19-
using EpochDuration = BigInt;
19+
using EpochDuration = int64_t;
20+
21+
constexpr ChainEpoch kChainEpochUndefined{-1};
2022

2123
} // namespace fc::primitives
2224

core/primitives/chain_epoch/chain_epoch_codec.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace fc::primitives::chain_epoch {
1313

1414
std::string encodeToByteString(const ChainEpoch &epoch) {
1515
// TODO (a.chernyshov) actor-specs uses Protobuf Varint encoding
16-
UVarint number(epoch.convert_to<int64_t>());
16+
UVarint number(epoch);
1717
auto encoded = number.toBytes();
1818
return std::string(encoded.begin(), encoded.end());
1919
}

core/primitives/ticket/epost_ticket_codec.hpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,7 @@ namespace fc::primitives::ticket {
2323
OUTCOME_HPP_DECLARE_ERROR(fc::primitives::ticket, EPoSTTicketCodecError)
2424

2525
namespace fc::primitives::ticket {
26-
/**
27-
* @brief cbor-encode EPostTicket instance
28-
* @tparam Stream cbor-encoder stream type
29-
* @param s stream reference
30-
* @param ticket Ticket const reference to encode
31-
* @return stream reference
32-
*/
33-
CBOR_ENCODE(EPostTicket, ticket) {
34-
return s << (s.list() << ticket.partial << ticket.sector_id
35-
<< ticket.challenge_index);
36-
}
26+
CBOR_ENCODE_TUPLE(EPostTicket, partial, sector_id, challenge_index)
3727

3828
/**
3929
* @brief cbor-decode EPostTicket instance
@@ -52,16 +42,7 @@ namespace fc::primitives::ticket {
5242
return s;
5343
}
5444

55-
/**
56-
* @brief cbor-encodes EPostProof instance
57-
* @tparam Stream cbor-encoder stream type
58-
* @param s stream reference
59-
* @param epp EPostProof const reference to encode
60-
* @return stream reference
61-
*/
62-
CBOR_ENCODE(EPostProof, epp) {
63-
return s << (s.list() << epp.proof << epp.post_rand << epp.candidates);
64-
}
45+
CBOR_ENCODE_TUPLE(EPostProof, proof, post_rand, candidates)
6546

6647
/**
6748
* @brief cbor-decodes EPostProof instance

core/primitives/ticket/ticket_codec.hpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
#include "primitives/ticket/ticket.hpp"
1515

1616
namespace fc::primitives::ticket {
17-
enum class TicketCodecError: int {
18-
INVALID_TICKET_LENGTH = 1, // ticket decode error, invalid data length
17+
enum class TicketCodecError : int {
18+
INVALID_TICKET_LENGTH = 1, // ticket decode error, invalid data length
1919
};
2020
} // namespace fc::primitives::ticket
2121

@@ -29,9 +29,7 @@ namespace fc::primitives::ticket {
2929
* @param ticket Ticket const reference to encode
3030
* @return stream reference
3131
*/
32-
CBOR_ENCODE(Ticket, ticket) {
33-
return s << (s.list() << ticket.bytes);
34-
}
32+
CBOR_ENCODE_TUPLE(Ticket, bytes)
3533

3634
/**
3735
* @brief cbor-decodes Ticket instance
@@ -52,4 +50,4 @@ namespace fc::primitives::ticket {
5250

5351
} // namespace fc::primitives::ticket
5452

55-
#endif //CPP_FILECOIN_CORE_PRIMITIVES_TICKET_TICKET_CODEC_HPP
53+
#endif // CPP_FILECOIN_CORE_PRIMITIVES_TICKET_TICKET_CODEC_HPP

0 commit comments

Comments
 (0)