Skip to content
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: 7 additions & 2 deletions src/cpp/common/py_monero_common_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,22 @@ void py_monero_bind_common(py::module_& m, PyMoneroTypes& t) {
});

// monero_rpc_payment_info
def_keyword_init(t.py_monero_rpc_payment_info, monero_rpc_payment_info_keywords());
t.py_monero_rpc_payment_info
.def(py::init<>())
.def_static("deserialize", [](const std::string& json) {
MONERO_CATCH_AND_RETHROW(monero_rpc_payment_info::deserialize(json));
}, py::arg("json"))
.def_readwrite("credits", &monero_rpc_payment_info::m_credits)
.def_readwrite("top_block_hash", &monero_rpc_payment_info::m_top_block_hash);

// monero_ssl_options
def_keyword_init(t.py_monero_ssl_options,
keyword("ssl_private_key_path", &ssl_options::m_ssl_private_key_path),
keyword("ssl_certificate_path", &ssl_options::m_ssl_certificate_path),
keyword("ssl_ca_file", &ssl_options::m_ssl_ca_file),
keyword("ssl_allowed_fingerprints", &ssl_options::m_ssl_allowed_fingerprints),
keyword("ssl_allow_any_cert", &ssl_options::m_ssl_allow_any_cert));
t.py_monero_ssl_options
.def(py::init<>())
.def_static("deserialize", [](const std::string& json) {
MONERO_CATCH_AND_RETHROW(ssl_options::deserialize(json));
}, py::arg("json"))
Expand Down
317 changes: 263 additions & 54 deletions src/cpp/daemon/py_monero_daemon_bindings.cpp

Large diffs are not rendered by default.

212 changes: 212 additions & 0 deletions src/cpp/py_monero_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,218 @@ using VectorMoneroSubaddress = std::vector<monero_subaddress>;
using VectorMoneroDestination = std::vector<std::shared_ptr<monero_destination>>;


// the argument of a keyword: when it is not given, the member keeps the value that the empty constructor gives it
template <typename Member>
struct keyword_value {
using type = boost::optional<Member>;
static void assign(Member& member, const type& value) {
if (value != boost::none) member = *value;
}
};

template <typename Value>
struct keyword_value<boost::optional<Value>> {
using type = boost::optional<Value>;
static void assign(boost::optional<Value>& member, const type& value) {
// a keyword that is not given keeps the value of the object that is copied
if (value != boost::none) member = value;
}
};

// a keyword of the constructor of a data class, and the member that it sets, which can be one of a base class
template <typename Class, typename Member>
struct keyword_field {
using type = typename keyword_value<Member>::type;
const char* name;
Member Class::* member;

template <typename Object>
void assign(const std::shared_ptr<Object>& object, const type& value) const {
keyword_value<Member>::assign((*object).*member, value);
}
};

// a keyword of an attribute that has a setter of its own, as the ones of the queries, which refer back to the query that has them
template <typename Class, typename Value>
struct keyword_setter {
using type = boost::optional<Value>;
const char* name;
void (*set)(const std::shared_ptr<Class>&, const Value&);

template <typename Object>
void assign(const std::shared_ptr<Object>& object, const type& value) const {
if (value != boost::none) set(object, *value);
}
};

template <typename Class, typename Member>
keyword_field<Class, Member> keyword(const char* name, Member Class::* member) {
return {name, member};
}

template <typename Class, typename Value>
keyword_setter<Class, Value> keyword(const char* name, void (*set)(const std::shared_ptr<Class>&, const Value&)) {
return {name, set};
}

// the constructor of a data class from keywords only, all of them optional, which also is the empty one. It has to be the only
// constructor of its class: among overloads, pybind11 converts the arguments of a call, as a list to a vector, only for the ones
// that have positional parameters (pybind11.h, the second pass of the dispatcher). Created is the class to make for an abstract one
template <typename Created = void, typename PyClass, typename... Keywords>
void def_keyword_init(PyClass& cls, const Keywords&... keywords) {
using Class = typename PyClass::type;
using Object = std::conditional_t<std::is_void<Created>::value, Class, Created>;
cls.def(py::init([keywords...](const typename Keywords::type&... values) {
std::shared_ptr<Class> object = std::make_shared<Object>();
(keywords.assign(object, values), ...);
return object;
}), py::kw_only(), (py::arg(keywords.name) = py::none())...);
}

