Skip to content

Commit 0629e12

Browse files
authored
Block (#68)
Signed-off-by: turuslan <[email protected]>
1 parent 5fbe0b1 commit 0629e12

File tree

14 files changed

+216
-36
lines changed

14 files changed

+216
-36
lines changed

core/codec/cbor/cbor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace fc::codec::cbor {
3838
template <typename T>
3939
outcome::result<T> decode(gsl::span<const uint8_t> input) {
4040
try {
41-
T data;
41+
T data{};
4242
CborDecodeStream decoder(input);
4343
decoder >> data;
4444
return data;

core/codec/cbor/cbor_common.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <cstdint>
1010

11+
#include <boost/optional.hpp>
1112
#include <libp2p/multi/content_identifier_codec.hpp>
1213

1314
#include "codec/cbor/cbor_errors.hpp"

core/codec/cbor/cbor_decode_stream.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,35 @@ namespace fc::codec::cbor {
5959
return *this;
6060
}
6161

62+
/// Decodes nullable optional value
63+
template <typename T>
64+
CborDecodeStream &operator>>(boost::optional<T> &optional) {
65+
if (isNull()) {
66+
optional = boost::none;
67+
next();
68+
} else {
69+
T value{};
70+
*this >> value;
71+
optional = value;
72+
}
73+
return *this;
74+
}
75+
76+
/// Decodes list elements into vector
77+
template <typename T>
78+
CborDecodeStream &operator>>(std::vector<T> &values) {
79+
auto n = listLength();
80+
auto l = list();
81+
values.clear();
82+
values.reserve(n);
83+
for (auto i = 0u; i < n; ++i) {
84+
T value{};
85+
l >> value;
86+
values.push_back(value);
87+
}
88+
return *this;
89+
}
90+
6291
/** Decodes bytes */
6392
CborDecodeStream &operator>>(std::vector<uint8_t> &bytes);
6493
/** Decodes string */

core/codec/cbor/cbor_encode_stream.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77

88
namespace fc::codec::cbor {
99
CborEncodeStream &CborEncodeStream::operator<<(
10-
const gsl::span<const uint8_t> &bytes) {
10+
const std::vector<uint8_t> &bytes) {
11+
return *this << gsl::make_span(bytes);
12+
}
13+
14+
CborEncodeStream &CborEncodeStream::operator<<(
15+
gsl::span<const uint8_t> bytes) {
1116
addCount(1);
1217

1318
std::vector<uint8_t> encoded(9 + bytes.size());

core/codec/cbor/cbor_encode_stream.hpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,31 @@ namespace fc::codec::cbor {
4040
return *this;
4141
}
4242

43+
/// Encodes nullable optional value
44+
template <typename T>
45+
CborEncodeStream &operator<<(const boost::optional<T> &optional) {
46+
if (optional) {
47+
*this << *optional;
48+
} else {
49+
*this << nullptr;
50+
}
51+
return *this;
52+
}
53+
54+
/// Encodes elements into list
55+
template <typename T>
56+
CborEncodeStream &operator<<(const std::vector<T> &values) {
57+
auto l = list();
58+
for (auto &value : values) {
59+
l << value;
60+
}
61+
return *this << l;
62+
}
63+
64+
/** Encodes bytes */
65+
CborEncodeStream &operator<<(const std::vector<uint8_t> &bytes);
4366
/** Encodes bytes */
44-
CborEncodeStream &operator<<(const gsl::span<const uint8_t> &bytes);
67+
CborEncodeStream &operator<<(gsl::span<const uint8_t> bytes);
4568
/** Encodes string */
4669
CborEncodeStream &operator<<(const std::string &str);
4770
/** Encodes CID */

core/primitives/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
#
55

66
add_subdirectory(address)
7+
add_subdirectory(block)
78
add_subdirectory(cid)
89
add_subdirectory(ticket)

core/primitives/block/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#
2+
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
6+
add_library(block INTERFACE)
7+
target_link_libraries(block INTERFACE
8+
address
9+
cbor
10+
tickets
11+
)

core/primitives/block/block.hpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <boost/optional.hpp>
2+
3+
#include "primitives/address/address.hpp"
4+
#include "primitives/address/address_codec.hpp"
5+
#include "primitives/big_int.hpp"
6+
#include "primitives/cid/cid.hpp"
7+
#include "primitives/ticket/epost_ticket.hpp"
8+
#include "primitives/ticket/epost_ticket_codec.hpp"
9+
#include "primitives/ticket/ticket.hpp"
10+
#include "primitives/ticket/ticket_codec.hpp"
11+
12+
namespace fc::primitives::block {
13+
using primitives::BigInt;
14+
using primitives::address::Address;
15+
using primitives::ticket::EPostProof;
16+
using primitives::ticket::Ticket;
17+
18+
// TODO(turuslan): FIL-72 signature
19+
using Signature = std::vector<uint8_t>;
20+
21+
struct BlockHeader {
22+
Address miner;
23+
boost::optional<Ticket> ticket;
24+
EPostProof epost_proof;
25+
std::vector<CID> parents;
26+
BigInt parent_weight;
27+
uint64_t height;
28+
CID parent_state_root;
29+
CID parent_message_receipts;
30+
CID messages;
31+
Signature bls_aggregate;
32+
uint64_t timestamp;
33+
boost::optional<Signature> block_sig;
34+
};
35+
36+
template <class Stream,
37+
typename = std::enable_if_t<
38+
std::remove_reference_t<Stream>::is_cbor_encoder_stream>>
39+
Stream &operator<<(Stream &&s, const BlockHeader &block) {
40+
return s << (s.list() << block.miner << block.ticket << block.epost_proof
41+
<< block.parents << block.parent_weight
42+
<< block.height << block.parent_state_root
43+
<< block.parent_message_receipts << block.messages
44+
<< block.bls_aggregate << block.timestamp
45+
<< block.block_sig);
46+
}
47+
48+
template <class Stream,
49+
typename = std::enable_if_t<
50+
std::remove_reference_t<Stream>::is_cbor_decoder_stream>>
51+
Stream &operator>>(Stream &&s, BlockHeader &block) {
52+
s.list() >> block.miner >> block.ticket >> block.epost_proof
53+
>> block.parents >> block.parent_weight >> block.height
54+
>> block.parent_state_root >> block.parent_message_receipts
55+
>> block.messages >> block.bls_aggregate >> block.timestamp
56+
>> block.block_sig;
57+
return s;
58+
}
59+
} // namespace fc::primitives::block

core/primitives/ticket/epost_ticket_codec.hpp

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,12 @@ namespace fc::primitives::ticket {
4545
typename = std::enable_if_t<
4646
std::remove_reference<Stream>::type::is_cbor_decoder_stream>>
4747
Stream &operator>>(Stream &&s, EPostTicket &ticket) {
48-
auto &&l = s.list();
4948
std::vector<uint8_t> data{};
50-
51-
l >> data;
49+
s.list() >> data >> ticket.sector_id >> ticket.challenge_index;
5250
if (data.size() != ticket.partial.size()) {
5351
outcome::raise(EPoSTTicketCodecError::INVALID_PARTIAL_LENGTH);
5452
}
5553
std::copy(data.begin(), data.end(), ticket.partial.begin());
56-
l >> ticket.sector_id >> ticket.challenge_index;
5754
return s;
5855
}
5956

@@ -68,14 +65,8 @@ namespace fc::primitives::ticket {
6865
typename = std::enable_if_t<
6966
std::remove_reference<Stream>::type::is_cbor_encoder_stream>>
7067
Stream &operator<<(Stream &&s, const EPostProof &epp) noexcept {
71-
auto && list = s.list();
72-
list << epp.proof << epp.post_rand;
73-
auto &&l = list.list();
74-
for (auto &item : epp.candidates) {
75-
l << item;
76-
}
77-
78-
return s << (list << l);
68+
return s << (s.list() << epp.proof << epp.post_rand
69+
<< epp.candidates);
7970
}
8071

8172
/**
@@ -89,30 +80,13 @@ namespace fc::primitives::ticket {
8980
typename = std::enable_if_t<
9081
std::remove_reference<Stream>::type::is_cbor_decoder_stream>>
9182
Stream &operator>>(Stream &&s, EPostProof &epp) {
92-
std::vector<uint8_t> proof{};
93-
auto &&list = s.list();
94-
list >> proof;
83+
std::vector<uint8_t> proof, rand;
84+
s.list() >> proof >> rand >> epp.candidates;
9585
epp.proof = common::Buffer(std::move(proof));
96-
std::vector<uint8_t> rand{};
97-
list >> rand;
9886
if (rand.size() != epp.post_rand.size()) {
9987
outcome::raise(EPoSTTicketCodecError::INVALID_POST_RAND_LENGTH);
10088
}
10189
std::copy(rand.begin(), rand.end(), epp.post_rand.begin());
102-
103-
auto n = list.listLength();
104-
std::vector<EPostTicket> candidates{};
105-
candidates.reserve(n);
106-
107-
auto &&l = list.list();
108-
for (size_t i = 0; i < n; ++i) {
109-
EPostTicket ticket{};
110-
l >> ticket;
111-
candidates.push_back(ticket);
112-
}
113-
114-
epp.candidates = std::move(candidates);
115-
11690
return s;
11791
}
11892
} // namespace fc::primitives::ticket

core/primitives/ticket/ticket_codec.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ namespace fc::primitives::ticket {
4545
typename = std::enable_if_t<
4646
std::remove_reference<Stream>::type::is_cbor_decoder_stream>>
4747
Stream &operator>>(Stream &&s, Ticket &ticket) {
48-
auto && l = s.list();
4948
std::vector<uint8_t> data{};
50-
l >> data;
49+
s.list() >> data;
5150
if (data.size() != ticket.bytes.size()) {
5251
outcome::raise(TicketCodecError::INVALID_TICKET_LENGTH);
5352
}

0 commit comments

Comments
 (0)