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

Change format of gobject, store/transmit vchData instead of hex-encoded string of a string #1934

Merged
merged 2 commits into from
Feb 15, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/governance-classes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,13 @@ void CGovernanceTriggerManager::CleanAndRemove()

if(remove) {
DBG(
string strdata = "NULL";
string strDataAsPlainString = "NULL";
CGovernanceObject* pgovobj = pSuperblock->GetGovernanceObject();
if(pgovobj) {
strdata = pgovobj->GetDataAsString();
strDataAsPlainString = pgovobj->GetDataAsPlainString();
}
cout << "CGovernanceTriggerManager::CleanAndRemove: Removing object: "
<< strdata
<< strDataAsPlainString
<< endl;
);
LogPrint("gobject", "CGovernanceTriggerManager::CleanAndRemove -- Removing trigger object\n");
Expand Down Expand Up @@ -245,7 +245,7 @@ std::vector<CSuperblock_sptr> CGovernanceTriggerManager::GetActiveTriggers()
CGovernanceObject* pObj = governance.FindGovernanceObject((*it).first);

if(pObj) {
DBG( cout << "GetActiveTriggers: pObj->GetDataAsString() = " << pObj->GetDataAsString() << endl; );
DBG( cout << "GetActiveTriggers: pObj->GetDataAsPlainString() = " << pObj->GetDataAsPlainString() << endl; );
vecResults.push_back(it->second);
}
++it;
Expand Down Expand Up @@ -293,7 +293,7 @@ bool CSuperblockManager::IsSuperblockTriggered(int nBlockHeight)
continue;
}

LogPrint("gobject", "CSuperblockManager::IsSuperblockTriggered -- data = %s\n", pObj->GetDataAsString());
LogPrint("gobject", "CSuperblockManager::IsSuperblockTriggered -- data = %s\n", pObj->GetDataAsPlainString());

// note : 12.1 - is epoch calculation correct?

Expand Down Expand Up @@ -482,7 +482,7 @@ CSuperblock(uint256& nHash)
}

DBG( cout << "CSuperblock Constructor pGovObj : "
<< pGovObj->GetDataAsString()
<< pGovObj->GetDataAsPlainString()
<< ", nObjectType = " << pGovObj->GetObjectType()
<< endl; );

