Skip to content

Commit

Permalink
import alerts code from emercoin
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgenijM86 committed Mar 19, 2019
1 parent e40ccef commit 818a646
Show file tree
Hide file tree
Showing 18 changed files with 201 additions and 78 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Expand Up @@ -77,6 +77,7 @@ endif
BITCOIN_CORE_H = \
addrdb.h \
addrman.h \
alert.h \
base58.h \
bignum.h \
bech32.h \
Expand Down Expand Up @@ -188,6 +189,7 @@ libbitcoin_server_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libbitcoin_server_a_SOURCES = \
addrdb.cpp \
addrman.cpp \
alert.cpp \
bloom.cpp \
blockencodings.cpp \
chain.cpp \
Expand Down
3 changes: 3 additions & 0 deletions src/chainparams.cpp
Expand Up @@ -105,6 +105,7 @@ class CMainParams : public CChainParams {
pchMessageStart[1] = 0xe8;
pchMessageStart[2] = 0xe9;
pchMessageStart[3] = 0xe5;
vAlertPubKey = ParseHex("04201f0f85178950503c20d5a947883dff81e727533f1f1da104755fa25275cf68c442b8ad50da3152c10a74ea621da614d3d2048ba25a14f39bfc40c41223543a");
nDefaultPort = 9901;
nPruneAfterHeight = 100000;

Expand Down Expand Up @@ -200,6 +201,7 @@ class CTestNetParams : public CChainParams {
pchMessageStart[1] = 0xf2;
pchMessageStart[2] = 0xc0;
pchMessageStart[3] = 0xef;
vAlertPubKey = ParseHex("04383862439513e940f6fcbf62d365c162a5256920c2c25b0b4266fdee4a443d71cfe224dbccff6fdb2ea57a37eb0cbec5637ebea06f63c70ca093672fbdc27643");
nDefaultPort = 9903;
nPruneAfterHeight = 1000;

Expand Down Expand Up @@ -289,6 +291,7 @@ class CRegTestParams : public CChainParams {
pchMessageStart[1] = 0xf2;
pchMessageStart[2] = 0xc0;
pchMessageStart[3] = 0xef;
vAlertPubKey = ParseHex("04383862439513e940f6fcbf62d365c162a5256920c2c25b0b4266fdee4a443d71cfe224dbccff6fdb2ea57a37eb0cbec5637ebea06f63c70ca093672fbdc27643");
nDefaultPort = 9903;
nPruneAfterHeight = 1000;

Expand Down
3 changes: 3 additions & 0 deletions src/chainparams.h
Expand Up @@ -53,6 +53,7 @@ class CChainParams

const Consensus::Params& GetConsensus() const { return consensus; }
const CMessageHeader::MessageStartChars& MessageStart() const { return pchMessageStart; }
const std::vector<unsigned char>& AlertKey() const { return vAlertPubKey; }
int GetDefaultPort() const { return nDefaultPort; }

const CBlock& GenesisBlock() const { return genesis; }
Expand All @@ -79,6 +80,8 @@ class CChainParams

Consensus::Params consensus;
CMessageHeader::MessageStartChars pchMessageStart;
//! Raw pub key bytes for the broadcast alert signing key.
std::vector<unsigned char> vAlertPubKey;
int nDefaultPort;
uint64_t nPruneAfterHeight;
std::vector<std::string> vSeeds;
Expand Down
3 changes: 3 additions & 0 deletions src/init.cpp
Expand Up @@ -325,6 +325,7 @@ std::string HelpMessage(HelpMessageMode mode)
std::string strUsage = HelpMessageGroup(_("Options:"));
strUsage += HelpMessageOpt("-?", _("Print this help message and exit"));
strUsage += HelpMessageOpt("-version", _("Print version and exit"));
strUsage += HelpMessageOpt("-alerts", strprintf(_("Receive and display P2P network alerts (default: %u)"), DEFAULT_ALERTS));
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("-blocknotify=<cmd>", _("Execute command when the best block changes (%s in cmd is replaced by block hash)"));
if (showDebug)
Expand Down Expand Up @@ -1046,6 +1047,8 @@ bool AppInitParameterInteraction()
fAcceptDatacarrier = gArgs.GetBoolArg("-datacarrier", DEFAULT_ACCEPT_DATACARRIER);
nMaxDatacarrierBytes = gArgs.GetArg("-datacarriersize", nMaxDatacarrierBytes);

fAlerts = gArgs.GetBoolArg("-alerts", DEFAULT_ALERTS);

// Option to startup with mocktime set (used for regression testing):
SetMockTime(gArgs.GetArg("-mocktime", 0)); // SetMockTime(0) is a no-op

Expand Down
37 changes: 37 additions & 0 deletions src/net_processing.cpp
Expand Up @@ -5,6 +5,7 @@

#include <net_processing.h>

#include <alert.h>
#include <addrman.h>
#include <arith_uint256.h>
#include <blockencodings.h>
Expand Down Expand Up @@ -1716,6 +1717,13 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
checkpointMessage.RelayTo(pfrom);
}

// peercoin: relay alerts
{
LOCK(cs_mapAlerts);
for (auto& item : mapAlerts)
item.second.RelayTo(pfrom);
}

std::string remoteAddr;
if (fLogIPs)
remoteAddr = ", peeraddr=" + pfrom->addr.ToString();
Expand Down Expand Up @@ -2998,6 +3006,35 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
}
}

