6#ifndef ALT_INTEGRATION_VERIBLOCK_SERDE_HPP
7#define ALT_INTEGRATION_VERIBLOCK_SERDE_HPP
15#include "entities/network_byte_pair.hpp"
16#include "read_stream.hpp"
19#include "write_stream.hpp"
57 typename =
typename std::enable_if<std::is_integral<T>::value>::type>
60 inputStream.writeBE(input);
61 return inputStream.data();
99template <
typename Container,
100 typename =
typename std::enable_if<
101 sizeof(
typename Container::value_type) == 1>::type>
108 if (!stream.readBE<uint8_t>(length, state)) {
109 return state.Invalid(
"readsingle-bad-length");
111 if (!
checkRange(length, minLen, maxLen, state)) {
112 return state.Invalid(
"readsingle-bad-range");
115 return stream.
read(length, out.data(), state);
129 typename =
typename std::enable_if<std::is_integral<T>::value>::type>
133 return state.Invalid(
"readsinglebe-bad-data");
136 return dataStream.readBE<T>(out, state, data.size());
168 typename =
typename std::enable_if<std::is_integral<T>::value>::type>
171 dataStream.writeBE<T>(value);
189size_t singleByteLenValueSize(
size_t valueSize);
192size_t singleBEValueSize(int64_t value);
196 typename =
typename std::enable_if<std::is_integral<T>::value>::type>
197size_t singleFixedBEValueSize(T value) {
199 dataStream.writeBE<T>(value);
200 return singleByteLenValueSize(dataStream.data());
204size_t varLenValueSize(Slice<const uint8_t> value);
207size_t varLenValueSize(
size_t valueSize);
255template <
typename Container>
262 typename Container::value_type&,
265 if (!readSingleBEValue<int32_t>(stream, count, state)) {
266 return state.Invalid(
"readarray-bad-count");
269 return state.Invalid(
"readarray-bad-range");
272 for (
size_t i = 0; i < (size_t)count; i++) {
273 typename Container::value_type item;
274 if (!readFunc(stream, item, state)) {
275 return state.Invalid(
"readarray-bad-item", i);
277 if (out.insert(item).second ==
false) {
278 return state.Invalid(
"readarray-duplicate-item", i);
295template <
typename Container>
299 std::function<
bool(Container&,
301 typename Container::value_type&,
303 int32_t max = std::numeric_limits<int32_t>::max();
304 return readContainer(stream, out, state, 0, max, readFunc);
327 if (!readSingleBEValue<int32_t>(stream, count, state)) {
328 return state.Invalid(
"readarray-bad-count");
331 return state.Invalid(
"readarray-bad-range");
336 for (
size_t i = 0; i < (size_t)count; i++) {
338 if (!readFunc(stream, item, state)) {
339 return state.Invalid(
"readarray-bad-item", i);
363 int32_t max = std::numeric_limits<int32_t>::max();
364 return readArrayOf<T>(stream, out, state, 0, max, readFunc);
368template <
typename Container>
372 std::function<
void(WriteStream&,
const typename Container::value_type& t)>
381template <
typename Container>
382size_t estimateContainerSize(
384 std::function<
size_t(
const typename Container::value_type& t)> f) {
386 size += singleBEValueSize((int64_t)t.size());
396 std::function<
size_t(
const T& t)> f) {
397 return estimateContainerSize<std::vector<T>>(t, f);
442 obj.toVbkEncoding(w);
457 return HexStr(SerializeToVbkEncoding<T>(obj));
463 return HexStr(SerializeToRaw<T>(obj));
473 VBK_ASSERT_MSG(result,
"Can't deserialize: %s", state.toString());
484 VBK_ASSERT_MSG(result,
"Can't deserialize: %s", state.toString());
495 VBK_ASSERT_MSG(result,
"Can't deserialize: %s", state.toString());
506 VBK_ASSERT_MSG(result,
"Can`t deserialize: %s", state.toString());
Class that is used for storing validation state.
Binary writer that is useful for binary serialization.
All constants in alt-cpp.
bool checkRange(uint64_t num, uint64_t min, uint64_t max, ValidationState &state)
Checks if expression 'min' <= 'num' <= 'max' is true.
T AssertDeserializeFromRaw(std::vector< uint8_t > raw)
Deserialize from RAW encoding.
bool DeserializeFromHex(const std::string &hex, T &out, ValidationState &state)
Deserialize from HEX VBK encoding.
void writeSingleBEValue(WriteStream &stream, int64_t value)
Write single Big-Endian value to the stream.
bool DeserializeFromRaw(ReadStream &stream, AltBlock &out, ValidationState &state, const AltBlock::hash_t &=AltBlock::hash_t{})
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::vector< uint8_t > SerializeToRaw(const T &obj)
Serialize to RAW encoding.
void writeNetworkByte(WriteStream &stream, NetworkBytePair networkOrType)
Write optional network byte to the stream.
std::vector< uint8_t > fixedArray(T input)
Converts the input to the byte array.
void writeSingleFixedBEValue(WriteStream &stream, T value)
Write single Big-Endian value to the stream.
TxType
Veriblock transaction type.
size_t estimateArraySizeOf(const std::vector< T > &t, std::function< size_t(const T &t)> f)
Estimate size of array in bytes.
void writeSingleByteLenValue(WriteStream &stream, Slice< const uint8_t > value)
Write single byte length value, which consists of N bytes vector Appends 1 byte data length to the st...
T AssertDeserializeFromRawHex(std::string hex)
Deserialize from HEX RAW encoding.
bool DeserializeFromVbkEncoding(ReadStream &stream, AltBlockAddon &out, ValidationState &state)
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::string HexStr(const T itbegin, const T itend)
Convert bytes to hex.
std::vector< uint8_t > SerializeToVbkEncoding(const T &obj)
Serialize to VBK encoding.
NetworkBytePair readNetworkByte(ReadStream &stream, TxType type)
Read optional network byte from the stream.
std::string SerializeToRawHex(const T &obj)
Serialize to HEX RAW encoding.
T AssertDeserializeFromHex(std::string hex)
Deserialize from HEX VBK encoding.
bool readSingleBEValue(ReadStream &stream, T &out, ValidationState &state)
Read single Big-Endian value from a stream.
void writeVarLenValue(WriteStream &stream, Slice< const uint8_t > value)
Write variable length value, which consists of N bytes vector Appends up to 8 bytes data length to th...
bool readVarLenValue(ReadStream &stream, Slice< const uint8_t > &out, ValidationState &state, uint64_t minLen, uint64_t maxLen)
Read variable length value, which consists of [N=(4 bytes = size of slice) | N bytes slice] Size of s...
bool readSingleByteLenValue(ReadStream &stream, Slice< const uint8_t > &out, ValidationState &state, uint64_t minLen, uint64_t maxLen)
Read variable length value, which consists of [N=(1 byte = size of slice) | N bytes slice] Size of sl...
bool readSetOf(ReadStream &stream, Container &out, ValidationState &state, size_t min, size_t max, std::function< bool(ReadStream &, typename Container::value_type &, ValidationState &)> readFunc)
Reads set of entities of.
std::vector< uint8_t > ParseHex(const char *psz)
Parse bytes from hex.
bool readArrayOf(ReadStream &stream, std::vector< T > &out, ValidationState &state, size_t min, size_t max, std::function< bool(ReadStream &, T &, ValidationState &)> readFunc)
Reads array of entities of type T.
bool DeserializeFromRawHex(const std::string &hex, T &out, ValidationState &state)
Deserialize from HEX RAW encoding.
std::string SerializeToHex(const T &obj)
Serialize to HEX VBK encoding.
std::vector< uint8_t > trimmedArray(int64_t input)
Converts the input to the byte array and trims it's size to the lowest possible value.
T AssertDeserializeFromVbkEncoding(Slice< const uint8_t > raw)
Deserialize from VBK encoding.
Stores pair of TxType and VBK network byte.
Binary reading stream, that is useful during binary deserialization.
bool read(size_t size, uint8_t *out, ValidationState &state)
Read type T of 'size' bytes.
Non-owning contiguous array.