Skip to content

Commit

Permalink
Round Voting initial implementation. Miner voting tested and working.…
Browse files Browse the repository at this point in the history
… Node voting implementation in progress (do not enable).
  • Loading branch information
CodeIsTheKey committed Feb 8, 2024
1 parent a5bccd4 commit 1983611
Show file tree
Hide file tree
Showing 30 changed files with 1,354 additions and 767 deletions.
7 changes: 2 additions & 5 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ BITCOIN_CORE_H = \
ui_interface.h \
undo.h \
unordered_lru_cache.h \
update/update.h \
util/bip32.h \
util/bytevectorhash.h \
util/check.h \
Expand All @@ -310,8 +311,6 @@ BITCOIN_CORE_H = \
util/validation.h \
validation.h \
validationinterface.h \
versionbits.h \
versionbitsinfo.h \
walletinitinterface.h \
wallet/bdb.h \
wallet/coincontrol.h \
Expand Down Expand Up @@ -445,7 +444,6 @@ libraptoreum_server_a_SOURCES = \
ui_interface.cpp \
validation.cpp \
validationinterface.cpp \
versionbits.cpp \
$(BITCOIN_CORE_H)

if ENABLE_WALLET
Expand Down Expand Up @@ -699,8 +697,7 @@ libraptoreum_common_a_SOURCES = \
script/descriptor.cpp \
script/sign.cpp \
script/standard.cpp \
versionbits.cpp \
versionbitsinfo.cpp \
update/update.cpp \
warnings.cpp \
$(BITCOIN_CORE_H)

Expand Down
1 change: 0 additions & 1 deletion src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ BITCOIN_TESTS =\
test/txindex_tests.cpp \
test/txvalidation_tests.cpp \
test/txvalidationcache_tests.cpp \
test/versionbits_tests.cpp \
test/uint256_tests.cpp \
test/util_tests.cpp

Expand Down
231 changes: 112 additions & 119 deletions src/chainparams.cpp

Large diffs are not rendered by default.

29 changes: 0 additions & 29 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,6 @@