// the same, with the keywords in a tuple, as the ones that the classes that derive from another one share with it
template <typename Created = void, typename PyClass, typename... Keywords>
void def_keyword_init(PyClass& cls, const std::tuple<Keywords...>& keywords) {
std::apply([&cls](const Keywords&... keyword) { def_keyword_init<Created>(cls, keyword...); }, keywords);
}

// as def_keyword_init, for a class that also copies another object of it, whose attributes the keywords replace
template <typename PyClass, typename Class, typename... Members>
void def_copy_keyword_init(PyClass& cls, const keyword_field<Class, Members>&... fields) {
cls.def(py::init([fields...](const boost::optional<std::shared_ptr<Class>>& config, const typename keyword_value<Members>::type&... values) {
bool copy = config != boost::none && config.get() != nullptr;
auto object = copy ? std::make_shared<Class>(*config.get()) : std::make_shared<Class>();
(keyword_value<Members>::assign((*object).*(fields.member), values), ...);
return object;
}), py::arg("config") = py::none(), py::kw_only(), (py::arg(fields.name) = py::none())..., py::keep_alive<1, 2>());
}

// the keywords of the classes that others derive from, each one declared once

inline auto monero_rpc_payment_info_keywords() {
return std::make_tuple(
keyword("credits", &monero_rpc_payment_info::m_credits),
keyword("top_block_hash", &monero_rpc_payment_info::m_top_block_hash));
}

inline auto monero_tx_keywords() {
return std::make_tuple(
keyword("hash", &monero_tx::m_hash),
keyword("version", &monero_tx::m_version),
keyword("is_miner_tx", &monero_tx::m_is_miner_tx),
keyword("payment_id", &monero_tx::m_payment_id),
keyword("fee", &monero_tx::m_fee),
keyword("ring_size", &monero_tx::m_ring_size),
keyword("relay", &monero_tx::m_relay),
keyword("is_relayed", &monero_tx::m_is_relayed),
keyword("is_confirmed", &monero_tx::m_is_confirmed),
keyword("in_tx_pool", &monero_tx::m_in_tx_pool),
keyword("is_locked", &monero_tx::m_is_locked),
keyword("num_confirmations", &monero_tx::m_num_confirmations),
keyword("unlock_time", &monero_tx::m_unlock_time),
keyword("last_relayed_timestamp", &monero_tx::m_last_relayed_timestamp),
keyword("received_timestamp", &monero_tx::m_received_timestamp),
keyword("is_double_spend_seen", &monero_tx::m_is_double_spend_seen),
keyword("key", &monero_tx::m_key),
keyword("full_hex", &monero_tx::m_full_hex),
keyword("pruned_hex", &monero_tx::m_pruned_hex),
keyword("prunable_hex", &monero_tx::m_prunable_hex),
keyword("prunable_hash", &monero_tx::m_prunable_hash),
keyword("size", &monero_tx::m_size),
keyword("weight", &monero_tx::m_weight),
keyword("inputs", &monero_tx::m_inputs),
keyword("outputs", &monero_tx::m_outputs),
keyword("output_indices", &monero_tx::m_output_indices),
keyword("metadata", &monero_tx::m_metadata),
keyword("common_tx_sets", &monero_tx::m_common_tx_sets),
keyword("extra", &monero_tx::m_extra),
keyword("rct_signatures", &monero_tx::m_rct_signatures),
keyword("rct_sig_prunable", &monero_tx::m_rct_sig_prunable),
keyword("is_kept_by_block", &monero_tx::m_is_kept_by_block),
keyword("is_failed", &monero_tx::m_is_failed),
keyword("last_failed_height", &monero_tx::m_last_failed_height),
keyword("last_failed_hash", &monero_tx::m_last_failed_hash),
keyword("max_used_block_height", &monero_tx::m_max_used_block_height),
keyword("max_used_block_hash", &monero_tx::m_max_used_block_hash),
keyword("signatures", &monero_tx::m_signatures));
}

