Skip to content

Commit

Permalink
feat: refactor domain objects deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
fpelliccioni committed Oct 27, 2024
1 parent 41ae580 commit 89abae5
Show file tree
Hide file tree
Showing 9 changed files with 400 additions and 52 deletions.
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class KnuthInfrastructureConan(KnuthConanFileV2):

def build_requirements(self):
if self.options.tests:
self.test_requires("catch2/3.6.0")
self.test_requires("catch2/3.7.1")

def requirements(self):
self.requires("secp256k1/0.19.0", transitive_headers=True, transitive_libs=True)
Expand Down
14 changes: 14 additions & 0 deletions include/kth/infrastructure/concepts.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2016-2024 Knuth Project developers.
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef KTH_INFRASTRUCTURE_CONCEPTS_HPP
#define KTH_INFRASTRUCTURE_CONCEPTS_HPP

#include <concepts>
#include <type_traits>

template <typename T>
concept trivially_copyable = std::is_trivially_copyable_v<T>;

#endif // KTH_INFRASTRUCTURE_CONCEPTS_HPP
38 changes: 37 additions & 1 deletion include/kth/infrastructure/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,43 @@ enum error_code_t {
low_benefit_transaction,
duplicate_transaction,
double_spend_mempool,
double_spend_blockchain
double_spend_blockchain,


//TODO: check C-API error codes, add the new ones there
//TODO: see the error to string functions

// Cash Tokens
invalid_bitfield,

// Domain object serialization/deserialization
read_past_end_of_buffer,
skip_past_end_of_buffer,
invalid_size,
invalid_script_type,
script_not_push_only,
script_invalid_size,
invalid_address_count,
bad_inventory_count,
version_too_low,
version_too_new,
invalid_compact_block,
unsupported_version,
invalid_filter_add,
invalid_filter_load,
bad_merkle_block_count,
illegal_value,


// Database cache
height_not_found,
hash_not_found,
empty_cache,
utxo_not_found,


// Last error code.
last_error_code
};

enum error_condition_t {};
Expand Down
3 changes: 2 additions & 1 deletion include/kth/infrastructure/impl/utility/array_slice.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace kth {
template <typename Iterable>
template <typename Container>
array_slice<Iterable>::array_slice(const Container& container)
: begin_(container.data()), end_(container.data() + container.size())
: begin_(container.data())
, end_(container.data() + container.size())
{}

template <typename Iterable>
Expand Down
2 changes: 1 addition & 1 deletion include/kth/infrastructure/impl/utility/data.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ template <typename Source>
bool starts_with(const typename Source::const_iterator& begin,
const typename Source::const_iterator& end, const Source& value) {
auto const length = std::distance(begin, end);
return length >= 0 && static_cast<size_t>(length) >= value.size() &&
return length >= 0 && size_t(length) >= value.size() &&
std::equal(value.begin(), value.end(), begin);
}

Expand Down
66 changes: 36 additions & 30 deletions include/kth/infrastructure/message/network_address.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <vector>

#include <kth/infrastructure/define.hpp>
#include <kth/infrastructure/utility/byte_reader.hpp>
#include <kth/infrastructure/utility/data.hpp>
#include <kth/infrastructure/utility/reader.hpp>
#include <kth/infrastructure/utility/writer.hpp>
Expand All @@ -32,7 +33,8 @@ class KI_API network_address {
: timestamp_(timestamp)
, services_(services)
, ip_(ip)
, port_(port) {}
, port_(port)
{}

network_address(network_address const& x) = default;
network_address& operator=(network_address const& x) = default;
Expand All @@ -56,29 +58,33 @@ class KI_API network_address {

size_t serialized_size(uint32_t version, bool with_timestamp) const;

bool from_data(data_chunk const& data, uint32_t version, bool with_timestamp);
bool from_data(data_source& stream, uint32_t version, bool with_timestamp);
// bool from_data(data_chunk const& data, uint32_t version, bool with_timestamp);
// bool from_data(data_source& stream, uint32_t version, bool with_timestamp);

template <typename R>
bool from_data(R& source, uint32_t version, bool with_timestamp) {
reset();
// template <typename R>
// bool from_data(R& source, uint32_t version, bool with_timestamp) {
// reset();

if (with_timestamp) {
timestamp_ = source.read_4_bytes_little_endian();
}
// if (with_timestamp) {
// timestamp_ = source.read_4_bytes_little_endian();
// }

services_ = source.read_8_bytes_little_endian();
auto ip = source.read_bytes(ip_.size());
port_ = source.read_2_bytes_big_endian();
// services_ = source.read_8_bytes_little_endian();
// auto ip = source.read_bytes(ip_.size());
// port_ = source.read_2_bytes_big_endian();

if ( ! source) {
reset();
}
// if ( ! source) {
// reset();
// }

// // TODO(legacy): add array to reader interface (can't use template).
// std::move(ip.begin(), ip.end(), ip_.data());
// return source;
// }

// TODO(legacy): add array to reader interface (can't use template).
std::move(ip.begin(), ip.end(), ip_.data());
return source;
}

static
expect<network_address> from_data(byte_reader& reader, uint32_t version, bool with_timestamp);

data_chunk to_data(uint32_t version, bool with_timestamp) const;
// void to_data(uint32_t version, std::ostream& stream, bool with_timestamp) const;
Expand All @@ -99,19 +105,19 @@ class KI_API network_address {
bool is_valid() const;
void reset();

static
network_address factory_from_data(data_chunk const& data, uint32_t version, bool with_timestamp);
// static
// network_address factory_from_data(data_chunk const& data, uint32_t version, bool with_timestamp);

static
network_address factory_from_data(data_source& stream, uint32_t version, bool with_timestamp);
// static
// network_address factory_from_data(data_source& stream, uint32_t version, bool with_timestamp);

template <typename R>
static
network_address factory_from_data(R& source, uint32_t version, bool with_timestamp) {
network_address instance;
instance.from_data(source, version, with_timestamp);
return instance;
}
// template <typename R>
// static
// network_address factory_from_data(R& source, uint32_t version, bool with_timestamp) {
// network_address instance;
// instance.from_data(source, version, with_timestamp);
// return instance;
// }

static
size_t satoshi_fixed_size(uint32_t version, bool with_timestamp);
Expand Down
Loading

0 comments on commit 89abae5

Please sign in to comment.