Skip to content

Commit 42605ce

Browse files
better prevention of inventory relaying during initial download,
message checksum between nodes with 0.2.9 or higher, optimization level up from -O0 to -O2, rpc functions: setlabel, getlabel, getaddressesbylabel, getreceivedbyaddress, getreceivedbylabel, listreceivedbyaddress, listreceivedbylabel -- version 0.2.9 git-svn-id: https://bitcoin.svn.sourceforge.net/svnroot/bitcoin/trunk@78 1a98c847-1fd6-4fd8-948a-caf3550aa51b
1 parent 124baa4 commit 42605ce

13 files changed

+364
-101
lines changed

Diff for: db.h

+2
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ class CWalletDB : public CDB
328328

329329
bool EraseName(const string& strAddress)
330330
{
331+
// This should only be used for sending addresses, never for receiving addresses,
332+
// receiving addresses must always have an address book entry if they're not change return.
331333
CRITICAL_BLOCK(cs_mapAddressBook)
332334
mapAddressBook.erase(strAddress);
333335
nWalletDBUpdated++;

Diff for: init.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,6 @@ bool CMyApp::Initialize(int& argc, wxChar** argv)
224224
}
225225
}
226226

227-
if (fDaemon)
228-
fprintf(stdout, "bitcoin server starting\n");
229-
230227
#ifdef __WXGTK__
231228
if (fDaemon || fCommandLine)
232229
{
@@ -447,7 +444,8 @@ bool CMyApp::OnInit2()
447444
//
448445
// Load data files
449446
//
450-
bool fFirstRun;
447+
if (fDaemon)
448+
fprintf(stdout, "bitcoin server starting\n");
451449
strErrors = "";
452450
int64 nStart;
453451

@@ -465,6 +463,7 @@ bool CMyApp::OnInit2()
465463

466464
printf("Loading wallet...\n");
467465
nStart = GetTimeMillis();
466+
bool fFirstRun;
468467
if (!LoadWallet(fFirstRun))
469468
strErrors += _("Error loading wallet.dat \n");
470469
printf(" wallet %15"PRI64d"ms\n", GetTimeMillis() - nStart);

Diff for: main.cpp

+50-26
Original file line numberDiff line numberDiff line change
@@ -1336,19 +1336,12 @@ bool CBlock::AcceptBlock()
13361336
if (!AddToBlockIndex(nFile, nBlockPos))
13371337
return error("AcceptBlock() : AddToBlockIndex failed");
13381338

1339-
// Don't relay old inventory during initial block download.
1340-
// Please keep this number updated to a few thousand below current block count.
1341-
if (hashBestChain == hash && nBestHeight > 55000)
1342-
RelayInventory(CInv(MSG_BLOCK, hash));
1343-
1344-
// // Add atoms to user reviews for coins created
1345-
// vector<unsigned char> vchPubKey;
1346-
// if (ExtractPubKey(vtx[0].vout[0].scriptPubKey, false, vchPubKey))
1347-
// {
1348-
// unsigned short nAtom = GetRand(USHRT_MAX - 100) + 100;
1349-
// vector<unsigned short> vAtoms(1, nAtom);
1350-
// AddAtomsAndPropagate(Hash(vchPubKey.begin(), vchPubKey.end()), vAtoms, true);
1351-
// }
1339+
// Relay inventory, but don't relay old inventory during initial block download
1340+
if (hashBestChain == hash)
1341+
CRITICAL_BLOCK(cs_vNodes)
1342+
foreach(CNode* pnode, vNodes)
1343+
if (nBestHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : 55000))
1344+
pnode->PushInventory(CInv(MSG_BLOCK, hash));
13521345

13531346
return true;
13541347
}
@@ -1721,19 +1714,21 @@ bool ProcessMessages(CNode* pfrom)
17211714
// (4) message start
17221715
// (12) command
17231716
// (4) size
1717+
// (4) checksum
17241718
// (x) data
17251719
//
17261720

