Skip to content

Commit

Permalink
Merge #1002: [PoS] Time Protocol v2
Browse files Browse the repository at this point in the history
4ea0476 [Consensus] Fix difficulty adjustment on first block of timeproto V2 (random-zebra)
c254df6 [P2P] Do not store more than 200 timedata samples. (random-zebra)
8ca3979 [Tests] (squashed) few regtest fixes (random-zebra)
3487e58 [PoS] Fix Stake maturity-check and mapHashedBlocks time (random-zebra)
55aeecd [Core] raise nTargetTimespan_V2 to 30 minutes (random-zebra)
79bdc83 [Core] Restore CheckBlock checks before previous block check (random-zebra)
3bb0c0a [PoS] Don't use chainActive.Tip() as prevIndex when creating new blocks (random-zebra)
55faec1 [Performance] Kernel, methods using object reference instead of copy the object. (furszy)
4afcba5 [PoS] Don't allow consecutive blocks within the same time slot (random-zebra)
c97e998 [PoS] StakeV1: iterate the time backwards. (random-zebra)
59cd3af [PoS] reduce difficulty by a factor 16 on nBlockTimeProtocolV2 (random-zebra)
3447dfb [Core] Make bnProofOfStakeLimit_V2 16 times v1 limit (random-zebra)
0bcf0c1 [PoS] Keep v1 miner until hard-fork (random-zebra)
ec45670 [Core] Guard the transition to v2 time protocol in MinPastBlockTime rule (random-zebra)
fee5f5c [PoS] set nTargetTimespan_V2 to 15 minutes (60 time slots) (random-zebra)
ca2b035 [PoS] Set target spacing to 60 seconds (random-zebra)
0578b0a [PoS] Adjust FutureBlockTimeDrift to 15 secs granularity (random-zebra)
dd71075 [Core] Set time granularity to 15 (instead of 16) (random-zebra)
8c3546f [Consensus] Change difficulty computation for V2 time protocol (random-zebra)
f892e45 [Core/PoS] Enforce timestamp mask in miner (random-zebra)
cdaf818 [Core] Define new Past Time Limit and enforce masked time (random-zebra)
bcb5836 [Core] Define new Future Time Drift (random-zebra)
159eda9 [Cleanup] Remove unused function CheckCoinStakeTimestamp (random-zebra)
9773c09 [Core] Add timestamp mask to chainparams (random-zebra)
1d8fb33 [COPY] Update missing headers (random-zebra)
6f09121 [Consensus] Add placeholder block height for Time Protocol V2 (random-zebra)

Pull request description:

  Main changes:

  - **granularity**: 15 secs, instead of 1
  - **future limit**: adjustedTime + 14 secs (current timeslot) instead of 3 mins
  - **past limit**: prevBlock time
  - **target timespan**: 15 mins (instead of 40)

ACKs for top commit:
  furszy:
    Great, ACK 4ea0476
  Mrs-X:
    ACK 4ea0476

Tree-SHA512: a365297ba169f78ad1c30cc2f318dce09d6e98038a43d35c645814923829e793147e7fdbcbb3f701878a1f5f4892f468f19451fcacc25973db63c92c02e40aa1
  • Loading branch information
furszy committed Nov 22, 2019
2 parents 8d425bd + 4ea0476 commit 5666184
Show file tree
Hide file tree
Showing 21 changed files with 295 additions and 136 deletions.
3 changes: 3 additions & 0 deletions COPYING
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Copyright (c) 2009-2015 Bitcoin Developers
Copyright (c) 2014-2015 Dash Developers
Copyright (c) 2011-2013 The PPCoin developers
Copyright (c) 2013-2014 The NovaCoin Developers
Copyright (c) 2014-2018 The BlackCoin Developers
Copyright (c) 2015-2019 PIVX Developers

Permission is hereby granted, free of charge, to any person obtaining a copy
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 @@ -121,6 +121,9 @@ def compile_copyright_regex(copyright_style, year_style, name):
"The Dash Core developers\n",
"The PIVX developers\n",
"The PPCoin developers\n",
"The NovaCoin Developers",
"The BlackCoin Developers\n",
"The Blackcoin More developers\n",
]

DOMINANT_STYLE_COMPILED = {}
Expand Down
25 changes: 25 additions & 0 deletions src/chain.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin developers
// Copyright (c) 2011-2013 The PPCoin developers
// Copyright (c) 2013-2014 The NovaCoin Developers
// Copyright (c) 2014-2018 The BlackCoin Developers
// Copyright (c) 2015-2019 The PIVX developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
Expand All @@ -10,6 +13,7 @@
#include "chainparams.h"
#include "pow.h"
#include "primitives/block.h"
#include "timedata.h"
#include "tinyformat.h"
#include "uint256.h"
#include "util.h"
Expand Down Expand Up @@ -339,6 +343,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
52 changes: 40 additions & 12 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,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 @@ -151,20 +169,27 @@ class CMainParams : public CChainParams
vAlertPubKey = ParseHex("0000098d3ba6ba6e7423fa5cbd6a89e0a9a5348f88d332b44a5cb1a8b7ed2c1eaa335fc8dc4f012cb8241cc0bdafd6ca70c5f5448916e4e6f511bcd746ed57dc50");
nDefaultPort = 51472;
bnProofOfWorkLimit = ~uint256(0) >> 20; // PIVX 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; // 1 minute
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 = 600;
nFutureTimeDriftPoW = 7200;
nFutureTimeDriftPoS = 180;
nMasternodeCountDrift = 20;
nMaxMoneyOut = 21000000 * COIN;
nMinColdStakingAmount = 1 * COIN;
nTimeSlotLength = 15; // 15 seconds