Expand Down Expand Up @@ -673,8 +673,8 @@ bool CSuperblock::IsValid(const CTransaction& txNew, int nBlockHeight, CAmount b
int nPayments = CountPayments();
int nMinerPayments = nOutputs - nPayments;

LogPrint("gobject", "CSuperblock::IsValid nOutputs = %d, nPayments = %d, strData = %s\n",
nOutputs, nPayments, GetGovernanceObject()->GetDataAsHex());
LogPrint("gobject", "CSuperblock::IsValid nOutputs = %d, nPayments = %d, GetDataAsHexString = %s\n",
nOutputs, nPayments, GetGovernanceObject()->GetDataAsHexString());

// We require an exact match (including order) between the expected
// superblock payments and the payments actually in the block.
Expand Down
51 changes: 26 additions & 25 deletions src/governance-object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CGovernanceObject::CGovernanceObject()
nTime(0),
nDeletionTime(0),
nCollateralHash(),
strData(),
vchData(),
masternodeOutpoint(),
vchSig(),
fCachedLocalValidity(false),
Expand All @@ -39,19 +39,19 @@ CGovernanceObject::CGovernanceObject()
cmmapOrphanVotes(),
fileVotes()
{
// PARSE JSON DATA STORAGE (STRDATA)
// PARSE JSON DATA STORAGE (VCHDATA)
LoadData();
}

CGovernanceObject::CGovernanceObject(const uint256& nHashParentIn, int nRevisionIn, int64_t nTimeIn, const uint256& nCollateralHashIn, const std::string& strDataIn)
CGovernanceObject::CGovernanceObject(const uint256& nHashParentIn, int nRevisionIn, int64_t nTimeIn, const uint256& nCollateralHashIn, const std::string& strDataHexIn)
: cs(),
nObjectType(GOVERNANCE_OBJECT_UNKNOWN),
nHashParent(nHashParentIn),
nRevision(nRevisionIn),
nTime(nTimeIn),
nDeletionTime(0),
nCollateralHash(nCollateralHashIn),
strData(strDataIn),
vchData(ParseHex(strDataHexIn)),
masternodeOutpoint(),
vchSig(),
fCachedLocalValidity(false),
Expand All @@ -67,7 +67,7 @@ CGovernanceObject::CGovernanceObject(const uint256& nHashParentIn, int nRevision
cmmapOrphanVotes(),
fileVotes()
{
// PARSE JSON DATA STORAGE (STRDATA)
// PARSE JSON DATA STORAGE (VCHDATA)
LoadData();
}

Expand All @@ -79,7 +79,7 @@ CGovernanceObject::CGovernanceObject(const CGovernanceObject& other)
nTime(other.nTime),
nDeletionTime(other.nDeletionTime),
nCollateralHash(other.nCollateralHash),
strData(other.strData),
vchData(other.vchData),
masternodeOutpoint(other.masternodeOutpoint),
vchSig(other.vchSig),
fCachedLocalValidity(other.fCachedLocalValidity),
Expand Down Expand Up @@ -217,7 +217,7 @@ std::string CGovernanceObject::GetSignatureMessage() const
std::string strMessage = nHashParent.ToString() + "|" +
boost::lexical_cast<std::string>(nRevision) + "|" +
boost::lexical_cast<std::string>(nTime) + "|" +
strData + "|" +
GetDataAsHexString() + "|" +
masternodeOutpoint.ToStringShort() + "|" +
nCollateralHash.ToString();

Expand Down Expand Up @@ -276,25 +276,25 @@ uint256 CGovernanceObject::GetHash() const
ss << nHashParent;
ss << nRevision;
ss << nTime;
ss << strData;
ss << GetDataAsHexString();
ss << masternodeOutpoint << uint8_t{} << 0xffffffff;
ss << vchSig;
// fee_tx is left out on purpose

DBG( printf("CGovernanceObject::GetHash %i %li %s\n", nRevision, nTime, strData.c_str()); );
DBG( printf("CGovernanceObject::GetHash %i %li %s\n", nRevision, nTime, GetDataAsHexString().c_str()); );

return ss.GetHash();
}

/**
Return the actual object from the strData JSON structure.
Return the actual object from the vchData JSON structure.

Returns an empty object on error.
*/
UniValue CGovernanceObject::GetJSONObject()
{
UniValue obj(UniValue::VOBJ);
if(strData.empty()) {
if(vchData.empty()) {
return obj;
}

Expand All @@ -312,7 +312,7 @@ UniValue CGovernanceObject::GetJSONObject()
* LoadData
* --------------------------------------------------------
*
* Attempt to load data from strData
* Attempt to load data from vchData
*
*/

Expand All @@ -321,17 +321,17 @@ void CGovernanceObject::LoadData()
// todo : 12.1 - resolved
//return;

if(strData.empty()) {
if(vchData.empty()) {
return;
}

try {
// ATTEMPT TO LOAD JSON STRING FROM STRDATA
// ATTEMPT TO LOAD JSON STRING FROM VCHDATA
UniValue objResult(UniValue::VOBJ);
GetData(objResult);

DBG( cout << "CGovernanceObject::LoadData strData = "
<< GetDataAsString()
DBG( cout << "CGovernanceObject::LoadData GetDataAsPlainString = "
<< GetDataAsPlainString()
<< endl; );

UniValue obj = GetJSONObject();
Expand Down Expand Up @@ -367,7 +367,7 @@ void CGovernanceObject::LoadData()
void CGovernanceObject::GetData(UniValue& objResult)
{
UniValue o(UniValue::VOBJ);
std::string s = GetDataAsString();
std::string s = GetDataAsPlainString();
o.read(s);
objResult = o;
}
Expand All @@ -378,17 +378,14 @@ void CGovernanceObject::GetData(UniValue& objResult)
*
*/

std::string CGovernanceObject::GetDataAsHex()
std::string CGovernanceObject::GetDataAsHexString() const
{
return strData;
return HexStr(vchData);
}

std::string CGovernanceObject::GetDataAsString()
std::string CGovernanceObject::GetDataAsPlainString() const
{
std::vector<unsigned char> v = ParseHex(strData);
std::string s(v.begin(), v.end());

return s;
return std::string(vchData.begin(), vchData.end());
}

void CGovernanceObject::UpdateLocalValidity()
Expand Down Expand Up @@ -421,6 +418,10 @@ bool CGovernanceObject::IsValidLocally(std::string& strError, bool& fMissingMast
case GOVERNANCE_OBJECT_PROPOSAL:
case GOVERNANCE_OBJECT_TRIGGER:
case GOVERNANCE_OBJECT_WATCHDOG:
if (vchData.size() > MAX_GOVERNANCE_OBJECT_DATA_SIZE) {
strError = strprintf("Invalid object size %d", vchData.size());
return false;
}
break;
default:
strError = strprintf("Invalid object type %d", nObjectType);
Expand Down Expand Up @@ -704,7 +705,7 @@ void CGovernanceObject::swap(CGovernanceObject& first, CGovernanceObject& second
swap(first.nTime, second.nTime);
swap(first.nDeletionTime, second.nDeletionTime);
swap(first.nCollateralHash, second.nCollateralHash);
swap(first.strData, second.strData);
swap(first.vchData, second.vchData);
swap(first.nObjectType, second.nObjectType);

// swap all cached valid flags
Expand Down
24 changes: 19 additions & 5 deletions src/governance-object.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "net.h"
#include "sync.h"
#include "util.h"
#include "utilstrencodings.h"

#include <univalue.h>

Expand Down Expand Up @@ -145,7 +146,7 @@ class CGovernanceObject
uint256 nCollateralHash;

/// Data field - can be used for anything
std::string strData;
std::vector<unsigned char> vchData;

/// Masternode info for signed objects
COutPoint masternodeOutpoint;
Expand Down Expand Up @@ -190,7 +191,7 @@ class CGovernanceObject
public:
CGovernanceObject();

CGovernanceObject(const uint256& nHashParentIn, int nRevisionIn, int64_t nTime, const uint256& nCollateralHashIn, const std::string& strDataIn);
CGovernanceObject(const uint256& nHashParentIn, int nRevisionIn, int64_t nTime, const uint256& nCollateralHashIn, const std::string& strDataHexIn);

CGovernanceObject(const CGovernanceObject& other);

Expand Down Expand Up @@ -293,8 +294,8 @@ class CGovernanceObject

// FUNCTIONS FOR DEALING WITH DATA STRING

std::string GetDataAsHex();
std::string GetDataAsString();
std::string GetDataAsHexString() const;
std::string GetDataAsPlainString() const;

// SERIALIZER

Expand All @@ -309,7 +310,20 @@ class CGovernanceObject
READWRITE(nRevision);
READWRITE(nTime);
READWRITE(nCollateralHash);
READWRITE(LIMITED_STRING(strData, MAX_GOVERNANCE_OBJECT_DATA_SIZE));
if (nVersion == 70208) {
// converting from/to old format
std::string strDataHex;
if (ser_action.ForRead()) {
READWRITE(LIMITED_STRING(strDataHex, MAX_GOVERNANCE_OBJECT_DATA_SIZE));
vchData = ParseHex(strDataHex);
} else {
strDataHex = HexStr(vchData);
READWRITE(LIMITED_STRING(strDataHex, MAX_GOVERNANCE_OBJECT_DATA_SIZE));
}
} else {
// using new format directly
READWRITE(vchData);
}
READWRITE(nObjectType);
if (nVersion == 70208) {
// converting from/to old format
Expand Down
10 changes: 5 additions & 5 deletions src/governance-validators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <algorithm>

CProposalValidator::CProposalValidator(const std::string& strDataHexIn)
: strData(),
: strDataHex(),
objJSON(UniValue::VOBJ),
fJSONValid(false),
strErrorMessages()
Expand All @@ -22,7 +22,7 @@ CProposalValidator::CProposalValidator(const std::string& strDataHexIn)

void CProposalValidator::Clear()
{
strData = std::string();
strDataHex = std::string();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a nit: There is .clear() on std::string

objJSON = UniValue(UniValue::VOBJ);
fJSONValid = false;
strErrorMessages = std::string();
Expand All @@ -31,7 +31,7 @@ void CProposalValidator::Clear()
void CProposalValidator::SetHexData(const std::string& strDataHexIn)
{
std::vector<unsigned char> v = ParseHex(strDataHexIn);
strData = std::string(v.begin(), v.end());
strDataHex = std::string(v.begin(), v.end());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a nit: There is .assign(begin, end) on std::string

ParseJSONData();
}

Expand Down Expand Up @@ -195,13 +195,13 @@ void CProposalValidator::ParseJSONData()
{
fJSONValid = false;

if(strData.empty()) {
if(strDataHex.empty()) {
return;
}

try {
UniValue obj(UniValue::VOBJ);
obj.read(strData);
obj.read(strDataHex);
std::vector<UniValue> arr1 = obj.getValues();
std::vector<UniValue> arr2 = arr1.at(0).getValues();
objJSON = arr2.at(1);
Expand Down
2 changes: 1 addition & 1 deletion src/governance-validators.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class CProposalValidator {
static bool CheckURL(const std::string& strURLIn);

private:
std::string strData;
std::string strDataHex;

UniValue objJSON;

Expand Down
4 changes: 2 additions & 2 deletions src/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ void CGovernanceManager::AddGovernanceObject(CGovernanceObject& govobj, CConnman

// SHOULD WE ADD THIS OBJECT TO ANY OTHER MANANGERS?

DBG( cout << "CGovernanceManager::AddGovernanceObject Before trigger block, strData = "
<< govobj.GetDataAsString()
DBG( cout << "CGovernanceManager::AddGovernanceObject Before trigger block, GetDataAsPlainString = "
<< govobj.GetDataAsPlainString()
<< ", nObjectType = " << govobj.nObjectType
<< endl; );

Expand Down
Loading