Skip to content

Commit

Permalink
DigiShield Test Code
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Oct 7, 2017
1 parent ea4a731 commit ffa5ade
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 13 deletions.
102 changes: 96 additions & 6 deletions src/main.cpp
@@ -1,7 +1,7 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2012 The Bitcoin developers
// Copyright (c) 2011-2012 Litecoin Developers
// Copyright (c) 2014 Zombiecoin Developers
// Copyright (c) 2014-2017 Zombiecoin Developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

Expand Down Expand Up @@ -954,6 +954,91 @@ unsigned int static KimotoGravityWell(const CBlockIndex* pindexLast, const CBloc
return bnNew.GetCompact();
}

//DigiShield Implementation

unsigned int static DigiShield(const CBlockIndex* pindexLast, const CBlock *pblock)
{
// DigiShield difficulty retarget system
// Credits to DigiByte developers
unsigned int nProofOfWorkLimit = bnProofOfWorkLimit.GetCompact();

int blockstogoback = 0;

//set default to pre-v2.0 values
int64 retargetTimespan = nTargetSpacing; //Make sure we retarget every block
int64 retargetSpacing = nTargetSpacing;
int64 retargetInterval = retargetTimespan / retargetSpacing;
// Genesis block
if (pindexLast == NULL) return nProofOfWorkLimit;

// Only change once per interval
if ((pindexLast->nHeight+1) % retargetInterval != 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 + retargetSpacing*2)
return nProofOfWorkLimit;
else {
// Return the last non-special-min-difficulty-rules-block
const CBlockIndex* pindex = pindexLast;
while (pindex->pprev && pindex->nHeight % retargetInterval != 0 && pindex->nBits == nProofOfWorkLimit)
pindex = pindex->pprev;
return pindex->nBits;
}
}
return pindexLast->nBits;
}

// DigiByte: 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
blockstogoback = retargetInterval-1;
if ((pindexLast->nHeight+1) != retargetInterval) blockstogoback = retargetInterval;

// 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);



CBigNum bnNew;
bnNew.SetCompact(pindexLast->nBits);

if (nActualTimespan < (retargetTimespan - (retargetTimespan/4)) ) nActualTimespan = (retargetTimespan - (retargetTimespan/4));
if (nActualTimespan > (retargetTimespan + (retargetTimespan/2)) ) nActualTimespan = (retargetTimespan + (retargetTimespan/2));

// Retarget
bnNew *= nActualTimespan;
bnNew /= retargetTimespan;

/// debug print
printf("DigiShield RETARGET \n");
printf("retargetTimespan = %"PRI64d" nActualTimespan = %"PRI64d"\n", retargetTimespan, 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());


if (bnNew > bnProofOfWorkLimit)
bnNew = bnProofOfWorkLimit;



return bnNew.GetCompact();
}

//End DigiShield Core Code






