Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RPC] Add blocksizenotify command #524

Merged
merged 2 commits into from
Feb 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ std::string HelpMessage(HelpMessageMode mode)
strUsage += HelpMessageOpt("-alertnotify=<cmd>", _("Execute command when a relevant alert is received or we see a really long fork (%s in cmd is replaced by message)"));
strUsage += HelpMessageOpt("-alerts", strprintf(_("Receive and display P2P network alerts (default: %u)"), DEFAULT_ALERTS));
strUsage += HelpMessageOpt("-blocknotify=<cmd>", _("Execute command when the best block changes (%s in cmd is replaced by block hash)"));
strUsage += HelpMessageOpt("-blocksizenotify=<cmd>", _("Execute command when the best block changes and its size is over (%s in cmd is replaced by block hash, %d with the block size)"));
strUsage += HelpMessageOpt("-checkblocks=<n>", strprintf(_("How many blocks to check at startup (default: %u, 0 = all)"), 500));
strUsage += HelpMessageOpt("-conf=<file>", strprintf(_("Specify configuration file (default: %s)"), "pivx.conf"));
if (mode == HMM_BITCOIND) {
Expand Down Expand Up @@ -585,6 +586,15 @@ static void BlockNotifyCallback(const uint256& hashNewTip)
boost::thread t(runCommand, strCmd); // thread runs free
}

static void BlockSizeNotifyCallback(int size, const uint256& hashNewTip)
{
std::string strCmd = GetArg("-blocksizenotify", "");

boost::replace_all(strCmd, "%s", hashNewTip.GetHex());
boost::replace_all(strCmd, "%d", std::to_string(size));
boost::thread t(runCommand, strCmd); // thread runs free
}

struct CImportingNow {
CImportingNow()
{
Expand Down Expand Up @@ -1624,6 +1634,9 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
if (mapArgs.count("-blocknotify"))
uiInterface.NotifyBlockTip.connect(BlockNotifyCallback);

if (mapArgs.count("-blocksizenotify"))
uiInterface.NotifyBlockSize.connect(BlockSizeNotifyCallback);

// scan for better chains in the block chain database, that are not yet connected in the active best chain
CValidationState state;
if (!ActivateBestChain(state))
Expand Down
6 changes: 6 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4051,6 +4051,12 @@ bool ActivateBestChain(CValidationState& state, CBlock* pblock, bool fAlreadyChe
// Note: uiInterface, should switch main signals.
uiInterface.NotifyBlockTip(hashNewTip);
GetMainSignals().UpdatedBlockTip(pindexNewTip);

unsigned size = GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION);
// If the size is over 1 MB notify external listeners, and it is within the last 5 minutes
if (size > MAX_BLOCK_SIZE_LEGACY && pblock->GetBlockTime() > GetAdjustedTime() - 300) {
uiInterface.NotifyBlockSize(static_cast<int>(size), hashNewTip);
}
}
} while (pindexMostWork != chainActive.Tip());
CheckBlockIndex();
Expand Down
3 changes: 3 additions & 0 deletions src/ui_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class CClientUIInterface
/** New block has been accepted */
boost::signals2::signal<void(const uint256& hash)> NotifyBlockTip;

/** New block has been accepted and is over a certain size */
boost::signals2::signal<void(int size, const uint256& hash)> NotifyBlockSize;

/** Banlist did change. */
boost::signals2::signal<void (void)> BannedListChanged;
};
Expand Down