Skip to content

Commit

Permalink
Port chain params
Browse files Browse the repository at this point in the history
  • Loading branch information
timemarkovqtum committed Jun 12, 2024
1 parent 79358e4 commit 42d533c
Show file tree
Hide file tree
Showing 12 changed files with 684 additions and 1,789 deletions.
29 changes: 28 additions & 1 deletion src/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <chain.h>
#include <tinyformat.h>
#include <util/time.h>
#include <pubkey.h>

std::string CBlockFileInfo::ToString() const
{
Expand Down Expand Up @@ -153,7 +154,7 @@ int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& fr
r = from.nChainWork - to.nChainWork;
sign = -1;
}
r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
r = r * arith_uint256(params.TargetSpacing(tip.nHeight)) / GetBlockProof(tip);
if (r.bits() > 63) {
return sign * std::numeric_limits<int64_t>::max();
}
Expand All @@ -178,3 +179,29 @@ const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex*
assert(pa == pb);
return pa;
}

std::vector<unsigned char> CBlockIndex::GetBlockSignature() const
{
if(vchBlockSigDlgt.size() < 2 * CPubKey::COMPACT_SIGNATURE_SIZE)
{
return vchBlockSigDlgt;
}

return std::vector<unsigned char>(vchBlockSigDlgt.begin(), vchBlockSigDlgt.end() - CPubKey::COMPACT_SIGNATURE_SIZE );
}

std::vector<unsigned char> CBlockIndex::GetProofOfDelegation() const
{
if(vchBlockSigDlgt.size() < 2 * CPubKey::COMPACT_SIGNATURE_SIZE)
{
return std::vector<unsigned char>();
}

return std::vector<unsigned char>(vchBlockSigDlgt.begin() + vchBlockSigDlgt.size() - CPubKey::COMPACT_SIGNATURE_SIZE, vchBlockSigDlgt.end());

}

bool CBlockIndex::HasProofOfDelegation() const
{
return vchBlockSigDlgt.size() >= 2 * CPubKey::COMPACT_SIGNATURE_SIZE;
}
55 changes: 54 additions & 1 deletion src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ class CBlockIndex
//! pointer to the index of the predecessor of this block
CBlockIndex* pprev{nullptr};

//! pointer to the index of the successor of this block
CBlockIndex* pnext{nullptr};

//! pointer to the index of some further predecessor of this block
CBlockIndex* pskip{nullptr};

Expand Down Expand Up @@ -204,6 +207,15 @@ class CBlockIndex
uint32_t nTime{0};
uint32_t nBits{0};
uint32_t nNonce{0};
uint256 hashStateRoot{}; // qtum
uint256 hashUTXORoot{}; // qtum
// block signature - proof-of-stake protect the block by signing the block using a stake holder private key
std::vector<unsigned char> vchBlockSigDlgt{};
uint256 nStakeModifier{};
// proof-of-stake specific fields
COutPoint prevoutStake{};
uint256 hashProof{}; // qtum
uint64_t nMoneySupply{0};

//! (memory only) Sequential id assigned to distinguish order in which blocks are received.
int32_t nSequenceId{0};
Expand All @@ -216,7 +228,11 @@ class CBlockIndex
hashMerkleRoot{block.hashMerkleRoot},
nTime{block.nTime},
nBits{block.nBits},
nNonce{block.nNonce}
nNonce{block.nNonce},
hashStateRoot{block.hashStateRoot},
hashUTXORoot{block.hashUTXORoot},
vchBlockSigDlgt{block.vchBlockSigDlgt},
prevoutStake{block.prevoutStake}
{
}

Expand Down Expand Up @@ -252,6 +268,10 @@ class CBlockIndex
block.nTime = nTime;
block.nBits = nBits;
block.nNonce = nNonce;
block.hashStateRoot = hashStateRoot; // qtum
block.hashUTXORoot = hashUTXORoot; // qtum
block.vchBlockSigDlgt = vchBlockSigDlgt;
block.prevoutStake = prevoutStake;
return block;
}

Expand Down Expand Up @@ -305,6 +325,22 @@ class CBlockIndex
return pbegin[(pend - pbegin) / 2];
}

bool IsProofOfWork() const // qtum
{
return !IsProofOfStake();
}

bool IsProofOfStake() const
{
return !prevoutStake.IsNull();
}

std::vector<unsigned char> GetBlockSignature() const;

std::vector<unsigned char> GetProofOfDelegation() const;

bool HasProofOfDelegation() const;

std::string ToString() const;

