Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
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;
}
Exemplo n.º 3
0
Arquivo: db.cpp Projeto: neiros/Dcoin
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;
}
Exemplo n.º 4
0
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);
}
Exemplo n.º 5
0
	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();
Exemplo n.º 6
0
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;
}