17271721
loop
17281722
{
17291723
// Scan for message start
17301724
CDataStream::iterator pstart = search(vRecv.begin(), vRecv.end(), BEGIN(pchMessageStart), END(pchMessageStart));
1731-
if (vRecv.end() - pstart < sizeof(CMessageHeader))
1725+
int nHeaderSize = vRecv.GetSerializeSize(CMessageHeader());
1726+
if (vRecv.end() - pstart < nHeaderSize)
17321727
{
1733-
if (vRecv.size() > sizeof(CMessageHeader))
1728+
if (vRecv.size() > nHeaderSize)
17341729
{
17351730
printf("\n\nPROCESSMESSAGE MESSAGESTART NOT FOUND\n\n");
1736-
vRecv.erase(vRecv.begin(), vRecv.end() - sizeof(CMessageHeader));
1731+
vRecv.erase(vRecv.begin(), vRecv.end() - nHeaderSize);
17371732
}
17381733
break;
17391734
}
@@ -1742,6 +1737,7 @@ bool ProcessMessages(CNode* pfrom)
17421737
vRecv.erase(vRecv.begin(), pstart);
17431738

17441739
// Read header
1740+
vector<char> vHeaderSave(vRecv.begin(), vRecv.begin() + nHeaderSize);
17451741
CMessageHeader hdr;
17461742
vRecv >> hdr;
17471743
if (!hdr.IsValid())
@@ -1757,17 +1753,30 @@ bool ProcessMessages(CNode* pfrom)
17571753
{
17581754
// Rewind and wait for rest of message
17591755
///// need a mechanism to give up waiting for overlong message size error
1760-
//if (fDebug)
1761-
// printf("message-break\n");
1762-
vRecv.insert(vRecv.begin(), BEGIN(hdr), END(hdr));
1763-
Sleep(100);
1756+
if (fDebug)
1757+
printf("message-break\n");
1758+
vRecv.insert(vRecv.begin(), vHeaderSave.begin(), vHeaderSave.end());
17641759
break;
17651760
}
17661761

17671762
// Copy message to its own buffer
17681763
CDataStream vMsg(vRecv.begin(), vRecv.begin() + nMessageSize, vRecv.nType, vRecv.nVersion);
17691764
vRecv.ignore(nMessageSize);
17701765

1766+
// Checksum
1767+
if (vRecv.GetVersion() >= 209)
1768+
{
1769+
uint256 hash = Hash(vMsg.begin(), vMsg.end());
1770+
unsigned int nChecksum = 0;
1771+
memcpy(&nChecksum, &hash, sizeof(nChecksum));
1772+
if (nChecksum != hdr.nChecksum)
1773+
{
1774+
printf("ProcessMessage(%s, %d bytes) : CHECKSUM ERROR nChecksum=%08x hdr.nChecksum=%08x\n",
1775+
strCommand.c_str(), nMessageSize, nChecksum, hdr.nChecksum);
1776+
continue;
1777+
}
1778+
}
1779+
17711780
// Process message
17721781
bool fRet = false;
17731782
try
@@ -1844,6 +1853,9 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
18441853
vRecv >> addrFrom >> nNonce;
18451854
if (pfrom->nVersion >= 106 && !vRecv.empty())
18461855
vRecv >> strSubVer;
1856+
if (pfrom->nVersion >= 209 && !vRecv.empty())
1857+
vRecv >> pfrom->nStartingHeight;
1858+
18471859
if (pfrom->nVersion == 0)
18481860
return false;
18491861

@@ -1854,9 +1866,6 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
18541866
return true;
18551867
}
18561868

1857-
pfrom->vSend.SetVersion(min(pfrom->nVersion, VERSION));
1858-
pfrom->vRecv.SetVersion(min(pfrom->nVersion, VERSION));
1859-
18601869
pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);
18611870
if (pfrom->fClient)
18621871
{
@@ -1866,6 +1875,13 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
18661875

18671876
AddTimeData(pfrom->addr.ip, nTime);
18681877

1878+
// Change version
1879+
if (pfrom->nVersion >= 209)
1880+
pfrom->PushMessage("verack");
1881+
pfrom->vSend.SetVersion(min(pfrom->nVersion, VERSION));
1882+
if (pfrom->nVersion < 209)
1883+
pfrom->vRecv.SetVersion(min(pfrom->nVersion, VERSION));
1884+
18691885
// Ask the first connected node for block updates
18701886
static bool fAskedForBlocks;
18711887
if (!fAskedForBlocks && !pfrom->fClient)
@@ -1876,7 +1892,7 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
18761892

18771893
pfrom->fSuccessfullyConnected = true;
18781894

1879-
printf("version message: version %d\n", pfrom->nVersion);
1895+
printf("version message: version %d, blocks=%d\n", pfrom->nVersion, pfrom->nStartingHeight);
18801896
}
18811897

