Skip to content

Commit

Permalink
Moved UpdateManager into ChainParams to provide better chain isolatio…
Browse files Browse the repository at this point in the history
…n. (#364)
  • Loading branch information
CodeIsTheKey authored Mar 12, 2024
1 parent db9b332 commit 06f2dd9
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 42 deletions.
22 changes: 14 additions & 8 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ class CMainParams : public CChainParams {
consensus.nAssetsForkBlock = 9999999;
consensus.nRootAssetsForkBlock = consensus.nAssetsForkBlock;

UpdateManager::Instance().Add
updateManager.Add
( // V17 voting blocks 419328-427391 in mainnet, 4032 voting, 4032 grace period, active at 427392
Update(EUpdate::DEPLOYMENT_V17, std::string("v17"), 0, 4032, 419328, 1, 3, 1, false, VoteThreshold(80, 60, 5), VoteThreshold(0, 0, 1), false, 427392)
);
// UpdateManager::Instance().Add
// updateManager.Add
// (
// Update(EUpdate::ROUND_VOTING, std::string("Round Voting"), 7, 100, 100000, 5, 10, 5, false, VoteThreshold(85, 85, 1), VoteThreshold(95, 95, 1))
// );
Expand Down Expand Up @@ -374,11 +374,11 @@ class CTestNetParams : public CChainParams {
consensus.nAssetsForkBlock = 411300;
consensus.nRootAssetsForkBlock = 9999999;

UpdateManager::Instance().Add
updateManager.Add
(
Update(EUpdate::DEPLOYMENT_V17, std::string("v17"), 0, 10, 0, 10, 100, 10, false, VoteThreshold(95, 95, 5), VoteThreshold(0, 0, 1))
);
// UpdateManager::Instance().Add
// updateManager.Add
// (
// Update(EUpdate::ROUND_VOTING, std::string("Round Voting"), 7, 100, 100000, 5, 10, 5, false, VoteThreshold(85, 85, 1), VoteThreshold(95, 95, 1))
// );
Expand Down Expand Up @@ -526,11 +526,11 @@ class CDevNetParams : public CChainParams {
consensus.nAssetsForkBlock = 1;
consensus.nRootAssetsForkBlock = 1;

UpdateManager::Instance().Add
updateManager.Add
(
Update(EUpdate::DEPLOYMENT_V17, std::string("v17"), 0, 10, 0, 10, 100, 10, false, VoteThreshold(95, 95, 5), VoteThreshold(0, 0, 1))
);
UpdateManager::Instance().Add
updateManager.Add
(
Update(EUpdate::ROUND_VOTING, std::string("Round Voting"), 1, 100, 2000, 5, 10, 5, false, VoteThreshold(85, 85, 1), VoteThreshold(0, 0, 1))
);
Expand Down Expand Up @@ -697,11 +697,11 @@ class CRegTestParams : public CChainParams {
consensus.nAssetsForkBlock = 1;
consensus.nRootAssetsForkBlock = 1;

UpdateManager::Instance().Add
updateManager.Add
(
Update(EUpdate::DEPLOYMENT_V17, std::string("v17"), 0, 10, 0, 10, 100, 10, false, VoteThreshold(95, 95, 5), VoteThreshold(0, 0, 1))
);
// UpdateManager::Instance().Add
// updateManager.Add
// (
// Update(EUpdate::ROUND_VOTING, std::string("Round Voting"), 7, 100, 100000, 5, 10, 5, false, VoteThreshold(85, 85, 1), VoteThreshold(95, 95, 1))
// );
Expand Down Expand Up @@ -965,6 +965,12 @@ const CChainParams &Params() {
return *globalChainParams;
}

UpdateManager& Updates() {
assert(globalChainParams);
return globalChainParams->Updates();
}


std::unique_ptr <CChainParams> CreateChainParams(const std::string &chain) {
if (chain == CBaseChainParams::MAIN)
return std::make_unique<CMainParams>();
Expand Down
12 changes: 10 additions & 2 deletions src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <primitives/block.h>
#include <protocol.h>
#include <chain.h>
#include <update/update.h>

#include <memory>
#include <vector>
Expand Down Expand Up @@ -121,8 +122,7 @@ class CChainParams {

void UpdateDIP3Parameters(int nActivationHeight, int nEnforcementHeight);

void
UpdateBudgetParameters(int nSmartnodePaymentsStartBlock, int nBudgetPaymentsStartBlock, int nSuperblockStartBlock);
void UpdateBudgetParameters(int nSmartnodePaymentsStartBlock, int nBudgetPaymentsStartBlock, int nSuperblockStartBlock);

void UpdateSubsidyAndDiffParams(int nMinimumDifficultyBlocks, int nHighSubsidyBlocks, int nHighSubsidyFactor);

Expand All @@ -138,6 +138,10 @@ class CChainParams {

int FulfilledRequestExpireTime() const { return nFulfilledRequestExpireTime; }

UpdateManager& Updates() { return updateManager; }

const UpdateManager& Updates() const { return updateManager; }

bool IsFutureActive(CBlockIndex *index) const {
int height = index == nullptr ? 0 : index->nHeight;
return height >= GetConsensus().nFutureForkBlock;
Expand All @@ -161,6 +165,7 @@ class CChainParams {
protected:
CChainParams() {}

UpdateManager updateManager;
Consensus::Params consensus;
CMessageHeader::MessageStartChars pchMessageStart;
int nDefaultPort;
Expand Down Expand Up @@ -208,6 +213,9 @@ std::unique_ptr <CChainParams> CreateChainParams(const std::string &chain);
*/
const CChainParams &Params();

/// Returns the UpdateManager for the currently selected chain
UpdateManager& Updates();

/**
* Sets the params returned by Params() to those for the given BIP70 chain name.
* @throws std::runtime_error when the chain is not supported.
Expand Down
2 changes: 1 addition & 1 deletion src/llmq/quorums_commitment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ namespace llmq {
if (checkSigs && IsBlsSigCheckEnabled(pQuorumBaseBlockIndex->GetBlockTime())) {

uint256 commitmentHash;
if (UpdateManager::Instance().IsActive(EUpdate::ROUND_VOTING, pQuorumBaseBlockIndex)) {
if (Updates().IsActive(EUpdate::ROUND_VOTING, pQuorumBaseBlockIndex)) {
commitmentHash = CLLMQUtils::BuildCommitmentHash(llmq_params.type, quorumHash, validMembers, quorumUpdateVotes, quorumPublicKey, quorumVvecHash);
}
else {
Expand Down
10 changes: 5 additions & 5 deletions src/llmq/quorums_dkgsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ namespace llmq {
}

CDKGContribution qc;
if (UpdateManager::Instance().IsActive(EUpdate::ROUND_VOTING, m_quorum_base_block_index)) {
if (Updates().IsActive(EUpdate::ROUND_VOTING, m_quorum_base_block_index)) {
qc.nVersion = 1;
// Vote on active update proposals
qc.nVersion = UpdateManager::Instance().ComputeBlockVersion(m_quorum_base_block_index);
qc.nVersion = Updates().ComputeBlockVersion(m_quorum_base_block_index);
}
qc.llmqType = params.type;
qc.quorumHash = m_quorum_base_block_index->GetBlockHash();
Expand Down Expand Up @@ -207,7 +207,7 @@ namespace llmq {
return false;
}

if (qc.nVersion < 2 && UpdateManager::Instance().IsActive(EUpdate::ROUND_VOTING, m_quorum_base_block_index)) {
if (qc.nVersion < 2 && Updates().IsActive(EUpdate::ROUND_VOTING, m_quorum_base_block_index)) {
logger.Batch("contribution from pre round voting node, rejecting");
return false;
}
Expand Down Expand Up @@ -1005,7 +1005,7 @@ namespace llmq {
}

uint256 commitmentHash;
if (UpdateManager::Instance().IsActive(EUpdate::ROUND_VOTING, m_quorum_base_block_index)) {
if (Updates().IsActive(EUpdate::ROUND_VOTING, m_quorum_base_block_index)) {
qc.roundVoting = true;
commitmentHash = CLLMQUtils::BuildCommitmentHash(qc.llmqType, qc.quorumHash, qc.validMembers, qc.quorumUpdateVotes, qc.quorumPublicKey, qc.quorumVvecHash);
}
Expand Down Expand Up @@ -1260,7 +1260,7 @@ namespace llmq {
}

uint256 commitmentHash;
if (UpdateManager::Instance().IsActive(EUpdate::ROUND_VOTING, m_quorum_base_block_index)) {
if (Updates().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 {
Expand Down
2 changes: 1 addition & 1 deletion src/llmq/quorums_dkgsessionmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ namespace llmq {
skContribution);

uint32_t nVersion = 0;
if (UpdateManager::Instance().IsActive(EUpdate::ROUND_VOTING, pQuorumBaseBlockIndex)) {
if (Updates().IsActive(EUpdate::ROUND_VOTING, pQuorumBaseBlockIndex)) {
db->Read(std::make_tuple(DB_NODE_VOTE, llmqType, pQuorumBaseBlockIndex->GetBlockHash(), proTxHash),
nVersion);
}
Expand Down
2 changes: 1 addition & 1 deletion src/llmq/quorums_instantsend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@ namespace llmq {

void CInstantSendManager::UpdatedBlockTip(const CBlockIndex *pindexNew) {
if (!fUpgradedDB) {
if (UpdateManager::Instance().IsActive(EUpdate::DEPLOYMENT_V17, pindexNew)) {
if (Updates().IsActive(EUpdate::DEPLOYMENT_V17, pindexNew)) {
db.Upgrade();
fUpgradedDB = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/llmq/quorums_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ namespace llmq {
break;
case Consensus::LLMQ_100_67:
case Consensus::LLMQ_TEST_V17:
if (!UpdateManager::Instance().IsActive(EUpdate::DEPLOYMENT_V17, pindex)) {
if (!Updates().IsActive(EUpdate::DEPLOYMENT_V17, pindex)) {
return false;
}
break;
Expand Down
4 changes: 2 additions & 2 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ std::unique_ptr <CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript &s
bool fDIP0003Active_context = chainparams.GetConsensus().DIP0003Enabled;
bool fDIP0008Active_context = chainparams.GetConsensus().DIP0008Enabled;

pblock->nVersion = UpdateManager::Instance().ComputeBlockVersion(pindexPrev);
pblock->nVersion = Updates().ComputeBlockVersion(pindexPrev);

// -regtest only: allow overriding block.nVersion with
// -blockversion=N to test forking scenarios
Expand Down Expand Up @@ -562,7 +562,7 @@ void static RaptoreumMiner(const CChainParams& chainparams, NodeContext& node) {
#ifdef ENABLE_WALLET
pWallet = GetFirstWallet();

// TODO: either add this function back in, or update this for more appropriate wallet functionality
// TODO: either add this function back in, or update this for more appropriate wallet functionality
// if (!EnsureWalletIsAvailable(pWallet, false)) {
// LogPrintf("RaptoreumMiner -- Wallet not available\n");
// }
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1689,10 +1689,10 @@ UniValue getblockchaininfo(const JSONRPCRequest &request) {
// BIP9SoftForkDescPushBack(bip9_softforks, consensusParams, static_cast<Consensus::DeploymentPos>(pos));
// }
for (int i = 0; i < static_cast<int>(EUpdate::MAX_VERSION_BITS_DEPLOYMENTS); ++i) {
StateInfo state = UpdateManager::Instance().State( static_cast<EUpdate>(i), ::ChainActive().Tip());
StateInfo state = Updates().State( static_cast<EUpdate>(i), ::ChainActive().Tip());
if (state.State == EUpdateState::Unknown)
continue;
const Update* update = UpdateManager::Instance().GetUpdate(static_cast<EUpdate>(i));
const Update* update = Updates().GetUpdate(static_cast<EUpdate>(i));
UniValue rv(UniValue::VOBJ);
switch (state.State) {
case EUpdateState::Defined: rv.pushKV("status", "defined"); break;
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,8 @@ static UniValue getblocktemplate(const JSONRPCRequest &request) {
UniValue aRules(UniValue::VARR);
UniValue vbavailable(UniValue::VOBJ);
for (int i = 0; i < static_cast<int>(EUpdate::MAX_VERSION_BITS_DEPLOYMENTS); ++i) {
StateInfo state = UpdateManager::Instance().State( static_cast<EUpdate>(i), ::ChainActive().Tip());
const Update* update = UpdateManager::Instance().GetUpdate(static_cast<EUpdate>(i));
StateInfo state = Updates().State( static_cast<EUpdate>(i), ::ChainActive().Tip());
const Update* update = Updates().GetUpdate(static_cast<EUpdate>(i));
switch (state.State) {
case EUpdateState::Defined:
case EUpdateState::Failed:
Expand Down
3 changes: 0 additions & 3 deletions src/update/update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
// 100..199 Round 2 votes, results active and cached at height 200
// 200..299 Round 3 votes, results active and cached at height 300

UpdateManager* UpdateManager::_instance;
std::once_flag UpdateManager::_instance_flag;

const int64_t VoteResult::scaleFactor = 100 * 100; // Scaled arithmetic (value 0.1234 represented by integer 1234)

std::ostream& operator<<(std::ostream& out, EUpdateState state)
Expand Down
11 changes: 0 additions & 11 deletions src/update/update.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,18 +302,7 @@ typedef struct StateInfo

class UpdateManager
{
private :
// Singleton
static UpdateManager* _instance;
static std::once_flag _instance_flag;

public:
static UpdateManager& Instance()
{
std::call_once(UpdateManager::_instance_flag, []() { UpdateManager::_instance = new UpdateManager(); });
return *UpdateManager::_instance;
}

UpdateManager();
virtual ~UpdateManager();

Expand Down
6 changes: 3 additions & 3 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2027,7 +2027,7 @@ static unsigned int GetBlockScriptFlags(const CBlockIndex *pindex, const Consens
flags |= SCRIPT_VERIFY_NULLDUMMY;
}

if (UpdateManager::Instance().IsActive(EUpdate::DEPLOYMENT_V17, pindex)) {
if (Updates().IsActive(EUpdate::DEPLOYMENT_V17, pindex)) {
flags |= SCRIPT_ENABLE_DIP0020_OPCODES;
}

Expand Down Expand Up @@ -2842,15 +2842,15 @@ EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
int nUpgraded = 0;
const CBlockIndex *pindex = pindexNew;

uint32_t nExpectedVersion = UpdateManager::Instance().ComputeBlockVersion(pindex);
uint32_t nExpectedVersion = Updates().ComputeBlockVersion(pindex);
if (pindexNew->nVersion != nExpectedVersion) {
const std::string strWarning = strprintf(_("Warning: unknown new rules activated: Expected: 0x%08x, Actual: 0x%08x"), nExpectedVersion, pindexNew->nVersion);
AppendWarning(warningMessages, strWarning);
}

// Check the version of the last 100 blocks to see if we need to upgrade:
for (int i = 0; i < 100 && pindex != nullptr; i++) {
nExpectedVersion = UpdateManager::Instance().ComputeBlockVersion(pindex->pprev);
nExpectedVersion = Updates().ComputeBlockVersion(pindex->pprev);
if (pindex->nVersion > VERSIONBITS_LAST_OLD_BLOCK_VERSION &&
(pindex->nVersion & ~nExpectedVersion) != 0)
++nUpgraded;
Expand Down

0 comments on commit 06f2dd9

Please sign in to comment.