//! Check whether this block index entry is valid up to the passed validity level.
Expand Down Expand Up @@ -416,6 +452,7 @@ class CDiskBlockIndex : public CBlockIndex
if (obj.nStatus & (BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO)) READWRITE(VARINT_MODE(obj.nFile, VarIntMode::NONNEGATIVE_SIGNED));
if (obj.nStatus & BLOCK_HAVE_DATA) READWRITE(VARINT(obj.nDataPos));
if (obj.nStatus & BLOCK_HAVE_UNDO) READWRITE(VARINT(obj.nUndoPos));
READWRITE(VARINT(obj.nMoneySupply));

// block header
READWRITE(obj.nVersion);
Expand All @@ -424,6 +461,12 @@ class CDiskBlockIndex : public CBlockIndex
READWRITE(obj.nTime);
READWRITE(obj.nBits);
READWRITE(obj.nNonce);
READWRITE(obj.hashStateRoot); // qtum
READWRITE(obj.hashUTXORoot); // qtum
READWRITE(obj.nStakeModifier);
READWRITE(obj.prevoutStake);
READWRITE(obj.hashProof);
READWRITE(obj.vchBlockSigDlgt); // qtum
}

uint256 ConstructBlockHash() const
Expand All @@ -435,6 +478,10 @@ class CDiskBlockIndex : public CBlockIndex
block.nTime = nTime;
block.nBits = nBits;
block.nNonce = nNonce;
block.hashStateRoot = hashStateRoot; // qtum
block.hashUTXORoot = hashUTXORoot; // qtum
block.vchBlockSigDlgt = vchBlockSigDlgt;
block.prevoutStake = prevoutStake;
return block.GetHash();
}

Expand Down Expand Up @@ -473,6 +520,12 @@ class CChain
return vChain[nHeight];
}

/** Compare two chains efficiently. */
friend bool operator==(const CChain &a, const CChain &b) {
return a.vChain.size() == b.vChain.size() &&
a.vChain[a.vChain.size() - 1] == b.vChain[b.vChain.size() - 1];
}

/** Efficiently check whether a block is present in this chain. */
bool Contains(const CBlockIndex* pindex) const
{
Expand Down
206 changes: 206 additions & 0 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, c
ReadRegTestArgs(args, opts);
return CChainParams::RegTest(opts);
}
case ChainType::UNITTEST: {
auto opts = CChainParams::RegTestOptions{};
ReadRegTestArgs(args, opts);
return CChainParams::UnitTest(opts);
}
}
assert(false);
}
Expand All @@ -132,3 +137,204 @@ void SelectParams(const ChainType chain)
SelectBaseParams(chain);
globalChainParams = CreateChainParams(gArgs, chain);
}

std::string CChainParams::EVMGenesisInfo() const
{
dev::eth::EVMConsensus evmConsensus;
evmConsensus.QIP6Height = consensus.QIP6Height;
evmConsensus.QIP7Height = consensus.QIP7Height;
evmConsensus.nMuirGlacierHeight = consensus.nMuirGlacierHeight;
evmConsensus.nLondonHeight = consensus.nLondonHeight;
evmConsensus.nShanghaiHeight = consensus.nShanghaiHeight;
return dev::eth::genesisInfoQtum(GetEVMNetwork(), evmConsensus);
}

std::string CChainParams::EVMGenesisInfo(int nHeight) const
{
dev::eth::EVMConsensus evmConsensus(nHeight);
return dev::eth::genesisInfoQtum(GetEVMNetwork(), evmConsensus);
}

std::string CChainParams::EVMGenesisInfo(const dev::eth::EVMConsensus& evmConsensus) const
{
return dev::eth::genesisInfoQtum(GetEVMNetwork(), evmConsensus);
}

dev::eth::Network CChainParams::GetEVMNetwork() const
{
return dev::eth::Network::qtumNetwork;
}

void CChainParams::UpdateOpSenderBlockHeight(int nHeight)
{
consensus.QIP5Height = nHeight;
}

void UpdateOpSenderBlockHeight(int nHeight)
{
const_cast<CChainParams*>(globalChainParams.get())->UpdateOpSenderBlockHeight(nHeight);
}

void CChainParams::UpdateBtcEcrecoverBlockHeight(int nHeight)
{
consensus.QIP6Height = nHeight;
}

void UpdateBtcEcrecoverBlockHeight(int nHeight)
{
const_cast<CChainParams*>(globalChainParams.get())->UpdateBtcEcrecoverBlockHeight(nHeight);
}

void CChainParams::UpdateConstantinopleBlockHeight(int nHeight)
{
consensus.QIP7Height = nHeight;
}

void UpdateConstantinopleBlockHeight(int nHeight)
{
const_cast<CChainParams*>(globalChainParams.get())->UpdateConstantinopleBlockHeight(nHeight);
}

