Browse Source

Merge pull request #743 from gavinandresen/blocknotify

-blocknotify : run a command when best-block changes
miguelfreitas
Gavin Andresen 13 years ago
parent
commit
3f64fa1369
  1. 1
      src/init.cpp
  2. 20
      src/main.cpp

1
src/init.cpp

@ -227,6 +227,7 @@ bool AppInit2(int argc, char* argv[])
" -rpcport=<port> \t\t " + _("Listen for JSON-RPC connections on <port> (default: 8332)") + "\n" + " -rpcport=<port> \t\t " + _("Listen for JSON-RPC connections on <port> (default: 8332)") + "\n" +
" -rpcallowip=<ip> \t\t " + _("Allow JSON-RPC connections from specified IP address") + "\n" + " -rpcallowip=<ip> \t\t " + _("Allow JSON-RPC connections from specified IP address") + "\n" +
" -rpcconnect=<ip> \t " + _("Send commands to node running on <ip> (default: 127.0.0.1)") + "\n" + " -rpcconnect=<ip> \t " + _("Send commands to node running on <ip> (default: 127.0.0.1)") + "\n" +
" -blocknotify=<cmd> " + _("Execute command when the best block changes (%s in cmd is replaced by block hash)") + "\n" +
" -keypool=<n> \t " + _("Set key pool size to <n> (default: 100)") + "\n" + " -keypool=<n> \t " + _("Set key pool size to <n> (default: 100)") + "\n" +
" -rescan \t " + _("Rescan the block chain for missing wallet transactions") + "\n"; " -rescan \t " + _("Rescan the block chain for missing wallet transactions") + "\n";

20
src/main.cpp

@ -7,6 +7,7 @@
#include "db.h" #include "db.h"
#include "net.h" #include "net.h"
#include "init.h" #include "init.h"
#include <boost/algorithm/string/replace.hpp>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp> #include <boost/filesystem/fstream.hpp>
@ -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) bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew)
{ {
uint256 hash = GetHash(); 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) // Update best block in wallet (so we can detect restored wallets)
if (!IsInitialBlockDownload()) bool fIsInitialDownload = IsInitialBlockDownload();
if (!fIsInitialDownload)
{ {
const CBlockLocator locator(pindexNew); const CBlockLocator locator(pindexNew);
::SetBestChain(locator); ::SetBestChain(locator);
@ -1336,6 +1346,14 @@ bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew)
nTransactionsUpdated++; nTransactionsUpdated++;
printf("SetBestChain: new best=%s height=%d work=%s\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, bnBestChainWork.ToString().c_str()); 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; return true;
} }

Loading…
Cancel
Save