Skip to content

Commit

Permalink
Fork to new algo block 76800
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar committed Aug 11, 2014
1 parent fe204f5 commit 0b8c280
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 9 deletions.
30 changes: 30 additions & 0 deletions src/hashblock.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,37 @@ inline uint256 Hash5(const T1 pbegin, const T1 pend)
}


template<typename T1>
inline uint256 Hash3(const T1 pbegin, const T1 pend)

{
sph_luffa512_context ctx_luffa;
sph_cubehash512_context ctx_cubehash;
sph_echo512_context ctx_echo;
static unsigned char pblank[1];

#ifndef QT_NO_DEBUG
//std::string strhash;
//strhash = "";
#endif


uint512 hash[3];

sph_luffa512_init(&ctx_luffa);
sph_luffa512 (&ctx_luffa, (pbegin == pend ? pblank : static_cast<const void*>(&pbegin[0])), (pend - pbegin) * sizeof(pbegin[0]));
sph_luffa512_close(&ctx_luffa, static_cast<void*>(&hash[0]));

sph_cubehash512_init(&ctx_cubehash);
sph_cubehash512 (&ctx_cubehash, static_cast<const void*>(&hash[0]), 64);
sph_cubehash512_close(&ctx_cubehash, static_cast<void*>(&hash[1]));

sph_echo512_init(&ctx_echo);
sph_echo512 (&ctx_echo, static_cast<const void*>(&hash[1]), 64);
sph_echo512_close(&ctx_echo, static_cast<void*>(&hash[2]));

return hash[2].trim256();
}



Expand Down
89 changes: 84 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ unsigned int nTransactionsUpdated = 0;
map<uint256, CBlockIndex*> mapBlockIndex;
uint256 hashGenesisBlock("0x0000003078b41d235f667a0908091ba42b1a5bbcdb62b24f8101fc302f32a2ee");
static CBigNum bnProofOfWorkLimit(~uint256(0) >> 26);
static CBigNum difficultyReset(~uint256(0) >> 32);
CBlockIndex* pindexGenesisBlock = NULL;
int nBestHeight = -1;
uint256 nBestChainWork = 0;
Expand Down Expand Up @@ -1127,6 +1128,75 @@ unsigned int ComputeMinWork(unsigned int nBase, int64 nTime)
return bnResult.GetCompact();
}

unsigned int static GetNextWorkRequired_V1(const CBlockIndex* pindexLast, const CBlockHeader *pblock)
{
unsigned int nProofOfWorkLimit = bnProofOfWorkLimit.GetCompact();

// Genesis block
if (pindexLast == NULL)
return nProofOfWorkLimit;

// Only change once per interval
if ((pindexLast->nHeight+1) % nInterval != 0)
{
// Special difficulty rule for testnet:
if (fTestNet)
{
// If the new block's timestamp is more than 2* 10 minutes
// then allow mining of a min-difficulty block.
if (pblock->nTime > pindexLast->nTime + nTargetSpacing*2)
return nProofOfWorkLimit;
else
{
// Return the last non-special-min-difficulty-rules-block
const CBlockIndex* pindex = pindexLast;
while (pindex->pprev && pindex->nHeight % nInterval != 0 && pindex->nBits == nProofOfWorkLimit)
pindex = pindex->pprev;
return pindex->nBits;
}
}

return pindexLast->nBits;
}

// This fixes an issue where a 51% attack can change difficulty at will.
// Go back the full period unless it's the first retarget after genesis. Code courtesy of Art Forz
int blockstogoback = nInterval-1;
if ((pindexLast->nHeight+1) != nInterval)
blockstogoback = nInterval;

// Go back by what we want to be 14 days worth of blocks
const CBlockIndex* pindexFirst = pindexLast;
for (int i = 0; pindexFirst && i < blockstogoback; i++)
pindexFirst = pindexFirst->pprev;
assert(pindexFirst);

// Limit adjustment step
int64 nActualTimespan = pindexLast->GetBlockTime() - pindexFirst->GetBlockTime();
printf(" nActualTimespan = %"PRI64d" before bounds\n", nActualTimespan);
if (nActualTimespan < nTargetTimespan/4)
nActualTimespan = nTargetTimespan/4;
if (nActualTimespan > nTargetTimespan*4)
nActualTimespan = nTargetTimespan*4;

// Retarget
CBigNum bnNew;
bnNew.SetCompact(pindexLast->nBits);
bnNew *= nActualTimespan;
bnNew /= nTargetTimespan;

if (bnNew > bnProofOfWorkLimit)
bnNew = bnProofOfWorkLimit;

/// debug print
printf("GetNextWorkRequired RETARGET\n");
printf("nTargetTimespan = %"PRI64d" nActualTimespan = %"PRI64d"\n", nTargetTimespan, nActualTimespan);
printf("Before: %08x %s\n", pindexLast->nBits, CBigNum().SetCompact(pindexLast->nBits).getuint256().ToString().c_str());
printf("After: %08x %s\n", bnNew.GetCompact(), bnNew.getuint256().ToString().c_str());

return bnNew.GetCompact();
}

