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: vm improvements #44

Merged
merged 1 commit into from
Feb 2, 2025
Merged
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
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ if(ENABLE_TSAN)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
endif()


if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -matomics -mbulk-memory")
_add_cxx_compile_flag(-matomics _has_matomics_flag)
_add_cxx_compile_flag(-mbulk-memory _has_mbulk_memory_flag)
endif()

_add_cxx_compile_flag(-Wno-missing-braces _has_no_missing_braces_warning_flag)
_add_cxx_compile_flag(-Wno-mismatched-tags _has_no_mismatched_tags_warning_flag)
_add_c_compile_flag(-Wno-deprecated-declarations _has_no_deprecated_declarations_warning_flag)
Expand Down Expand Up @@ -174,6 +181,7 @@ set(kth_sources_just_legacy
src/chain/script.cpp
src/chain/transaction_basis.cpp
src/chain/transaction.cpp
src/chain/utxo.cpp

src/machine/interpreter.cpp

Expand Down Expand Up @@ -364,6 +372,7 @@ set(kth_headers
include/kth/domain/chain/point.hpp
include/kth/domain/chain/output_basis.hpp
include/kth/domain/chain/point_value.hpp
include/kth/domain/chain/utxo.hpp
include/kth/domain/define.hpp
include/kth/domain/multi_crypto_support.hpp
include/kth/domain/constants.hpp
Expand Down
10 changes: 4 additions & 6 deletions conan.lock
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
{
"version": "0.5",
"requires": [
"zlib/1.3.1#b8bc2603263cf7eccbd6e17e66b0ed76%1736442063.163",
"spdlog/1.15.0#da21f74dd84627fa68601c4e3b9c3f00%1731353167.297",
"secp256k1/0.21.0#ed9709c61dfdb38ac8181f876503dca6%1732364020.326",
"libbacktrace/cci.20210118#a7691bfccd8caaf66309df196790a5a1%1725632951.012",
"infrastructure/0.39.0#55f5992bcbce2037eb333af5ba88dd6e%1736516560.537014",
"infrastructure/0.39.0#827fe58dfc8a00ac65933d1dc05f812d%1738181472.350903",
"gmp/6.3.0#df20ffb6d21c34d67704305bcd1dea9e%1716966936.742",
"fmt/11.0.2#b4a24d70b93466b9b508ddb7b014643b%1735899162.196",
"expected-lite/0.8.0#f87b3ec27a4f46894950b70f8d08af24%1717770563.402",
"ctre/3.9.0#318a83b26476a59466795daac928f3ec%1716966898.508",
"bzip2/1.0.8#d00dac990f08d991998d624be81a9526%1725632951.439",
"ctre/3.9.0#318a83b26476a59466795daac928f3ec%1715925273.754",
"boost/1.86.0#ce76e7477e466d7d8cbcf738c5d64175%1736442226.917"
],
"build_requires": [
"nodejs/18.15.0#2e468fc390ccdb9595ea1a5bbb74d59f%1699800364.91",
"m4/1.4.19#b38ced39a01e31fef5435bc634461fd2%1700758725.451",
"b2/5.2.1#91bc73931a0acb655947a81569ed8b80%1718416612.241"
"emsdk/3.1.66#dbe775f114c0ec093539734107e2ab5e%1733138978.7973351"
],
"python_requires": [],
"config_requires": []
Expand Down
23 changes: 23 additions & 0 deletions include/kth/domain/chain/coin_selection.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2016-2025 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_DOMAIN_CHAIN_COIN_SELECTION_HPP
#define KTH_DOMAIN_CHAIN_COIN_SELECTION_HPP

namespace kth::domain::chain {

enum class coin_selection_algorithm {
smallest_first, // Prioriza UTXOs más pequeños
largest_first, // Prioriza UTXOs más grandes
// knapsack, // Algoritmo de la mochila para optimizar
// fifo, // Primero en entrar, primero en salir
// branch_and_bound, // Branch and Bound para optimización global
manual, // Mantiene el orden original de los UTXOs
// privacy, // Prioriza privacidad (mismo tamaño de UTXOs)
send_all // Usa todos los UTXOs disponibles
};

} // namespace kth::domain::chain

#endif
29 changes: 28 additions & 1 deletion include/kth/domain/chain/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <kth/domain/chain/output.hpp>
#include <kth/domain/chain/point.hpp>
#include <kth/domain/chain/transaction_basis.hpp>
#include <kth/domain/chain/utxo.hpp>

#include <kth/domain/constants.hpp>
#include <kth/domain/define.hpp>
#include <kth/domain/multi_crypto_settings.hpp>
Expand All @@ -31,14 +33,20 @@
#include <kth/infrastructure/utility/container_sink.hpp>
#include <kth/infrastructure/utility/container_source.hpp>
#include <kth/infrastructure/utility/reader.hpp>
#include <kth/infrastructure/utility/thread.hpp>
#include <kth/infrastructure/utility/writer.hpp>

#include <kth/domain/utils.hpp>
#include <kth/domain/concepts.hpp>

#include <nonstd/expected.hpp>

#include <kth/domain/wallet/payment_address.hpp>
#include <kth/domain/chain/coin_selection.hpp>

namespace kth::domain::chain {

using template_result = std::tuple<transaction, std::vector<uint32_t>, std::vector<wallet::payment_address>, std::vector<uint64_t>>;

class KD_API transaction : public transaction_basis {
public:
using ins = input::list;
Expand Down Expand Up @@ -237,6 +245,25 @@ class KD_API transaction : public transaction_basis {

bool is_standard() const;

static
nonstd::expected<template_result, std::error_code> create_template(
std::vector<utxo> available_utxos,
uint64_t amount_to_send_hint,
wallet::payment_address const& destination_address,
std::vector<wallet::payment_address> const& change_addresses,
coin_selection_algorithm selection_algo
);

static
nonstd::expected<template_result, std::error_code> create_template(
std::vector<utxo> available_utxos,
uint64_t amount_to_send_hint,
wallet::payment_address const& destination_address,
std::vector<wallet::payment_address> const& change_addresses,
std::vector<double> const& change_ratios,
coin_selection_algorithm selection_algo
);

// protected:
void reset();

Expand Down
38 changes: 38 additions & 0 deletions include/kth/domain/chain/utxo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2016-2025 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_DOMAIN_CHAIN_UTXO_HPP
#define KTH_DOMAIN_CHAIN_UTXO_HPP

#include <cstdint>

#include <kth/domain/chain/output_point.hpp>
#include <kth/domain/define.hpp>

namespace kth::domain::chain {

class KD_API utxo {
public:
utxo() = default;

utxo(output_point const& point, uint64_t amount);
utxo(output_point&& point, uint64_t amount);

// Getters
output_point& point();
output_point const& point() const;
uint64_t amount() const;

// Setters
void set_point(output_point const& point);
void set_amount(uint64_t amount);

private:
output_point point_;
uint64_t amount_;
};

} // namespace kth::domain::chain

#endif
17 changes: 10 additions & 7 deletions include/kth/domain/impl/machine/program.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@ bool program::pop(int32_t& out_value) {
number value;
if ( ! pop(value, max_integer_size_legacy())) {
return false;
}

}
out_value = value.int32();
return true;
}
Expand All @@ -216,7 +215,6 @@ bool program::pop(int64_t& out_value) {
if ( ! pop(value, max_integer_size_legacy())) {
return false;
}

out_value = value.int64();
return true;
}
Expand Down Expand Up @@ -267,11 +265,11 @@ inline
bool program::pop(data_stack& section, size_t count) {
if (size() < count) {
return false;
}
}

for (size_t i = 0; i < count; ++i) {
section.push_back(pop());
}
}

return true;
}
Expand Down Expand Up @@ -305,8 +303,7 @@ void program::erase(const stack_iterator& position) {

// pop1/pop2/.../pop[i]/pop[first]/.../pop[last]/push[i]/.../push2/push1
inline
void program::erase(const stack_iterator& first,
const stack_iterator& last) {
void program::erase(const stack_iterator& first, const stack_iterator& last) {
primary_.erase(first, last);
}

Expand Down Expand Up @@ -505,6 +502,12 @@ bool program::succeeded() const {
////return std::all_of(condition_.begin(), condition_.end(), true);
}

//TODO: temp:
inline
chain::script const& program::get_script() const {
return script_;
}

} // namespace kth::domain::machine

#endif
15 changes: 15 additions & 0 deletions include/kth/domain/machine/interpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,21 @@ class KD_API interpreter {
static
code run(operation const& op, program& program);


// Debug step by step
// ----------------------------------------------------------------------------
static
std::pair<code, size_t> debug_start(program const& program);

static
bool debug_steps_available(program const& program, size_t step);

static
std::tuple<code, size_t, program> debug_step(program program, size_t step);

static
code debug_end(program const& program);

private:
static
result run_op(operation const& op, program& program);
Expand Down
6 changes: 6 additions & 0 deletions include/kth/domain/machine/program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ class KD_API program {
[[nodiscard]]
bool succeeded() const;


// TODO: temp:
[[nodiscard]]
chain::script const& get_script() const;


private:
// A space-efficient dynamic bitset (specialized).
using bool_stack = std::vector<bool>;
Expand Down
4 changes: 4 additions & 0 deletions include/kth/domain/wallet/payment_address.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class KD_API payment_address {
explicit
payment_address(chain::script const& script, uint8_t version = mainnet_p2sh);

/// Factories
static
payment_address from_pay_key_hash_script(chain::script const& script, uint8_t version);

/// Operators.
bool operator==(payment_address const& x) const;
bool operator!=(payment_address const& x) const;
Expand Down
27 changes: 20 additions & 7 deletions src/chain/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,14 +814,22 @@ bool script::is_pay_public_key_pattern(operation::list const& ops) {
}

bool script::is_pay_key_hash_pattern(operation::list const& ops) {
return ops.size() == 5 && ops[0].code() == opcode::dup && ops[1].code() == opcode::hash160 && ops[2].data().size() == short_hash_size && ops[3].code() == opcode::equalverify && ops[4].code() == opcode::checksig;
return ops.size() == 5 &&
ops[0].code() == opcode::dup &&
ops[1].code() == opcode::hash160 &&
ops[2].data().size() == short_hash_size &&
ops[3].code() == opcode::equalverify &&
ops[4].code() == opcode::checksig;
}

//*****************************************************************************
// CONSENSUS: this pattern is used to activate bip16 validation rules.
//*****************************************************************************
bool script::is_pay_script_hash_pattern(operation::list const& ops) {
return ops.size() == 3 && ops[0].code() == opcode::hash160 && ops[1].code() == opcode::push_size_20 && ops[2].code() == opcode::equal;
return ops.size() == 3 &&
ops[0].code() == opcode::hash160 &&
ops[1].code() == opcode::push_size_20 &&
ops[2].code() == opcode::equal;
}

//*****************************************************************************
Expand Down Expand Up @@ -869,8 +877,10 @@ operation::list script::to_pay_public_key_pattern(data_slice point) {
return {};
}

return operation::list{{to_chunk(point)},
{opcode::checksig}};
return operation::list{
{to_chunk(point)},
{opcode::checksig}
};
}

operation::list script::to_pay_key_hash_pattern(short_hash const& hash) {
Expand All @@ -879,14 +889,16 @@ operation::list script::to_pay_key_hash_pattern(short_hash const& hash) {
{opcode::hash160},
{to_chunk(hash)},
{opcode::equalverify},
{opcode::checksig}};
{opcode::checksig}
};
}

operation::list script::to_pay_script_hash_pattern(short_hash const& hash) {
return operation::list{
{opcode::hash160},
{to_chunk(hash)},
{opcode::equal}};
{opcode::equal}
};
}

operation::list script::to_pay_multisig_pattern(uint8_t signatures,
Expand Down Expand Up @@ -1153,7 +1165,8 @@ code script::verify(transaction const& tx, uint32_t input, uint32_t forks) {
auto const& prevout = in.previous_output().validation.cache;

#if ! defined(KTH_SEGWIT_ENABLED)
return verify(tx, input, forks, in.script(), prevout.script(), prevout.value());
auto res = verify(tx, input, forks, in.script(), prevout.script(), prevout.value());
return res;
#else
return verify(tx, input, forks, in.script(), in.witness(), prevout.script(), prevout.value());
#endif
Expand Down
5 changes: 4 additions & 1 deletion src/chain/script_basis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,10 @@ bool script_basis::is_pay_key_hash_pattern(operation::list const& ops) {
// CONSENSUS: this pattern is used to activate bip16 validation rules.
//*****************************************************************************
bool script_basis::is_pay_script_hash_pattern(operation::list const& ops) {
return ops.size() == 3 && ops[0].code() == opcode::hash160 && ops[1].code() == opcode::push_size_20 && ops[2].code() == opcode::equal;
return ops.size() == 3 &&
ops[0].code() == opcode::hash160 &&
ops[1].code() == opcode::push_size_20 &&
ops[2].code() == opcode::equal;
}

//*****************************************************************************
Expand Down
Loading
Loading