18821898

@@ -1887,6 +1903,12 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
18871903
}
18881904

18891905

1906+
else if (strCommand == "verack")
1907+
{
1908+
pfrom->vRecv.SetVersion(min(pfrom->nVersion, VERSION));
1909+
}
1910+
1911+
18901912
else if (strCommand == "addr")
18911913
{
18921914
vector<CAddress> vAddr;
@@ -2101,9 +2123,8 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
21012123
vRecv >> *pblock;
21022124

21032125
//// debug print
2104-
// printf("received block:\n");
2105-
// pblock->print();
21062126
printf("received block %s\n", pblock->GetHash().ToString().substr(0,16).c_str());
2127+
// pblock->print();
21072128

21082129
CInv inv(MSG_BLOCK, pblock->GetHash());
21092130
pfrom->AddInventoryKnown(inv);
@@ -2388,8 +2409,11 @@ void GenerateBitcoins(bool fGenerate)
23882409
int nAddThreads = nProcessors - vnThreadsRunning[3];
23892410
printf("Starting %d BitcoinMiner threads\n", nAddThreads);
23902411
for (int i = 0; i < nAddThreads; i++)
2412+
{
23912413
if (!CreateThread(ThreadBitcoinMiner, NULL))
23922414
printf("Error: CreateThread(ThreadBitcoinMiner) failed\n");
2415+
Sleep(10);
2416+
}
23932417
}
23942418
}
23952419

Diff for: makefile.mingw

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ LIBS= \
2929

3030
WXDEFS=-DWIN32 -D__WXMSW__ -D_WINDOWS -DNOPCH
3131
DEBUGFLAGS=-g -D__WXDEBUG__
32-
CFLAGS=-mthreads -O0 -w -Wno-invalid-offsetof -Wformat $(DEBUGFLAGS) $(WXDEFS) $(INCLUDEPATHS)
32+
CFLAGS=-mthreads -O2 -w -Wno-invalid-offsetof -Wformat $(DEBUGFLAGS) $(WXDEFS) $(INCLUDEPATHS)
3333
HEADERS=headers.h strlcpy.h serialize.h uint256.h util.h key.h bignum.h base58.h \
3434
script.h db.h net.h irc.h main.h rpc.h uibase.h ui.h init.h sha.h
3535

Diff for: makefile.unix

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ LIBS= \
2929

3030
WXDEFS=-D__WXGTK__ -DNOPCH
3131
DEBUGFLAGS=-g -D__WXDEBUG__
32-
CFLAGS=-O0 -Wno-invalid-offsetof -Wformat $(DEBUGFLAGS) $(WXDEFS) $(INCLUDEPATHS)
32+
CFLAGS=-O2 -Wno-invalid-offsetof -Wformat $(DEBUGFLAGS) $(WXDEFS) $(INCLUDEPATHS)
3333
HEADERS=headers.h strlcpy.h serialize.h uint256.h util.h key.h bignum.h base58.h \
3434
script.h db.h net.h irc.h main.h rpc.h uibase.h ui.h init.h sha.h
3535

Diff for: net.h

+24-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class CInv;
88
class CRequestTracker;
99
class CNode;
1010
class CBlockIndex;
11+
extern int nBestHeight;
1112

1213

1314

@@ -59,31 +60,32 @@ class CMessageHeader
5960
char pchMessageStart[sizeof(::pchMessageStart)];
6061
char pchCommand[COMMAND_SIZE];
6162
unsigned int nMessageSize;
62-
//unsigned int nChecksum;
63+
unsigned int nChecksum;
6364