unsigned int static NiteGravityWell(const CBlockIndex* pindexLast, const CBlockHeader *pblock, uint64 TargetBlocksSpacingSeconds, uint64 PastBlocksMin, uint64 PastBlocksMax) {
/* current difficulty formula, megacoin - kimoto gravity well */
const CBlockIndex *BlockLastSolved = pindexLast;
Expand Down Expand Up @@ -1188,8 +1258,16 @@ unsigned int static NiteGravityWell(const CBlockIndex* pindexLast, const CBlockH

unsigned int static GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock)
{
if (pindexLast->nHeight + 1 > 2851200)
nTargetSpacing = 2* 60; // Deepcoin: 2 minute block target after
int nHeight = pindexLast->nHeight + 1;
int diffMode = 0;
if (nHeight >= nHardFork || (fTestNet && (nHeight >= nTestFork))) {
if (!x3Fork) x3Fork = true;
nTargetSpacing = 2 * 60; // Deepcoin: 2 minute block target after
if (nHeight == nHardFork) return(difficultyReset.GetCompact());
if (nHeight < nHardFork + 10) diffMode = 1;
if (fTestNet && (nHeight == nTestFork)) return(difficultyReset.GetCompact());
if (fTestNet && (nHeight < nTestFork + 10)) diffMode = 1;
}

static const int64 BlocksTargetSpacing = nTargetSpacing;
static const unsigned int TimeDaySeconds = nTargetTimespan;
Expand All @@ -1198,6 +1276,7 @@ unsigned int static GetNextWorkRequired(const CBlockIndex* pindexLast, const CBl
uint64 PastBlocksMin = PastSecondsMin / BlocksTargetSpacing;
uint64 PastBlocksMax = PastSecondsMax / BlocksTargetSpacing;

if (diffMode == 1) return GetNextWorkRequired_V1(pindexLast, pblock);
return NiteGravityWell(pindexLast, pblock, BlocksTargetSpacing, PastBlocksMin, PastBlocksMax);
}

Expand Down Expand Up @@ -2120,7 +2199,7 @@ bool CBlock::CheckBlock(CValidationState &state, bool fCheckPOW, bool fCheckMerk
return state.DoS(100, error("CheckBlock() : size limits failed"));

// Check proof of work matches claimed amount
if (fCheckPOW && !CheckProofOfWork(GetHash(), nBits))
if (fCheckPOW && !CheckProofOfWork(GetPoWHash(), nBits))
return state.DoS(50, error("CheckBlock() : proof of work failed"));

// Check timestamp
Expand Down Expand Up @@ -4586,7 +4665,7 @@ void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash

bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey)
{
uint256 hash = pblock->GetHash();
uint256 hash = pblock->GetPoWHash();
uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();

if (hash > hashTarget)
Expand Down Expand Up @@ -4659,7 +4738,7 @@ void static DeepcoinMiner(CWallet *pwallet)
uint256 hash;
loop
{
hash = pblock->GetHash();
hash = pblock->GetPoWHash();
if (hash <= hashTarget){
// nHashesDone += pblock->nNonce;
SetThreadPriority(THREAD_PRIORITY_NORMAL);
Expand Down
30 changes: 26 additions & 4 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ static const int fHaveUPnP = true;
static const int fHaveUPnP = false;
#endif

static const int nTestFork = 40;
static const int nHardFork = 76800;
static const int nSoftFork = 1408406400;

extern CScript COINBASE_FLAGS;

Expand Down Expand Up @@ -1374,6 +1377,29 @@ class CBlock : public CBlockHeader
vMerkleTree.clear();
}

uint256 GetPoWHash() const
{
int nHeight = GetBlockHeight();
if ((nHeight >= nHardFork) || (fTestNet && (nHeight >= nTestFork))) {
return Hash3(BEGIN(nVersion), END(nNonce));
}
return Hash5(BEGIN(nVersion), END(nNonce));
}

int GetBlockHeight() const {
if(vtx.size()) {
std::vector<unsigned char>::const_iterator scriptsig = vtx[0].vin[0].scriptSig.begin();
unsigned char i, scount = scriptsig[0];
if(scount < 4) {
int height = 0;
unsigned char *pheight = (unsigned char *) &height;
for(i = 0; i < scount; i++)
pheight[i] = scriptsig[i + 1];
return(height);
}
}
return(-1);
}
CBlockHeader GetBlockHeader() const
{
CBlockHeader block;
Expand Down Expand Up @@ -1485,10 +1511,6 @@ class CBlock : public CBlockHeader
return error("%s() : deserialize or I/O error", __PRETTY_FUNCTION__);
}

// Check the header
if (!CheckProofOfWork(GetHash(), nBits))
return error("CBlock::ReadFromDisk() : errors in block header");

return true;
}

Expand Down
1 change: 1 addition & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ bool fPrintToConsole = false;
bool fPrintToDebugger = false;
bool fDaemon = false;
bool fServer = false;
bool x3Fork = false;
bool fCommandLine = false;
string strMiscWarning;
bool fTestNet = false;
Expand Down
1 change: 1 addition & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ extern bool fPrintToConsole;
extern bool fPrintToDebugger;
extern bool fDaemon;
extern bool fServer;
extern bool x3Fork;
extern bool fCommandLine;
extern std::string strMiscWarning;
extern bool fTestNet;
Expand Down

0 comments on commit 0b8c280

Please sign in to comment.