Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor domain objects deserialization #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ if (WITH_QRENCODE)
add_definitions(-DWITH_QRENCODE)
endif()

add_definitions(-DJEMALLOC)


set(LOG_LIBRARY "boost" CACHE STRING "Setting for the logging library (boost|spdlog|binlog).")

Expand Down Expand Up @@ -95,6 +97,21 @@ if (${CURRENCY} STREQUAL "LTC")
find_package(OpenSSL 1.0.1 REQUIRED)
endif()

# # jemalloc
# find_package(PkgConfig REQUIRED)
# pkg_check_modules(JEMALLOC jemalloc)

# message("-- Found jemalloc: JEMALLOC_INCLUDE_DIRS: ${JEMALLOC_INCLUDE_DIRS}")
# message("-- Found jemalloc: JEMALLOC_LIBRARY_DIRS: ${JEMALLOC_LIBRARY_DIRS}")
# message("-- Found jemalloc: JEMALLOC_CFLAGS_OTHER: ${JEMALLOC_CFLAGS_OTHER}")

# include_directories(${JEMALLOC_INCLUDE_DIRS})
# link_directories(${JEMALLOC_LIBRARY_DIRS})
# add_definitions(${JEMALLOC_CFLAGS_OTHER})




include(CheckCXXCompilerFlag)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/ci_utils/cmake)
Expand Down Expand Up @@ -268,13 +285,6 @@ set(kth_sources_just_kth
src/wallet/transaction_functions.cpp
)

if (NOT ${CURRENCY} STREQUAL "BCH")
set(kth_sources_just_legacy
${kth_sources_just_legacy}
src/chain/witness.cpp
)
endif()

