Skip to content

Commit

Permalink
Merge pull request #48 from dogecash/time
Browse files Browse the repository at this point in the history
Time Protocolv2
  • Loading branch information
Liquid369 committed Jun 26, 2020
2 parents faeb5b2 + 945a04e commit 732f006
Show file tree
Hide file tree
Showing 25 changed files with 407 additions and 283 deletions.
6 changes: 3 additions & 3 deletions configure.ac
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
dnl require autoconf 2.60 (AS_ECHO/AS_ECHO_N)
AC_PREREQ([2.60])
define(_CLIENT_VERSION_MAJOR, 5)
define(_CLIENT_VERSION_MINOR, 1)
define(_CLIENT_VERSION_MINOR, 2)
define(_CLIENT_VERSION_REVISION, 1)
define(_CLIENT_VERSION_BUILD, 7)
define(_CLIENT_VERSION_BUILD, 0)
define(_CLIENT_VERSION_RC, 0)
define(_CLIENT_VERSION_IS_RELEASE, true)
define(_COPYRIGHT_YEAR, 2019)
define(_COPYRIGHT_YEAR, 2020)
AC_INIT([DogeCash Core],m4_join([.], _CLIENT_VERSION_MAJOR, _CLIENT_VERSION_MINOR, _CLIENT_VERSION_REVISION, m4_if(_CLIENT_VERSION_BUILD, [0], [], _CLIENT_VERSION_BUILD))m4_if(_CLIENT_VERSION_RC, [0], [], [rc]_CLIENT_VERSION_RC),[https://github.com/dogecash/dogecash/issues],[DogeCash],[https://dogec.io/])
AC_CONFIG_SRCDIR([src/main.cpp])
AC_CONFIG_HEADERS([src/config/dogecash-config.h])
Expand Down
3 changes: 3 additions & 0 deletions contrib/devtools/copyright_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ def compile_copyright_regex(copyright_style, year_style, name):
"The PIVX developers\n",
"The DogeCash developers\n",
"The PPCoin developers\n",
"The NovaCoin Developers",
"The BlackCoin Developers\n",
"The Blackcoin More developers\n",
]

DOMINANT_STYLE_COMPILED = {}
Expand Down
24 changes: 23 additions & 1 deletion src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "chainparams.h"
#include "pow.h"
#include "primitives/block.h"
#include "timedata.h"
#include "tinyformat.h"
#include "uint256.h"
#include "arith_uint256.h"
Expand Down Expand Up @@ -239,7 +240,7 @@ class CBlockIndex
nTime = block.nTime;
nBits = block.nBits;
nNonce = block.nNonce;
if(block.nVersion > 3)
if(block.nVersion > 3 && block.nVersion < 7)
nAccumulatorCheckpoint = block.nAccumulatorCheckpoint;

if (block.IsProofOfStake()) {
Expand Down Expand Up @@ -344,6 +345,27 @@ class CBlockIndex
return pbegin[(pend - pbegin) / 2];
}

int64_t MaxFutureBlockTime() const
{
return GetAdjustedTime() + Params().FutureBlockTimeDrift(nHeight+1);
}

int64_t MinPastBlockTime() const
{
// Time Protocol v1: pindexPrev->MedianTimePast + 1
if (!Params().IsTimeProtocolV2(nHeight+1))
return GetMedianTimePast();

// on the transition from Time Protocol v1 to v2
// pindexPrev->nTime might be in the future (up to the allowed drift)
// so we allow the nBlockTimeProtocolV2 to be at most (180-14) seconds earlier than previous block
if (nHeight + 1 == Params().BlockStartTimeProtocolV2())
return GetBlockTime() - Params().FutureBlockTimeDrift(nHeight) + Params().FutureBlockTimeDrift(nHeight + 1);

// Time Protocol v2: pindexPrev->nTime
return GetBlockTime();
}

bool IsProofOfWork() const
{
return !(nFlags & BLOCK_PROOF_OF_STAKE);
Expand Down
81 changes: 63 additions & 18 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ boost::assign::map_list_of
(210000,uint256("c189cc15bf90899ddc25990d9ce82a22b9dd30938307101a36289d53add58918"))
(220000,uint256("4bd6999b5feefeb255bac4e6651f99d961b422e942af963f1d2575b2f89d3336"))
(230000,uint256("c063c978716d6d0d8ca56708dcac28e323bc503c50a16f4b95aca9f0e21f3409"))
(238620,uint256("4e83603994564be49ef250f0f724e197b2abbaee9ae662b531285003d428ad31"))
(238621,uint256("6b9c0cf87ea802a8f5aa60461bb71934736b2085ca4f0aee9694c2d9c748770f"))
(240000,uint256("57c0e1a7168fca9e032a8d129fcaa7996cd216d4124b054d261b23c4a4f778db"))
(250000,uint256("9e82fc0e7498d76d6fbfe515312f9a378f349635f772688b81f7859e74685561"))
(270000,uint256("cb2737887b9d0f11fd01c124c7a34feff9d449a0913caadce43b334730623d79"))
Expand Down Expand Up @@ -110,8 +112,7 @@ boost::assign::map_list_of
(540000,uint256("b225698083f4a077490dbb8ed7d73c6ad3696c7fda820fb81ee289c84a277869"))
(550000,uint256("18fa071492467696ecd34c8f7c95980216e4ee961e452873cb6a824e427f5411"))
(560000,uint256("fbd89a525344ee3829ee22f69125099010ea6f311da246fe602b2a0a66af8844"))
(570000,uint256("74687dbc5671933f53704345a0863d62cbce67c004c69707bd545fced2ef8279"))
(573154,uint256("ec34e12befff7dbffb79c0945fe4b21eddaec39e2e4b733d36cb08ae11fcff60"));
(570000,uint256("74687dbc5671933f53704345a0863d62cbce67c004c69707bd545fced2ef8279"));
static const Checkpoints::CCheckpointData data = {
&mapCheckpoints,
1593088525,// * UNIX timestamp of last checkpoint block
Expand Down Expand Up @@ -157,17 +158,35 @@ libzerocoin::ZerocoinParams* CChainParams::Zerocoin_Params(bool useModulusV1) co

bool CChainParams::HasStakeMinAgeOrDepth(const int contextHeight, const uint32_t contextTime, const int utxoFromBlockHeight, const uint32_t utxoFromBlockTime) const
{
if (NetworkID() == CBaseChainParams::REGTEST)
return true;

// before stake modifier V2, the age required was 60 * 60 (1 hour) / not required on regtest
// before stake modifier V2, the age required was 60 * 60 (1 hour). Not required for regtest
if (!IsStakeModifierV2(contextHeight))
return (utxoFromBlockTime + 3600 <= contextTime);
return NetworkID() == CBaseChainParams::REGTEST || (utxoFromBlockTime + nStakeMinAge <= contextTime);

// after stake modifier V2, we require the utxo to be nStakeMinDepth deep in the chain
return(contextHeight - utxoFromBlockHeight >= nStakeMinDepth);
}

int CChainParams::FutureBlockTimeDrift(const int nHeight) const
{
if (IsTimeProtocolV2(nHeight))
// PoS (TimeV2): 14 seconds
return TimeSlotLength() - 1;

// PoS (TimeV1): 3 minutes
// PoW: 2 hours
return (nHeight > LAST_POW_BLOCK()) ? nFutureTimeDriftPoS : nFutureTimeDriftPoW;
}

bool CChainParams::IsValidBlockTimeStamp(const int64_t nTime, const int nHeight) const
{
// Before time protocol V2, blocks can have arbitrary timestamps
if (!IsTimeProtocolV2(nHeight))
return true;

// Time protocol v2 requires time in slots
return (nTime % TimeSlotLength()) == 0;
}

class CMainParams : public CChainParams
{
public:
Expand All @@ -188,13 +207,20 @@ class CMainParams : public CChainParams
vAlertPubKey = ParseHex("04e70ca66d0bc8bd6c105952488fcdbd9f809d459af257646e35c5ec36f5e03158c916442aaa9592e63eeca8afa9660e505b160ee599c8efcc0a5f111058ba8c74");
nDefaultPort = 56740;
bnProofOfWorkLimit = ~uint256(0) >> 20; // dogecash starting difficulty is 1 / 2^12
bnProofOfStakeLimit = ~uint256(0) >> 24;
bnProofOfStakeLimit_V2 = ~uint256(0) >> 20; // 60/4 = 15 ==> use 2**4 higher limit
nSubsidyHalvingInterval = 210000;
nMaxReorganizationDepth = 100;
nEnforceBlockUpgradeMajority = 8100; // 75%
nRejectBlockOutdatedMajority = 10260; // 95%
nToCheckBlockUpgradeMajority = 10800; // Approximate expected amount of blocks in 7 days (1440*7.5)
nMinerThreads = 0;
nTargetSpacing = 1 * 60; //dogecash: 1 Min
nTargetSpacing = 1 * 60; // 1 minute
nTargetTimespan = 40 * 60; // 40 minutes
nTimeSlotLength = 15; // 15 seconds
nTargetTimespan_V2 = 2 * nTimeSlotLength * 60; // 30 minutes
nMaturity = 100;
nStakeMinAge = 60 * 60; // 1 hour
nStakeMinDepth = 100;
nFutureTimeDriftPoW = 7200;
nFutureTimeDriftPoS = 180;
Expand All @@ -204,6 +230,10 @@ class CMainParams : public CChainParams
nStakeCollateralMin = 100 * COIN;
nMaxMoneyOut = 21000000 * COIN; //21 mill
nMinColdStakingAmount = 1 * COIN;

nDogecBadBlockTime = 1593294062; // Skip nBit validation of Block 259201 per PR #915
nDogecBadBlocknBits = 0x1d19b36d; // Skip nBit validation of Block 201 per PR #915

/** Height or Time Based Activations **/
nLastPOWBlock = 200;
nZerocoinStartHeight = INT_MAX;
Expand All @@ -216,16 +246,20 @@ class CMainParams : public CChainParams
//nInvalidAmountFiltered = 268200*COIN; //Amount of invalid coins filtered through exchanges, that should be considered valid
nBlockZerocoinV2 = INT_MAX; //!> The block that zerocoin v2 becomes active - roughly Tuesday, May 8, 2018 4:00:00 AM GMT
nBlockDoubleAccumulated = 1050010;
nEnforceNewSporkKey = 1425158000; //!> Sporks signed after (GMT): Tuesday, May 1, 2018 7:00:00 AM GMT must use the new spork key
nRejectOldSporkKey = 1527811200; //!> Fully reject old spork key after (GMT): Friday, June 1, 2018 12:00:00 AM
nBlockStakeModifierlV2 = 261028; //!> Enforce new Stake Protocols add test/regtest
nModifierUpdateBlock = nBlockStakeModifierlV2 + 5;
nEnforceNewSporkKey = 1566860400; //!> Sporks signed after Monday, August 26, 2019 11:00:00 PM GMT must use the new spork key
nRejectOldSporkKey = 1569538800; //!> Fully reject old spork key after Thursday, September 26, 2019 11:00:00 PM GMT
nBlockStakeModifierlV2 = 261028;
nBlockTimeProtocolV2 = 575000;

// New P2P messages signatures
nBlockEnforceNewMessageSignatures = nBlockStakeModifierlV2 + 10;

// New P2P messages signatures

// Blocks v7
nBlockLastAccumulatorCheckpoint = nBlockV7StartHeight - 10;
nBlockV7StartHeight = nBlockTimeProtocolV2 - 10;

// Fake Serial Attack
nFakeSerialBlockheightEnd = 1686229;
/**
Expand Down Expand Up @@ -335,9 +369,10 @@ class CTestNetParams : public CMainParams
nRejectBlockOutdatedMajority = 5472; // 95%
nToCheckBlockUpgradeMajority = 5760; // 4 days
nMinerThreads = 0;
nTargetSpacing = 1 * 60; // dogecash: 1 minute
nLastPOWBlock = 125;
nMaturity = 1;
nLastPOWBlock = 200;
nDogecBadBlockTime = 1558145668; // Skip nBit validation of Block 259201 per PR #915
nDogecBadBlocknBits = 0x1d19b36d; // Skip nBit validation of Block 201 per PR #915
nMaturity = 15;
nStakeMinDepth = 50;
nMasternodeCountDrift = 4;
nStakeCollateralMin = 10;
Expand All @@ -354,11 +389,16 @@ class CTestNetParams : public CMainParams
nBlockZerocoinV2 = INT_MAX; //!> The block that zerocoin v2 becomes active
nEnforceNewSporkKey = 1521604800; //!> Sporks signed after Wednesday, March 21, 2018 4:00:00 AM GMT must use the new spork key
nRejectOldSporkKey = 1522454400; //!> Reject old spork key after Saturday, March 31, 2018 12:00:00 AM GMT
nBlockStakeModifierlV2 = 125; //!> Enforce new Stake Protocols add test/regtest
nBlockStakeModifierlV2 = 1214000;
nBlockTimeProtocolV2 = 2214000;

// New P2P messages signatures
nBlockEnforceNewMessageSignatures = 1;

// Blocks v7
nBlockLastAccumulatorCheckpoint = nBlockV7StartHeight - 10;
nBlockV7StartHeight = nBlockTimeProtocolV2 - 10;

// Fake Serial Attack
nFakeSerialBlockheightEnd = -1;
nSupplyBeforeFakeSerial = 0;
Expand Down Expand Up @@ -435,17 +475,22 @@ class CRegTestParams : public CTestNetParams
bnProofOfWorkLimit = ~uint256(0) >> 1;
nLastPOWBlock = 250;
nMaturity = 100;
nStakeMinAge = 0;
nStakeMinDepth = 0;
nTimeSlotLength = 1; // time not masked on RegNet
nMasternodeCountDrift = 4;
nStakeCollateralMin = 0;
nModifierUpdateBlock = 0; //approx Mon, 17 Apr 2017 04:00:00 GMT
nModifierUpdateBlock = 0; //approx Mon, 17 Apr 2017 04:00:00 GMT
nMaxMoneyOut = 21000000 * COIN;
nZerocoinStartHeight = INT_MAX;
nStakeCollateralMin = 0;
nBlockZerocoinV2 = 300;
nZerocoinStartTime = INT_MAX;
nBlockEnforceSerialRange = 1; //Enforce serial range starting this block
nBlockRecalculateAccumulators = 999999999; //Trigger a recalculation of accumulators
nBlockFirstFraudulent = 999999999; //First block that bad serials emerged
nBlockLastGoodCheckpoint = 999999999; //Last valid accumulator checkpoint
nBlockStakeModifierlV2 = 255;
nBlockTimeProtocolV2 = 999999999;

// New P2P messages signatures
nBlockEnforceNewMessageSignatures = 1;
Expand Down
31 changes: 28 additions & 3 deletions src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class CChainParams
const std::vector<unsigned char>& AlertKey() const { return vAlertPubKey; }
int GetDefaultPort() const { return nDefaultPort; }
const uint256& ProofOfWorkLimit() const { return bnProofOfWorkLimit; }
const uint256& ProofOfStakeLimit(const bool fV2) const { return fV2 ? bnProofOfStakeLimit_V2 : bnProofOfStakeLimit; }
int SubsidyHalvingInterval() const { return nSubsidyHalvingInterval; }
/** Used to check majorities for block version upgrade */
int EnforceBlockUpgradeMajority() const { return nEnforceBlockUpgradeMajority; }
Expand All @@ -74,15 +75,23 @@ class CChainParams
/** Make standard checks */
bool RequireStandard() const { return fRequireStandard; }
int64_t TargetSpacing() const { return nTargetSpacing; }
/** returns the coinbase maturity **/
int64_t TargetTimespan(const bool fV2 = true) const { return fV2 ? nTargetTimespan_V2 : nTargetTimespan; }

/** returns the coinbase maturity **/
int COINBASE_MATURITY() const { return nMaturity; }

/** returns the coinstake maturity (min depth required) **/
int COINSTAKE_MIN_AGE() const { return nStakeMinAge; }
int COINSTAKE_MIN_DEPTH() const { return nStakeMinDepth; }
bool HasStakeMinAgeOrDepth(const int contextHeight, const uint32_t contextTime, const int utxoFromBlockHeight, const uint32_t utxoFromBlockTime) const;

/** returns the max future time (and drift in seconds) allowed for a block in the future **/
int FutureBlockTimeDrift(const bool isPoS) const { return isPoS ? nFutureTimeDriftPoS : nFutureTimeDriftPoW; }
/** Time Protocol V2 **/
int BlockStartTimeProtocolV2() const { return nBlockTimeProtocolV2; }
bool IsTimeProtocolV2(const int nHeight) const { return nHeight >= BlockStartTimeProtocolV2(); }
int TimeSlotLength() const { return nTimeSlotLength; }
int FutureBlockTimeDrift(const int nHeight) const;
bool IsValidBlockTimeStamp(const int64_t nTime, const int nHeight) const;

uint32_t MaxFutureBlockTime(uint32_t time, const bool isPoS) const { return time + FutureBlockTimeDrift(isPoS); }
CAmount MaxMoneyOut() const { return nMaxMoneyOut; }
CAmount nStakeMinInput() const { return nStakeCollateralMin; }
Expand All @@ -108,6 +117,8 @@ class CChainParams
int64_t GetProposalEstablishmentTime() const { return nProposalEstablishmentTime; }

CAmount GetMinColdStakingAmount() const { return nMinColdStakingAmount; }
int DogecBadBlockTime() const { return nDogecBadBlockTime; }
int DogecBadBlocknBits() const { return nDogecBadBlocknBits; }

/** Spork key and Masternode Handling **/
std::string SporkPubKey() const { return strSporkPubKey; }
Expand Down Expand Up @@ -144,6 +155,7 @@ class CChainParams
int Zerocoin_Block_V2_Start() const { return nBlockZerocoinV2; }
int NewSigsActive(const int nHeight) const { return nHeight >= nBlockEnforceNewMessageSignatures; }
bool IsStakeModifierV2(const int nHeight) const { return nHeight >= nBlockStakeModifierlV2; }
int Block_V7_StartHeight() const { return nBlockV7StartHeight; }

// fake serial attack
int Zerocoin_Block_EndFakeSerial() const { return nFakeSerialBlockheightEnd; }
Expand All @@ -152,6 +164,7 @@ class CChainParams
int Zerocoin_Block_Double_Accumulated() const { return nBlockDoubleAccumulated; }
CAmount InvalidAmountFiltered() const { return nInvalidAmountFiltered; };

int Zerocoin_Block_Last_Checkpoint() const { return nBlockLastAccumulatorCheckpoint; }
protected:
CChainParams() {}

Expand All @@ -161,21 +174,30 @@ class CChainParams
std::vector<unsigned char> vAlertPubKey;
int nDefaultPort;
uint256 bnProofOfWorkLimit;
uint256 bnProofOfStakeLimit;
uint256 bnProofOfStakeLimit_V2;
int nMaxReorganizationDepth;
int nSubsidyHalvingInterval;
int nEnforceBlockUpgradeMajority;
int nRejectBlockOutdatedMajority;
int nToCheckBlockUpgradeMajority;
int64_t nTargetSpacing;
int64_t nTargetTimespan;
int64_t nTargetTimespan_V2;
int nLastPOWBlock;
int64_t nDogecBadBlockTime;
unsigned int nDogecBadBlocknBits;
int nMinStakeReserve;
int nMasternodeCollateralLimit;
int nMasternodeCountDrift;
int nMaturity;
int nModifierUpdateBlock;
int nStakeMinDepth;
int nStakeMinAge;
int nFutureTimeDriftPoW;
int nFutureTimeDriftPoS;
int nTimeSlotLength;

//Stake Protocol Switch
int nBlockNewStakeProtocol;
CAmount nStakeCollateralMin;
Expand Down Expand Up @@ -228,6 +250,9 @@ class CChainParams
int nBlockDoubleAccumulated;
int nBlockEnforceNewMessageSignatures;
int nBlockStakeModifierlV2;
int nBlockTimeProtocolV2;
int nBlockV7StartHeight;
int nBlockLastAccumulatorCheckpoint;
CAmount nMinColdStakingAmount;
// fake serial attack
int nFakeSerialBlockheightEnd = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1587,7 +1587,7 @@ int64_t nStart;
if (GetBoolArg("-reindexaccumulators", false)) {
if (chainHeight > Params().Zerocoin_Block_V2_Start()) {
CBlockIndex *pindex = chainActive[Params().Zerocoin_Block_V2_Start()];
while (pindex->nHeight < chainActive.Height()) {
while (pindex && pindex->nHeight < std::min(chainActive.Height(), Params().Zerocoin_Block_Last_Checkpoint()+1)) {
if (!count(listAccCheckpointsNoDB.begin(), listAccCheckpointsNoDB.end(),
pindex->nAccumulatorCheckpoint))
listAccCheckpointsNoDB.emplace_back(pindex->nAccumulatorCheckpoint);
Expand Down

0 comments on commit 732f006

Please sign in to comment.