Skip to content

Commit

Permalink
Merge pull request #1492 from evoskuil/master
Browse files Browse the repository at this point in the history
Add polymorphic allocating byte_reader methods.
  • Loading branch information
evoskuil authored Jul 7, 2024
2 parents 3046b83 + b5fcb79 commit 706d771
Show file tree
Hide file tree
Showing 50 changed files with 407 additions and 163 deletions.
4 changes: 2 additions & 2 deletions include/bitcoin/system/chain/checkpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace chain {
class BC_API checkpoint
{
public:
typedef std_vector<checkpoint> list;
typedef std::vector<checkpoint> list;

DEFAULT_COPY_MOVE_DESTRUCT(checkpoint);

Expand Down Expand Up @@ -111,7 +111,7 @@ bool operator!=(const checkpoint& left, const checkpoint& right) NOEXCEPT;
std::istream& operator>>(std::istream& stream, checkpoint& out) THROWS;
std::ostream& operator<<(std::ostream& stream, const checkpoint& in) NOEXCEPT;

typedef std_vector<checkpoint> checkpoints;
typedef std::vector<checkpoint> checkpoints;

DECLARE_JSON_VALUE_CONVERTORS(checkpoint);

Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/system/chain/stripper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ inline bool operator==(const operation& op, const stripper& strip) NOEXCEPT
return op.code() == strip.code() && op.data() == strip.data();
}

typedef std_vector<stripper> strippers;
typedef std::vector<stripper> strippers;

} // namespace chain
} // namespace system
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/system/config/hash256.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace config {
class BC_API hash256 final
{
public:
typedef std_vector<hash256> list;
typedef std::vector<hash256> list;

hash256() NOEXCEPT;
hash256(hash_digest&& value) NOEXCEPT;
Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/system/config/parameter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ namespace config {
typedef std::pair<const std::string, int> argument_pair;

/// A type to represent the list of positional argument name counts.
typedef std_vector<argument_pair> argument_list;
typedef std::vector<argument_pair> argument_list;

/// A type to represent a list of parameter structures.
class parameter;
typedef std_vector<parameter> parameter_list;
typedef std::vector<parameter> parameter_list;

/// Not thread safe, non-virtual.
/// Normalized storage for command line arguments and options.
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/system/config/printer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class BC_API printer final
/// This always sets at least one line and always collapses whitespace.
/// paragraph The paragraph to columnize.
/// return The column, as a list of fragments.
std_vector<std::string> columnize(const std::string& paragraph,
std::vector<std::string> columnize(const std::string& paragraph,
size_t width) NOEXCEPT;

/// Format stuff.
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/system/config/script.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class BC_API script final

/// Split or unsplit tokens.
script(const std::string& mnemonic) THROWS;
script(const std_vector<std::string>& tokens) THROWS;
script(const std::vector<std::string>& tokens) THROWS;

/// Default text encoding is mnemonic, so provide data for base16.
script(const data_chunk& value) NOEXCEPT;
Expand Down
5 changes: 3 additions & 2 deletions include/bitcoin/system/constraints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ using if_little_endian_integral_integer = bool_if<
is_integral_integer<Integer> &&
is_little_endian>;

/// std_array/std_vector
/// std_array/std_vector/std::vector

template <typename Type>
using if_std_array = bool_if<
Expand All @@ -254,7 +254,8 @@ using if_integral_array = bool_if<
template <typename Type>
using if_byte_insertable = bool_if<
std::is_base_of<std::string, Type>::value ||
std::is_base_of<std_vector<uint8_t>, Type>::value>;
std::is_base_of<std_vector<uint8_t>, Type>::value ||
std::is_base_of<std::vector<uint8_t>, Type>::value> ;

} // namespace libbitcoin

Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/system/crypto/ring_signature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ namespace system {
/// valid signature, we must use a private key from each of those sets.
/// For example A and E and X. We can summarize this operation as:
/// (A or B or C) and (D or E or F) and (X or Y)
typedef std_vector<compressed_list> key_rings;
typedef std::vector<compressed_list> key_rings;

/// A borromean ring signature.
/// theta = {e_0, s_{i_j} : 0 <= i <= n, 0 <= j <= m_i}
struct BC_API ring_signature
{
typedef std_vector<secret_list> proof_list;
typedef std::vector<secret_list> proof_list;

ec_secret challenge;
proof_list proofs;
Expand Down
2 changes: 2 additions & 0 deletions include/bitcoin/system/data/data_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace system {
/// Byte array of a specified length.
template <size_t Size>
using data_array = std_array<uint8_t, Size>;
template <size_t Size>
using data_array_cptr = std::shared_ptr<const data_array<Size>>;

/// Return type for splitters.
template <size_t Size>
Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/system/data/data_slab.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ class data_slab
template <size_type Size>
constexpr std_array<value_type, Size> to_array() const NOEXCEPT;

/// Copy data to a vector.
VCONSTEXPR std_vector<value_type> to_chunk() const NOEXCEPT;
/// Copy data to a pmr vector (not constexpr).
inline std_vector<value_type> to_chunk() const NOEXCEPT;

/// Convert data to a string (casts uint8_t to char).
SCONSTEXPR std::string to_string() const NOEXCEPT;
Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/system/data/data_slice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ class data_slice
template <size_type Size>
constexpr std_array<value_type, Size> to_array() const NOEXCEPT;

/// Copy data to a vector.
VCONSTEXPR std_vector<value_type> to_chunk() const NOEXCEPT;
/// Copy data to a pmr vector (not constexpr).
inline std_vector<value_type> to_chunk() const NOEXCEPT;

/// Convert data to a string (casts uint8_t to char).
SCONSTEXPR std::string to_string() const NOEXCEPT;
Expand Down
9 changes: 7 additions & 2 deletions include/bitcoin/system/data/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ inline std::shared_ptr<const Type> to_shared(const Type& value) NOEXCEPT
}

/// Construct shared pointer to const from moved constructor parameters.
template <typename Type, typename... Args>
template <typename Type, typename ...Args>
inline std::shared_ptr<const Type> to_shared(Args&&... values) NOEXCEPT
{
return std::make_shared<const Type>(Type{ std::forward<Args>(values)... });
Expand All @@ -79,6 +79,11 @@ template <typename Type>
std::shared_ptr<std_vector<std::shared_ptr<const Type>>>
to_shareds(const std_vector<Type>& values) NOEXCEPT;

/// Allocate a shared instance and construct with given arguments.
template <typename Type, typename Allocator, typename ...Args>
std::shared_ptr<const Type> to_allocated(const Allocator& allocator,
Args&&... args) NOEXCEPT;

/// unique_ptr
/// ---------------------------------------------------------------------------

Expand All @@ -97,7 +102,7 @@ inline std::unique_ptr<const Type> to_unique(const Type& value) NOEXCEPT
}

/// Construct unique pointer to const from moved constructor parameters.
template <typename Type, typename... Args>
template <typename Type, typename ...Args>
inline std::unique_ptr<const Type> to_unique(Args&&... values) NOEXCEPT
{
return std::make_unique<const Type>(Type{ std::forward<Args>(values)... });
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/system/data/no_fill_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class no_fill_allocator
::new(static_cast<void*>(ptr)) T;
}

template <typename T, typename...Args>
template <typename T, typename ...Args>
void construct(T* ptr, Args&&... args) noexcept(
std::is_nothrow_default_constructible_v<Allocator>)
{
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/system/define.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

/// Standard includes (do not include directly).
/// All except <array> are included here by include ancestory.
#include <new> // Must be early, macOS w/pmr.
#include <array> // TODO: purge
#include <cstddef> // purged
#include <cstdint> // purged
Expand Down
6 changes: 2 additions & 4 deletions include/bitcoin/system/endian/minimal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ constexpr data_array<Size> to_little_endian_size(Integer value) NOEXCEPT;

template <typename Integer,
if_integer<Integer> = true>
VCONSTEXPR data_chunk to_big_endian_size(Integer value,
size_t excess=zero) NOEXCEPT;
data_chunk to_big_endian_size(Integer value, size_t excess=zero) NOEXCEPT;

template <typename Integer,
if_integer<Integer> = true>
VCONSTEXPR data_chunk to_little_endian_size(Integer value,
size_t excess=zero) NOEXCEPT;
data_chunk to_little_endian_size(Integer value, size_t excess=zero) NOEXCEPT;

/// From chunk to uintx.
/// ---------------------------------------------------------------------------
Expand Down
23 changes: 16 additions & 7 deletions include/bitcoin/system/hash/functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,25 @@ typedef data_array<short_hash_size> short_hash;
typedef data_array<quarter_hash_size> quarter_hash;
typedef data_array<half_hash_size> half_hash;
typedef data_array<mini_hash_size> mini_hash;

/// Shared pointers to common bitcoin hash containers.
typedef std::shared_ptr<long_hash> long_hash_ptr;
typedef std::shared_ptr<hash_digest> hash_ptr;
typedef std::shared_ptr<short_hash> short_hash_ptr;
typedef std::shared_ptr<quarter_hash> quarter_hash_ptr;
typedef std::shared_ptr<half_hash> half_hash_ptr;
typedef std::shared_ptr<mini_hash> mini_hash_ptr;

/// Shared pointers to const common bitcoin hash containers.
typedef std::shared_ptr<const long_hash> long_hash_cptr;
typedef std::shared_ptr<const hash_digest> hash_cptr;
typedef std::shared_ptr<const short_hash> short_hash_cptr;
typedef std::shared_ptr<const quarter_hash> quarter_hash_cptr;
typedef std::shared_ptr<const half_hash> half_hash_cptr;
typedef std::shared_ptr<const mini_hash> mini_hash_cptr;

/// Lists of common bitcoin hashes.
typedef std_vector<long_hash> long_hashes;
typedef std_vector<hash_digest> hashes;
typedef std_vector<short_hash> short_hashes;
typedef std_vector<half_hash> half_hashes;
typedef std_vector<quarter_hash> quarter_hashes;
typedef std_vector<mini_hash> mini_hashes;
/// Hashes are not pmr types, preserves constexpr.
typedef std::vector<hash_digest> hashes;

/// Null-valued common hashes.
constexpr long_hash null_long_hash{};
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/system/hash/sha/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class algorithm
template <size_t Size>
using ablocks_t = std_array<block_t, Size>;
using iblocks_t = iterable<block_t>;
using digests_t = std_vector<digest_t>;
using digests_t = std::vector<digest_t>;

/// Constants (and count_t).
/// -----------------------------------------------------------------------
Expand Down
12 changes: 4 additions & 8 deletions include/bitcoin/system/impl/data/data_reference.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,26 @@ constexpr data_reference::data_reference() NOEXCEPT
{
}

constexpr data_reference::data_reference(
const data_slice& data) NOEXCEPT
constexpr data_reference::data_reference(const data_slice& data) NOEXCEPT
: data_slice(data)
{
}

// data_slice(std::string) is SCONSTEXPR.
SCONSTEXPR data_reference::data_reference(
const std::string& text) NOEXCEPT
SCONSTEXPR data_reference::data_reference(const std::string& text) NOEXCEPT
: data_slice(text)
{
}

// data_slice(std_vector) is VCONSTEXPR.
VCONSTEXPR data_reference::data_reference(
const data_chunk& data) NOEXCEPT
VCONSTEXPR data_reference::data_reference(const data_chunk& data) NOEXCEPT
: data_slice(data)
{
}

// data_array constructor.
template <data_reference::size_type Size>
constexpr data_reference::data_reference(
const data_array<Size>& data) NOEXCEPT
constexpr data_reference::data_reference(const data_array<Size>& data) NOEXCEPT
: data_slice(data)
{
}
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/system/impl/data/data_slab.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ SVCONSTEXPR data_slab data_slab::from_size_(const Pointer begin,
// methods
// ----------------------------------------------------------------------------

VCONSTEXPR std_vector<data_slab::value_type> data_slab::to_chunk() const NOEXCEPT
inline std_vector<data_slab::value_type> data_slab::to_chunk() const NOEXCEPT
{
return { begin_, end_ };
}
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/system/impl/data/data_slice.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ constexpr data_slice::to_array() const NOEXCEPT
return out;
}

VCONSTEXPR std_vector<data_slice::value_type> data_slice::
inline std_vector<data_slice::value_type> data_slice::
to_chunk() const NOEXCEPT
{
return { begin_, end_ };
Expand Down
15 changes: 13 additions & 2 deletions include/bitcoin/system/impl/data/memory.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <algorithm>
#include <memory>
#include <memory_resource>
#include <utility>
#include <vector>
#include <bitcoin/system/define.hpp>
Expand All @@ -30,7 +31,7 @@ namespace system {

BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)

/// Create shared pointer to vector of const shared ptr from the moved vector.
// Create shared pointer to vector of const shared ptr from the moved vector.
template <typename Type>
std::shared_ptr<std_vector<std::shared_ptr<const Type>>>
to_shareds(std_vector<Type>&& values) NOEXCEPT
Expand All @@ -47,7 +48,7 @@ to_shareds(std_vector<Type>&& values) NOEXCEPT
return out;
}

/// Create shared pointer to vector of const shared ptr from the copied vector.
// Create shared pointer to vector of const shared ptr from the copied vector.
template <typename Type>
std::shared_ptr<std_vector<std::shared_ptr<const Type>>> to_shareds(
const std_vector<Type>& values) NOEXCEPT
Expand All @@ -64,6 +65,16 @@ std::shared_ptr<std_vector<std::shared_ptr<const Type>>> to_shareds(
return out;
}

// Allocate a shared instance and construct with given arguments.
template <typename Type, typename Allocator, typename ...Args>
std::shared_ptr<const Type> to_allocated(const Allocator& allocator,
Args&&... args) NOEXCEPT
{
return std::allocate_shared<const Type,
std::pmr::polymorphic_allocator<const Type>>(allocator,
std::forward<Args>(args)...);
}

BC_POP_WARNING()

} // namespace system
Expand Down
6 changes: 2 additions & 4 deletions include/bitcoin/system/impl/endian/minimal.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ constexpr data_array<Size> to_little_endian_size(Integer value) NOEXCEPT

template <typename Integer,
if_integer<Integer>>
VCONSTEXPR data_chunk to_big_endian_size(Integer value,
size_t excess) NOEXCEPT
data_chunk to_big_endian_size(Integer value, size_t excess) NOEXCEPT
{
BC_ASSERT(!is_add_overflow(byte_width(value), excess));

Expand All @@ -66,8 +65,7 @@ VCONSTEXPR data_chunk to_big_endian_size(Integer value,

template <typename Integer,
if_integer<Integer>>
VCONSTEXPR data_chunk to_little_endian_size(Integer value,
size_t excess) NOEXCEPT
data_chunk to_little_endian_size(Integer value, size_t excess) NOEXCEPT
{
BC_ASSERT(!is_add_overflow(byte_width(value), excess));

Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/system/impl/machine/interpreter.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ op_pick() NOEXCEPT
// 1 {999 times}, 998 OP_ROLL{ 200 times }"
// bitslog.com/2017/04/17/new-quadratic-delays-in-bitcoin-scripts
// Shifting larger chunks does not change time, as vector stores references.
// This remains the current satoshi implementation (std_vector).
// This remains in the current satoshi implementation (std_vector).
// ****************************************************************************
template <typename Stack>
inline op_error_t interpreter<Stack>::
Expand Down
6 changes: 3 additions & 3 deletions include/bitcoin/system/impl/machine/program.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -713,10 +713,10 @@ set_subscript(const op_iterator& op) NOEXCEPT
return true;
}

using strippers = std_vector<stripper>;
inline strippers create_strip_ops(const chunk_xptrs& endorsements) NOEXCEPT
inline chain::strippers create_strip_ops(
const chunk_xptrs& endorsements) NOEXCEPT
{
strippers strip{};
chain::strippers strip{};

BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
strip.reserve(add1(endorsements.size()));
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/system/impl/radix/base_16.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ SVCONSTEXPR std::string base16_string(const char(&string)[Size]) NOEXCEPT
}

template <size_t Size, if_odd<Size>>
VCONSTEXPR data_chunk base16_chunk(const char(&string)[Size]) NOEXCEPT
data_chunk base16_chunk(const char(&string)[Size]) NOEXCEPT
{
data_chunk out;
decode_base16(out, string);
Expand Down
3 changes: 2 additions & 1 deletion include/bitcoin/system/impl/stream/streamers/bit_reader.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ namespace system {

template <typename IStream>
bit_reader<IStream>::bit_reader(IStream& source) NOEXCEPT
: byte_reader<IStream>(source), byte_(byte_reader<IStream>::pad()),
: byte_reader<IStream>(source),
byte_(byte_reader<IStream>::pad()),
offset_(byte_bits)
{
}
Expand Down
Loading

0 comments on commit 706d771

Please sign in to comment.