Skip to content

Commit

Permalink
Add checkpoint at block 14189, v3 lock-in since supermajority and bnP…
Browse files Browse the repository at this point in the history
…roofOfStakeLimit switch since block #15000
  • Loading branch information
CryptoManiac committed Apr 6, 2013
1 parent c34c841 commit 10db7ed
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 228 deletions.
1 change: 1 addition & 0 deletions src/checkpoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace Checkpoints
( 6000, uint256("0x000000000945e3c9d8e15df834e802521eb79f9ceb4191a27bdfadad4b777f4a"))
( 8700, uint256("0x00000000014270724837789c9a69859290f6bdee38556bc4561c21f17935a178"))
( 13560, uint256("0xa1591a0fcbf11f282d671581edb9f0aadcd06fee69761081e0a3245914c13729"))
( 14189, uint256("0x00000000020f76474d2522b19c7bfafc43ba6ecbabae54293bcd9546159c8c1d"))
;

bool CheckHardened(int nHeight, const uint256& hash)
Expand Down
92 changes: 62 additions & 30 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ map<uint256, CBlockIndex*> mapBlockIndex;
set<pair<COutPoint, unsigned int> > setStakeSeen;
uint256 hashGenesisBlock = hashGenesisBlockOfficial;
static CBigNum bnProofOfWorkLimit(~uint256(0) >> 20);
static CBigNum bnProofOfStakeLimit(~uint256(0) >> 30);
static CBigNum bnProofOfStakeLimit(~uint256(0) >> 24);
static CBigNum bnProofOfStakeHardLimit(~uint256(0) >> 30);
static CBigNum bnInitialHashTarget(~uint256(0) >> 20);
unsigned int nStakeMinAge = STAKE_MIN_AGE;
int nCoinbaseMaturity = COINBASE_MATURITY_PPC;
Expand Down Expand Up @@ -891,19 +892,30 @@ static const int64 nTargetSpacingWorkMax = 12 * STAKE_TARGET_SPACING; // 2-hour
// minimum amount of work that could possibly be required nTime after
// minimum work required was nBase
//
unsigned int ComputeMinWork(unsigned int nBase, int64 nTime)
unsigned int ComputeMinWork(unsigned int nBase, int64 nTime, bool fProofOfStake, int nHeight)
{
CBigNum bnTargetLimit = bnProofOfWorkLimit;

if(fProofOfStake)
{
// Proof-of-Stake blocks has own target limit since nVersion=3 supermajority on mainNet and always on testNet
if(fTestNet || nHeight > 15000)
bnTargetLimit = bnProofOfStakeLimit;
else if(nHeight > 14060)
bnTargetLimit = bnProofOfStakeHardLimit;
}

CBigNum bnResult;
bnResult.SetCompact(nBase);
bnResult *= 2;
while (nTime > 0 && bnResult < bnProofOfWorkLimit)
while (nTime > 0 && bnResult < bnTargetLimit)
{
// Maximum 200% adjustment per day...
bnResult *= 2;
nTime -= 24 * 60 * 60;
}
if (bnResult > bnProofOfWorkLimit)
bnResult = bnProofOfWorkLimit;
if (bnResult > bnTargetLimit)
bnResult = bnTargetLimit;
return bnResult.GetCompact();
}