6465
CMessageHeader()
6566
{
6667
memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
6768
memset(pchCommand, 0, sizeof(pchCommand));
6869
pchCommand[1] = 1;
6970
nMessageSize = -1;
70-
//nChecksum = 0;
71+
nChecksum = 0;
7172
}
7273

7374
CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
7475
{
7576
memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
7677
strncpy(pchCommand, pszCommand, COMMAND_SIZE);
7778
nMessageSize = nMessageSizeIn;
79+
nChecksum = 0;
7880
}
7981

8082
IMPLEMENT_SERIALIZE
8183
(
8284
READWRITE(FLATDATA(pchMessageStart));
8385
READWRITE(FLATDATA(pchCommand));
8486
READWRITE(nMessageSize);
85-
//if (nVersion >= 209 && GetCommand() != "version")
86-
// READWRITE(nChecksum);
87+
if (nVersion >= 209)
88+
READWRITE(nChecksum);
8789
)
8890

8991
string GetCommand()
@@ -475,6 +477,7 @@ extern CAddress addrProxy;
475477

476478

477479

480+
478481
class CNode
479482
{
480483
public:
@@ -507,6 +510,7 @@ class CNode
507510
uint256 hashContinue;
508511
CBlockIndex* pindexLastGetBlocksBegin;
509512
uint256 hashLastGetBlocksEnd;
513+
int nStartingHeight;
510514

511515
// flood
512516
vector<CAddress> vAddrToSend;
@@ -529,7 +533,9 @@ class CNode
529533
nServices = 0;
530534
hSocket = hSocketIn;
531535
vSend.SetType(SER_NETWORK);
536+
vSend.SetVersion(0);
532537
vRecv.SetType(SER_NETWORK);
538+
vRecv.SetVersion(0);
533539
nLastSend = 0;
534540
nLastRecv = 0;
535541
nLastSendEmpty = GetTime();
@@ -548,6 +554,7 @@ class CNode
548554
hashContinue = 0;
549555
pindexLastGetBlocksBegin = 0;
550556
hashLastGetBlocksEnd = 0;
557+
nStartingHeight = -1;
551558
fGetAddr = false;
552559
nNextSendTxInv = 0;
553560
vfSubscribe.assign(256, false);
@@ -558,7 +565,8 @@ class CNode
558565
CAddress addrYou = (fUseProxy ? CAddress("0.0.0.0") : addr);
559566
CAddress addrMe = (fUseProxy ? CAddress("0.0.0.0") : addrLocalHost);
560567
RAND_bytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
561-
PushMessage("version", VERSION, nLocalServices, nTime, addrYou, addrMe, nLocalHostNonce, string(pszSubVer));
568+
PushMessage("version", VERSION, nLocalServices, nTime, addrYou, addrMe,
569+
nLocalHostNonce, string(pszSubVer), nBestHeight);
562570
}
563571

564572
~CNode()
@@ -680,10 +688,20 @@ class CNode
680688
if (nHeaderStart == -1)
681689
return;
682690

683-
// Patch in the size
691+
// Set the size
684692
unsigned int nSize = vSend.size() - nMessageStart;
685693
memcpy((char*)&vSend[nHeaderStart] + offsetof(CMessageHeader, nMessageSize), &nSize, sizeof(nSize));
686694

695+
// Set the checksum
696+
if (vSend.GetVersion() >= 209)
697+
{
698+
uint256 hash = Hash(vSend.begin() + nMessageStart, vSend.end());
699+
unsigned int nChecksum = 0;
700+
memcpy(&nChecksum, &hash, sizeof(nChecksum));
701+
assert(nMessageStart - nHeaderStart >= offsetof(CMessageHeader, nChecksum) + sizeof(nChecksum));
702+
memcpy((char*)&vSend[nHeaderStart] + offsetof(CMessageHeader, nChecksum), &nChecksum, sizeof(nChecksum));
703+
}
704+
687705
printf("(%d bytes) ", nSize);
688706
printf("\n");
689707

0 commit comments

Comments
 (0)