From d237f62c2360107235fc75f4533711e98c70e2aa Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Tue, 3 Jan 2012 15:24:28 -0500 Subject: [PATCH] -blocknotify=command argument, implemented using system() and boost::thread --- src/init.cpp | 1 + src/main.cpp | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/init.cpp b/src/init.cpp index 8cbf21cc4..a2e77b423 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -227,6 +227,7 @@ bool AppInit2(int argc, char* argv[]) " -rpcport= \t\t " + _("Listen for JSON-RPC connections on (default: 8332)") + "\n" + " -rpcallowip= \t\t " + _("Allow JSON-RPC connections from specified IP address") + "\n" + " -rpcconnect= \t " + _("Send commands to node running on (default: 127.0.0.1)") + "\n" + + " -blocknotify= " + _("Execute command when the best block changes (%s in cmd is replaced by block hash)") + "\n" + " -keypool= \t " + _("Set key pool size to (default: 100)") + "\n" + " -rescan \t " + _("Rescan the block chain for missing wallet transactions") + "\n"; diff --git a/src/main.cpp b/src/main.cpp index 15f047bf7..52a3c3fde 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ #include "db.h" #include "net.h" #include "init.h" +#include #include #include @@ -1278,6 +1279,14 @@ bool static Reorganize(CTxDB& txdb, CBlockIndex* pindexNew) } +static void +runCommand(std::string strCommand) +{ + int nErr = ::system(strCommand.c_str()); + if (nErr) + printf("runCommand error: system(%s) returned %d\n", strCommand.c_str(), nErr); +} + bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew) { uint256 hash = GetHash(); @@ -1321,7 +1330,8 @@ bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew) } // Update best block in wallet (so we can detect restored wallets) - if (!IsInitialBlockDownload()) + bool fIsInitialDownload = IsInitialBlockDownload(); + if (!fIsInitialDownload) { const CBlockLocator locator(pindexNew); ::SetBestChain(locator); @@ -1336,6 +1346,14 @@ bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew) nTransactionsUpdated++; printf("SetBestChain: new best=%s height=%d work=%s\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, bnBestChainWork.ToString().c_str()); + std::string strCmd = GetArg("-blocknotify", ""); + + if (!fIsInitialDownload && !strCmd.empty()) + { + boost::replace_all(strCmd, "%s", hashBestChain.GetHex()); + boost::thread t(runCommand, strCmd); // thread runs free + } + return true; }