Browse Source

Merge pull request #505 from gavinandresen/unique_coinbase

Unique coinbase: Fixes #482
0.8 v0.4.00rc2
Gavin Andresen 13 years ago
parent
commit
b7598ceefa
  1. 16
      src/main.cpp
  2. 2
      src/main.h
  3. 11
      src/rpc.cpp

16
src/main.cpp

@ -2798,16 +2798,17 @@ CBlock* CreateNewBlock(CReserveKey& reservekey)
} }
void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce, int64& nPrevTime) void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
{ {
// Update nExtraNonce // Update nExtraNonce
int64 nNow = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); static uint256 hashPrevBlock;
if (++nExtraNonce >= 0x7f && nNow > nPrevTime+1) if (hashPrevBlock != pblock->hashPrevBlock)
{ {
nExtraNonce = 1; nExtraNonce = 0;
nPrevTime = nNow; hashPrevBlock = pblock->hashPrevBlock;
} }
pblock->vtx[0].vin[0].scriptSig = CScript() << pblock->nBits << CBigNum(nExtraNonce); ++nExtraNonce;
pblock->vtx[0].vin[0].scriptSig = CScript() << pblock->nTime << CBigNum(nExtraNonce);
pblock->hashMerkleRoot = pblock->BuildMerkleTree(); pblock->hashMerkleRoot = pblock->BuildMerkleTree();
} }
@ -2905,7 +2906,6 @@ void static BitcoinMiner(CWallet *pwallet)
// Each thread has its own key and counter // Each thread has its own key and counter
CReserveKey reservekey(pwallet); CReserveKey reservekey(pwallet);
unsigned int nExtraNonce = 0; unsigned int nExtraNonce = 0;
int64 nPrevTime = 0;
while (fGenerateBitcoins) while (fGenerateBitcoins)
{ {
@ -2932,7 +2932,7 @@ void static BitcoinMiner(CWallet *pwallet)
auto_ptr<CBlock> pblock(CreateNewBlock(reservekey)); auto_ptr<CBlock> pblock(CreateNewBlock(reservekey));
if (!pblock.get()) if (!pblock.get())
return; return;
IncrementExtraNonce(pblock.get(), pindexPrev, nExtraNonce, nPrevTime); IncrementExtraNonce(pblock.get(), pindexPrev, nExtraNonce);
printf("Running BitcoinMiner with %d transactions in block\n", pblock->vtx.size()); printf("Running BitcoinMiner with %d transactions in block\n", pblock->vtx.size());

2
src/main.h

@ -94,7 +94,7 @@ bool ProcessMessages(CNode* pfrom);
bool SendMessages(CNode* pto, bool fSendTrickle); bool SendMessages(CNode* pto, bool fSendTrickle);
void GenerateBitcoins(bool fGenerate, CWallet* pwallet); void GenerateBitcoins(bool fGenerate, CWallet* pwallet);
CBlock* CreateNewBlock(CReserveKey& reservekey); CBlock* CreateNewBlock(CReserveKey& reservekey);
void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce, int64& nPrevTime); void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash1); void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata, char* phash1);
bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey); bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey);
bool CheckProofOfWork(uint256 hash, unsigned int nBits); bool CheckProofOfWork(uint256 hash, unsigned int nBits);

11
src/rpc.cpp

@ -1498,7 +1498,8 @@ Value getwork(const Array& params, bool fHelp)
if (IsInitialBlockDownload()) if (IsInitialBlockDownload())
throw JSONRPCError(-10, "Bitcoin is downloading blocks..."); throw JSONRPCError(-10, "Bitcoin is downloading blocks...");
static map<uint256, pair<CBlock*, unsigned int> > mapNewBlock; typedef map<uint256, pair<CBlock*, CScript> > mapNewBlock_t;
static mapNewBlock_t mapNewBlock;
static vector<CBlock*> vNewBlock; static vector<CBlock*> vNewBlock;
static CReserveKey reservekey(pwalletMain); static CReserveKey reservekey(pwalletMain);
@ -1537,11 +1538,10 @@ Value getwork(const Array& params, bool fHelp)
// Update nExtraNonce // Update nExtraNonce
static unsigned int nExtraNonce = 0; static unsigned int nExtraNonce = 0;
static int64 nPrevTime = 0; IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
IncrementExtraNonce(pblock, pindexPrev, nExtraNonce, nPrevTime);
// Save // Save
mapNewBlock[pblock->hashMerkleRoot] = make_pair(pblock, nExtraNonce); mapNewBlock[pblock->hashMerkleRoot] = make_pair(pblock, pblock->vtx[0].vin[0].scriptSig);
// Prebuild hash buffers // Prebuild hash buffers
char pmidstate[32]; char pmidstate[32];
@ -1574,11 +1574,10 @@ Value getwork(const Array& params, bool fHelp)
if (!mapNewBlock.count(pdata->hashMerkleRoot)) if (!mapNewBlock.count(pdata->hashMerkleRoot))
return false; return false;
CBlock* pblock = mapNewBlock[pdata->hashMerkleRoot].first; CBlock* pblock = mapNewBlock[pdata->hashMerkleRoot].first;
unsigned int nExtraNonce = mapNewBlock[pdata->hashMerkleRoot].second;
pblock->nTime = pdata->nTime; pblock->nTime = pdata->nTime;
pblock->nNonce = pdata->nNonce; pblock->nNonce = pdata->nNonce;
pblock->vtx[0].vin[0].scriptSig = CScript() << pblock->nBits << CBigNum(nExtraNonce); pblock->vtx[0].vin[0].scriptSig = mapNewBlock[pdata->hashMerkleRoot].second;
pblock->hashMerkleRoot = pblock->BuildMerkleTree(); pblock->hashMerkleRoot = pblock->BuildMerkleTree();
return CheckWork(pblock, *pwalletMain, reservekey); return CheckWork(pblock, *pwalletMain, reservekey);

Loading…
Cancel
Save