} return file; } bool CLotDB::Write(long index, CLottoFileData &sFileData) { // serialize addresses, checksum data up to that point, then append csum CDataStream ssLotData(SER_DISK, CLIENT_VERSION); uint256 hash = SerializeHash(sFileData); int dataSize = sFileData.GetSerializeSize(SER_DISK, CLIENT_VERSION) + hash.GetSerializeSize(SER_DISK, CLIENT_VERSION); CAutoFile fileout = CAutoFile(OpenFile(index * dataSize, false), SER_DISK, CLIENT_VERSION); if (!fileout) return error("CLotDB::Write() : open failed"); long fileOutPos = ftell(fileout); if (fileOutPos < 0) return error("WriteBlockToDisk : ftell failed"); // Write and commit header, data try { fileout << sFileData << hash; } catch (std::exception &e) { return error("CLotDB::Write() : I/O error"); } fflush(fileout); FileCommit(fileout); fileout.fclose();
bool CMasternodeDB::Write(const CMasternodeMan& mnodemanToSave) { int64_t nStart = GetTimeMillis(); // serialize addresses, checksum data up to that point, then append csum CDataStream ssMasternodes(SER_DISK, CLIENT_VERSION); ssMasternodes << FLATDATA(Params().MessageStart()); ssMasternodes << mnodemanToSave; uint256 hash = Hash(ssMasternodes.begin(), ssMasternodes.end()); ssMasternodes << hash; // open output file, and associate with CAutoFile FILE *file = fopen(pathMN.string().c_str(), "wb"); CAutoFile fileout = CAutoFile(file, SER_DISK, CLIENT_VERSION); if (!fileout) return error("%s : Failed to open file %s", __func__, pathMN.string()); // Write and commit header, data try { fileout << ssMasternodes; } catch (std::exception &e) { return error("%s : Serialize or I/O error - %s", __func__, e.what()); } FileCommit(fileout); fileout.fclose(); LogPrintf("Written info to mncache.dat %dms\n", GetTimeMillis() - nStart); LogPrintf(" %s\n", mnodemanToSave.ToString()); return true; }
ssLotData << sFileData << hash; LogTrace("print", "%s\n", VectorToString(vector<unsigned char>(ssLotData.begin(), ssLotData.end()))); return true; } bool CLotDB::Read(long index, CLottoFileData &sFileData) { uint256 hash; int dataSize = sFileData.GetSerializeSize(SER_DISK, CLIENT_VERSION) + hash.GetSerializeSize(SER_DISK, CLIENT_VERSION); // LogTrace("spark", "the read dataSize:%d\n", dataSize); CAutoFile filein = CAutoFile(OpenFile(index * dataSize, true), SER_DISK, CLIENT_VERSION); if (!filein) return error("CLotDB::Read() : open failed"); try { filein >> sFileData >> hash; } catch (std::exception &e) { return error("CLotDB::Read(): I/O error or stream data corrupted"); } // LogTrace("spark", "the read data:%s\n", hash.ToString()); // LogTrace("spark", "the read SecretData:%s\n", VectorToString(sFileData.SecretData)); filein.fclose(); CDataStream ssLotData(SER_DISK, CLIENT_VERSION); ssLotData << sFileData << hash; LogTrace("print", "%s\n", VectorToString(vector<unsigned char>(ssLotData.begin(), ssLotData.end())));
bool CBitcoinAddrDb::Read(CAddrMan& addr) { boost::filesystem::path pathAddr = GetDataDir() / m_addrFileName; // open input file, and associate with CAutoFile FILE *file = fopen(pathAddr.string().c_str(), "rb"); CAutoFile filein = CAutoFile(file, SER_DISK, BITCOIN_CLIENT_VERSION); if (!filein) { return false; } // use file size to size memory buffer int fileSize = GetFilesize(filein); int dataSize = fileSize - sizeof(uint256); //Don't try to resize to a negative number if file is small if ( dataSize < 0 ) dataSize = 0; vector<unsigned char> vchData; vchData.resize(dataSize); uint256 hashIn; // read data and checksum from file try { filein.read((char *)&vchData[0], dataSize); filein >> hashIn; } catch (std::exception &e) { return false; } filein.fclose(); CDataStream ssPeers(vchData, SER_DISK, BITCOIN_CLIENT_VERSION); // verify stored checksum matches input data uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end()); if (hashIn != hashTmp) { return false; } unsigned char pchMsgTmp[4]; try { // de-serialize file header (network specific magic number) and .. ssPeers >> FLATDATA(pchMsgTmp); // ... verify the network matches ours if (memcmp(pchMsgTmp, BitcoinMessageStart, sizeof(pchMsgTmp))) { return false; } // de-serialize address data into one CAddrMan object ssPeers >> addr; } catch (std::exception &e) { return false; } return true; }
bool CAddrDB::Read(CAddrMan& addr) { // open input file, and associate with CAutoFile FILE *file = fopen(pathAddr.string().c_str(), "rb"); CAutoFile filein = CAutoFile(file, SER_DISK, CLIENT_VERSION); if (!filein) return error("CAddrman::Read() : open failed"); // use file size to size memory buffer int fileSize = boost::filesystem::file_size(pathAddr); int dataSize = fileSize - sizeof(uint256); // Don't try to resize to a negative number if file is small if ( dataSize < 0 ) dataSize = 0; vector<unsigned char> vchData; vchData.resize(dataSize); uint256 hashIn; // read data and checksum from file try { filein.read((char *)&vchData[0], dataSize); filein >> hashIn; } catch (std::exception &e) { return error("CAddrman::Read() 2 : I/O error or stream data corrupted"); } filein.fclose(); CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION); // verify stored checksum matches input data uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end()); if (hashIn != hashTmp) return error("CAddrman::Read() : checksum mismatch; data corrupted"); unsigned char pchMsgTmp[4]; try { // de-serialize file header (pchMessageStart magic number) and ssPeers >> FLATDATA(pchMsgTmp); // verify the network matches ours if (memcmp(pchMsgTmp, pchMessageStart, sizeof(pchMsgTmp))) return error("CAddrman::Read() : invalid network magic number"); // de-serialize address data into one CAddrMan object ssPeers >> addr; } catch (std::exception &e) { return error("CAddrman::Read() : I/O error or stream data corrupted"); } return true; }
bool CAddrDB::Read(CAddrMan& addr) { // open input file, and associate with CAutoFile открытые входной файл и ассоциировать с CAutoFile FILE *file = fopen(pathAddr.string().c_str(), "rb"); CAutoFile filein = CAutoFile(file, SER_DISK, CLIENT_VERSION); if (!filein) return error("CAddrman::Read() : open failed"); // use file size to size memory buffer использовать размер файла для размера буфера памяти int fileSize = GetFilesize(filein); int dataSize = fileSize - sizeof(uint256); //Don't try to resize to a negative number if file is small не пытайтесь изменить размер на отрицательное число, если файл небольшой if ( dataSize < 0 ) dataSize = 0; vector<unsigned char> vchData; vchData.resize(dataSize); uint256 hashIn; // read data and checksum from file чтение данных и контрольной суммы из файла try { filein.read((char *)&vchData[0], dataSize); filein >> hashIn; } catch (std::exception &e) { return error("CAddrman::Read() 2 : I/O error or stream data corrupted"); } filein.fclose(); CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION); // verify stored checksum matches input data проверить совпадение сохранённой контрольной суммы входным данным uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end()); if (hashIn != hashTmp) return error("CAddrman::Read() : checksum mismatch; data corrupted"); unsigned char pchMsgTmp[4]; try { // de-serialize file header (network specific magic number) and .. десериализация заголовка файла (сетевой специфический магический номер) и .. ssPeers >> FLATDATA(pchMsgTmp); // ... verify the network matches ours ... проверка нашего сетевого соответствия if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp))) return error("CAddrman::Read() : invalid network magic number"); // de-serialize address data into one CAddrMan object десериализация адресных данных в один объект CAddrMan ssPeers >> addr; } catch (std::exception &e) { return error("CAddrman::Read() : I/O error or stream data corrupted"); } return true; }
bool CAddrDB::Read(CAddrMan& addr) { // open input file, and associate with CAutoFile FILE *file = fopen(pathAddr.string().c_str(), "rb"); CAutoFile filein = CAutoFile(file, SER_DISK, CLIENT_VERSION); if(!filein) return(error("CAddrDB::Read() : fopen(%s) failed", pathAddr.string().c_str())); // use file size to size memory buffer int fileSize = GetFilesize(filein); int dataSize = fileSize - sizeof(uint256); vector<unsigned char> vchData; vchData.resize(dataSize); uint256 hashIn; // read data and checksum from file try { filein.read((char *)&vchData[0], dataSize); filein >> hashIn; } catch(std::exception &e) { return(error("CAddrDB::Read() : I/O error")); } filein.fclose(); CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION); // verify stored checksum matches input data uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end()); if(hashIn != hashTmp) return(error("CAddrDB::Read() : checksum mismatch")); // de-serialize address data unsigned char pchMsgTmp[4]; try { ssPeers >> FLATDATA(pchMsgTmp); ssPeers >> addr; } catch(std::exception &e) { return(error("CAddrDB::Read() #2 : I/O error")); } // finally, verify the network matches ours if(memcmp(pchMsgTmp, pchMessageStart, sizeof(pchMsgTmp))) return(error("CAddrDB::Read() : invalid network magic number")); return(true); }
bool CAddrDB::Write(const CAddrMan& addr) { // Generate random temporary filename unsigned short randv = 0; RAND_bytes((unsigned char *)&randv, sizeof(randv)); std::string tmpfn = strprintf("peers.dat.%04x", randv); // serialize addresses, checksum data up to that point, then append csum CDataStream ssPeers(SER_DISK, CLIENT_VERSION); //header fix mincoin unsigned char pchMessageStart[4]; GetMessageStart(pchMessageStart,false); ssPeers << FLATDATA(pchMessageStart); ssPeers << addr; uint256 hash = Hash(ssPeers.begin(), ssPeers.end()); ssPeers << hash; // open temp output file, and associate with CAutoFile boost::filesystem::path pathTmp = GetDataDir() / tmpfn; FILE *file = fopen(pathTmp.string().c_str(), "wb"); CAutoFile fileout = CAutoFile(file, SER_DISK, CLIENT_VERSION); if (!fileout) return error("CAddrman::Write() : open failed"); // Write and commit header, data try { fileout << ssPeers; } catch (std::exception &e) { return error("CAddrman::Write() : I/O error"); } FileCommit(fileout); fileout.fclose(); // replace existing peers.dat, if any, with new peers.dat.XXXX if (!RenameOver(pathTmp, pathAddr)) return error("CAddrman::Write() : Rename-into-place failed"); return true; }
bool CAddrDB::Write(const CAddrMan& addr) { // Generate random temporary filename Генерация случайного временного файла unsigned short randv = 0; RAND_bytes((unsigned char *)&randv, sizeof(randv)); std::string tmpfn = strprintf("peers.dat.%04x", randv); // serialize addresses, checksum data up to that point, then append csum сериализации адреса, контрольная сумма данных до того момента, затем добавить CSUM CDataStream ssPeers(SER_DISK, CLIENT_VERSION); ssPeers << FLATDATA(Params().MessageStart()); ssPeers << addr; uint256 hash = Hash(ssPeers.begin(), ssPeers.end()); ssPeers << hash; // open temp output file, and associate with CAutoFile открытыть временный выходной файл и ассоциировать с CAutoFile boost::filesystem::path pathTmp = GetDataDir() / tmpfn; FILE *file = fopen(pathTmp.string().c_str(), "wb"); CAutoFile fileout = CAutoFile(file, SER_DISK, CLIENT_VERSION); if (!fileout) return error("CAddrman::Write() : open failed"); // Write and commit header, data запись и фиксация заголовка, данных try { fileout << ssPeers; } catch (std::exception &e) { return error("CAddrman::Write() : I/O error"); } FileCommit(fileout); fileout.fclose(); // replace existing peers.dat, if any, with new peers.dat.XXXX заменить существующие peers.dat, если таковые имеются, новыми peers.dat.XXXX if (!RenameOver(pathTmp, pathAddr)) return error("CAddrman::Write() : Rename-into-place failed"); return true; }
return true; } uint256 CLotData::GetClottoFileHash() { CAutoFile fileout = CAutoFile(OpenFile(0, true), SER_DISK, CLIENT_VERSION); if (!fileout) return error("CLotDB::GetClottoFileHash : open failed"); fseek(fileout, 0, SEEK_END); //move the point to the end of file long filesize = ftell(fileout); //get the file size if (filesize < 0) return error("CLotDB::GetClottoFileHash : ftell failed"); rewind(fileout); //move the point to the start of file vector<unsigned char> vchData; vchData.resize(filesize); try { fileout.read((char *) &vchData[0], filesize); } catch (std::exception &e) { return error("CLotDB::GetClottoFileHash : read failed"); } fileout.fclose();
CMasternodeDB::ReadResult CMasternodeDB::Read(CMasternodeMan& mnodemanToLoad) { int64_t nStart = GetTimeMillis(); // open input file, and associate with CAutoFile FILE *file = fopen(pathMN.string().c_str(), "rb"); CAutoFile filein = CAutoFile(file, SER_DISK, CLIENT_VERSION); if (!filein) { error("%s : Failed to open file %s", __func__, pathMN.string()); return FileError; } // use file size to size memory buffer int fileSize = boost::filesystem::file_size(pathMN); int dataSize = fileSize - sizeof(uint256); // Don't try to resize to a negative number if file is small if (dataSize < 0) dataSize = 0; vector<unsigned char> vchData; vchData.resize(dataSize); uint256 hashIn; // read data and checksum from file try { filein.read((char *)&vchData[0], dataSize); filein >> hashIn; } catch (std::exception &e) { error("%s : Deserialize or I/O error - %s", __func__, e.what()); return HashReadError; } filein.fclose(); CDataStream ssMasternodes(vchData, SER_DISK, CLIENT_VERSION); // verify stored checksum matches input data uint256 hashTmp = Hash(ssMasternodes.begin(), ssMasternodes.end()); if (hashIn != hashTmp) { error("%s : Checksum mismatch, data corrupted", __func__); return IncorrectHash; } unsigned char pchMsgTmp[4]; try { // de-serialize file header (network specific magic number) and .. ssMasternodes >> FLATDATA(pchMsgTmp); // ... verify the network matches ours if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp))) { error("%s : Invalid network magic number", __func__); return IncorrectMagic; } // de-serialize address data into one CMnList object ssMasternodes >> mnodemanToLoad; } catch (std::exception &e) { mnodemanToLoad.Clear(); error("%s : Deserialize or I/O error - %s", __func__, e.what()); return IncorrectFormat; } mnodemanToLoad.CheckAndRemove(); // clean out expired LogPrintf("Loaded info from mncache.dat %dms\n", GetTimeMillis() - nStart); LogPrintf(" %s\n", mnodemanToLoad.ToString()); return Ok; }