if (${CURRENCY} STREQUAL "LTC")
set(kth_sources_just_kth
${kth_sources_just_kth}
Expand Down Expand Up @@ -352,7 +362,6 @@ set(kth_headers
include/kth/domain/chain/script_basis.hpp
include/kth/domain/chain/transaction_basis.hpp
include/kth/domain/chain/stealth.hpp
include/kth/domain/chain/witness.hpp
include/kth/domain/chain/point_iterator.hpp
include/kth/domain/chain/header.hpp
include/kth/domain/chain/history.hpp
Expand Down Expand Up @@ -447,6 +456,7 @@ if (${CURRENCY} STREQUAL "LTC")
endif()

target_link_libraries(${PROJECT_NAME} PUBLIC infrastructure::infrastructure)
target_link_libraries(${PROJECT_NAME} PUBLIC ${JEMALLOC_LIBRARIES})

if(NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
target_link_libraries(${PROJECT_NAME} PUBLIC Boost::thread)
Expand Down
3 changes: 2 additions & 1 deletion conan.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"zlib/1.3.1#f52e03ae3d251dec704634230cd806a2%1725632949.456",
"spdlog/1.14.1#972bbf70be1da4bc57ea589af0efde03%1716966910.455",
"secp256k1/0.19.0#a41750412727606c6bed7fdf3695c43f%1725622574.44",
"libiconv/1.17#73fefc1b696e069df90fd1d18aa63edd%1725635059.278",
"libbacktrace/cci.20210118#a7691bfccd8caaf66309df196790a5a1%1725632951.012",
"infrastructure/0.35.0#aacb707e8eb3ddfc4fe57ca3d0a80e66%1725634936.328094",
"infrastructure/0.35.0#aacb707e8eb3ddfc4fe57ca3d0a80e66%1725628750.49",
"gmp/6.3.0#df20ffb6d21c34d67704305bcd1dea9e%1716966936.742",
"fmt/10.2.1#8d582aeebdf05b6f0abe2911b926c3f4%1720016029.904",
"expected-lite/0.8.0#f87b3ec27a4f46894950b70f8d08af24%1717770563.402",
Expand Down
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 KnuthDomainConan(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("infrastructure/0.35.0", transitive_headers=True, transitive_libs=True)
Expand Down
4 changes: 0 additions & 4 deletions include/kth/domain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
#include <kth/domain/chain/stealth.hpp>
#include <kth/domain/chain/transaction.hpp>

#if defined(KTH_SEGWIT_ENABLED)
#include <kth/domain/chain/witness.hpp>
#endif

#include <kth/domain/config/network.hpp>
#include <kth/domain/config/parser.hpp>

Expand Down
42 changes: 15 additions & 27 deletions include/kth/domain/chain/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ class KD_API block : public block_basis {
block(chain::header const& header, transaction::list&& transactions);
block(chain::header const& header, transaction::list const& transactions);

explicit
block(block_basis const& basis)
: block_basis(basis)
{}

explicit
block(block_basis&& basis)
: block_basis(std::move(basis))
{}

//Note(kth): cannot be defaulted because of the mutex data member.
block(block const& x);
block(block&& x) noexcept;
Expand All @@ -79,25 +89,19 @@ class KD_API block : public block_basis {

// Deserialization.
//-------------------------------------------------------------------------

template <typename R, KTH_IS_READER(R)>
bool from_data(R& source, bool witness = false) {
validation.start_deserialize = asio::steady_clock::now();
block_basis::from_data(source, witness);
validation.end_deserialize = asio::steady_clock::now();
return source;
}
static
expect<block> from_data(byte_reader& reader, bool wire = true);

// Serialization.
//-------------------------------------------------------------------------

using block_basis::to_data;
data_chunk to_data(bool witness = false) const;
data_chunk to_data() const;

// Properties (size, accessors, cache).
//-------------------------------------------------------------------------

size_t serialized_size(bool witness = false) const;
size_t serialized_size() const;

// void set_header(chain::header const& value);
void set_transactions(transaction::list const& value);
Expand Down Expand Up @@ -132,11 +136,6 @@ class KD_API block : public block_basis {
static
indexes locator_heights(size_t top);

#if defined(KTH_SEGWIT_ENABLED)
/// Clear witness from all inputs (does not change default hash).
void strip_witness();
#endif

// Validation.
//-------------------------------------------------------------------------

Expand All @@ -146,11 +145,6 @@ class KD_API block : public block_basis {

size_t total_inputs(bool with_coinbase = true) const;

#if defined(KTH_SEGWIT_ENABLED)
size_t weight() const;
bool is_segregated() const;
#endif

code check() const;
code accept(bool transactions = true) const;
code accept(chain_state const& state, bool transactions = true) const;
Expand All @@ -160,14 +154,8 @@ class KD_API block : public block_basis {
mutable validation_t validation{};

private:
// These share a mutex as they are not expected to contend.
#if defined(KTH_SEGWIT_ENABLED)
mutable std::optional<bool> segregated_;
#endif

mutable std::optional<size_t> total_inputs_;
mutable std::optional<size_t> base_size_;
mutable std::optional<size_t> total_size_;
mutable std::optional<size_t> base_size_; // total size

#if ! defined(__EMSCRIPTEN__)
mutable upgrade_mutex mutex_;
Expand Down
117 changes: 49 additions & 68 deletions include/kth/domain/chain/block_basis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,6 @@ namespace kth::domain::chain {

using indexes = std::vector<size_t>;

#if defined(KTH_SEGWIT_ENABLED)
constexpr inline
size_t weight(size_t serialized_size_true, size_t serialized_size_false) {
// Block weight is 3 * Base size * + 1 * Total size (bip141).
return base_size_contribution * serialized_size_false + total_size_contribution * serialized_size_true;
}

class KD_API block_basis;
void strip_witness(block_basis& blk);
bool is_segregated(block_basis const& blk);
#endif

class KD_API block_basis {
public:
using list = std::vector<block_basis>;
Expand All @@ -67,45 +55,47 @@ class KD_API block_basis {
// Deserialization.
//-------------------------------------------------------------------------

template <typename R, KTH_IS_READER(R)>
bool from_data(R& source, bool witness = false) {
// validation.start_deserialize = asio::steady_clock::now();
reset();

if ( ! header_.from_data(source, true)) {
return false;
}

auto const count = source.read_size_little_endian();

// Guard against potential for arbitary memory allocation.
if (count > static_absolute_max_block_size()) {
source.invalidate();
} else {
transactions_.resize(count);
}

// Order is required, explicit loop allows early termination.
for (auto& tx : transactions_) {
if ( ! entity_from_data(tx, source, true, witness_val(witness))) {
break;
}
}

#if defined(KTH_SEGWIT_ENABLED)
// TODO(legacy): optimize by having reader skip witness data.
if ( ! witness_val(witness)) {
strip_witness(*this);
}
#endif

if ( ! source) {
reset();
}

// validation.end_deserialize = asio::steady_clock::now();
return source;
}
// template <typename R, KTH_IS_READER(R)>
// bool from_data(R& source) {
// // validation.start_deserialize = asio::steady_clock::now();
// reset();

// if ( ! header_.from_data(source, true)) {
// return false;
// }

// auto const count = source.read_size_little_endian();

// // Guard against potential for arbitary memory allocation.
// if (count > static_absolute_max_block_size()) {
// source.invalidate();
// } else {
// transactions_.resize(count);
// }

// // Order is required, explicit loop allows early termination.
// for (auto& tx : transactions_) {
// if ( ! entity_from_data(tx, source, true, witness_val(witness))) {
// break;
// }
// }

// #if defined(KTH_SEGWIT_ENABLED)
// // TODO(legacy): optimize by having reader skip witness data.
// if ( ! witness_val(witness)) {
// strip_witness(*this);
// }
// #endif

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

// // validation.end_deserialize = asio::steady_clock::now();
// return source;
// }
static
expect<block_basis> from_data(byte_reader& reader, bool /*wire*/);

[[nodiscard]]
bool is_valid() const;
Expand All @@ -114,23 +104,23 @@ class KD_API block_basis {
//-------------------------------------------------------------------------

// [[nodiscard]]
data_chunk to_data(size_t serialized_size, bool witness = false) const;
data_chunk to_data(size_t serialized_size) const;

void to_data(data_sink& stream, bool witness = false) const;
void to_data(data_sink& stream) const;

template <typename W>
void to_data(W& sink, bool witness = false) const {
void to_data(W& sink) const {
header_.to_data(sink, true);
sink.write_size_little_endian(transactions_.size());
auto const to = [&sink, witness](transaction const& tx) {
tx.to_data(sink, true, witness_val(witness));
auto const to = [&sink](transaction const& tx) {
tx.to_data(sink, true);
};

std::for_each(transactions_.begin(), transactions_.end(), to);
}

[[nodiscard]]
hash_list to_hashes(bool witness = false) const;
hash_list to_hashes() const;

// Properties (size, accessors, cache).
//-------------------------------------------------------------------------
Expand Down Expand Up @@ -177,7 +167,7 @@ class KD_API block_basis {
uint256_t proof() const;

[[nodiscard]]
hash_digest generate_merkle_root(bool witness = false) const;
hash_digest generate_merkle_root() const;

[[nodiscard]]
size_t signature_operations(bool bip16, bool bip141) const;
Expand All @@ -197,11 +187,6 @@ class KD_API block_basis {
[[nodiscard]]
bool is_valid_coinbase_script(size_t height) const;

#if defined(KTH_SEGWIT_ENABLED)
[[nodiscard]]
bool is_valid_witness_commitment() const;
#endif

[[nodiscard]]
bool is_forward_reference() const;

Expand All @@ -221,11 +206,7 @@ class KD_API block_basis {
code check_transactions() const;

[[nodiscard]]
#if defined(KTH_SEGWIT_ENABLED)
code accept(chain_state const& state, size_t serialized_size, size_t weight, bool transactions = true) const;
#else
code accept(chain_state const& state, size_t serialized_size, bool transactions = true) const;
#endif

[[nodiscard]]
code accept_transactions(chain_state const& state) const;
Expand Down Expand Up @@ -256,7 +237,7 @@ indexes locator_heights(size_t top);

size_t total_inputs(block_basis const& blk, bool with_coinbase = true);

size_t serialized_size(block_basis const& blk, bool witness = false);
size_t serialized_size(block_basis const& blk);

} // namespace kth::domain::chain

Expand Down
Loading
Loading