Changeset View
Changeset View
Standalone View
Standalone View
src/addrdb.cpp
| Show All 9 Lines | |||||
| #include "clientversion.h" | #include "clientversion.h" | ||||
| #include "fs.h" | #include "fs.h" | ||||
| #include "hash.h" | #include "hash.h" | ||||
| #include "random.h" | #include "random.h" | ||||
| #include "streams.h" | #include "streams.h" | ||||
| #include "tinyformat.h" | #include "tinyformat.h" | ||||
| #include "util.h" | #include "util.h" | ||||
| CBanDB::CBanDB() { | CBanDB::CBanDB(const CChainParams &chainParams) : chainParams(chainParams) { | ||||
| pathBanlist = GetDataDir() / "banlist.dat"; | pathBanlist = GetDataDir() / "banlist.dat"; | ||||
| } | } | ||||
| bool CBanDB::Write(const banmap_t &banSet) { | bool CBanDB::Write(const banmap_t &banSet) { | ||||
| // Generate random temporary filename | // Generate random temporary filename | ||||
| unsigned short randv = 0; | unsigned short randv = 0; | ||||
| GetRandBytes((uint8_t *)&randv, sizeof(randv)); | GetRandBytes((uint8_t *)&randv, sizeof(randv)); | ||||
| std::string tmpfn = strprintf("banlist.dat.%04x", randv); | std::string tmpfn = strprintf("banlist.dat.%04x", randv); | ||||
| // serialize banlist, checksum data up to that point, then append csum | // serialize banlist, checksum data up to that point, then append csum | ||||
| CDataStream ssBanlist(SER_DISK, CLIENT_VERSION); | CDataStream ssBanlist(SER_DISK, CLIENT_VERSION); | ||||
| ssBanlist << FLATDATA(Params().DiskMagic()); | ssBanlist << FLATDATA(chainParams.DiskMagic()); | ||||
| ssBanlist << banSet; | ssBanlist << banSet; | ||||
| uint256 hash = Hash(ssBanlist.begin(), ssBanlist.end()); | uint256 hash = Hash(ssBanlist.begin(), ssBanlist.end()); | ||||
| ssBanlist << hash; | ssBanlist << hash; | ||||
| // open temp output file, and associate with CAutoFile | // open temp output file, and associate with CAutoFile | ||||
| fs::path pathTmp = GetDataDir() / tmpfn; | fs::path pathTmp = GetDataDir() / tmpfn; | ||||
| FILE *file = fsbridge::fopen(pathTmp, "wb"); | FILE *file = fsbridge::fopen(pathTmp, "wb"); | ||||
| CAutoFile fileout(file, SER_DISK, CLIENT_VERSION); | CAutoFile fileout(file, SER_DISK, CLIENT_VERSION); | ||||
| ▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | if (hashIn != hashTmp) | ||||
| return error("%s: Checksum mismatch, data corrupted", __func__); | return error("%s: Checksum mismatch, data corrupted", __func__); | ||||
| uint8_t pchMsgTmp[4]; | uint8_t pchMsgTmp[4]; | ||||
| try { | try { | ||||
| // de-serialize file header (network specific magic number) and .. | // de-serialize file header (network specific magic number) and .. | ||||
| ssBanlist >> FLATDATA(pchMsgTmp); | ssBanlist >> FLATDATA(pchMsgTmp); | ||||
| // ... verify the network matches ours | // ... verify the network matches ours | ||||
| if (memcmp(pchMsgTmp, std::begin(Params().DiskMagic()), | if (memcmp(pchMsgTmp, std::begin(chainParams.DiskMagic()), | ||||
| sizeof(pchMsgTmp))) { | sizeof(pchMsgTmp))) { | ||||
| return error("%s: Invalid network magic number", __func__); | return error("%s: Invalid network magic number", __func__); | ||||
| } | } | ||||
| // de-serialize ban data | // de-serialize ban data | ||||
| ssBanlist >> banSet; | ssBanlist >> banSet; | ||||
| } catch (const std::exception &e) { | } catch (const std::exception &e) { | ||||
| return error("%s: Deserialize or I/O error - %s", __func__, e.what()); | return error("%s: Deserialize or I/O error - %s", __func__, e.what()); | ||||
| } | } | ||||
| return true; | return true; | ||||
| } | } | ||||
| CAddrDB::CAddrDB() { | CAddrDB::CAddrDB(const CChainParams &chainParams) : chainParams(chainParams) { | ||||
| pathAddr = GetDataDir() / "peers.dat"; | pathAddr = GetDataDir() / "peers.dat"; | ||||
| } | } | ||||
| bool CAddrDB::Write(const CAddrMan &addr) { | bool CAddrDB::Write(const CAddrMan &addr) { | ||||
| // Generate random temporary filename | // Generate random temporary filename | ||||
| unsigned short randv = 0; | unsigned short randv = 0; | ||||
| GetRandBytes((uint8_t *)&randv, sizeof(randv)); | GetRandBytes((uint8_t *)&randv, sizeof(randv)); | ||||
| std::string tmpfn = strprintf("peers.dat.%04x", randv); | std::string tmpfn = strprintf("peers.dat.%04x", randv); | ||||
| // serialize addresses, checksum data up to that point, then append csum | // serialize addresses, checksum data up to that point, then append csum | ||||
| CDataStream ssPeers(SER_DISK, CLIENT_VERSION); | CDataStream ssPeers(SER_DISK, CLIENT_VERSION); | ||||
| ssPeers << FLATDATA(Params().DiskMagic()); | ssPeers << FLATDATA(chainParams.DiskMagic()); | ||||
| ssPeers << addr; | ssPeers << addr; | ||||
| uint256 hash = Hash(ssPeers.begin(), ssPeers.end()); | uint256 hash = Hash(ssPeers.begin(), ssPeers.end()); | ||||
| ssPeers << hash; | ssPeers << hash; | ||||
| // open temp output file, and associate with CAutoFile | // open temp output file, and associate with CAutoFile | ||||
| fs::path pathTmp = GetDataDir() / tmpfn; | fs::path pathTmp = GetDataDir() / tmpfn; | ||||
| FILE *file = fsbridge::fopen(pathTmp, "wb"); | FILE *file = fsbridge::fopen(pathTmp, "wb"); | ||||
| CAutoFile fileout(file, SER_DISK, CLIENT_VERSION); | CAutoFile fileout(file, SER_DISK, CLIENT_VERSION); | ||||
| ▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | |||||
| bool CAddrDB::Read(CAddrMan &addr, CDataStream &ssPeers) { | bool CAddrDB::Read(CAddrMan &addr, CDataStream &ssPeers) { | ||||
| uint8_t pchMsgTmp[4]; | uint8_t pchMsgTmp[4]; | ||||
| try { | try { | ||||
| // de-serialize file header (network specific magic number) and .. | // de-serialize file header (network specific magic number) and .. | ||||
| ssPeers >> FLATDATA(pchMsgTmp); | ssPeers >> FLATDATA(pchMsgTmp); | ||||
| // ... verify the network matches ours | // ... verify the network matches ours | ||||
| if (memcmp(pchMsgTmp, std::begin(Params().DiskMagic()), | if (memcmp(pchMsgTmp, std::begin(chainParams.DiskMagic()), | ||||
| sizeof(pchMsgTmp))) { | sizeof(pchMsgTmp))) { | ||||
| return error("%s: Invalid network magic number", __func__); | return error("%s: Invalid network magic number", __func__); | ||||
| } | } | ||||
| // de-serialize address data into one CAddrMan object | // de-serialize address data into one CAddrMan object | ||||
| ssPeers >> addr; | ssPeers >> addr; | ||||
| } catch (const std::exception &e) { | } catch (const std::exception &e) { | ||||
| // de-serialization has failed, ensure addrman is left in a clean state | // de-serialization has failed, ensure addrman is left in a clean state | ||||
| addr.Clear(); | addr.Clear(); | ||||
| return error("%s: Deserialize or I/O error - %s", __func__, e.what()); | return error("%s: Deserialize or I/O error - %s", __func__, e.what()); | ||||
| } | } | ||||
| return true; | return true; | ||||
| } | } | ||||