Expand All @@ -917,7 +929,16 @@ const CBlockIndex* GetLastBlockIndex(const CBlockIndex* pindex, bool fProofOfSta

unsigned int static GetNextTargetRequired(const CBlockIndex* pindexLast, bool fProofOfStake)
{
CBigNum bnTargetLimit = (fProofOfStake && CBlockIndex::IsSuperMajority(3, pindexLast, 950, 1000)) ? bnProofOfStakeLimit : bnProofOfWorkLimit;
CBigNum bnTargetLimit = bnProofOfWorkLimit;

if(fProofOfStake)
{
// Proof-of-Stake blocks has own target limit since nVersion=3 supermajority on mainNet and always on testNet
if(fTestNet || (pindexLast->nHeight + 1 > 15000))
bnTargetLimit = bnProofOfStakeLimit;
else if(pindexLast->nHeight + 1 > 14060)
bnTargetLimit = bnProofOfStakeHardLimit;
}

if (pindexLast == NULL)
return bnTargetLimit.GetCompact(); // genesis block
Expand Down Expand Up @@ -1924,6 +1945,25 @@ bool CBlock::CheckBlock() const
return true;
}

int CBlock::GetBlockHeight() const
{
if(nVersion == 1)
return 0;

if(vtx[0].vin[0].scriptSig[0] > 4)
return vtx[0].vin[0].scriptSig[0] - 80;

int nBlockHeight = 0;

memcpy((void *)&nBlockHeight, (const void *)&vtx[0].vin[0].scriptSig[1], vtx[0].vin[0].scriptSig[0]);

#ifdef BIGENDIAN
return htonl(nBlockHeight);
#else
return nBlockHeight;
#endif
}

bool CBlock::AcceptBlock()
{
// Check for duplicate
Expand Down Expand Up @@ -1959,22 +1999,21 @@ bool CBlock::AcceptBlock()
if (!Checkpoints::CheckSync(hash, pindexPrev))
return error("AcceptBlock() : rejected by synchronized checkpoint");

// Reject block.nVersion < 3 blocks when 95% (75% on testnet) of the network has upgraded:
if (nVersion < 3)
{
if ((!fTestNet && CBlockIndex::IsSuperMajority(3, pindexPrev, 950, 1000)) || (fTestNet && CBlockIndex::IsSuperMajority(3, pindexPrev, 75, 100)))
{
return error("CheckBlock() : rejected nVersion < 3 block");
}
}
// Reject block.nVersion < 3 blocks since 95% threshold on mainNet and always on testNet:
if (nVersion < 3 && ((!fTestNet && nHeight > 14060) || (fTestNet && nHeight > 0)))
return error("CheckBlock() : rejected nVersion < 3 block");

if(nHeight > 0)
{
CScript expect = CScript() << nHeight;
CScript expect = CScript() << nHeight;
if (!std::equal(expect.begin(), expect.end(), vtx[0].vin[0].scriptSig.begin()))
return DoS(100, error("AcceptBlock() : block height mismatch in coinbase"));

/**
* TODO: replace previous check with this.
*/

// if(nHeight != GetBlockHeight())
// return DoS(100, error("AcceptBlock() : block height mismatch in coinbase"));

if (!std::equal(expect.begin(), expect.end(), vtx[0].vin[0].scriptSig.begin()))
return DoS(100, error("AcceptBlock() : block height mismatch in coinbase"));
}

// Write block to history file
if (!CheckDiskSpace(::GetSerializeSize(*this, SER_DISK, CLIENT_VERSION)))
Expand Down Expand Up @@ -2042,7 +2081,7 @@ bool ProcessBlock(CNode* pfrom, CBlock* pblock)
CBigNum bnNewBlock;
bnNewBlock.SetCompact(pblock->nBits);
CBigNum bnRequired;
bnRequired.SetCompact(ComputeMinWork(GetLastBlockIndex(pcheckpoint, pblock->IsProofOfStake())->nBits, deltaTime));
bnRequired.SetCompact(ComputeMinWork(GetLastBlockIndex(pcheckpoint, pblock->IsProofOfStake())->nBits, deltaTime, pblock->IsProofOfStake(), pblock->GetBlockHeight()));

if (bnNewBlock > bnRequired)
{
Expand Down Expand Up @@ -2124,11 +2163,10 @@ bool CBlock::SignBlock(const CKeyStore& keystore)
{
vector<valtype> vSolutions;
txnouttype whichType;
int nVouts = nTime < 1361664000 ? 1 : vtx[0].vout.size();

if(!IsProofOfStake())
{
for(int i = 0; i < nVouts; i++)
for(int i = 0; i < vtx[0].vout.size(); i++)
{
const CTxOut& txout = vtx[0].vout[i];

Expand Down Expand Up @@ -2186,7 +2224,6 @@ bool CBlock::CheckBlockSignature() const

vector<valtype> vSolutions;
txnouttype whichType;
int nVouts = nTime < 1361664000 ? 1 : vtx[0].vout.size();

if(IsProofOfStake())
{
Expand All @@ -2207,7 +2244,7 @@ bool CBlock::CheckBlockSignature() const
}
else
{
for(int i = 0; i < nVouts; i++)
for(int i = 0; i < vtx[0].vout.size(); i++)
{
const CTxOut& txout = vtx[0].vout[i];

Expand All @@ -2233,11 +2270,6 @@ bool CBlock::CheckBlockSignature() const
return false;
}






bool CheckDiskSpace(uint64 nAdditionalBytes)
{
uint64 nFreeBytesAvailable = filesystem::space(GetDataDir()).available;
Expand Down
18 changes: 2 additions & 16 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey);
bool CheckProofOfWork(uint256 hash, unsigned int nBits);
int64 GetProofOfWorkReward(unsigned int nBits);
int64 GetProofOfStakeReward(int64 nCoinAge);
unsigned int ComputeMinWork(unsigned int nBase, int64 nTime);
unsigned int ComputeMinWork(unsigned int nBase, int64 nTime, bool fProofOfStake, int nHeight);
int GetNumBlocksOfPeers();
bool IsInitialBlockDownload();
std::string GetWarnings(std::string strFor);
Expand Down Expand Up @@ -957,6 +957,7 @@ class CBlock
return thash;
}

int GetBlockHeight() const;

int64 GetBlockTime() const
{
Expand Down Expand Up @@ -1365,18 +1366,6 @@ class CBlockIndex
return (nFlags & BLOCK_PROOF_OF_STAKE);
}

static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned int nRequired, unsigned int nToCheck)
{
unsigned int nFound = 0;
for (unsigned int i = 0; i < nToCheck && nFound < nRequired && pstart != NULL; i++)
{
if (pstart->nVersion >= minVersion)
++nFound;
pstart = pstart->pprev;
}
return (nFound >= nRequired);
}

void SetProofOfStake()
{
nFlags |= BLOCK_PROOF_OF_STAKE;
Expand Down Expand Up @@ -1434,13 +1423,11 @@ class CDiskBlockIndex : public CBlockIndex
public:
uint256 hashPrev;
uint256 hashNext;
int nProtocolVersion;

CDiskBlockIndex()
{
hashPrev = 0;
hashNext = 0;
nProtocolVersion = PROTOCOL_VERSION;
}

explicit CDiskBlockIndex(CBlockIndex* pindex) : CBlockIndex(*pindex)
Expand All @@ -1462,7 +1449,6 @@ class CDiskBlockIndex : public CBlockIndex
READWRITE(nMoneySupply);
READWRITE(nFlags);
READWRITE(nStakeModifier);
READWRITE(nProtocolVersion);
if (IsProofOfStake())
{
READWRITE(prevoutStake);
Expand Down
Loading

0 comments on commit 10db7ed

Please sign in to comment.