Skip to content

Commit

Permalink
Merge pull request #5762 from knst/bitcoinserver-15639-p2
Browse files Browse the repository at this point in the history
refactor: pull libbitcoin_server (governance) code out of wallet code 4/N
  • Loading branch information
PastaPastaPasta authored Dec 22, 2023
2 parents 78a6904 + b2ad530 commit 9bdb746
Show file tree
Hide file tree
Showing 23 changed files with 257 additions and 167 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ BITCOIN_CORE_H = \
dsnotificationinterface.h \
governance/governance.h \
governance/classes.h \
governance/common.h \
governance/exceptions.h \
governance/object.h \
governance/validators.h \
Expand Down Expand Up @@ -702,6 +703,7 @@ libbitcoin_common_a_SOURCES = \
core_read.cpp \
core_write.cpp \
deploymentinfo.cpp \
governance/common.cpp \
key.cpp \
key_io.cpp \
merkleblock.cpp \
Expand Down
1 change: 1 addition & 0 deletions src/coinjoin/coinjoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <chain.h>
#include <chainparams.h>
#include <consensus/validation.h>
#include <governance/common.h>
#include <llmq/chainlocks.h>
#include <llmq/instantsend.h>
#include <llmq/utils.h>
Expand Down
73 changes: 73 additions & 0 deletions src/governance/common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) 2014-2023 The Dash Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <governance/common.h>

#include <util/strencodings.h>
#include <util/underlying.h>
#include <hash.h>
#include <univalue.h>

namespace Governance
{

Object::Object(const uint256& nHashParent, int nRevision, int64_t nTime, const uint256& nCollateralHash, const std::string& strDataHex) :
hashParent{nHashParent},
revision{nRevision},
time{nTime},
collateralHash{nCollateralHash},
masternodeOutpoint{},
vchSig{},
vchData{ParseHex(strDataHex)}
{
}

uint256 Object::GetHash() const
{
// Note: doesn't match serialization

// CREATE HASH OF ALL IMPORTANT PIECES OF DATA

CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
ss << hashParent;
ss << revision;
ss << time;
ss << HexStr(vchData);
ss << masternodeOutpoint << uint8_t{} << 0xffffffff; // adding dummy values here to match old hashing
ss << vchSig;
// fee_tx is left out on purpose

return ss.GetHash();
}

UniValue Object::ToJson() const
{
UniValue obj(UniValue::VOBJ);
obj.pushKV("objectHash", GetHash().ToString());
obj.pushKV("parentHash", hashParent.ToString());
obj.pushKV("collateralHash", collateralHash.ToString());
obj.pushKV("createdAt", time);
obj.pushKV("revision", revision);
UniValue data;
if (!data.read(GetDataAsPlainString())) {
data.clear();
data.setObject();
data.pushKV("plain", GetDataAsPlainString());
}
data.pushKV("hex", GetDataAsHexString());
obj.pushKV("data", data);
return obj;
}

std::string Object::GetDataAsHexString() const
{
return HexStr(vchData);
}

std::string Object::GetDataAsPlainString() const
{
return std::string(vchData.begin(), vchData.end());
}

} // namespace Governance
86 changes: 86 additions & 0 deletions src/governance/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) 2014-2023 The Dash Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_GOVERNANCE_COMMON_H
#define BITCOIN_GOVERNANCE_COMMON_H

#include <primitives/transaction.h>
#include <uint256.h>

#include <serialize.h>

#include <string>
#include <vector>

/**
* This module is a public interface of governance module that can be used
* in other components such as wallet
*/

class UniValue;

enum class GovernanceObject : int {
UNKNOWN = 0,
PROPOSAL,
TRIGGER
};
template<> struct is_serializable_enum<GovernanceObject> : std::true_type {};

namespace Governance
{
class Object
{
public:
Object() = default;

Object(const uint256& nHashParent, int nRevision, int64_t nTime, const uint256& nCollateralHash, const std::string& strDataHex);

UniValue ToJson() const;

uint256 GetHash() const;

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

/// Object typecode
GovernanceObject type{GovernanceObject::UNKNOWN};

/// parent object, 0 is root
uint256 hashParent{};

/// object revision in the system
int revision{0};

/// time this object was created
int64_t time{0};

/// fee-tx
uint256 collateralHash{};

/// Masternode info for signed objects
COutPoint masternodeOutpoint;
std::vector<unsigned char> vchSig{};

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

SERIALIZE_METHODS(Object, obj)
{
READWRITE(
obj.hashParent,
obj.revision,
obj.time,
obj.collateralHash,
obj.vchData,
obj.type,
obj.masternodeOutpoint
);
if (!(s.GetType() & SER_GETHASH)) {
READWRITE(obj.vchSig);
}
}
};

} // namespace Governance
#endif
3 changes: 2 additions & 1 deletion src/governance/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <evo/deterministicmns.h>
#include <flat-database.h>
#include <governance/classes.h>
#include <governance/common.h>
#include <governance/validators.h>
#include <masternode/meta.h>
#include <masternode/node.h>
Expand Down Expand Up @@ -333,7 +334,7 @@ void CGovernanceManager::AddGovernanceObject(CGovernanceObject& govobj, CConnman
CheckOrphanVotes(govobj, connman);

// SEND NOTIFICATION TO SCRIPT/ZMQ
GetMainSignals().NotifyGovernanceObject(std::make_shared<const CGovernanceObject>(govobj));
GetMainSignals().NotifyGovernanceObject(std::make_shared<const Governance::Object>(govobj.Object()));
}

void CGovernanceManager::UpdateCachesAndClean()
Expand Down
Loading

0 comments on commit 9bdb746

Please sign in to comment.