Skip to content

Commit

Permalink
Update NeoScrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar committed Sep 1, 2017
1 parent 601e483 commit 2dd3723
Show file tree
Hide file tree
Showing 9 changed files with 10,654 additions and 1,159 deletions.
15 changes: 15 additions & 0 deletions src/init.cpp
Expand Up @@ -63,6 +63,8 @@ enum BindFlags {
BF_REPORT_ERROR = (1U << 1)
};

/* Assembly level processor optimisation features */
unsigned int opt_flags = 0;

//////////////////////////////////////////////////////////////////////////////
//
Expand Down Expand Up @@ -473,6 +475,19 @@ bool AppInit2(boost::thread_group& threadGroup)

// ********************************************************* Step 2: parameter interactions

opt_flags = cpu_vec_exts();
if(GetBoolArg("-sse2", true)) {
/* Verify hardware SSE2 support */
if(opt_flags & 0x00000020) {
printf("SSE2 optimisations enabled\n");
nNeoScryptOptions |= 0x1000;
} else {
printf("SSE2 unsupported, optimisations disabled\n");
}
} else {
printf("SSE2 optimisations disabled\n");
}

if (mapArgs.count("-bind")) {
// when specifying an explicit binding address, you want to listen on it
// even when -connect or -proxy is specified
Expand Down
68 changes: 42 additions & 26 deletions src/miner.cpp
Expand Up @@ -410,6 +410,42 @@ void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash
memcpy(phash1, &tmp.hash1, 64);
}

/* Prepares a block header for transmission using RPC getwork */
void FormatDataBuffer(CBlock *pblock, unsigned int *pdata) {
unsigned int i;

struct {
int nVersion;
uint256 hashPrevBlock;
uint256 hashMerkleRoot;
unsigned int nTime;
unsigned int nBits;
unsigned int nNonce;
} data;

data.nVersion = pblock->nVersion;
data.hashPrevBlock = pblock->hashPrevBlock;
data.hashMerkleRoot = pblock->hashMerkleRoot;
data.nTime = pblock->nTime;
data.nBits = pblock->nBits;
data.nNonce = pblock->nNonce;

if(fNeoScrypt) {
/* Copy the LE data */
for(i = 0; i < 20; i++)
pdata[i] = ((unsigned int *) &data)[i];
} else {
/* Block header size in bits */
pdata[31] = 640;
/* Convert LE to BE and copy */
for(i = 0; i < 20; i++)
pdata[i] = ByteReverse(((unsigned int *) &data)[i]);
/* Erase the remaining part */
for(i = 20; i < 31; i++)
pdata[i] = 0;
}
}

#ifdef ENABLE_WALLET
//////////////////////////////////////////////////////////////////////////////
//
Expand Down Expand Up @@ -538,40 +574,22 @@ void static FeathercoinMiner(CWallet *pwallet)

// if (fDebug) LogPrintf("Running FeathercoinMiner with %u transactions in block (%u bytes)\n", pblock->vtx.size(), ::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION));

//
// Pre-build hash buffers
//
char pmidstatebuf[32+16]; char* pmidstate = alignup<16>(pmidstatebuf);
char pdatabuf[128+16]; char* pdata = alignup<16>(pdatabuf);
char phash1buf[64+16]; char* phash1 = alignup<16>(phash1buf);

FormatHashBuffers(pblock, pmidstate, pdata, phash1);

unsigned int& nBlockTime = *(unsigned int*)(pdata + 64 + 4);
unsigned int& nBlockBits = *(unsigned int*)(pdata + 64 + 8);
unsigned int& nBlockNonce = *(unsigned int*)(pdata + 64 + 12);


//
// Search
//
int64_t nStart = GetTime();
uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
uint256 hashbuf[2];
uint256& hash = *alignup<16>(hashbuf);
while (true)
{
unsigned int nHashesDone = 0;
unsigned int profile = fNeoScrypt ? 0x0 : 0x3;
uint256 thash;
//char scratchpad[SCRYPT_SCRATCHPAD_SIZE];
unsigned int profile = fNeoScrypt ? 0x0 : 0x3;
uint256 hash;

while (true)
{

//scrypt_1024_1_1_256_sp(BEGIN(pblock->nVersion), BEGIN(thash), scratchpad);
neoscrypt((unsigned char *) &pblock->nVersion, (unsigned char *) &hash, profile);

if (thash <= hashTarget)
if (hash <= hashTarget)
{
// Found a solution
SetThreadPriority(THREAD_PRIORITY_NORMAL);
Expand Down Expand Up @@ -625,7 +643,7 @@ void static FeathercoinMiner(CWallet *pwallet)
boost::this_thread::interruption_point();
if (vNodes.empty() && Params().NetworkID() != CChainParams::REGTEST)
break;
if (nBlockNonce >= 0xffff0000)
if (pblock->nNonce >= 0xffff0000)
break;
if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60)
break;
Expand All @@ -634,11 +652,9 @@ void static FeathercoinMiner(CWallet *pwallet)

// Update nTime every few seconds
UpdateTime(*pblock, pindexPrev);
nBlockTime = ByteReverse(pblock->nTime);
if (TestNet())
{
// Changing pblock->nTime can change work required on testnet:
nBlockBits = ByteReverse(pblock->nBits);
/* UpdateTime() can change work required on testnet */
hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/miner.h
Expand Up @@ -24,6 +24,7 @@ CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey);
void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
/** Do mining precalculation */
void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash1);
void FormatDataBuffer(CBlock *pblock, unsigned int *pdata);
/** Check mined block */
bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey);
/** Base sha256 mining transform */
Expand Down

0 comments on commit 2dd3723

Please sign in to comment.