From 13852678e1c43485e7d49b3a7238332c6158ad4d Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Wed, 9 Jan 2019 01:05:47 +0000 Subject: [PATCH 1/4] common: set MONERO_DEFAULT_LOG_CATEGORY for notify and spawn --- src/common/notify.cpp | 3 +++ src/common/spawn.cpp | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/common/notify.cpp b/src/common/notify.cpp index cadc68ea7..533debe71 100644 --- a/src/common/notify.cpp +++ b/src/common/notify.cpp @@ -32,6 +32,9 @@ #include "spawn.h" #include "notify.h" +#undef MONERO_DEFAULT_LOG_CATEGORY +#define MONERO_DEFAULT_LOG_CATEGORY "notify" + namespace tools { diff --git a/src/common/spawn.cpp b/src/common/spawn.cpp index b2d03f62f..e03552f8c 100644 --- a/src/common/spawn.cpp +++ b/src/common/spawn.cpp @@ -42,6 +42,9 @@ #include "util.h" #include "spawn.h" +#undef MONERO_DEFAULT_LOG_CATEGORY +#define MONERO_DEFAULT_LOG_CATEGORY "spawn" + namespace tools { From ff9592166828721e0f394571ab7c0d82036e9049 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Wed, 9 Jan 2019 01:06:28 +0000 Subject: [PATCH 2/4] notify: warn if the spec contains one of '"\ These aren't processed as a shell does, so this may surprise users --- src/common/notify.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/common/notify.cpp b/src/common/notify.cpp index 533debe71..0ac3d501b 100644 --- a/src/common/notify.cpp +++ b/src/common/notify.cpp @@ -49,6 +49,8 @@ Notify::Notify(const char *spec) boost::split(args, spec, boost::is_any_of(" ")); CHECK_AND_ASSERT_THROW_MES(args.size() > 0, "Failed to parse spec"); + if (strchr(spec, '\'') || strchr(spec, '\"') || strchr(spec, '\\')) + MWARNING("A notification spec contains a quote or backslash: note that these are handled verbatim, which may not be the intent"); filename = args[0]; CHECK_AND_ASSERT_THROW_MES(epee::file_io_utils::is_file_exist(filename), "File not found: " << filename); } From f6db59b011914c2180b42ed4f6f144679a126994 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Wed, 9 Jan 2019 14:28:42 +0000 Subject: [PATCH 3/4] notify: handle arbitrary tags --- src/common/notify.cpp | 22 +++++++++++++++++++--- src/common/notify.h | 2 +- src/cryptonote_core/blockchain.cpp | 2 +- src/wallet/wallet2.cpp | 2 +- tests/unit_tests/notify.cpp | 2 +- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/common/notify.cpp b/src/common/notify.cpp index 0ac3d501b..c3165fb05 100644 --- a/src/common/notify.cpp +++ b/src/common/notify.cpp @@ -27,6 +27,7 @@ // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include +#include #include "misc_log_ex.h" #include "file_io_utils.h" #include "spawn.h" @@ -55,11 +56,26 @@ Notify::Notify(const char *spec) CHECK_AND_ASSERT_THROW_MES(epee::file_io_utils::is_file_exist(filename), "File not found: " << filename); } -int Notify::notify(const char *parameter) +static void replace(std::vector &v, const char *tag, const char *s) +{ + for (std::string &str: v) + boost::replace_all(str, tag, s); +} + +int Notify::notify(const char *tag, const char *s, ...) { std::vector margs = args; - for (std::string &s: margs) - boost::replace_all(s, "%s", parameter); + + replace(margs, tag, s); + + va_list ap; + va_start(ap, s); + while ((tag = va_arg(ap, const char*))) + { + s = va_arg(ap, const char*); + replace(margs, tag, s); + } + va_end(ap); return tools::spawn(filename.c_str(), margs, false); } diff --git a/src/common/notify.h b/src/common/notify.h index 81aacebb0..f813e8def 100644 --- a/src/common/notify.h +++ b/src/common/notify.h @@ -39,7 +39,7 @@ class Notify public: Notify(const char *spec); - int notify(const char *parameter); + int notify(const char *tag, const char *s, ...); private: std::string filename; diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 9f1e2a0c0..7620dfd61 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -3701,7 +3701,7 @@ leave: std::shared_ptr block_notify = m_block_notify; if (block_notify) - block_notify->notify(epee::string_tools::pod_to_hex(id).c_str()); + block_notify->notify("%s", epee::string_tools::pod_to_hex(id).c_str(), NULL); return true; } diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index a8fe9c7cb..eaffe6472 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -2041,7 +2041,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote { std::shared_ptr tx_notify = m_tx_notify; if (tx_notify) - tx_notify->notify(epee::string_tools::pod_to_hex(txid).c_str()); + tx_notify->notify("%s", epee::string_tools::pod_to_hex(txid).c_str(), NULL); } } //---------------------------------------------------------------------------------------------------- diff --git a/tests/unit_tests/notify.cpp b/tests/unit_tests/notify.cpp index cd70b7739..ceeba8649 100644 --- a/tests/unit_tests/notify.cpp +++ b/tests/unit_tests/notify.cpp @@ -67,7 +67,7 @@ TEST(notify, works) + " " + name_template + " %s"; tools::Notify notify(spec.c_str()); - notify.notify("1111111111111111111111111111111111111111111111111111111111111111"); + notify.notify("%s", "1111111111111111111111111111111111111111111111111111111111111111", NULL); bool ok = false; for (int i = 0; i < 10; ++i) From 23813c7160fa1236cc882cae0ae40f3bee339f91 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Wed, 9 Jan 2019 14:29:28 +0000 Subject: [PATCH 4/4] blockchain: add --reorg-notify This will trigger if a reorg is seen. This may be used to do things like stop automated withdrawals on large reorgs. %s is replaced by the height at the split point %h is replaced by the height of the new chain %n is replaced by the number of new blocks after the reorg --- src/cryptonote_core/blockchain.cpp | 5 +++++ src/cryptonote_core/blockchain.h | 10 +++++++++- src/cryptonote_core/cryptonote_core.cpp | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 7620dfd61..41357e72e 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -1073,6 +1073,11 @@ bool Blockchain::switch_to_alternative_blockchain(std::listreorganize_from_chain_height(split_height); + std::shared_ptr reorg_notify = m_reorg_notify; + if (reorg_notify) + reorg_notify->notify("%s", std::to_string(split_height).c_str(), "%h", std::to_string(m_db->height()).c_str(), + "%n", std::to_string(m_db->height() - split_height).c_str(), NULL); + MGINFO_GREEN("REORGANIZE SUCCESS! on height: " << split_height << ", new blockchain size: " << m_db->height()); return true; } diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h index 4952116ac..c742bec91 100644 --- a/src/cryptonote_core/blockchain.h +++ b/src/cryptonote_core/blockchain.h @@ -731,10 +731,17 @@ namespace cryptonote /** * @brief sets a block notify object to call for every new block * - * @param notify the notify object to cal at every new block + * @param notify the notify object to call at every new block */ void set_block_notify(const std::shared_ptr ¬ify) { m_block_notify = notify; } + /** + * @brief sets a reorg notify object to call for every reorg + * + * @param notify the notify object to call at every reorg + */ + void set_reorg_notify(const std::shared_ptr ¬ify) { m_reorg_notify = notify; } + /** * @brief Put DB in safe sync mode */ @@ -1077,6 +1084,7 @@ namespace cryptonote bool m_btc_valid; std::shared_ptr m_block_notify; + std::shared_ptr m_reorg_notify; /** * @brief collects the keys for all outputs being "spent" as an input diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp index 48c607ff1..1513bb750 100644 --- a/src/cryptonote_core/cryptonote_core.cpp +++ b/src/cryptonote_core/cryptonote_core.cpp @@ -185,6 +185,13 @@ namespace cryptonote , "Prune blockchain" , false }; + static const command_line::arg_descriptor arg_reorg_notify = { + "reorg-notify" + , "Run a program for each reorg, '%s' will be replaced by the split height, " + "'%h' will be replaced by the new blockchain height, and '%n' will be " + "replaced by the number of new blocks in the new chain" + , "" + }; //----------------------------------------------------------------------------------------------- core::core(i_cryptonote_protocol* pprotocol): @@ -300,6 +307,7 @@ namespace cryptonote command_line::add_arg(desc, arg_pad_transactions); command_line::add_arg(desc, arg_block_notify); command_line::add_arg(desc, arg_prune_blockchain); + command_line::add_arg(desc, arg_reorg_notify); miner::init_options(desc); BlockchainDB::init_options(desc); @@ -582,6 +590,16 @@ namespace cryptonote MERROR("Failed to parse block notify spec"); } + try + { + if (!command_line::is_arg_defaulted(vm, arg_reorg_notify)) + m_blockchain_storage.set_reorg_notify(std::shared_ptr(new tools::Notify(command_line::get_arg(vm, arg_reorg_notify).c_str()))); + } + catch (const std::exception &e) + { + MERROR("Failed to parse reorg notify spec"); + } + const std::pair regtest_hard_forks[3] = {std::make_pair(1, 0), std::make_pair(Blockchain::get_hard_fork_heights(MAINNET).back().version, 1), std::make_pair(0, 0)}; const cryptonote::test_options regtest_test_options = { regtest_hard_forks