Merge pull request #5053

23813c71 blockchain: add --reorg-notify (moneromooo-monero)
f6db59b0 notify: handle arbitrary tags (moneromooo-monero)
ff959216 notify: warn if the spec contains one of '"\ (moneromooo-monero)
13852678 common: set MONERO_DEFAULT_LOG_CATEGORY for notify and spawn (moneromooo-monero)
This commit is contained in:
Riccardo Spagni 2019-01-28 21:35:46 +02:00
commit 40bb66cc1e
No known key found for this signature in database
GPG Key ID: 55432DF31CCD4FCD
8 changed files with 63 additions and 8 deletions

View File

@ -27,11 +27,15 @@
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <stdarg.h>
#include "misc_log_ex.h" #include "misc_log_ex.h"
#include "file_io_utils.h" #include "file_io_utils.h"
#include "spawn.h" #include "spawn.h"
#include "notify.h" #include "notify.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "notify"
namespace tools namespace tools
{ {
@ -46,15 +50,32 @@ Notify::Notify(const char *spec)
boost::split(args, spec, boost::is_any_of(" ")); boost::split(args, spec, boost::is_any_of(" "));
CHECK_AND_ASSERT_THROW_MES(args.size() > 0, "Failed to parse spec"); 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]; filename = args[0];
CHECK_AND_ASSERT_THROW_MES(epee::file_io_utils::is_file_exist(filename), "File not found: " << filename); 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<std::string> &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<std::string> margs = args; std::vector<std::string> 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); return tools::spawn(filename.c_str(), margs, false);
} }

View File

@ -39,7 +39,7 @@ class Notify
public: public:
Notify(const char *spec); Notify(const char *spec);
int notify(const char *parameter); int notify(const char *tag, const char *s, ...);
private: private:
std::string filename; std::string filename;

View File

@ -42,6 +42,9 @@
#include "util.h" #include "util.h"
#include "spawn.h" #include "spawn.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "spawn"
namespace tools namespace tools
{ {

View File

@ -1073,6 +1073,11 @@ bool Blockchain::switch_to_alternative_blockchain(std::list<blocks_ext_by_hash::
m_hardfork->reorganize_from_chain_height(split_height); m_hardfork->reorganize_from_chain_height(split_height);
std::shared_ptr<tools::Notify> 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()); MGINFO_GREEN("REORGANIZE SUCCESS! on height: " << split_height << ", new blockchain size: " << m_db->height());
return true; return true;
} }
@ -3701,7 +3706,7 @@ leave:
std::shared_ptr<tools::Notify> block_notify = m_block_notify; std::shared_ptr<tools::Notify> block_notify = m_block_notify;
if (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; return true;
} }

View File

@ -731,10 +731,17 @@ namespace cryptonote
/** /**
* @brief sets a block notify object to call for every new block * @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<tools::Notify> &notify) { m_block_notify = notify; } void set_block_notify(const std::shared_ptr<tools::Notify> &notify) { 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<tools::Notify> &notify) { m_reorg_notify = notify; }
/** /**
* @brief Put DB in safe sync mode * @brief Put DB in safe sync mode
*/ */
@ -1077,6 +1084,7 @@ namespace cryptonote
bool m_btc_valid; bool m_btc_valid;
std::shared_ptr<tools::Notify> m_block_notify; std::shared_ptr<tools::Notify> m_block_notify;
std::shared_ptr<tools::Notify> m_reorg_notify;
/** /**
* @brief collects the keys for all outputs being "spent" as an input * @brief collects the keys for all outputs being "spent" as an input

View File

@ -185,6 +185,13 @@ namespace cryptonote
, "Prune blockchain" , "Prune blockchain"
, false , false
}; };
static const command_line::arg_descriptor<std::string> 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): 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_pad_transactions);
command_line::add_arg(desc, arg_block_notify); command_line::add_arg(desc, arg_block_notify);
command_line::add_arg(desc, arg_prune_blockchain); command_line::add_arg(desc, arg_prune_blockchain);
command_line::add_arg(desc, arg_reorg_notify);
miner::init_options(desc); miner::init_options(desc);
BlockchainDB::init_options(desc); BlockchainDB::init_options(desc);
@ -582,6 +590,16 @@ namespace cryptonote
MERROR("Failed to parse block notify spec"); 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<tools::Notify>(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<uint8_t, uint64_t> 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 std::pair<uint8_t, uint64_t> 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 = { const cryptonote::test_options regtest_test_options = {
regtest_hard_forks regtest_hard_forks

View File

@ -2048,7 +2048,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote
{ {
std::shared_ptr<tools::Notify> tx_notify = m_tx_notify; std::shared_ptr<tools::Notify> tx_notify = m_tx_notify;
if (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);
} }
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------

View File

@ -67,7 +67,7 @@ TEST(notify, works)
+ " " + name_template + " %s"; + " " + name_template + " %s";
tools::Notify notify(spec.c_str()); tools::Notify notify(spec.c_str());
notify.notify("1111111111111111111111111111111111111111111111111111111111111111"); notify.notify("%s", "1111111111111111111111111111111111111111111111111111111111111111", NULL);
bool ok = false; bool ok = false;
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)