Skip to content

Commit 40cd036

Browse files
Satoshi Nakamotogavinandresen
Satoshi Nakamoto
authored andcommitted
Gavin Andresen's JSON-RPC HTTP authentication,
faster initial block download -- version 0.3.3
1 parent fe98cf8 commit 40cd036

12 files changed

+93
-25
lines changed

Diff for: db.cpp

+30-6
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,14 @@ void CDB::Close()
130130
vTxn.front()->abort();
131131
vTxn.clear();
132132
pdb = NULL;
133-
dbenv.txn_checkpoint(0, 0, 0);
133+
134+
// Flush database activity from memory pool to disk log
135+
unsigned int nMinutes = 0;
136+
if (strFile == "addr.dat")
137+
nMinutes = 2;
138+
if (strFile == "blkindex.dat" && IsInitialBlockDownload() && nBestHeight % 500 != 0)
139+
nMinutes = 1;
140+
dbenv.txn_checkpoint(0, nMinutes, 0);
134141

135142
CRITICAL_BLOCK(cs_db)
136143
--mapFileUseCount[strFile];
@@ -357,11 +364,12 @@ CBlockIndex* InsertBlockIndex(uint256 hash)
357364

358365
bool CTxDB::LoadBlockIndex()
359366
{
360-
// Get cursor
367+
// Get database cursor
361368
Dbc* pcursor = GetCursor();
362369
if (!pcursor)
363370
return false;
364371

372+
// Load mapBlockIndex
365373
unsigned int fFlags = DB_SET_RANGE;
366374
loop
367375
{
@@ -398,7 +406,7 @@ bool CTxDB::LoadBlockIndex()
398406
pindexNew->nBits = diskindex.nBits;
399407
pindexNew->nNonce = diskindex.nNonce;
400408

401-
// Watch for genesis block and best block
409+
// Watch for genesis block
402410
if (pindexGenesisBlock == NULL && diskindex.GetBlockHash() == hashGenesisBlock)
403411
pindexGenesisBlock = pindexNew;
404412
}
@@ -409,17 +417,33 @@ bool CTxDB::LoadBlockIndex()
409417
}
410418
pcursor->close();
411419

420+
// Calculate bnChainWork
421+
vector<pair<int, CBlockIndex*> > vSortedByHeight;
422+
vSortedByHeight.reserve(mapBlockIndex.size());
423+
foreach(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
424+
{
425+
CBlockIndex* pindex = item.second;
426+
vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
427+
}
428+
sort(vSortedByHeight.begin(), vSortedByHeight.end());
429+
foreach(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
430+
{
431+
CBlockIndex* pindex = item.second;
432+
pindex->bnChainWork = (pindex->pprev ? pindex->pprev->bnChainWork : 0) + pindex->GetBlockWork();
433+
}
434+
435+
// Load hashBestChain pointer to end of best chain
412436
if (!ReadHashBestChain(hashBestChain))
413437
{
414438
if (pindexGenesisBlock == NULL)
415439
return true;
416-
return error("CTxDB::LoadBlockIndex() : hashBestChain not found");
440+
return error("CTxDB::LoadBlockIndex() : hashBestChain not loaded");
417441
}
418-
419442
if (!mapBlockIndex.count(hashBestChain))
420-
return error("CTxDB::LoadBlockIndex() : blockindex for hashBestChain not found");
443+
return error("CTxDB::LoadBlockIndex() : hashBestChain not found in the block index");
421444
pindexBest = mapBlockIndex[hashBestChain];
422445
nBestHeight = pindexBest->nHeight;
446+
bnBestChainWork = pindexBest->bnChainWork;
423447
printf("LoadBlockIndex(): hashBestChain=%s height=%d\n", hashBestChain.ToString().substr(0,16).c_str(), nBestHeight);
424448

425449
return true;

Diff for: db.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extern map<string, string> mapAddressBook;
1616
extern CCriticalSection cs_mapAddressBook;
1717
extern vector<unsigned char> vchDefaultKey;
1818
extern bool fClient;
19-
19+
extern int nBestHeight;
2020

2121

2222
extern unsigned int nWalletDBUpdated;
@@ -210,7 +210,7 @@ class CDB
210210
if (!pdb)
211211
return false;
212212
DbTxn* ptxn = NULL;
213-
int ret = dbenv.txn_begin(GetTxn(), &ptxn, 0);
213+
int ret = dbenv.txn_begin(GetTxn(), &ptxn, DB_TXN_NOSYNC);
214214
if (!ptxn || ret != 0)
215215
return false;
216216
vTxn.push_back(ptxn);

Diff for: main.cpp

+34-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ map<uint256, CBlockIndex*> mapBlockIndex;
2424
const uint256 hashGenesisBlock("0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f");
2525
CBlockIndex* pindexGenesisBlock = NULL;
2626
int nBestHeight = -1;
27+
CBigNum bnBestChainWork = 0;
2728
uint256 hashBestChain = 0;
2829
CBlockIndex* pindexBest = NULL;
2930
int64 nTimeBestReceived = 0;
@@ -848,6 +849,23 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast)
848849
return bnNew.GetCompact();
849850
}
850851