namespace Consensus {

enum DeploymentPos {
DEPLOYMENT_TESTDUMMY,
DEPLOYMENT_V17, // Hard Fork v17

// NOTE: Also add new deployments to VersionBitsDeploymentInfo in versionbits.cpp
MAX_VERSION_BITS_DEPLOYMENTS
};

/**
* Struct for each individual consensus rule change using BIP9.
*/
struct BIP9Deployment {
/** Bit position to select the particular bit in nVersion. */
int bit;
/** Start MedianTime for version bits miner confirmation. Can be a date in the past */
int64_t nStartTime;
/** Timeout/expiry MedianTime for the deployment attempt. */
int64_t nTimeout;
/** The number of past blocks (including the block under consideration) to be taken into account for locking in a fork. */
int64_t nWindowSize{0};
/** A starting number of blocks, in the range of 1..nWindowSize, which must signal for a fork in order to lock it in. */
int64_t nThresholdStart{0};
/** A minimum number of blocks, in the range of 1..nWindowSize, which must signal for a fork in order to lock it in. */
int64_t nThresholdMin{0};
/** A coefficient which adjusts the speed a required number of signaling blocks is decreasing from nThresholdStart to nThresholdMin at with each period. */
int64_t nFalloffCoeff{0};
};

/**
* future fee share for smartnode, miner and dev
*/
Expand Down Expand Up @@ -101,7 +73,6 @@ namespace Consensus {
uint32_t nRuleChangeActivationThreshold;
// Default BIP9Deployment::nWindowSize value for deployments where it's not specified and for unknown deployments.
uint32_t nMinerConfirmationWindow;
BIP9Deployment vDeployments[MAX_VERSION_BITS_DEPLOYMENTS];
/** Proof of work parameters */
uint256 powLimit;
bool fPowAllowMinDifficultyBlocks;
Expand Down
43 changes: 21 additions & 22 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,26 @@ bool AppInitMain(const util::Ref &context, NodeContext &node, interfaces::BlockA
return InitError(_("Failed to load sporks cache from") + "\n" + (GetDataDir() / "sporks.dat").string());
}

// ********************************************************* Step 7b: load block chain
// ********************************************************* Step 7b: load powcache.dat
{
fs::path pathDB = GetDataDir();
std::string strDBName = "powcache.dat";

LOCK(cs_pow);
// Always load the powcache if available:
uiInterface.InitMessage(_("Loading POW cache..."));
fs::path powCacheFile = pathDB / strDBName;
if (!fs::exists(powCacheFile)) {
uiInterface.InitMessage("Loading POW cache for the first time. This could take a minute...");
}

CFlatDB <CPowCache> flatdb7(strDBName, "powCache");
if (!flatdb7.Load(CPowCache::Instance())) {
return InitError(_("Failed to load POW cache from") + "\n" + (pathDB / strDBName).string());
}
}

// ********************************************************* Step 7c: load block chain

fReindex = gArgs.GetBoolArg("-reindex", false);
bool fReindexChainState = gArgs.GetBoolArg("-reindex-chainstate", false);
Expand Down Expand Up @@ -2419,27 +2438,7 @@ bool AppInitMain(const util::Ref &context, NodeContext &node, interfaces::BlockA
::feeEstimator.Read(est_filein);
fFeeEstimatesInitialized = true;

// ********************************************************* Step 8a: load powcache.dat

{
fs::path pathDB = GetDataDir();
std::string strDBName = "powcache.dat";

LOCK(cs_pow);
// Always load the powcache if available:
uiInterface.InitMessage(_("Loading POW cache..."));
fs::path powCacheFile = pathDB / strDBName;
if (!fs::exists(powCacheFile)) {
uiInterface.InitMessage("Loading POW cache for the first time. This could take a minute...");
}

CFlatDB <CPowCache> flatdb7(strDBName, "powCache");
if (!flatdb7.Load(CPowCache::Instance())) {
return InitError(_("Failed to load POW cache from") + "\n" + (pathDB / strDBName).string());
}
}

// ********************************************************* Step 8b: start indexers
// ********************************************************* Step 8: start indexers
if (gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
g_txindex = MakeUnique<TxIndex>(nTxIndexCache, false, fReindex);
g_txindex->Start();
Expand Down
3 changes: 2 additions & 1 deletion src/llmq/quorums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,9 @@ namespace llmq {
std::vector <uint16_t> memberIndexes;
std::vector <BLSVerificationVectorPtr> vvecs;
BLSSecretKeyVector skContributions;
Consensus::CQuorumUpdateVoteVec updateVoteVec;
if (!dkgManager.GetVerifiedContributions((Consensus::LLMQType) fqc->llmqType, quorum->m_quorum_base_block_index,
fqc->validMembers, memberIndexes, vvecs, skContributions)) {
fqc->validMembers, memberIndexes, vvecs, skContributions, updateVoteVec)) {
return false;
}

Expand Down
1 change: 1 addition & 0 deletions src/llmq/quorums_blockprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <llmq/quorums_utils.h>

#include <primitives/block.h>
#include <unordered_map>
#include <unordered_lru_cache.h>
#include <saltedhasher.h>
Expand Down
11 changes: 9 additions & 2 deletions src/llmq/quorums_commitment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <logging.h>
#include <spork.h>
#include <validation.h>
#include <update/update.h>

bool IsBlsSigCheckEnabled(int64_t blockTime) {
int64_t activeTime = sporkManager.GetSporkValue(SPORK_17_QUORUM_DKG_ENABLED);
Expand Down Expand Up @@ -86,8 +87,14 @@ namespace llmq {

// sigs are only checked when the block is processed
if (checkSigs && IsBlsSigCheckEnabled(pQuorumBaseBlockIndex->GetBlockTime())) {
uint256 commitmentHash = CLLMQUtils::BuildCommitmentHash(llmq_params.type, quorumHash, validMembers,
quorumPublicKey, quorumVvecHash);

uint256 commitmentHash;
if (UpdateManager::Instance().IsActive(EUpdate::ROUND_VOTING, pQuorumBaseBlockIndex)) {
commitmentHash = CLLMQUtils::BuildCommitmentHash(llmqType, quorumHash, validMembers, quorumUpdateVotes, quorumPublicKey, quorumVvecHash);
}
else {
commitmentHash = CLLMQUtils::BuildCommitmentHash(llmq_params.type, quorumHash, validMembers, quorumPublicKey, quorumVvecHash);
}

std::vector <CBLSPublicKey> memberPubKeys;
for (size_t i = 0; i < members.size(); i++) {
Expand Down
25 changes: 19 additions & 6 deletions src/llmq/quorums_commitment.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace llmq {
uint256 quorumHash;
std::vector<bool> signers;
std::vector<bool> validMembers;
Consensus::CQuorumUpdateVoteVec quorumUpdateVotes;

CBLSPublicKey quorumPublicKey;
uint256 quorumVvecHash;
Expand All @@ -51,11 +52,14 @@ namespace llmq {

bool VerifySizes(const Consensus::LLMQParams &params) const;

SERIALIZE_METHODS(CFinalCommitment, obj
)
SERIALIZE_METHODS(CFinalCommitment, obj)
{
READWRITE(obj.nVersion, obj.llmqType, obj.quorumHash, DYNBITSET(obj.signers), DYNBITSET(obj.validMembers),
obj.quorumPublicKey, obj.quorumVvecHash, obj.quorumSig, obj.membersSig);
READWRITE(obj.nVersion, obj.llmqType, obj.quorumHash, DYNBITSET(obj.signers), DYNBITSET(obj.validMembers));
if (obj.nVersion > 1)
{
READWRITE(obj.quorumUpdateVotes);
}
READWRITE(obj.quorumPublicKey, obj.quorumVvecHash, obj.quorumSig, obj.membersSig);
}

bool IsNull() const {
Expand Down Expand Up @@ -83,6 +87,16 @@ namespace llmq {
obj.pushKV("validMembers", CLLMQUtils::ToHexStr(validMembers));
obj.pushKV("quorumPublicKey", quorumPublicKey.ToString());
obj.pushKV("quorumVvecHash", quorumVvecHash.ToString());
if (quorumUpdateVotes.size() > 0) {
UniValue votes(UniValue::VARR);
for (const auto& vote: quorumUpdateVotes) {
UniValue eObj(UniValue::VOBJ);
eObj.pushKV("bit", vote.bit);
eObj.pushKV("votes", vote.votes);
votes.push_back(eObj);
}
obj.pushKV("updateVotes", votes);
}
obj.pushKV("quorumSig", quorumSig.ToString());
obj.pushKV("membersSig", membersSig.ToString());
}
Expand All @@ -98,8 +112,7 @@ namespace llmq {
uint32_t nHeight{(uint32_t) - 1};
CFinalCommitment commitment;

SERIALIZE_METHODS(CFinalCommitmentTxPayload, obj
)
SERIALIZE_METHODS(CFinalCommitmentTxPayload, obj)
{
READWRITE(obj.nVersion, obj.nHeight, obj.commitment);
}
Expand Down
46 changes: 40 additions & 6 deletions src/llmq/quorums_dkgsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <netmessagemaker.h>
#include <univalue.h>
#include <validation.h>
#include <update/update.h>

#include <cxxtimer.hpp>
#include <memory>
Expand Down Expand Up @@ -72,6 +73,7 @@ namespace llmq {
members.resize(mns.size());
memberIds.resize(members.size());
receivedVvecs.resize(members.size());
receivedVersions.resize(members.size());
receivedSkContributions.resize(members.size());
vecEncryptedContributions.resize(members.size());

Expand Down Expand Up @@ -147,6 +149,10 @@ namespace llmq {
}

CDKGContribution qc;
if (UpdateManager::Instance().IsActive(EUpdate::ROUND_VOTING, m_quorum_base_block_index)) {
qc.nVersion = 1;
// TODO: JB Vote on active update proposals
}
qc.llmqType = params.type;
qc.quorumHash = m_quorum_base_block_index->GetBlockHash();
qc.proTxHash = myProTxHash;
Expand Down Expand Up @@ -277,6 +283,7 @@ namespace llmq {
}

receivedVvecs[member->idx] = qc.vvec;
receivedVersions[member->idx] = qc.nVersion;

int receivedCount = ranges::count_if(members, [](const auto &m) { return !m->contributions.empty(); });

Expand Down Expand Up @@ -946,7 +953,7 @@ namespace llmq {
std::vector <BLSVerificationVectorPtr> vvecs;
BLSSecretKeyVector skContributions;
if (!dkgManager.GetVerifiedContributions(params.type, m_quorum_base_block_index, qc.validMembers, memberIndexes,
vvecs, skContributions)) {
vvecs, skContributions, qc.quorumUpdateVotes)) {
logger.Batch("failed to get valid contributions");
return;
}
Expand Down Expand Up @@ -986,8 +993,13 @@ namespace llmq {
(*qc.quorumVvecHash.begin())++;
}

uint256 commitmentHash = CLLMQUtils::BuildCommitmentHash(qc.llmqType, qc.quorumHash, qc.validMembers,
qc.quorumPublicKey, qc.quorumVvecHash);
uint256 commitmentHash;
if (UpdateManager::Instance().IsActive(EUpdate::ROUND_VOTING, m_quorum_base_block_index)) {
commitmentHash = CLLMQUtils::BuildCommitmentHash(qc.llmqType, qc.quorumHash, qc.validMembers, qc.quorumUpdateVotes, qc.quorumPublicKey, qc.quorumVvecHash);
}
else {
commitmentHash = CLLMQUtils::BuildCommitmentHash(qc.llmqType, qc.quorumHash, qc.validMembers, qc.quorumPublicKey, qc.quorumVvecHash);
}

if (lieType == 2) {
(*commitmentHash.begin())++;
Expand Down Expand Up @@ -1053,6 +1065,21 @@ namespace llmq {
retBan = true;
return false;
}

Consensus::CQuorumUpdateVoteVec quorumUpdateVotes;
for (size_t i = 0; i < members.size(); ++i) {
if (qc.validMembers[i]) {
uint32_t version = receivedVersions[i];
quorumUpdateVotes.AddVotes(version);
}
}

if (quorumUpdateVotes != qc.quorumUpdateVotes) {
logger.Batch("Inconsistent update vote counts");
retBan = true;
return false;
}

if (!qc.sig.IsValid()) {
logger.Batch("invalid membersSig");
retBan = true;
Expand Down Expand Up @@ -1110,8 +1137,9 @@ namespace llmq {
std::vector <BLSVerificationVectorPtr> vvecs;
BLSSecretKeyVector skContributions;
BLSVerificationVectorPtr quorumVvec;
Consensus::CQuorumUpdateVoteVec updateVoteVec;
if (dkgManager.GetVerifiedContributions(params.type, m_quorum_base_block_index, qc.validMembers, memberIndexes,
vvecs, skContributions)) {
vvecs, skContributions, updateVoteVec)) {
quorumVvec = cache.BuildQuorumVerificationVector(::SerializeHash(memberIndexes), vvecs);
}

Expand Down Expand Up @@ -1214,9 +1242,15 @@ namespace llmq {
fqc.validMembers = first.validMembers;
fqc.quorumPublicKey = first.quorumPublicKey;
fqc.quorumVvecHash = first.quorumVvecHash;
fqc.quorumUpdateVotes = first.quorumUpdateVotes;

uint256 commitmentHash = CLLMQUtils::BuildCommitmentHash(fqc.llmqType, fqc.quorumHash, fqc.validMembers,
fqc.quorumPublicKey, fqc.quorumVvecHash);
uint256 commitmentHash;
if (UpdateManager::Instance().IsActive(EUpdate::ROUND_VOTING, m_quorum_base_block_index)) {
commitmentHash = CLLMQUtils::BuildCommitmentHash(fqc.llmqType, fqc.quorumHash, fqc.validMembers, fqc.quorumUpdateVotes, fqc.quorumPublicKey, fqc.quorumVvecHash);
}
else {
commitmentHash = CLLMQUtils::BuildCommitmentHash(fqc.llmqType, fqc.quorumHash, fqc.validMembers, fqc.quorumPublicKey, fqc.quorumVvecHash);
}

std::vector <CBLSSignature> aggSigs;
std::vector <CBLSPublicKey> aggPks;
Expand Down
Loading

0 comments on commit 1983611

Please sign in to comment.