unsigned int static GetNextWorkRequired_V2(const CBlockIndex* pindexLast, const CBlock *pblock)
{
static const int64 BlocksTargetSpacing = 2.5 * 60; // 2.5 minute
Expand Down Expand Up @@ -1047,16 +1132,21 @@ unsigned int static GetNextWorkRequired_V1(const CBlockIndex* pindexLast, const
unsigned int static GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlock *pblock)
{
int DiffMode = 1;
if (fTestNet) {
if (fTestNet)
{
if (pindexLast->nHeight+1 >= FIX_RETARGET_HEIGHT){ DiffMode=2; }
}
else {
if (pindexLast->nHeight+1 >= FIX_RETARGET_HEIGHT){ DiffMode=2; }
else
{
if (pindexLast->nHeight+1 >= FIX_RETARGET_HEIGHT && pindexLast->nHeight+1 < 465000) { DiffMode=2; }
else if (pindexLast->nHeight+1 >= 465000) { DiffMode = 3; }
}

if (DiffMode == 1) { return GetNextWorkRequired_V1(pindexLast, pblock); }
else if (DiffMode == 2) { return GetNextWorkRequired_V2(pindexLast, pblock); }
return GetNextWorkRequired_V2(pindexLast, pblock);
else if (DiffMode == 3) { return DigiShield(pindexLast, pblock); }
return DigiShield(pindexLast, pblock);
// return GetNextWorkRequired_V2(pindexLast, pblock);
}

bool CheckProofOfWork(uint256 hash, unsigned int nBits)
Expand Down
102 changes: 96 additions & 6 deletions src/main.cpp~
@@ -1,7 +1,7 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2012 The Bitcoin developers
// Copyright (c) 2011-2012 Litecoin Developers
// Copyright (c) 2014 Zombiecoin Developers
// Copyright (c) 2014-2017 Zombiecoin Developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

Expand Down Expand Up @@ -954,6 +954,91 @@ unsigned int static KimotoGravityWell(const CBlockIndex* pindexLast, const CBloc
return bnNew.GetCompact();
}

//DigiShield Implementation

unsigned int static DigiShield(const CBlockIndex* pindexLast, const CBlock *pblock)
{
// DigiShield difficulty retarget system
// Credits to DigiByte developers
unsigned int nProofOfWorkLimit = bnProofOfWorkLimit.GetCompact();

int blockstogoback = 0;

//set default to pre-v2.0 values
int64 retargetTimespan = nTargetSpacing; //Make sure we retarget every block
int64 retargetSpacing = nTargetSpacing;
int64 retargetInterval = retargetTimespan / retargetSpacing;
// Genesis block
if (pindexLast == NULL) return nProofOfWorkLimit;

// Only change once per interval
if ((pindexLast->nHeight+1) % retargetInterval != 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 + retargetSpacing*2)
return nProofOfWorkLimit;
else {
// Return the last non-special-min-difficulty-rules-block
const CBlockIndex* pindex = pindexLast;
while (pindex->pprev && pindex->nHeight % retargetInterval != 0 && pindex->nBits == nProofOfWorkLimit)
pindex = pindex->pprev;
return pindex->nBits;
}
}
return pindexLast->nBits;
}

// DigiByte: 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
blockstogoback = retargetInterval-1;
if ((pindexLast->nHeight+1) != retargetInterval) blockstogoback = retargetInterval;

// 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);



CBigNum bnNew;
bnNew.SetCompact(pindexLast->nBits);

if (nActualTimespan < (retargetTimespan - (retargetTimespan/4)) ) nActualTimespan = (retargetTimespan - (retargetTimespan/4));
if (nActualTimespan > (retargetTimespan + (retargetTimespan/2)) ) nActualTimespan = (retargetTimespan + (retargetTimespan/2));

// Retarget
bnNew *= nActualTimespan;
bnNew /= retargetTimespan;

/// debug print
printf("DigiShield RETARGET \n");
printf("retargetTimespan = %"PRI64d" nActualTimespan = %"PRI64d"\n", retargetTimespan, 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());


if (bnNew > bnProofOfWorkLimit)
bnNew = bnProofOfWorkLimit;



return bnNew.GetCompact();
}

//End DigiShield Core Code






unsigned int static GetNextWorkRequired_V2(const CBlockIndex* pindexLast, const CBlock *pblock)
{
static const int64 BlocksTargetSpacing = 2.5 * 60; // 2.5 minute
Expand Down Expand Up @@ -1047,16 +1132,21 @@ unsigned int static GetNextWorkRequired_V1(const CBlockIndex* pindexLast, const
unsigned int static GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlock *pblock)
{
int DiffMode = 1;
if (fTestNet) {
if (fTestNet)
{
if (pindexLast->nHeight+1 >= FIX_RETARGET_HEIGHT){ DiffMode=2; }
}
else {
if (pindexLast->nHeight+1 >= FIX_RETARGET_HEIGHT){ DiffMode=2; }
else
{
if (pindexLast->nHeight+1 >= FIX_RETARGET_HEIGHT && pindexLast->nHeight+1 < 465000) { DiffMode=2; }
else if (pindexLast->nHeight+1 >= 465000) { DiffMode = 3; }
}

if (DiffMode == 1) { return GetNextWorkRequired_V1(pindexLast, pblock); }
else if (DiffMode == 2) { return GetNextWorkRequired_V2(pindexLast, pblock); }
return GetNextWorkRequired_V2(pindexLast, pblock);
else if (DiffMode == 3) { return DigiShield(pindexLast, pblock); }
return DigiShield(pindexLast, pblock);
// return GetNextWorkRequired_V2(pindexLast, pblock);
}

bool CheckProofOfWork(uint256 hash, unsigned int nBits)
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Expand Up @@ -13,7 +13,7 @@

// These need to be macro's, as version.cpp's voodoo requires it
#define CLIENT_VERSION_MAJOR 1
#define CLIENT_VERSION_MINOR 1
#define CLIENT_VERSION_MINOR 2 //DigiShield test implementation
#define CLIENT_VERSION_REVISION 1
#define CLIENT_VERSION_BUILD 3

Expand Down
Binary file modified src/zombiecoind
Binary file not shown.

0 comments on commit ffa5ade

Please sign in to comment.