852+
vector<int> vStartingHeight;
853+
void AddStartingHeight(int nStartingHeight)
854+
{
855+
if (nStartingHeight != -1)
856+
{
857+
vStartingHeight.push_back(nStartingHeight);
858+
sort(vStartingHeight.begin(), vStartingHeight.end());
859+
}
860+
}
861+
862+
bool IsInitialBlockDownload()
863+
{
864+
int nMedian = 69000;
865+
if (vStartingHeight.size() >= 5)
866+
nMedian = vStartingHeight[vStartingHeight.size()/2];
867+
return nBestHeight < nMedian-1000;
868+
}
851869

852870

853871

@@ -1208,13 +1226,14 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
12081226
pindexNew->pprev = (*miPrev).second;
12091227
pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
12101228
}
1229+
pindexNew->bnChainWork = (pindexNew->pprev ? pindexNew->pprev->bnChainWork : 0) + pindexNew->GetBlockWork();
12111230

12121231
CTxDB txdb;
12131232
txdb.TxnBegin();
12141233
txdb.WriteBlockIndex(CDiskBlockIndex(pindexNew));
12151234

12161235
// New best
1217-
if (pindexNew->nHeight > nBestHeight)
1236+
if (pindexNew->bnChainWork > bnBestChainWork)
12181237
{
12191238
if (pindexGenesisBlock == NULL && hash == hashGenesisBlock)
12201239
{
@@ -1253,6 +1272,7 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos)
12531272
hashBestChain = hash;
12541273
pindexBest = pindexNew;
12551274
nBestHeight = pindexBest->nHeight;
1275+
bnBestChainWork = pindexNew->bnChainWork;
12561276
nTimeBestReceived = GetTime();
12571277
nTransactionsUpdated++;
12581278
printf("AddToBlockIndex: new best=%s height=%d\n", hashBestChain.ToString().substr(0,16).c_str(), nBestHeight);
@@ -1900,6 +1920,7 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
19001920
}
19011921

19021922
AddTimeData(pfrom->addr.ip, nTime);
1923+
AddStartingHeight(pfrom->nStartingHeight);
19031924

19041925
// Change version
19051926
if (pfrom->nVersion >= 209)
@@ -2845,6 +2866,10 @@ int64 GetBalance()
28452866
}
28462867

28472868

2869+
int GetRandInt(int nMax)
2870+
{
2871+
return GetRand(nMax);
2872+
}
28482873

