Skip to content

Commit d004d72

Browse files
committed
Move CAddrDB frrom db to net
This was a leftover from the times in which peers.dat depended in BDB. Other functions in db.cpp still depend on BerkelyDB, to be able to compile without BDB this (small) functionality needs to be moved to another file.
1 parent 48ba56c commit d004d72

File tree

4 files changed

+111
-128
lines changed

4 files changed

+111
-128
lines changed

src/db.cpp

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -479,113 +479,3 @@ void CDBEnv::Flush(bool fShutdown)
479479
}
480480
}
481481

482-
483-
484-
485-
486-
487-
488-
489-
490-
491-
492-
//
493-
// CAddrDB
494-
//
495-
496-
CAddrDB::CAddrDB()
497-
{
498-
pathAddr = GetDataDir() / "peers.dat";
499-
}
500-
501-
bool CAddrDB::Write(const CAddrMan& addr)
502-
{
503-
// Generate random temporary filename
504-
unsigned short randv = 0;
505-
RAND_bytes((unsigned char *)&randv, sizeof(randv));
506-
std::string tmpfn = strprintf("peers.dat.%04x", randv);
507-
508-
// serialize addresses, checksum data up to that point, then append csum
509-
CDataStream ssPeers(SER_DISK, CLIENT_VERSION);
510-
ssPeers << FLATDATA(Params().MessageStart());
511-
ssPeers << addr;
512-
uint256 hash = Hash(ssPeers.begin(), ssPeers.end());
513-
ssPeers << hash;
514-
515-
// open temp output file, and associate with CAutoFile
516-
boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
517-
FILE *file = fopen(pathTmp.string().c_str(), "wb");
518-
CAutoFile fileout = CAutoFile(file, SER_DISK, CLIENT_VERSION);
519-
if (!fileout)
520-
return error("CAddrman::Write() : open failed");
521-
522-
// Write and commit header, data
523-
try {
524-
fileout << ssPeers;
525-
}
526-
catch (std::exception &e) {
527-
return error("CAddrman::Write() : I/O error");
528-
}
529-
FileCommit(fileout);
530-
fileout.fclose();
531-
532-
// replace existing peers.dat, if any, with new peers.dat.XXXX
533-
if (!RenameOver(pathTmp, pathAddr))
534-
return error("CAddrman::Write() : Rename-into-place failed");
535-
536-
return true;
537-
}
538-
539-
bool CAddrDB::Read(CAddrMan& addr)
540-
{
541-
// open input file, and associate with CAutoFile
542-
FILE *file = fopen(pathAddr.string().c_str(), "rb");
543-
CAutoFile filein = CAutoFile(file, SER_DISK, CLIENT_VERSION);
544-
if (!filein)
545-
return error("CAddrman::Read() : open failed");
546-
547-
// use file size to size memory buffer
548-
int fileSize = GetFilesize(filein);
549-
int dataSize = fileSize - sizeof(uint256);
550-
//Don't try to resize to a negative number if file is small
551-
if ( dataSize < 0 ) dataSize = 0;
552-
vector<unsigned char> vchData;
553-
vchData.resize(dataSize);
554-
uint256 hashIn;
555-
556-
// read data and checksum from file
557-
try {
558-
filein.read((char *)&vchData[0], dataSize);
559-
filein >> hashIn;
560-
}
561-
catch (std::exception &e) {
562-
return error("CAddrman::Read() 2 : I/O error or stream data corrupted");
563-
}
564-
filein.fclose();
565-
566-
CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION);
567-
568-
// verify stored checksum matches input data
569-
uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end());
570-
if (hashIn != hashTmp)
571-
return error("CAddrman::Read() : checksum mismatch; data corrupted");
572-
573-
unsigned char pchMsgTmp[4];
574-
try {
575-
// de-serialize file header (network specific magic number) and ..
576-
ssPeers >> FLATDATA(pchMsgTmp);
577-
578-
// ... verify the network matches ours
579-
if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp)))
580-
return error("CAddrman::Read() : invalid network magic number");
581-
582-
// de-serialize address data into one CAddrMan object
583-
ssPeers >> addr;
584-
}
585-
catch (std::exception &e) {
586-
return error("CAddrman::Read() : I/O error or stream data corrupted");
587-
}
588-
589-
return true;
590-
}
591-

src/db.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -305,22 +305,4 @@ class CDB
305305
bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
306306
};
307307

308-
309-
310-
311-
312-
313-
314-
315-
/** Access to the (IP) address database (peers.dat) */
316-
class CAddrDB
317-
{
318-
private:
319-
boost::filesystem::path pathAddr;
320-
public:
321-
CAddrDB();
322-
bool Write(const CAddrMan& addr);
323-
bool Read(CAddrMan& addr);
324-
};
325-
326308
#endif // BITCOIN_DB_H