/** Height or Time Based Activations **/
nLastPOWBlock = 259200;
Expand All @@ -185,12 +210,13 @@ class CMainParams : public CChainParams
nRejectOldSporkKey = 1569538800; //!> Fully reject old spork key after Thursday, September 26, 2019 11:00:00 PM GMT
nBlockStakeModifierlV2 = 1967000;
nBIP65ActivationHeight = 1808634;
nBlockTimeProtocolV2 = 2967000;

// Public coin spend enforcement
nPublicZCSpends = 1880000;

// New P2P messages signatures
nBlockEnforceNewMessageSignatures = 2967000;
nBlockEnforceNewMessageSignatures = nBlockTimeProtocolV2;

// Fake Serial Attack
nFakeSerialBlockheightEnd = 1686229;
Expand Down Expand Up @@ -304,7 +330,6 @@ class CTestNetParams : public CMainParams
nRejectBlockOutdatedMajority = 5472; // 95%
nToCheckBlockUpgradeMajority = 5760; // 4 days
nMinerThreads = 0;
nTargetSpacing = 1 * 60; // PIVX: 1 minute
nLastPOWBlock = 200;
nPivxBadBlockTime = 1489001494; // Skip nBit validation of Block 259201 per PR #915
nPivxBadBlocknBits = 0x1e0a20bd; // Skip nBit validation of Block 201 per PR #915
Expand All @@ -326,12 +351,13 @@ class CTestNetParams : public CMainParams
nRejectOldSporkKey = 1569538800; //!> Reject old spork key after Thursday, September 26, 2019 11:00:00 PM GMT
nBlockStakeModifierlV2 = 1214000;
nBIP65ActivationHeight = 851019;
nBlockTimeProtocolV2 = 2214000;

// Public coin spend enforcement
nPublicZCSpends = 1106100;

// New P2P messages signatures
nBlockEnforceNewMessageSignatures = 2214000;
nBlockEnforceNewMessageSignatures = nBlockTimeProtocolV2;

// Fake Serial Attack
nFakeSerialBlockheightEnd = -1;
Expand Down Expand Up @@ -408,13 +434,14 @@ class CRegTestParams : public CTestNetParams
nRejectBlockOutdatedMajority = 950;
nToCheckBlockUpgradeMajority = 1000;
nMinerThreads = 1;
nTargetSpacing = 1 * 60; // PIVX: 1 minutes
bnProofOfWorkLimit = ~uint256(0) >> 1;
nLastPOWBlock = 250;
nMaturity = 100;
nStakeMinAge = 0;
nStakeMinDepth = 0;
nTimeSlotLength = 1; // time not masked on RegNet
nMasternodeCountDrift = 4;
nModifierUpdateBlock = 0; //approx Mon, 17 Apr 2017 04:00:00 GMT
nModifierUpdateBlock = 0; //approx Mon, 17 Apr 2017 04:00:00 GMT
nMaxMoneyOut = 43199500 * COIN;
nZerocoinStartHeight = 300;
nBlockZerocoinV2 = 300;
Expand All @@ -423,7 +450,8 @@ class CRegTestParams : public CTestNetParams
nBlockRecalculateAccumulators = 999999999; //Trigger a recalculation of accumulators
nBlockFirstFraudulent = 999999999; //First block that bad serials emerged
nBlockLastGoodCheckpoint = 999999999; //Last valid accumulator checkpoint
nBlockStakeModifierlV2 = std::numeric_limits<int>::max(); // max integer value (never switch on regtest)
nBlockStakeModifierlV2 = 255;
nBlockTimeProtocolV2 = 999999999;

// Public coin spend enforcement
nPublicZCSpends = 350;
Expand Down
19 changes: 16 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,17 +75,22 @@ class CChainParams
/** Make standard checks */
bool RequireStandard() const { return fRequireStandard; }
int64_t TargetSpacing() const { return nTargetSpacing; }
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; }
uint32_t MaxFutureBlockTime(uint32_t time, const bool isPoS) const { return time + FutureBlockTimeDrift(isPoS); }
/** 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;

CAmount MaxMoneyOut() const { return nMaxMoneyOut; }
/** The masternode count that we will allow the see-saw reward payments to be off by */
Expand Down Expand Up @@ -164,20 +170,26 @@ 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 nPivxBadBlockTime;
unsigned int nPivxBadBlocknBits;
int nMasternodeCountDrift;
int nMaturity;
int nStakeMinDepth;
int nStakeMinAge;
int nFutureTimeDriftPoW;
int nFutureTimeDriftPoS;
int nTimeSlotLength;

int nModifierUpdateBlock;
CAmount nMaxMoneyOut;
Expand Down Expand Up @@ -229,6 +241,7 @@ class CChainParams
int nBlockDoubleAccumulated;
int nPublicZCSpends;
int nBlockStakeModifierlV2;
int nBlockTimeProtocolV2;
int nBlockEnforceNewMessageSignatures;

CAmount nMinColdStakingAmount;
Expand Down
3 changes: 3 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin developers
// Copyright (c) 2014-2015 The Dash developers
// Copyright (c) 2011-2013 The PPCoin developers
// Copyright (c) 2013-2014 The NovaCoin Developers
// Copyright (c) 2014-2018 The BlackCoin Developers
// Copyright (c) 2015-2019 The PIVX developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
Expand Down

0 comments on commit 5666184

Please sign in to comment.