|
|
@ -159,7 +159,7 @@ bool AddToWallet(const CWalletTx& wtxIn) |
|
|
|
if (txout.scriptPubKey == scriptDefaultKey) |
|
|
|
{ |
|
|
|
CWalletDB walletdb; |
|
|
|
vchDefaultKey = walletdb.GetKeyFromKeyPool(); |
|
|
|
vchDefaultKey = GetKeyFromKeyPool(); |
|
|
|
walletdb.WriteDefaultKey(vchDefaultKey); |
|
|
|
walletdb.WriteName(PubKeyToAddress(vchDefaultKey), ""); |
|
|
|
} |
|
|
@ -1557,31 +1557,25 @@ bool ProcessBlock(CNode* pfrom, CBlock* pblock) |
|
|
|
|
|
|
|
// Preliminary checks
|
|
|
|
if (!pblock->CheckBlock()) |
|
|
|
{ |
|
|
|
delete pblock; |
|
|
|
return error("ProcessBlock() : CheckBlock FAILED"); |
|
|
|
} |
|
|
|
|
|
|
|
// If don't already have its previous block, shunt it off to holding area until we get it
|
|
|
|
if (!mapBlockIndex.count(pblock->hashPrevBlock)) |
|
|
|
{ |
|
|
|
printf("ProcessBlock: ORPHAN BLOCK, prev=%s\n", pblock->hashPrevBlock.ToString().substr(0,20).c_str()); |
|
|
|
mapOrphanBlocks.insert(make_pair(hash, pblock)); |
|
|
|
mapOrphanBlocksByPrev.insert(make_pair(pblock->hashPrevBlock, pblock)); |
|
|
|
CBlock* pblock2 = new CBlock(*pblock); |
|
|
|
mapOrphanBlocks.insert(make_pair(hash, pblock2)); |
|
|
|
mapOrphanBlocksByPrev.insert(make_pair(pblock2->hashPrevBlock, pblock2)); |
|
|
|
|
|
|
|
// Ask this guy to fill in what we're missing
|
|
|
|
if (pfrom) |
|
|
|
pfrom->PushGetBlocks(pindexBest, GetOrphanRoot(pblock)); |
|
|
|
pfrom->PushGetBlocks(pindexBest, GetOrphanRoot(pblock2)); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
// Store to disk
|
|
|
|
if (!pblock->AcceptBlock()) |
|
|
|
{ |
|
|
|
delete pblock; |
|
|
|
return error("ProcessBlock() : AcceptBlock FAILED"); |
|
|
|
} |
|
|
|
delete pblock; |
|
|
|
|
|
|
|
// Recursively process any orphan blocks that depended on this one
|
|
|
|
vector<uint256> vWorkQueue; |
|
|
@ -2504,7 +2498,7 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) |
|
|
|
CInv inv(MSG_BLOCK, pblock->GetHash()); |
|
|
|
pfrom->AddInventoryKnown(inv); |
|
|
|
|
|
|
|
if (ProcessBlock(pfrom, pblock.release())) |
|
|
|
if (ProcessBlock(pfrom, pblock.get())) |
|
|
|
mapAlreadyAskedFor.erase(inv); |
|
|
|
} |
|
|
|
|
|
|
@ -2549,7 +2543,7 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) |
|
|
|
|
|
|
|
// Keep giving the same key to the same ip until they use it
|
|
|
|
if (!mapReuseKey.count(pfrom->addr.ip)) |
|
|
|
mapReuseKey[pfrom->addr.ip] = CWalletDB().GetKeyFromKeyPool(); |
|
|
|
mapReuseKey[pfrom->addr.ip] = GetKeyFromKeyPool(); |
|
|
|
|
|
|
|
// Send back approval of order and pubkey to use
|
|
|
|
CScript scriptPubKey; |
|
|
@ -3000,19 +2994,19 @@ inline void SHA256Transform(void* pstate, void* pinput, const void* pinit) |
|
|
|
// ScanHash scans nonces looking for a hash with at least some zero bits.
|
|
|
|
// It operates on big endian data. Caller does the byte reversing.
|
|
|
|
// All input buffers are 16-byte aligned. nNonce is usually preserved
|
|
|
|
// between calls, but periodically or if nNonce is above 0xff000000,
|
|
|
|
// between calls, but periodically or if nNonce is 0xffff0000 or above,
|
|
|
|
// the block is rebuilt and nNonce starts over at zero.
|
|
|
|
//
|
|
|
|
unsigned int ScanHash_CryptoPP(char* pmidstate, char* pblock, char* phash1, char* phash, unsigned int& nHashesDone) |
|
|
|
unsigned int ScanHash_CryptoPP(char* pmidstate, char* pdata, char* phash1, char* phash, unsigned int& nHashesDone) |
|
|
|
{ |
|
|
|
unsigned int& nNonce = *(unsigned int*)(pblock + 12); |
|
|
|
unsigned int& nNonce = *(unsigned int*)(pdata + 12); |
|
|
|
for (;;) |
|
|
|
{ |
|
|
|
// Crypto++ SHA-256
|
|
|
|
// Hash pblock using pmidstate as the starting state into
|
|
|
|
// Hash pdata using pmidstate as the starting state into
|
|
|
|
// preformatted buffer phash1, then hash phash1 into phash
|
|
|
|
nNonce++; |
|
|
|
SHA256Transform(phash1, pblock, pmidstate); |
|
|
|
SHA256Transform(phash1, pdata, pmidstate); |
|
|
|
SHA256Transform(phash, phash1, pSHA256InitState); |
|
|
|
|
|
|
|
// Return the nonce if the hash has at least some zero bits,
|
|
|
@ -3055,62 +3049,22 @@ public: |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void BitcoinMiner() |
|
|
|
CBlock* CreateNewBlock(CReserveKey& reservekey) |
|
|
|
{ |
|
|
|
printf("BitcoinMiner started\n"); |
|
|
|
SetThreadPriority(THREAD_PRIORITY_LOWEST); |
|
|
|
bool f4WaySSE2 = Detect128BitSSE2(); |
|
|
|
if (mapArgs.count("-4way")) |
|
|
|
f4WaySSE2 = (mapArgs["-4way"] != "0"); |
|
|
|
|
|
|
|
CReserveKey reservekey; |
|
|
|
unsigned int nExtraNonce = 0; |
|
|
|
int64 nPrevTime = 0; |
|
|
|
while (fGenerateBitcoins) |
|
|
|
{ |
|
|
|
if (AffinityBugWorkaround(ThreadBitcoinMiner)) |
|
|
|
return; |
|
|
|
if (fShutdown) |
|
|
|
return; |
|
|
|
while (vNodes.empty() || IsInitialBlockDownload()) |
|
|
|
{ |
|
|
|
Sleep(1000); |
|
|
|
if (fShutdown) |
|
|
|
return; |
|
|
|
if (!fGenerateBitcoins) |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
unsigned int nTransactionsUpdatedLast = nTransactionsUpdated; |
|
|
|
CBlockIndex* pindexPrev = pindexBest; |
|
|
|
unsigned int nBits = GetNextWorkRequired(pindexPrev); |
|
|
|
|
|
|
|
// Create new block
|
|
|
|
auto_ptr<CBlock> pblock(new CBlock()); |
|
|
|
if (!pblock.get()) |
|
|
|
return NULL; |
|
|
|
|
|
|
|
//
|
|
|
|
// Create coinbase tx
|
|
|
|
//
|
|
|
|
CTransaction txNew; |
|
|
|
txNew.vin.resize(1); |
|
|
|
txNew.vin[0].prevout.SetNull(); |
|
|
|
int64 nNow = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); |
|
|
|
if (++nExtraNonce >= 0x7f && nNow > nPrevTime+1) |
|
|
|
{ |
|
|
|
nExtraNonce = 1; |
|
|
|
nPrevTime = nNow; |
|
|
|
} |
|
|
|
txNew.vin[0].scriptSig << nBits << CBigNum(nExtraNonce); |
|
|
|
txNew.vout.resize(1); |
|
|
|
txNew.vout[0].scriptPubKey << reservekey.GetReservedKey() << OP_CHECKSIG; |
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Create new block
|
|
|
|
//
|
|
|
|
auto_ptr<CBlock> pblock(new CBlock()); |
|
|
|
if (!pblock.get()) |
|
|
|
return; |
|
|
|
|
|
|
|
// Add our coinbase tx as first transaction
|
|
|
|
pblock->vtx.push_back(txNew); |
|
|
|
|
|
|
@ -3241,15 +3195,39 @@ void BitcoinMiner() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
pblock->nBits = nBits; |
|
|
|
pblock->vtx[0].vout[0].nValue = GetBlockValue(pindexPrev->nHeight+1, nFees); |
|
|
|
printf("Running BitcoinMiner with %d transactions in block\n", pblock->vtx.size()); |
|
|
|
|
|
|
|
// Fill in header
|
|
|
|
pblock->hashPrevBlock = pindexPrev->GetBlockHash(); |
|
|
|
pblock->hashMerkleRoot = pblock->BuildMerkleTree(); |
|
|
|
pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); |
|
|
|
pblock->nBits = GetNextWorkRequired(pindexPrev); |
|
|
|
pblock->nNonce = 0; |
|
|
|
|
|
|
|
return pblock.release(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce, int64& nPrevTime) |
|
|
|
{ |
|
|
|
// Update nExtraNonce
|
|
|
|
int64 nNow = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); |
|
|
|
if (++nExtraNonce >= 0x7f && nNow > nPrevTime+1) |
|
|
|
{ |
|
|
|
nExtraNonce = 1; |
|
|
|
nPrevTime = nNow; |
|
|
|
} |
|
|
|
pblock->vtx[0].vin[0].scriptSig = CScript() << pblock->nBits << CBigNum(nExtraNonce); |
|
|
|
pblock->hashMerkleRoot = pblock->BuildMerkleTree(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash1) |
|
|
|
{ |
|
|
|
//
|
|
|
|
// Prebuild hash buffer
|
|
|
|
// Prebuild hash buffers
|
|
|
|
//
|
|
|
|
struct tmpworkspace |
|
|
|
struct |
|
|
|
{ |
|
|
|
struct unnamed2 |
|
|
|
{ |
|
|
@ -3264,28 +3242,124 @@ void BitcoinMiner() |
|
|
|
unsigned char pchPadding0[64]; |
|
|
|
uint256 hash1; |
|
|
|
unsigned char pchPadding1[64]; |
|
|
|
}; |
|
|
|
char tmpbuf[sizeof(tmpworkspace)+16]; |
|
|
|
tmpworkspace& tmp = *(tmpworkspace*)alignup<16>(tmpbuf); |
|
|
|
} |
|
|
|
tmp; |
|
|
|
memset(&tmp, 0, sizeof(tmp)); |
|
|
|
|
|
|
|
tmp.block.nVersion = pblock->nVersion; |
|
|
|
tmp.block.hashPrevBlock = pblock->hashPrevBlock = pindexPrev->GetBlockHash(); |
|
|
|
tmp.block.hashMerkleRoot = pblock->hashMerkleRoot = pblock->BuildMerkleTree(); |
|
|
|
tmp.block.nTime = pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); |
|
|
|
tmp.block.nBits = pblock->nBits = nBits; |
|
|
|
tmp.block.nNonce = pblock->nNonce = 0; |
|
|
|
tmp.block.hashPrevBlock = pblock->hashPrevBlock; |
|
|
|
tmp.block.hashMerkleRoot = pblock->hashMerkleRoot; |
|
|
|
tmp.block.nTime = pblock->nTime; |
|
|
|
tmp.block.nBits = pblock->nBits; |
|
|
|
tmp.block.nNonce = pblock->nNonce; |
|
|
|
|
|
|
|
unsigned int nBlocks0 = FormatHashBlocks(&tmp.block, sizeof(tmp.block)); |
|
|
|
unsigned int nBlocks1 = FormatHashBlocks(&tmp.hash1, sizeof(tmp.hash1)); |
|
|
|
FormatHashBlocks(&tmp.block, sizeof(tmp.block)); |
|
|
|
FormatHashBlocks(&tmp.hash1, sizeof(tmp.hash1)); |
|
|
|
|
|
|
|
// Byte swap all the input buffer
|
|
|
|
for (int i = 0; i < sizeof(tmp)/4; i++) |
|
|
|
((unsigned int*)&tmp)[i] = ByteReverse(((unsigned int*)&tmp)[i]); |
|
|
|
|
|
|
|
// Precalc the first half of the first hash, which stays constant
|
|
|
|
uint256 midstatebuf[2]; |
|
|
|
uint256& midstate = *alignup<16>(midstatebuf); |
|
|
|
SHA256Transform(&midstate, &tmp.block, pSHA256InitState); |
|
|
|
SHA256Transform(pmidstate, &tmp.block, pSHA256InitState); |
|
|
|
|
|
|
|
memcpy(pdata, &tmp.block, 128); |
|
|
|
memcpy(phash1, &tmp.hash1, 64); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool CheckWork(CBlock* pblock, CReserveKey& reservekey) |
|
|
|
{ |
|
|
|
uint256 hash = pblock->GetHash(); |
|
|
|
uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256(); |
|
|
|
|
|
|
|
if (hash > hashTarget) |
|
|
|
return false; |
|
|
|
|
|
|
|
//// debug print
|
|
|
|
printf("BitcoinMiner:\n"); |
|
|
|
printf("proof-of-work found \n hash: %s \ntarget: %s\n", hash.GetHex().c_str(), hashTarget.GetHex().c_str()); |
|
|
|
pblock->print(); |
|
|
|
printf("%s ", DateTimeStrFormat("%x %H:%M", GetTime()).c_str()); |
|
|
|
printf("generated %s\n", FormatMoney(pblock->vtx[0].vout[0].nValue).c_str()); |
|
|
|
|
|
|
|
// Found a solution
|
|
|
|
CRITICAL_BLOCK(cs_main) |
|
|
|
{ |
|
|
|
if (pblock->hashPrevBlock != hashBestChain) |
|
|
|
return error("BitcoinMiner : generated block is stale"); |
|
|
|
|
|
|
|
// Remove key from key pool
|
|
|
|
reservekey.KeepKey(); |
|
|
|
|
|
|
|
// Track how many getdata requests this block gets
|
|
|
|
CRITICAL_BLOCK(cs_mapRequestCount) |
|
|
|
mapRequestCount[pblock->GetHash()] = 0; |
|
|
|
|
|
|
|
// Process this block the same as if we had received it from another node
|
|
|
|
if (!ProcessBlock(NULL, pblock)) |
|
|
|
return error("BitcoinMiner : ProcessBlock, block not accepted"); |
|
|
|
} |
|
|
|
|
|
|
|
Sleep(2000); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void BitcoinMiner() |
|
|
|
{ |
|
|
|
printf("BitcoinMiner started\n"); |
|
|
|
SetThreadPriority(THREAD_PRIORITY_LOWEST); |
|
|
|
bool f4WaySSE2 = Detect128BitSSE2(); |
|
|
|
if (mapArgs.count("-4way")) |
|
|
|
f4WaySSE2 = (mapArgs["-4way"] != "0"); |
|
|
|
|
|
|
|
// Each thread has its own key and counter
|
|
|
|
CReserveKey reservekey; |
|
|
|
unsigned int nExtraNonce = 0; |
|
|
|
int64 nPrevTime = 0; |
|
|
|
|
|
|
|
while (fGenerateBitcoins) |
|
|
|
{ |
|
|
|
if (AffinityBugWorkaround(ThreadBitcoinMiner)) |
|
|
|
return; |
|
|
|
if (fShutdown) |
|
|
|
return; |
|
|
|
while (vNodes.empty() || IsInitialBlockDownload()) |
|
|
|
{ |
|
|
|
Sleep(1000); |
|
|
|
if (fShutdown) |
|
|
|
return; |
|
|
|
if (!fGenerateBitcoins) |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Create new block
|
|
|
|
//
|
|
|
|
unsigned int nTransactionsUpdatedLast = nTransactionsUpdated; |
|
|
|
CBlockIndex* pindexPrev = pindexBest; |
|
|
|
|
|
|
|
auto_ptr<CBlock> pblock(CreateNewBlock(reservekey)); |
|
|
|
if (!pblock.get()) |
|
|
|
return; |
|
|
|
IncrementExtraNonce(pblock.get(), pindexPrev, nExtraNonce, nPrevTime); |
|
|
|
|
|
|
|
printf("Running BitcoinMiner with %d transactions in block\n", pblock->vtx.size()); |
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Prebuild 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.get(), pmidstate, pdata, phash1); |
|
|
|
|
|
|
|
unsigned int& nBlockTime = *(unsigned int*)(pdata + 64 + 4); |
|
|
|
unsigned int& nBlockNonce = *(unsigned int*)(pdata + 64 + 12); |
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
@ -3303,11 +3377,11 @@ void BitcoinMiner() |
|
|
|
#ifdef FOURWAYSSE2 |
|
|
|
if (f4WaySSE2) |
|
|
|
// tcatm's 4-way 128-bit SSE2 SHA-256
|
|
|
|
nNonceFound = ScanHash_4WaySSE2((char*)&midstate, (char*)&tmp.block + 64, (char*)&tmp.hash1, (char*)&hash, nHashesDone); |
|
|
|
nNonceFound = ScanHash_4WaySSE2(pmidstate, pdata + 64, phash1, (char*)&hash, nHashesDone); |
|
|
|
else |
|
|
|
#endif |
|
|
|
// Crypto++ SHA-256
|
|
|
|
nNonceFound = ScanHash_CryptoPP((char*)&midstate, (char*)&tmp.block + 64, (char*)&tmp.hash1, (char*)&hash, nHashesDone); |
|
|
|
nNonceFound = ScanHash_CryptoPP(pmidstate, pdata + 64, phash1, (char*)&hash, nHashesDone); |
|
|
|
|
|
|
|
// Check if something found
|
|
|
|
if (nNonceFound != -1) |
|
|
@ -3321,33 +3395,9 @@ void BitcoinMiner() |
|
|
|
pblock->nNonce = ByteReverse(nNonceFound); |
|
|
|
assert(hash == pblock->GetHash()); |
|
|
|
|
|
|
|
//// debug print
|
|
|
|
printf("BitcoinMiner:\n"); |
|
|
|
printf("proof-of-work found \n hash: %s \ntarget: %s\n", hash.GetHex().c_str(), hashTarget.GetHex().c_str()); |
|
|
|
pblock->print(); |
|
|
|
printf("%s ", DateTimeStrFormat("%x %H:%M", GetTime()).c_str()); |
|
|
|
printf("generated %s\n", FormatMoney(pblock->vtx[0].vout[0].nValue).c_str()); |
|
|
|
|
|
|
|
SetThreadPriority(THREAD_PRIORITY_NORMAL); |
|
|
|
CRITICAL_BLOCK(cs_main) |
|
|
|
{ |
|
|
|
if (pindexPrev == pindexBest) |
|
|
|
{ |
|
|
|
// Remove key from key pool
|
|
|
|
reservekey.KeepKey(); |
|
|
|
|
|
|
|
// Track how many getdata requests this block gets
|
|
|
|
CRITICAL_BLOCK(cs_mapRequestCount) |
|
|
|
mapRequestCount[pblock->GetHash()] = 0; |
|
|
|
|
|
|
|
// Process this block the same as if we had received it from another node
|
|
|
|
if (!ProcessBlock(NULL, pblock.release())) |
|
|
|
printf("ERROR in BitcoinMiner, ProcessBlock, block not accepted\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
CheckWork(pblock.get(), reservekey); |
|
|
|
SetThreadPriority(THREAD_PRIORITY_LOWEST); |
|
|
|
|
|
|
|
Sleep(500); |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
@ -3393,7 +3443,7 @@ void BitcoinMiner() |
|
|
|
return; |
|
|
|
if (vNodes.empty()) |
|
|
|
break; |
|
|
|
if (tmp.block.nNonce >= 0xff000000) |
|
|
|
if (nBlockNonce >= 0xffff0000) |
|
|
|
break; |
|
|
|
if (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 60) |
|
|
|
break; |
|
|
@ -3402,7 +3452,7 @@ void BitcoinMiner() |
|
|
|
|
|
|
|
// Update nTime every few seconds
|
|
|
|
pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); |
|
|
|
tmp.block.nTime = ByteReverse(pblock->nTime); |
|
|
|
nBlockTime = ByteReverse(pblock->nTime); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|