Skip to content

Commit

Permalink
Chainparams: Decouple CAlert from CChainParams
Browse files Browse the repository at this point in the history
  • Loading branch information
jtimon committed Apr 4, 2015
1 parent 2dc679d commit f14e687
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
9 changes: 4 additions & 5 deletions src/alert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "alert.h"

#include "chainparams.h"
#include "clientversion.h"
#include "net.h"
#include "pubkey.h"
Expand Down Expand Up @@ -145,9 +144,9 @@ bool CAlert::RelayTo(CNode* pnode) const
return false;
}

bool CAlert::CheckSignature() const
bool CAlert::CheckSignature(const std::vector<unsigned char>& alertKey) const
{
CPubKey key(Params().AlertKey());
CPubKey key(alertKey);
if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig))
return error("CAlert::CheckSignature(): verify signature failed");

Expand All @@ -169,9 +168,9 @@ CAlert CAlert::getAlertByHash(const uint256 &hash)
return retval;
}

bool CAlert::ProcessAlert(bool fThread)
bool CAlert::ProcessAlert(const std::vector<unsigned char>& alertKey, bool fThread)
{
if (!CheckSignature())
if (!CheckSignature(alertKey))
return false;
if (!IsInEffect())
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/alert.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ class CAlert : public CUnsignedAlert
bool AppliesTo(int nVersion, std::string strSubVerIn) const;
bool AppliesToMe() const;
bool RelayTo(CNode* pnode) const;
bool CheckSignature() const;
bool ProcessAlert(bool fThread = true); // fThread means run -alertnotify in a free-running thread
bool CheckSignature(const std::vector<unsigned char>& alertKey) const;
bool ProcessAlert(const std::vector<unsigned char>& alertKey, bool fThread = true); // fThread means run -alertnotify in a free-running thread
static void Notify(const std::string& strMessage, bool fThread);

/*
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4262,7 +4262,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
uint256 alertHash = alert.GetHash();
if (pfrom->setKnown.count(alertHash) == 0)
{
if (alert.ProcessAlert())
if (alert.ProcessAlert(Params().AlertKey()))
{
// Relay
pfrom->setKnown.insert(alertHash);
Expand Down
7 changes: 5 additions & 2 deletions src/test/alert_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "clientversion.h"
#include "data/alertTests.raw.h"

#include "chainparams.h"
#include "serialize.h"
#include "streams.h"
#include "util.h"
Expand Down Expand Up @@ -119,10 +120,11 @@ BOOST_FIXTURE_TEST_SUITE(Alert_tests, ReadAlerts)
BOOST_AUTO_TEST_CASE(AlertApplies)
{
SetMockTime(11);
const std::vector<unsigned char>& alertKey = Params(CBaseChainParams::MAIN).AlertKey();

BOOST_FOREACH(const CAlert& alert, alerts)
{
BOOST_CHECK(alert.CheckSignature());
BOOST_CHECK(alert.CheckSignature(alertKey));
}

BOOST_CHECK(alerts.size() >= 3);
Expand Down Expand Up @@ -159,14 +161,15 @@ BOOST_AUTO_TEST_CASE(AlertApplies)
BOOST_AUTO_TEST_CASE(AlertNotify)
{
SetMockTime(11);
const std::vector<unsigned char>& alertKey = Params(CBaseChainParams::MAIN).AlertKey();

boost::filesystem::path temp = GetTempPath() / "alertnotify.txt";
boost::filesystem::remove(temp);

mapArgs["-alertnotify"] = std::string("echo %s >> ") + temp.string();

BOOST_FOREACH(CAlert alert, alerts)
alert.ProcessAlert(false);
alert.ProcessAlert(alertKey, false);

std::vector<std::string> r = read_lines(temp);
BOOST_CHECK_EQUAL(r.size(), 4u);
Expand Down

0 comments on commit f14e687

Please sign in to comment.