src/net.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,3 +1942,103 @@ void CNode::Fuzz(int nChance)
19421942
// (more changes exponentially less likely):
19431943
Fuzz(2);
19441944
}
1945+
1946+
//
1947+
// CAddrDB
1948+
//
1949+
1950+
CAddrDB::CAddrDB()
1951+
{
1952+
pathAddr = GetDataDir() / "peers.dat";
1953+
}
1954+
1955+
bool CAddrDB::Write(const CAddrMan& addr)
1956+
{
1957+
// Generate random temporary filename
1958+
unsigned short randv = 0;
1959+
RAND_bytes((unsigned char *)&randv, sizeof(randv));
1960+
std::string tmpfn = strprintf("peers.dat.%04x", randv);
1961+
1962+
// serialize addresses, checksum data up to that point, then append csum
1963+
CDataStream ssPeers(SER_DISK, CLIENT_VERSION);
1964+
ssPeers << FLATDATA(Params().MessageStart());
1965+
ssPeers << addr;
1966+
uint256 hash = Hash(ssPeers.begin(), ssPeers.end());
1967+
ssPeers << hash;
1968+
1969+
// open temp output file, and associate with CAutoFile
1970+
boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
1971+
FILE *file = fopen(pathTmp.string().c_str(), "wb");
1972+
CAutoFile fileout = CAutoFile(file, SER_DISK, CLIENT_VERSION);
1973+
if (!fileout)
1974+
return error("CAddrman::Write() : open failed");
1975+
1976+
// Write and commit header, data
1977+
try {
1978+
fileout << ssPeers;
1979+
}
1980+
catch (std::exception &e) {
1981+
return error("CAddrman::Write() : I/O error");
1982+
}
1983+
FileCommit(fileout);
1984+
fileout.fclose();
1985+
1986+
// replace existing peers.dat, if any, with new peers.dat.XXXX
1987+
if (!RenameOver(pathTmp, pathAddr))
1988+
return error("CAddrman::Write() : Rename-into-place failed");
1989+
1990+
return true;
1991+
}
1992+
1993+
bool CAddrDB::Read(CAddrMan& addr)
1994+
{
1995+
// open input file, and associate with CAutoFile
1996+
FILE *file = fopen(pathAddr.string().c_str(), "rb");
1997+
CAutoFile filein = CAutoFile(file, SER_DISK, CLIENT_VERSION);
1998+
if (!filein)
1999+
return error("CAddrman::Read() : open failed");
2000+
2001+
// use file size to size memory buffer
2002+
int fileSize = GetFilesize(filein);
2003+
int dataSize = fileSize - sizeof(uint256);
2004+
//Don't try to resize to a negative number if file is small
2005+
if ( dataSize < 0 ) dataSize = 0;
2006+
vector<unsigned char> vchData;
2007+
vchData.resize(dataSize);
2008+
uint256 hashIn;
2009+
2010+
// read data and checksum from file
2011+
try {
2012+
filein.read((char *)&vchData[0], dataSize);
2013+
filein >> hashIn;
2014+
}
2015+
catch (std::exception &e) {
2016+
return error("CAddrman::Read() 2 : I/O error or stream data corrupted");
2017+
}
2018+
filein.fclose();
2019+
2020+
CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION);
2021+
2022+
// verify stored checksum matches input data
2023+
uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end());
2024+
if (hashIn != hashTmp)
2025+
return error("CAddrman::Read() : checksum mismatch; data corrupted");
2026+
2027+
unsigned char pchMsgTmp[4];
2028+
try {
2029+
// de-serialize file header (network specific magic number) and ..
2030+
ssPeers >> FLATDATA(pchMsgTmp);
2031+
2032+
// ... verify the network matches ours
2033+
if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp)))
2034+
return error("CAddrman::Read() : invalid network magic number");
2035+
2036+
// de-serialize address data into one CAddrMan object
2037+
ssPeers >> addr;
2038+
}
2039+
catch (std::exception &e) {
2040+
return error("CAddrman::Read() : I/O error or stream data corrupted");
2041+
}
2042+
2043+
return true;
2044+
}

src/net.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,4 +690,15 @@ class CTransaction;
690690
void RelayTransaction(const CTransaction& tx, const uint256& hash);
691691
void RelayTransaction(const CTransaction& tx, const uint256& hash, const CDataStream& ss);
692692

693+
/** Access to the (IP) address database (peers.dat) */
694+
class CAddrDB
695+
{
696+
private:
697+
boost::filesystem::path pathAddr;
698+
public:
699+
CAddrDB();
700+
bool Write(const CAddrMan& addr);
701+
bool Read(CAddrMan& addr);
702+
};
703+
693704
#endif

0 commit comments

Comments
 (0)