Skip to content

Commit bfb774c

Browse files
authored
vm interpreter (#96)
Signed-off-by: turuslan <[email protected]>
1 parent c8c9dfb commit bfb774c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+986
-258
lines changed

core/codec/cbor/cbor_decode_stream.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@ namespace fc::codec::cbor {
1717
value_.remaining = UINT32_MAX;
1818
}
1919

20-
CborDecodeStream &CborDecodeStream::operator>>(std::vector<uint8_t> &bytes) {
21-
if (!cbor_value_is_byte_string(&value_)) {
22-
outcome::raise(CborDecodeError::WRONG_TYPE);
23-
}
24-
size_t size;
25-
if (CborNoError != cbor_value_get_string_length(&value_, &size)) {
26-
outcome::raise(CborDecodeError::INVALID_CBOR);
20+
CborDecodeStream &CborDecodeStream::operator>>(gsl::span<uint8_t> bytes) {
21+
auto size = bytesLength();
22+
if (static_cast<size_t>(bytes.size()) != size) {
23+
outcome::raise(CborDecodeError::WRONG_SIZE);
2724
}
28-
bytes.resize(size);
2925
auto value = value_;
3026
value.remaining = 1;
3127
if (CborNoError
@@ -36,6 +32,11 @@ namespace fc::codec::cbor {
3632
return *this;
3733
}
3834

35+
CborDecodeStream &CborDecodeStream::operator>>(std::vector<uint8_t> &bytes) {
36+
bytes.resize(bytesLength());
37+
return *this >> gsl::make_span(bytes);
38+
}
39+
3940
CborDecodeStream &CborDecodeStream::operator>>(std::string &str) {
4041
if (!cbor_value_is_text_string(&value_)) {
4142
outcome::raise(CborDecodeError::WRONG_TYPE);
@@ -186,6 +187,17 @@ namespace fc::codec::cbor {
186187
return map;
187188
}
188189

190+
size_t CborDecodeStream::bytesLength() const {
191+
if (!cbor_value_is_byte_string(&value_)) {
192+
outcome::raise(CborDecodeError::WRONG_TYPE);
193+
}
194+
size_t size;
195+
if (CborNoError != cbor_value_get_string_length(&value_, &size)) {
196+
outcome::raise(CborDecodeError::INVALID_CBOR);
197+
}
198+
return size;
199+
}
200+
189201
CborDecodeStream CborDecodeStream::container() const {
190202
auto stream = *this;
191203
if (CborNoError != cbor_value_enter_container(&value_, &stream.value_)) {

core/codec/cbor/cbor_decode_stream.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ namespace fc::codec::cbor {
9797
return *this;
9898
}
9999

100+
/// Decodes bytes
101+
CborDecodeStream &operator>>(gsl::span<uint8_t> bytes);
100102
/** Decodes bytes */
101103
CborDecodeStream &operator>>(std::vector<uint8_t> &bytes);
102104
/** Decodes string */
@@ -121,6 +123,8 @@ namespace fc::codec::cbor {
121123
std::vector<uint8_t> raw();
122124
/** Creates map container decode substream map */
123125
std::map<std::string, CborDecodeStream> map();
126+
/// Returns bytestring length
127+
size_t bytesLength() const;
124128

125129
private:
126130
CborDecodeStream container() const;

core/codec/cbor/cbor_errors.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ OUTCOME_CPP_DEFINE_CATEGORY(fc::codec::cbor, CborDecodeError, e) {
3030
return "Invalid CBOR CID";
3131
case CborDecodeError::INVALID_CID:
3232
return "Invalid CID";
33+
case CborDecodeError::WRONG_SIZE:
34+
return "Wrong size";
3335
default:
3436
return "Unknown error";
3537
}

core/codec/cbor/cbor_errors.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ namespace fc::codec::cbor {
1616
WRONG_TYPE,
1717
INT_OVERFLOW,
1818
INVALID_CBOR_CID,
19-
INVALID_CID
19+
INVALID_CID,
20+
WRONG_SIZE,
2021
};
2122
} // namespace fc::codec::cbor
2223

core/common/blob.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ namespace fc::common {
133133
* @param blob value to encode
134134
* @return reference to stream
135135
*/
136-
template <class Stream, size_t size>
136+
template <class Stream,
137+
size_t size,
138+
typename = std::enable_if_t<
139+
!std::remove_reference_t<Stream>::is_cbor_encoder_stream>>
137140
Stream &operator<<(Stream &s, const Blob<size> &blob) {
138141
for (auto &&it = blob.begin(), end = blob.end(); it != end; ++it) {
139142
s << *it;
@@ -149,7 +152,10 @@ namespace fc::common {
149152
* @param blob value to encode
150153
* @return reference to stream
151154
*/
152-
template <class Stream, size_t size>
155+
template <class Stream,
156+
size_t size,
157+
typename = std::enable_if_t<
158+
!std::remove_reference_t<Stream>::is_cbor_decoder_stream>>
153159
Stream &operator>>(Stream &s, Blob<size> &blob) {
154160
for (auto &&it = blob.begin(), end = blob.end(); it != end; ++it) {
155161
s >> *it;
@@ -162,7 +168,7 @@ namespace fc::common {
162168
return os << blob.toHex();
163169
}
164170

165-
} // namespace filecoin::common
171+
} // namespace fc::common
166172

167173
template <size_t N>
168174
struct std::hash<fc::common::Blob<N>> {

core/common/buffer.hpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <gsl/span>
1212
#include <string_view>
1313
#include <vector>
14+
15+
#include "codec/cbor/streams_annotation.hpp"
1416
#include "common/outcome.hpp"
1517

1618
namespace fc::common {
@@ -223,9 +225,7 @@ namespace fc::common {
223225
* @param buffer value to encode
224226
* @return reference to stream
225227
*/
226-
template <class Stream,
227-
typename = std::enable_if_t<Stream::is_cbor_encoder_stream>>
228-
Stream &operator<<(Stream &s, const Buffer &buffer) {
228+
CBOR_ENCODE(Buffer, buffer) {
229229
return s << buffer.toVector();
230230
}
231231

@@ -236,9 +236,7 @@ namespace fc::common {
236236
* @param buffer value to decode
237237
* @return reference to stream
238238
*/
239-
template <class Stream,
240-
typename = std::enable_if_t<Stream::is_cbor_decoder_stream>>
241-
Stream &operator>>(Stream &s, Buffer &buffer) {
239+
CBOR_DECODE(Buffer, buffer) {
242240
std::vector<uint8_t> data;
243241
s >> data;
244242
buffer.put(data);

core/common/outcome_throw.hpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,35 @@
99
#include <boost/system/system_error.hpp>
1010
#include <boost/throw_exception.hpp>
1111

12+
#define PP_CAT(a, b) PP_CAT_I(a, b)
13+
#define PP_CAT_I(a, b) PP_CAT_II(~, a##b)
14+
#define PP_CAT_II(p, res) res
15+
16+
#define UNIQUE_NAME(base) PP_CAT(base, __LINE__)
17+
18+
#define _OUTCOME_EXCEPT(var, val, expr) \
19+
auto &&var = expr; \
20+
if (!var) ::fc::outcome::raise(var.error()); \
21+
auto &&val = var.value();
22+
#define OUTCOME_EXCEPT(val, expr) _OUTCOME_EXCEPT(UNIQUE_NAME(_r), val, expr)
23+
1224
namespace fc::outcome {
25+
/**
26+
* @brief throws outcome::result error as boost exception
27+
* @param t error code
28+
*/
29+
inline void raise(const std::error_code &ec) {
30+
boost::throw_exception(std::system_error(ec));
31+
}
32+
1333
/**
1434
* @brief throws outcome::result error as boost exception
1535
* @tparam T enum error type
1636
* @param t error value
1737
*/
1838
template <typename T, typename = std::enable_if_t<std::is_enum_v<T>>>
1939
void raise(T t) {
20-
std::error_code ec = make_error_code(t);
21-
boost::throw_exception(std::system_error(ec));
40+
raise(make_error_code(t));
2241
}
2342
} // namespace fc::outcome
2443

core/crypto/signature/signature.hpp

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,22 @@
88

99
#include <boost/variant.hpp>
1010

11-
#include "codec/cbor/cbor.hpp"
11+
#include "codec/cbor/streams_annotation.hpp"
1212
#include "common/outcome_throw.hpp"
1313
#include "common/visitor.hpp"
1414
#include "crypto/bls/bls_types.hpp"
1515
#include "crypto/secp256k1/secp256k1_provider.hpp"
1616

17+
namespace fc::crypto::signature {
18+
enum class SignatureError {
19+
INVALID_SIGNATURE_LENGTH = 1,
20+
WRONG_SIGNATURE_TYPE,
21+
INVALID_KEY_LENGTH
22+
};
23+
} // namespace fc::crypto::signature
24+
25+
OUTCOME_HPP_DECLARE_ERROR(fc::crypto::signature, SignatureError);
26+
1727
namespace fc::crypto::signature {
1828
using BlsSignature = bls::Signature;
1929
using Secp256k1Signature = secp256k1::Signature;
@@ -22,15 +32,6 @@ namespace fc::crypto::signature {
2232

2333
constexpr uint64_t kSignatureMaxLength = 200;
2434

25-
/**
26-
* @brief Signature error codes
27-
*/
28-
enum class SignatureError {
29-
INVALID_SIGNATURE_LENGTH = 1,
30-
WRONG_SIGNATURE_TYPE,
31-
INVALID_KEY_LENGTH
32-
};
33-
3435
struct Signature : public boost::variant<BlsSignature, Secp256k1Signature> {
3536
using variant::variant;
3637
using base_type = boost::variant<BlsSignature, Secp256k1Signature>;
@@ -42,10 +43,7 @@ namespace fc::crypto::signature {
4243

4344
Type typeCode(const Signature &s);
4445

45-
template <class Stream,
46-
typename = std::enable_if_t<
47-
std::remove_reference<Stream>::type::is_cbor_encoder_stream>>
48-
Stream &operator<<(Stream &&s, const Signature &signature) {
46+
CBOR_ENCODE(Signature, signature) {
4947
std::vector<uint8_t> bytes{};
5048
visit_in_place(signature,
5149
[&bytes](const BlsSignature &v) {
@@ -59,10 +57,7 @@ namespace fc::crypto::signature {
5957
return s << bytes;
6058
}
6159

62-
template <class Stream,
63-
typename = std::enable_if_t<
64-
std::remove_reference<Stream>::type::is_cbor_decoder_stream>>
65-
Stream &operator>>(Stream &&s, Signature &signature) {
60+
CBOR_DECODE(Signature, signature) {
6661
std::vector<uint8_t> data{};
6762
s >> data;
6863
if (data.empty() || data.size() > kSignatureMaxLength) {
@@ -91,9 +86,4 @@ namespace fc::crypto::signature {
9186

9287
} // namespace fc::crypto::signature
9388

94-
/**
95-
* @brief Outcome errors declaration
96-
*/
97-
OUTCOME_HPP_DECLARE_ERROR(fc::crypto::signature, SignatureError);
98-
9989
#endif // CPP_FILECOIN_CRYPTO_SIGNATURE_HPP

core/primitives/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ add_subdirectory(address)
77
add_subdirectory(block)
88
add_subdirectory(chain_epoch)
99
add_subdirectory(cid)
10+
add_subdirectory(rle_bitset)
1011
add_subdirectory(ticket)
1112
add_subdirectory(tipset)

core/primitives/address/address_codec.hpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <vector>
1111

1212
#include "address.hpp"
13-
#include "codec/cbor/cbor.hpp"
13+
#include "codec/cbor/streams_annotation.hpp"
1414
#include "common/outcome.hpp"
1515
#include "common/outcome_throw.hpp"
1616

@@ -46,17 +46,11 @@ namespace fc::primitives::address {
4646
*/
4747
outcome::result<Address> decodeFromByteString(const std::string &s);
4848

49-
template <class Stream,
50-
typename = std::enable_if_t<
51-
std::remove_reference<Stream>::type::is_cbor_encoder_stream>>
52-
Stream &operator<<(Stream &&s, const Address &address) {
49+
CBOR_ENCODE(Address, address) {
5350
return s << encode(address);
5451
}
5552

56-
template <class Stream,
57-
typename = std::enable_if_t<
58-
std::remove_reference<Stream>::type::is_cbor_decoder_stream>>
59-
Stream &operator>>(Stream &&s, Address &address) {
53+
CBOR_DECODE(Address, address) {
6054
std::vector<uint8_t> data{};
6155
s >> data;
6256
auto res = decode(data);

0 commit comments

Comments
 (0)