else if (fAlerts && strCommand == NetMsgType::ALERT)
{
CAlert alert;
vRecv >> alert;

uint256 alertHash = alert.GetHash();
if (pfrom->setKnown.count(alertHash) == 0)
{
if (alert.ProcessAlert(chainparams.AlertKey()))
{
// Relay
pfrom->setKnown.insert(alertHash);
if (g_connman)
g_connman->ForEachNode([&alert](CNode* pnode) {
alert.RelayTo(pnode);
});
}
else {
// Small DoS penalty so peers that send us lots of
// duplicate/expired/invalid-signature/whatever alerts
// eventually get banned.
// This isn't a Misbehaving(100) (immediate ban) because the
// peer might be an older or different implementation with
// a different signature key, etc.
Misbehaving(pfrom->GetId(), 10);
}
}
}

else if (strCommand == NetMsgType::CHECKPOINT)
{
CSyncCheckpoint checkpoint;
Expand Down
2 changes: 2 additions & 0 deletions src/protocol.cpp
Expand Up @@ -28,6 +28,7 @@ const char *GETADDR="getaddr";
const char *MEMPOOL="mempool";
const char *PING="ping";
const char *PONG="pong";
const char *ALERT="alert";
const char *NOTFOUND="notfound";
const char *FILTERLOAD="filterload";
const char *FILTERADD="filteradd";
Expand Down Expand Up @@ -61,6 +62,7 @@ const static std::string allNetMessageTypes[] = {
NetMsgType::MEMPOOL,
NetMsgType::PING,
NetMsgType::PONG,
NetMsgType::ALERT,
NetMsgType::NOTFOUND,
NetMsgType::FILTERLOAD,
NetMsgType::FILTERADD,
Expand Down
7 changes: 7 additions & 0 deletions src/protocol.h
Expand Up @@ -157,6 +157,13 @@ extern const char *PING;
* @see https://bitcoin.org/en/developer-reference#pong
*/
extern const char *PONG;
/**
* The alert message warns nodes of problems that may affect them or the rest
* of the network.
* @since protocol version 311.
* @see https://bitcoin.org/en/developer-reference#alert
*/
extern const char *ALERT;
/**
* The notfound message is a reply to a getdata message which requested an
* object the receiving node does not have available for relay.
Expand Down
29 changes: 23 additions & 6 deletions src/qt/clientmodel.cpp
Expand Up @@ -9,6 +9,7 @@
#include <qt/guiutil.h>
#include <qt/peertablemodel.h>

#include <alert.h>
#include <chain.h>
#include <chainparams.h>
#include <checkpoints.h>
Expand All @@ -20,6 +21,8 @@
#include <util.h>
#include <warnings.h>

#include <uint256.h>

#include <stdint.h>

#include <QDebug>
Expand Down Expand Up @@ -164,8 +167,20 @@ void ClientModel::updateNetworkActive(bool networkActive)
Q_EMIT networkActiveChanged(networkActive);
}

void ClientModel::updateAlert()
void ClientModel::updateAlert(const QString &hash, int status)
{
// Show error message notification for new alert
if(status == CT_NEW)
{
uint256 hash_256;
hash_256.SetHex(hash.toStdString());
CAlert alert = CAlert::getAlertByHash(hash_256);
if(!alert.IsNull())
{
Q_EMIT message(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), CClientUIInterface::ICON_ERROR);
}
}

Q_EMIT alertsChanged(getStatusBarWarnings());
}

Expand Down Expand Up @@ -273,10 +288,12 @@ static void NotifyNetworkActiveChanged(ClientModel *clientmodel, bool networkAct
Q_ARG(bool, networkActive));
}

static void NotifyAlertChanged(ClientModel *clientmodel)
static void NotifyAlertChanged(ClientModel *clientmodel, const uint256 &hash, ChangeType status)
{
qDebug() << "NotifyAlertChanged";
QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection);
qDebug() << "NotifyAlertChanged: " + QString::fromStdString(hash.GetHex()) + " status=" + QString::number(status);
QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection,
Q_ARG(QString, QString::fromStdString(hash.GetHex())),
Q_ARG(int, status));
}

static void BannedListChanged(ClientModel *clientmodel)
Expand Down Expand Up @@ -319,7 +336,7 @@ void ClientModel::subscribeToCoreSignals()
uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2));
uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, _1));
uiInterface.NotifyNetworkActiveChanged.connect(boost::bind(NotifyNetworkActiveChanged, this, _1));
uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this));
uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this, _1, _2));
uiInterface.BannedListChanged.connect(boost::bind(BannedListChanged, this));
uiInterface.NotifyBlockTip.connect(boost::bind(BlockTipChanged, this, _1, _2, false));
uiInterface.NotifyHeaderTip.connect(boost::bind(BlockTipChanged, this, _1, _2, true));
Expand All @@ -331,7 +348,7 @@ void ClientModel::unsubscribeFromCoreSignals()
uiInterface.ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2));
uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1));
uiInterface.NotifyNetworkActiveChanged.disconnect(boost::bind(NotifyNetworkActiveChanged, this, _1));
uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this));
uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this, _1, _2));
uiInterface.BannedListChanged.disconnect(boost::bind(BannedListChanged, this));
uiInterface.NotifyBlockTip.disconnect(boost::bind(BlockTipChanged, this, _1, _2, false));
uiInterface.NotifyHeaderTip.disconnect(boost::bind(BlockTipChanged, this, _1, _2, true));
Expand Down
2 changes: 1 addition & 1 deletion src/qt/clientmodel.h
Expand Up @@ -112,7 +112,7 @@ public Q_SLOTS:
void updateTimer();
void updateNumConnections(int numConnections);
void updateNetworkActive(bool networkActive);
void updateAlert();
void updateAlert(const QString &hash, int status);
void updateBanlist();
};

Expand Down
2 changes: 1 addition & 1 deletion src/qt/mintingtablemodel.cpp
Expand Up @@ -226,7 +226,7 @@ class MintingTablePriv
std::map<uint256, CWalletTx>::iterator mi = wallet->mapWallet.find(rec->hash);
if(mi != wallet->mapWallet.end())
{
return TransactionDesc::toHTML(wallet, mi->second, nullptr, 0); //ppcTODO - fix the last 2 parameters
return TransactionDesc::toHTML(wallet, mi->second, nullptr, BitcoinUnits::BTC); //ppcTODO - fix the last 2 parameters
}
}
return QString("");
Expand Down
7 changes: 7 additions & 0 deletions src/rpc/client.cpp
Expand Up @@ -135,6 +135,13 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "echojson", 9, "arg9" },
{ "rescanblockchain", 0, "start_height"},
{ "rescanblockchain", 1, "stop_height"},

// ppcoin:
{ "sendalert", 2, "minver"},
{ "sendalert", 3, "maxver"},
{ "sendalert", 4, "priority"},
{ "sendalert", 5, "id"},
{ "sendalert", 6, "cancelupto"},
};

class CRPCConvertTable
Expand Down

0 comments on commit 818a646

Please sign in to comment.