Skip to content

Commit

Permalink
extract sporkmanager from sporkmessage (#2234)
Browse files Browse the repository at this point in the history
The SporkMessage is a lower-level construct which shouldn't be aware of
SporkManager. This commit moves the `sporkManager.IsSporkActive()` calls
outside the SporkMessage functions. This follows the dependency injection
pattern and makes it easier to test SporkMessage as well.
  • Loading branch information
nmarley authored and UdjinM6 committed Aug 22, 2018
1 parent 8da88ec commit 73c2ddd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/spork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void CSporkManager::ProcessSpork(CNode* pfrom, const std::string& strCommand, CD
}
}

if(!spork.CheckSignature(sporkPubKeyID)) {
if(!spork.CheckSignature(sporkPubKeyID, IsSporkActive(SPORK_6_NEW_SIGS))) {
LOCK(cs_main);
LogPrintf("CSporkManager::ProcessSpork -- ERROR: invalid signature\n");
Misbehaving(pfrom->GetId(), 100);
Expand Down Expand Up @@ -127,7 +127,7 @@ bool CSporkManager::UpdateSpork(int nSporkID, int64_t nValue, CConnman& connman)
{
CSporkMessage spork = CSporkMessage(nSporkID, nValue, GetAdjustedTime());

if(spork.Sign(sporkPrivKey)) {
if(spork.Sign(sporkPrivKey, IsSporkActive(SPORK_6_NEW_SIGS))) {
spork.Relay(connman);
LOCK(cs);
mapSporksByHash[spork.GetHash()] = spork;
Expand Down Expand Up @@ -244,7 +244,7 @@ bool CSporkManager::SetPrivKey(const std::string& strPrivKey)
}

CSporkMessage spork;
if (spork.Sign(key)) {
if (spork.Sign(key, IsSporkActive(SPORK_6_NEW_SIGS))) {
LOCK(cs);
// Test signing successful, proceed
LogPrintf("CSporkManager::SetPrivKey -- Successfully initialized as spork signer\n");
Expand Down Expand Up @@ -274,7 +274,7 @@ uint256 CSporkMessage::GetSignatureHash() const
return GetHash();
}

bool CSporkMessage::Sign(const CKey& key)
bool CSporkMessage::Sign(const CKey& key, bool fSporkSixActive)
{
if (!key.IsValid()) {
LogPrintf("CSporkMessage::Sign -- signing key is not valid\n");
Expand All @@ -284,7 +284,7 @@ bool CSporkMessage::Sign(const CKey& key)
CKeyID pubKeyId = key.GetPubKey().GetID();
std::string strError = "";

if (sporkManager.IsSporkActive(SPORK_6_NEW_SIGS)) {
if (fSporkSixActive) {
uint256 hash = GetSignatureHash();

if(!CHashSigner::SignHash(hash, key, vchSig)) {
Expand Down Expand Up @@ -313,11 +313,11 @@ bool CSporkMessage::Sign(const CKey& key)
return true;
}

bool CSporkMessage::CheckSignature(const CKeyID& pubKeyId) const
bool CSporkMessage::CheckSignature(const CKeyID& pubKeyId, bool fSporkSixActive) const
{
std::string strError = "";

if (sporkManager.IsSporkActive(SPORK_6_NEW_SIGS)) {
if (fSporkSixActive) {
uint256 hash = GetSignatureHash();

if (!CHashSigner::VerifyHash(hash, pubKeyId, vchSig, strError)) {
Expand Down
4 changes: 2 additions & 2 deletions src/spork.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ class CSporkMessage
uint256 GetHash() const;
uint256 GetSignatureHash() const;

bool Sign(const CKey& key);
bool CheckSignature(const CKeyID& pubKeyId) const;
bool Sign(const CKey& key, bool fSporkSixActive);
bool CheckSignature(const CKeyID& pubKeyId, bool fSporkSixActive) const;
void Relay(CConnman& connman);
};

Expand Down

0 comments on commit 73c2ddd

Please sign in to comment.