inline auto monero_daemon_update_check_result_keywords() {
return std::make_tuple(
keyword("is_update_available", &monero_daemon_update_check_result::m_is_update_available),
keyword("version", &monero_daemon_update_check_result::m_version),
keyword("hash", &monero_daemon_update_check_result::m_hash),
keyword("auto_uri", &monero_daemon_update_check_result::m_auto_uri),
keyword("user_uri", &monero_daemon_update_check_result::m_user_uri));
}

inline auto monero_block_header_keywords() {
return std::make_tuple(
keyword("hash", &monero_block_header::m_hash),
keyword("height", &monero_block_header::m_height),
keyword("timestamp", &monero_block_header::m_timestamp),
keyword("size", &monero_block_header::m_size),
keyword("weight", &monero_block_header::m_weight),
keyword("long_term_weight", &monero_block_header::m_long_term_weight),
keyword("depth", &monero_block_header::m_depth),
keyword("difficulty_high", &monero_block_header::m_difficulty_high),
keyword("difficulty_low", &monero_block_header::m_difficulty_low),
keyword("cumulative_difficulty_high", &monero_block_header::m_cumulative_difficulty_high),
keyword("cumulative_difficulty_low", &monero_block_header::m_cumulative_difficulty_low),
keyword("major_version", &monero_block_header::m_major_version),
keyword("minor_version", &monero_block_header::m_minor_version),
keyword("nonce", &monero_block_header::m_nonce),
keyword("miner_tx_hash", &monero_block_header::m_miner_tx_hash),
keyword("num_txs", &monero_block_header::m_num_txs),
keyword("orphan_status", &monero_block_header::m_orphan_status),
keyword("prev_hash", &monero_block_header::m_prev_hash),
keyword("reward", &monero_block_header::m_reward),
keyword("pow_hash", &monero_block_header::m_pow_hash));
}

inline auto monero_check_keywords() {
return std::make_tuple(
keyword("is_good", &monero_check::m_is_good));
}

inline auto monero_output_keywords() {
return std::make_tuple(
keyword("key_image", &monero_output::m_key_image),
keyword("amount", &monero_output::m_amount),
keyword("index", &monero_output::m_index),
keyword("stealth_public_key", &monero_output::m_stealth_public_key),
keyword("mask", &monero_output::m_mask),
keyword("ring_output_indices", &monero_output::m_ring_output_indices));
}

inline auto monero_transfer_keywords() {
return std::make_tuple(
keyword("account_index", &monero_transfer::m_account_index),
keyword("amount", &monero_transfer::m_amount));
}

inline auto monero_output_wallet_keywords() {
return std::tuple_cat(monero_output_keywords(), std::make_tuple(
keyword("account_index", &monero_output_wallet::m_account_index),
keyword("subaddress_index", &monero_output_wallet::m_subaddress_index),
keyword("is_spent", &monero_output_wallet::m_is_spent),
keyword("is_frozen", &monero_output_wallet::m_is_frozen)));
}

inline auto monero_tx_wallet_keywords() {
return std::tuple_cat(monero_tx_keywords(), std::make_tuple(
keyword("is_incoming", &monero_tx_wallet::m_is_incoming),
keyword("is_outgoing", &monero_tx_wallet::m_is_outgoing),
keyword("incoming_transfers", &monero_tx_wallet::m_incoming_transfers),
keyword("outgoing_transfer", &monero_tx_wallet::m_outgoing_transfer),
keyword("note", &monero_tx_wallet::m_note),
keyword("input_sum", &monero_tx_wallet::m_input_sum),
keyword("output_sum", &monero_tx_wallet::m_output_sum),
keyword("change_address", &monero_tx_wallet::m_change_address),
keyword("change_amount", &monero_tx_wallet::m_change_amount),
keyword("num_dummy_outputs", &monero_tx_wallet::m_num_dummy_outputs),
keyword("extra_hex", &monero_tx_wallet::m_extra_hex)));
}

/**
* Holds every pybind11 type handle that must be registered before any
* method is bound, so classes can reference each other (as base classes
Expand Down
Loading
Loading