void CChainParams::UpdateDifficultyChangeBlockHeight(int nHeight)
{
consensus.nSubsidyHalvingInterval = 985500; // qtum halving every 4 years
consensus.nSubsidyHalvingIntervalV2 = consensus.nBlocktimeDownscaleFactor*985500; // qtum halving every 4 years (nSubsidyHalvingInterval * nBlocktimeDownscaleFactor)
consensus.posLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.QIP9PosLimit = uint256S("0000000000001fffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.RBTPosLimit = uint256S("0000000000003fffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.QIP9Height = nHeight;
consensus.fPowAllowMinDifficultyBlocks = false;
consensus.fPowNoRetargeting = true;
consensus.fPoSNoRetargeting = false;
consensus.nLastPOWBlock = 5000;
consensus.nMPoSRewardRecipients = 10;
consensus.nFirstMPoSBlock = consensus.nLastPOWBlock +
consensus.nMPoSRewardRecipients +
consensus.nCoinbaseMaturity;
consensus.nLastMPoSBlock = 0;
}

void UpdateDifficultyChangeBlockHeight(int nHeight)
{
const_cast<CChainParams*>(globalChainParams.get())->UpdateDifficultyChangeBlockHeight(nHeight);
}

void CChainParams::UpdateOfflineStakingBlockHeight(int nHeight)
{
consensus.nOfflineStakeHeight = nHeight;
}

void UpdateOfflineStakingBlockHeight(int nHeight)
{
const_cast<CChainParams*>(globalChainParams.get())->UpdateOfflineStakingBlockHeight(nHeight);
}

void CChainParams::UpdateDelegationsAddress(const uint160& address)
{
consensus.delegationsAddress = address;
}

void UpdateDelegationsAddress(const uint160& address)
{
const_cast<CChainParams*>(globalChainParams.get())->UpdateDelegationsAddress(address);
}

void CChainParams::UpdateLastMPoSBlockHeight(int nHeight)
{
consensus.nLastMPoSBlock = nHeight;
}

void UpdateLastMPoSBlockHeight(int nHeight)
{
const_cast<CChainParams*>(globalChainParams.get())->UpdateLastMPoSBlockHeight(nHeight);
}

void CChainParams::UpdateReduceBlocktimeHeight(int nHeight)
{
consensus.nReduceBlocktimeHeight = nHeight;
}

void UpdateReduceBlocktimeHeight(int nHeight)
{
const_cast<CChainParams*>(globalChainParams.get())->UpdateReduceBlocktimeHeight(nHeight);
}

void CChainParams::UpdatePowAllowMinDifficultyBlocks(bool fValue)
{
consensus.fPowAllowMinDifficultyBlocks = fValue;
}

void UpdatePowAllowMinDifficultyBlocks(bool fValuet)
{
const_cast<CChainParams*>(globalChainParams.get())->UpdatePowAllowMinDifficultyBlocks(fValuet);
}

void CChainParams::UpdatePowNoRetargeting(bool fValue)
{
consensus.fPowNoRetargeting = fValue;
}

void UpdatePowNoRetargeting(bool fValuet)
{
const_cast<CChainParams*>(globalChainParams.get())->UpdatePowNoRetargeting(fValuet);
}

void CChainParams::UpdatePoSNoRetargeting(bool fValue)
{
consensus.fPoSNoRetargeting = fValue;
}

void UpdatePoSNoRetargeting(bool fValuet)
{
const_cast<CChainParams*>(globalChainParams.get())->UpdatePoSNoRetargeting(fValuet);
}

void CChainParams::UpdateMuirGlacierHeight(int nHeight)
{
consensus.nMuirGlacierHeight = nHeight;
}

void UpdateMuirGlacierHeight(int nHeight)
{
const_cast<CChainParams*>(globalChainParams.get())->UpdateMuirGlacierHeight(nHeight);
}

void CChainParams::UpdateLondonHeight(int nHeight)
{
consensus.nLondonHeight = nHeight;
}

void UpdateLondonHeight(int nHeight)
{
const_cast<CChainParams*>(globalChainParams.get())->UpdateLondonHeight(nHeight);
}

void CChainParams::UpdateTaprootHeight(int nHeight)
{
if(nHeight == 0)
{
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nStartTime = Consensus::BIP9Deployment::ALWAYS_ACTIVE;
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].min_activation_height = 0; // No activation delay
}
else
{
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nStartTime = 0;
// Min block number for activation, the number must be divisible with 144
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].min_activation_height = nHeight;
}
}

void UpdateTaprootHeight(int nHeight)
{
const_cast<CChainParams*>(globalChainParams.get())->UpdateTaprootHeight(nHeight);
}

void CChainParams::UpdateShanghaiHeight(int nHeight)
{
consensus.nShanghaiHeight = nHeight;
}

void UpdateShanghaiHeight(int nHeight)
{
const_cast<CChainParams*>(globalChainParams.get())->UpdateShanghaiHeight(nHeight);
}
Loading

0 comments on commit 42d533c

Please sign in to comment.