From 1f33e3c6bad1cb81c00452af4e10824e431f388a Mon Sep 17 00:00:00 2001 From: woodser Date: Wed, 31 Jul 2019 12:43:44 -0400 Subject: [PATCH] start cnBlockToBlock(), cnTransactionToTx() utilities; onNewBlock(uint64_t) only (no block info provided by w2); --- CMakeLists.txt | 6 ++--- src/daemon/MoneroDaemonModel.h | 2 ++ src/utils/MoneroUtils.cpp | 33 ++++++++++++++++++++++++ src/utils/MoneroUtils.h | 46 +++++++++++++++++++++++----------- src/wallet/MoneroWallet.cpp | 11 ++++---- src/wallet/MoneroWallet.h | 2 +- 6 files changed, 77 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5096061..abdaa54 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -228,11 +228,11 @@ set_target_properties(version PROPERTIES IMPORTED_LOCATION set( LIBRARY_SRC_FILES + src/utils/MoneroUtils.cpp src/daemon/MoneroDaemonModel.cpp src/daemon/MoneroDaemon.cpp src/wallet/MoneroWalletModel.cpp src/wallet/MoneroWallet.cpp - src/utils/MoneroUtils.cpp ) add_library(monero-cpp SHARED ${LIBRARY_SRC_FILES}) @@ -285,7 +285,7 @@ target_link_libraries(monero-cpp # Build C++ scratchpad ######################## -###[[ --- START BLOCK COMMENT --- +#[[ --- START BLOCK COMMENT --- set( SCRATCHPAD_SRC_FILES @@ -338,4 +338,4 @@ target_link_libraries(scratchpad ${EXTRA_LIBRARIES} ) -###]] # --- END BLOCK COMMENT --- \ No newline at end of file +#]] # --- END BLOCK COMMENT --- \ No newline at end of file diff --git a/src/daemon/MoneroDaemonModel.h b/src/daemon/MoneroDaemonModel.h index 7a753f7..95d2879 100644 --- a/src/daemon/MoneroDaemonModel.h +++ b/src/daemon/MoneroDaemonModel.h @@ -109,6 +109,8 @@ namespace monero { /** * Models a Monero block header which contains information about the block. + * + * TODO: a header that is transmitted may have fewer fields like cryptonote::block_header; separate? */ struct MoneroBlockHeader : public SerializableStruct { boost::optional id; diff --git a/src/utils/MoneroUtils.cpp b/src/utils/MoneroUtils.cpp index 2ccea46..c8d871f 100644 --- a/src/utils/MoneroUtils.cpp +++ b/src/utils/MoneroUtils.cpp @@ -469,3 +469,36 @@ boost::property_tree::ptree MoneroUtils::toPropertyTree(const vector n } return numsNode; } + +shared_ptr MoneroUtils::cnBlockToBlock(const cryptonote::block& cnBlock) { + cryptonote::block temp = cnBlock; + cout << cryptonote::obj_to_json_str(temp) << endl; + shared_ptr block = make_shared(); + block->majorVersion = cnBlock.major_version; + block->minorVersion = cnBlock.minor_version; + block->timestamp = cnBlock.timestamp; + block->prevId = epee::string_tools::pod_to_hex(cnBlock.prev_id); + block->nonce = cnBlock.nonce; + block->coinbaseTx = MoneroUtils::cnTransactionToTx(cnBlock.miner_tx); + for (const crypto::hash& txHash : cnBlock.tx_hashes) { + block->txIds.push_back(epee::string_tools::pod_to_hex(txHash)); + } + return block; +} + +shared_ptr MoneroUtils::cnTransactionToTx(const cryptonote::transaction& cnTx) { + shared_ptr tx = make_shared(); + //tx->version = cnTx.version; + tx->unlockTime = cnTx.unlock_time; + tx->id = epee::string_tools::pod_to_hex(cnTx.hash); + return tx; + + // TODO: finish this +// std::vector vin; +// std::vector vout; +// std::vector extra; +// std::vector > signatures; +// rct::rctSig rct_signatures; +// mutable size_t blob_size; +// throw runtime_error("cnTransactionToTx not implemented"); +} diff --git a/src/utils/MoneroUtils.h b/src/utils/MoneroUtils.h index 7239c3a..009a850 100644 --- a/src/utils/MoneroUtils.h +++ b/src/utils/MoneroUtils.h @@ -84,6 +84,38 @@ namespace MoneroUtils string serialize(const boost::property_tree::ptree& node); + /** + * Convert a Monero Core cryptonote::block to a block in this library's native model. + * + * @param cnBlock is the Core block to convert + * @return a block in this library's native model + */ + shared_ptr cnBlockToBlock(const cryptonote::block& cnBlock); + + /** + * Convert a Monero Core cryptonote::transaction to a transaction in this library's native model. + * + * @param cnTransaction is the Core transaction to convert + * @return a transaction in this library's native model + */ + shared_ptr cnTransactionToTx(const cryptonote::transaction& cnTransaction); + + /** + * Modified from core_rpc_server.cpp to return a string. + * + * TODO: remove this duplicate, use core_rpc_server instead + */ + static std::string get_pruned_tx_json(cryptonote::transaction &tx) + { + std::stringstream ss; + json_archive ar(ss); + bool r = tx.serialize_base(ar); + CHECK_AND_ASSERT_MES(r, std::string(), "Failed to serialize rct signatures base"); + return ss.str(); + } + + // ------------------------- VALUE RECONCILATION- --------------------------- + // TODO: template implementation here, could move to MoneroUtils.hpp per https://stackoverflow.com/questions/3040480/c-template-function-compiles-in-header-but-not-implementation template boost::property_tree::ptree toPropertyTree(const vector> types) { boost::property_tree::ptree typeNodes; @@ -193,19 +225,5 @@ namespace MoneroUtils // boost::optional reconcile(const boost::optional& val1, const boost::optional& val2) { // return reconcile(val1, val2, boost::none, boost::none, boost::none); // } - - /** - * Modified from core_rpc_server.cpp to return a string. - * - * TODO: remove this duplicate, use core_rpc_server instead - */ - static std::string get_pruned_tx_json(cryptonote::transaction &tx) - { - std::stringstream ss; - json_archive ar(ss); - bool r = tx.serialize_base(ar); - CHECK_AND_ASSERT_MES(r, std::string(), "Failed to serialize rct signatures base"); - return ss.str(); - } } #endif /* MoneroUtils_h */ diff --git a/src/wallet/MoneroWallet.cpp b/src/wallet/MoneroWallet.cpp index 229d326..a1bb3d5 100644 --- a/src/wallet/MoneroWallet.cpp +++ b/src/wallet/MoneroWallet.cpp @@ -698,12 +698,13 @@ namespace monero { virtual void on_new_block(uint64_t height, const cryptonote::block& cnBlock) { + // // convert cryptonote block to library model + // shared_ptr block = MoneroUtils::cnBlockToBlock(cnBlock); + // block->height = height; + // listener->onNewBlock(*block); + // notify listener of block - if (listener != boost::none) { - MoneroBlock block; - block.height = height; - listener.get().onNewBlock(block); - } + if (listener != boost::none) listener->onNewBlock(height); // notify listeners of sync progress if (syncStartHeight != boost::none && height >= *syncStartHeight) { diff --git a/src/wallet/MoneroWallet.h b/src/wallet/MoneroWallet.h index 07c2b6f..aef88fa 100644 --- a/src/wallet/MoneroWallet.h +++ b/src/wallet/MoneroWallet.h @@ -126,7 +126,7 @@ namespace monero { * * @param block is the newly processed block */ - virtual void onNewBlock(MoneroBlock& block) {}; + virtual void onNewBlock(uint64_t height) {}; }; // forward declaration of internal wallet2 listener