MCPcopy Create free account
hub / github.com/WaykiChain/WaykiChain / Write

Method Write

src/net.cpp:1519–1553  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1517CAddrDB::CAddrDB() { pathAddr = GetDataDir() / "peers.dat"; }
1518
1519bool CAddrDB::Write(const CAddrMan& addr) {
1520 // Generate random temporary filename
1521 uint16_t randv = 0;
1522 RAND_bytes((uint8_t*)&randv, sizeof(randv));
1523 string tmpfn = strprintf("peers.dat.%04x", randv);
1524
1525 // serialize addresses, checksum data up to that point, then append csum
1526 CDataStream ssPeers(SER_DISK, CLIENT_VERSION);
1527 ssPeers << FLATDATA(SysCfg().MessageStart());
1528 ssPeers << addr;
1529 uint256 hash = Hash(ssPeers.begin(), ssPeers.end());
1530 ssPeers << hash;
1531
1532 // open temp output file, and associate with CAutoFile
1533 boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
1534 FILE* file = fopen(pathTmp.string().c_str(), "wb");
1535 CAutoFile fileout = CAutoFile(file, SER_DISK, CLIENT_VERSION);
1536 if (!fileout)
1537 return ERRORMSG("Failed to open file %s", pathTmp.string());
1538
1539 // Write and commit header, data
1540 try {
1541 fileout << ssPeers;
1542 } catch (std::exception& e) {
1543 return ERRORMSG("Serialize or I/O error - %s", e.what());
1544 }
1545 FileCommit(fileout);
1546 fileout.fclose();
1547
1548 // replace existing peers.dat, if any, with new peers.dat.XXXX
1549 if (!RenameOver(pathTmp, pathAddr))
1550 return ERRORMSG("Rename-into-path failed");
1551
1552 return true;
1553}
1554
1555bool CAddrDB::Read(CAddrMan& addr) {
1556 // open input file, and associate with CAutoFile

Callers 2

ComputeEntryMethod · 0.45
DumpAddressesFunction · 0.45

Calls 9

fopenFunction · 0.85
CAutoFileClass · 0.85
FileCommitFunction · 0.85
RenameOverFunction · 0.85
fcloseMethod · 0.80
HashFunction · 0.50
beginMethod · 0.45
endMethod · 0.45
whatMethod · 0.45

Tested by

no test coverage detected