28492874
bool SelectCoins(int64 nTargetValue, set<CWalletTx*>& setCoinsRet)
28502875
{
@@ -2858,9 +2883,14 @@ bool SelectCoins(int64 nTargetValue, set<CWalletTx*>& setCoinsRet)
28582883

28592884
CRITICAL_BLOCK(cs_mapWallet)
28602885
{
2861-
for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
2862-
{
2863-
CWalletTx* pcoin = &(*it).second;
2886+
vector<CWalletTx*> vCoins;
2887+
vCoins.reserve(mapWallet.size());
2888+
for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
2889+
vCoins.push_back(&(*it).second);
2890+
random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);
2891+
2892+
foreach(CWalletTx* pcoin, vCoins)
2893+
{
28642894
if (!pcoin->IsFinal() || pcoin->fSpent)
28652895
continue;
28662896
int64 n = pcoin->GetCredit();

Diff for: main.h

+15-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ extern map<uint256, CBlockIndex*> mapBlockIndex;
3232
extern const uint256 hashGenesisBlock;
3333
extern CBlockIndex* pindexGenesisBlock;
3434
extern int nBestHeight;
35+
extern CBigNum bnBestChainWork;
3536
extern uint256 hashBestChain;
3637
extern CBlockIndex* pindexBest;
3738
extern unsigned int nTransactionsUpdated;
@@ -78,6 +79,7 @@ string SendMoneyToBitcoinAddress(string strAddress, int64 nValue, CWalletTx& wtx
7879
void GenerateBitcoins(bool fGenerate);
7980
void ThreadBitcoinMiner(void* parg);
8081
void BitcoinMiner();
82+
bool IsInitialBlockDownload();
8183

8284

8385

@@ -986,11 +988,14 @@ class CBlock
986988

987989
// Flush stdio buffers and commit to disk before returning
988990
fflush(fileout);
991+
if (!IsInitialBlockDownload() || (nBestHeight+1) % 500 == 0)
992+
{
989993
#ifdef __WXMSW__
990-
_commit(_fileno(fileout));
994+
_commit(_fileno(fileout));
991995
#else
992-
fsync(fileno(fileout));
996+
fsync(fileno(fileout));
993997
#endif
998+
}
994999

9951000
return true;
9961001
}
@@ -1072,6 +1077,7 @@ class CBlockIndex
10721077
unsigned int nFile;
10731078
unsigned int nBlockPos;
10741079
int nHeight;
1080+
CBigNum bnChainWork;
10751081

10761082
// block header
10771083
int nVersion;
@@ -1089,6 +1095,7 @@ class CBlockIndex
10891095
nFile = 0;
10901096
nBlockPos = 0;
10911097
nHeight = 0;
1098+
bnChainWork = 0;
10921099

10931100
nVersion = 0;
10941101
hashMerkleRoot = 0;
@@ -1105,6 +1112,7 @@ class CBlockIndex
11051112
nFile = nFileIn;
11061113
nBlockPos = nBlockPosIn;
11071114
nHeight = 0;
1115+
bnChainWork = 0;
11081116

11091117
nVersion = block.nVersion;
11101118
hashMerkleRoot = block.hashMerkleRoot;
@@ -1118,6 +1126,11 @@ class CBlockIndex
11181126
return *phashBlock;
11191127
}
11201128

1129+
CBigNum GetBlockWork() const
1130+
{
1131+
return (CBigNum(1)<<256) / (CBigNum().SetCompact(nBits)+1);
1132+
}
1133+
11211134
bool IsInMainChain() const
11221135
{
11231136
return (pnext || this == pindexBest);

Diff for: makefile.unix

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ WXLIBS= \
2121

2222
LIBS= \
2323
-Wl,-Bstatic \
24-
-l boost_system -l boost_filesystem -l boost_program_options \
24+
-l boost_system -l boost_filesystem -l boost_program_options \
2525
-l db_cxx \
2626
-l crypto \
2727
-Wl,-Bdynamic \

Diff for: rpc.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void PrintConsole(const char* format, ...)
3939
#if defined(__WXMSW__) && wxUSE_GUI
4040
MyMessageBox(buffer, "Bitcoin", wxOK | wxICON_EXCLAMATION);
4141
#else
42-
fprintf(stdout, buffer);
42+
fprintf(stdout, "%s", buffer);
4343
#endif
4444
}
4545

Diff for: serialize.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class CScript;
1919
class CDataStream;
2020
class CAutoFile;
2121

22-
static const int VERSION = 302;
23-
static const char* pszSubVer = ".2";
22+
static const int VERSION = 303;
23+
static const char* pszSubVer = "";
2424

2525

2626

Diff for: setup.nsi

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ RequestExecutionLevel highest
77

88
# General Symbol Definitions
99
!define REGKEY "SOFTWARE\$(^Name)"
10-
!define VERSION 0.3.2
10+
!define VERSION 0.3.3
1111
!define COMPANY "Bitcoin project"
1212
!define URL http://www.bitcoin.org/
1313

@@ -42,12 +42,12 @@ Var StartMenuGroup
4242
!insertmacro MUI_LANGUAGE English
4343

4444
# Installer attributes
45-
OutFile bitcoin-0.3.2-win32-setup.exe
45+
OutFile bitcoin-0.3.3-win32-setup.exe
4646
InstallDir $PROGRAMFILES\Bitcoin
4747
CRCCheck on
4848
XPStyle on
4949
ShowInstDetails show
50-
VIProductVersion 0.3.2.0
50+
VIProductVersion 0.3.3.0
5151
VIAddVersionKey ProductName Bitcoin
5252
VIAddVersionKey ProductVersion "${VERSION}"
5353
VIAddVersionKey CompanyName "${COMPANY}"

Diff for: ui.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,7 @@ void COptionsDialog::OnButtonApply(wxCommandEvent& event)
16141614
CAboutDialog::CAboutDialog(wxWindow* parent) : CAboutDialogBase(parent)
16151615
{
16161616
m_staticTextVersion->SetLabel(strprintf(_("version %d.%d.%d beta"), VERSION/10000, (VERSION/100)%100, VERSION%100));
1617+
//m_staticTextVersion->SetLabel(strprintf(_("version %d.%d.%d%s beta"), VERSION/10000, (VERSION/100)%100, VERSION%100, pszSubVer));
16171618

16181619
// Change (c) into UTF-8 or ANSI copyright symbol
16191620
wxString str = m_staticTextMain->GetLabel();

Diff for: uibase.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ class CAboutDialogBase : public wxDialog
227227

228228
public:
229229
wxStaticText* m_staticTextVersion;
230-
CAboutDialogBase( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = _("About Bitcoin"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 532,329 ), long style = wxDEFAULT_DIALOG_STYLE );
230+
CAboutDialogBase( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = _("About Bitcoin"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 532,333 ), long style = wxDEFAULT_DIALOG_STYLE );
231231
~CAboutDialogBase();
232232

233233
};

Diff for: uiproject.fbp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2866,7 +2866,7 @@
28662866
<property name="minimum_size"></property>
28672867
<property name="name">CAboutDialogBase</property>
28682868
<property name="pos"></property>
2869-
<property name="size">532,329</property>
2869+
<property name="size">532,333</property>
28702870
<property name="style">wxDEFAULT_DIALOG_STYLE</property>
28712871
<property name="subclass"></property>
28722872
<property name="title">About Bitcoin</property>

Diff for: util.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ void ReadConfigFile(map<string, string>& mapSettingsRet,
642642

643643
set<string> setOptions;
644644
setOptions.insert("*");
645-
645+
646646
for (pod::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
647647
{
648648
// Don't overwrite existing settings so command line settings override bitcoin.conf
@@ -732,7 +732,7 @@ void AddTimeData(unsigned int ip, int64 nTime)
732732
sort(vTimeOffsets.begin(), vTimeOffsets.end());
733733
int64 nMedian = vTimeOffsets[vTimeOffsets.size()/2];
734734
nTimeOffset = nMedian;
735-
if ((nMedian > 0 ? nMedian : -nMedian) > 36 * 60 * 60)
735+
if ((nMedian > 0 ? nMedian : -nMedian) > 70 * 60)
736736
{
737737
// Only let other nodes change our clock so far before we
738738
// go to the NTP servers

0 commit comments

Comments
 (0)