Exemplo n.º 1
0
bool CBasicKeyStore::HaveCScript(const uint160& hash) const
{
    bool result;
    CRITICAL_BLOCK(cs_KeyStore)
        result = (mapScripts.count(hash) > 0);
    return result;
}
Exemplo n.º 2
0
bool CBasicKeyStore::AddKey(const CKey& key)
{
    bool fCompressed = false;
    CSecret secret = key.GetSecret(fCompressed);
    CRITICAL_BLOCK(cs_KeyStore)
        mapKeys[CBitshekelAddress(key.GetPubKey())] = make_pair(secret, fCompressed);
    return true;
}
Exemplo n.º 3
0
bool CBasicKeyStore::HaveCScript(const uint160& hash) const
{
    bool result;
    CBitcoinAddress address;
    std::vector<unsigned char> vchPubKey;
    address.SetScriptHash160(hash);
    CRITICAL_BLOCK(cs_KeyStore)
        result = (mapScripts.count(hash) > 0) || GetPubKey(address, vchPubKey);
    return result;
}
Exemplo n.º 4
0
void CDB::Close()
{
    if (!pdb)
        return;
    if (!vTxn.empty())
        vTxn.front()->abort();
    vTxn.clear();
    pdb->close(0);
    delete pdb;
    pdb = NULL;
    dbenv.txn_checkpoint(0, 0, 0);

    CRITICAL_BLOCK(cs_db)
        --mapFileUseCount[strFile];

    RandAddSeed();
}
Exemplo n.º 5
0
void CDB::Close()
{
    if (!pdb)
        return;
    if (!vTxn.empty())
        vTxn.front()->abort();
    vTxn.clear();
    pdb = NULL;

    // Flush database activity from memory pool to disk log
    unsigned int nMinutes = 0;
    if (strFile == "addr.dat")
        nMinutes = 2;
    if (strFile == "blkindex.dat" && IsInitialBlockDownload() && nBestHeight % 500 != 0)
        nMinutes = 1;
    dbenv.txn_checkpoint(0, nMinutes, 0);

    CRITICAL_BLOCK(cs_db)
        --mapFileUseCount[strFile];
}
Exemplo n.º 6
0
CDB::CDB(const char* pszFile, const char* pszMode, bool fTxn) : pdb(NULL)
{
    int ret;
    if (pszFile == NULL)
        return;

    bool fCreate = strchr(pszMode, 'c');
    bool fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
    unsigned int nFlags = DB_THREAD;
    if (fCreate)
        nFlags |= DB_CREATE;
    else if (fReadOnly)
        nFlags |= DB_RDONLY;
    if (!fReadOnly || fTxn)
        nFlags |= DB_AUTO_COMMIT;

    CRITICAL_BLOCK(cs_db)
    {
        if (!fDbEnvInit)
        {
            string strAppDir = GetAppDir();
            string strLogDir = strAppDir + "\\database";
            _mkdir(strLogDir.c_str());
            printf("dbenv.open strAppDir=%s\n", strAppDir.c_str());

            dbenv.set_lg_dir(strLogDir.c_str());
            dbenv.set_lg_max(10000000);
            dbenv.set_lk_max_locks(10000);
            dbenv.set_lk_max_objects(10000);
            dbenv.set_errfile(fopen("db.log", "a")); /// debug
            ///dbenv.log_set_config(DB_LOG_AUTO_REMOVE, 1); /// causes corruption
            ret = dbenv.open(strAppDir.c_str(),
                             DB_CREATE     |
                             DB_INIT_LOCK  |
                             DB_INIT_LOG   |
                             DB_INIT_MPOOL |
                             DB_INIT_TXN   |
                             DB_THREAD     |
                             DB_PRIVATE    |
                             DB_RECOVER,
                             0);
            if (ret > 0)
                throw runtime_error(strprintf("CDB() : error %d opening database environment\n", ret));
            fDbEnvInit = true;
        }

        strFile = pszFile;
        ++mapFileUseCount[strFile];
    }

    pdb = new Db(&dbenv, 0);

    ret = pdb->open(NULL,      // Txn pointer
                    pszFile,   // Filename
                    "main",    // Logical db name
                    DB_BTREE,  // Database type
                    nFlags,    // Flags
                    0);

    if (ret > 0)
    {
        delete pdb;
        pdb = NULL;
        CRITICAL_BLOCK(cs_db)
            --mapFileUseCount[strFile];
        strFile = "";
        throw runtime_error(strprintf("CDB() : can't open database file %s, error %d\n", pszFile, ret));
    }

    if (fCreate && !Exists(string("version")))
        WriteVersion(VERSION);

    RandAddSeed();
}
Exemplo n.º 7
0
bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
{
    CRITICAL_BLOCK(cs_KeyStore)
        mapScripts[Hash160(redeemScript)] = redeemScript;
    return true;
}
Exemplo n.º 8
0
bool CBasicKeyStore::AddCScript(const uint160 &hash, const CScript& redeemScript)
{
    CRITICAL_BLOCK(cs_KeyStore)
        mapScripts[hash] = redeemScript;
    return true;
}
Exemplo n.º 9
0
bool CBasicKeyStore::AddKey(const CKey& key)
{
    CRITICAL_BLOCK(cs_KeyStore)
        mapKeys[CBitcoinAddress(key.GetPubKey())] = key.GetSecret();
    return true;
}
void RemoteMinerThreadGPU::Run(void *arg)
{
	threaddata *td=(threaddata *)arg;
	int64 currentblockid=-1;

	static const unsigned int SHA256InitState[8] ={0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
	uint256 tempbuff[4];
	uint256 &temphash=*alignup<16>(tempbuff);
	uint256 hashbuff[4];
	uint256 &hash=*alignup<16>(hashbuff);
	uint256 currenttarget;
	gpurunnertype gpu;

	unsigned char currentmidbuff[256];
	unsigned char currentblockbuff[256];
	unsigned char *midbuffptr;
	unsigned char *blockbuffptr;
	unsigned int *nonce;
	midbuffptr=alignup<16>(currentmidbuff);
	blockbuffptr=alignup<16>(currentblockbuff);
	nonce=(unsigned int *)(blockbuffptr+12);

	uint256 besthash=~(uint256(0));
	unsigned int besthashnonce=0;

	unsigned char *metahash=0;
	unsigned int metahashsize=td->m_metahashsize;
	unsigned int metahashpos=0;
	unsigned int metahashstartnonce=0;

	SetThreadPriority(THREAD_PRIORITY_LOWEST);

	FormatHashBlocks(&temphash,sizeof(temphash));
	for(int i=0; i<64/4; i++)
	{
		((unsigned int*)&temphash)[i] = CryptoPP::ByteReverse(((unsigned int*)&temphash)[i]);
	}

	gpu.FindBestConfiguration();

	while(td->m_generate)
	{
		if(td->m_havework)
		{
			if(metahashstartnonce==0)
			{
				CRITICAL_BLOCK(td->m_cs);
				if(currentblockid!=td->m_nextblock.m_blockid)
				{
					currenttarget=td->m_nextblock.m_target;
					currentblockid=td->m_nextblock.m_blockid;
					::memcpy(midbuffptr,&td->m_nextblock.m_midstate[0],32);
					::memcpy(blockbuffptr,&td->m_nextblock.m_block[0],64);
					metahashpos=0;
					metahashstartnonce=0;
					(*nonce)=0;
					besthash=~(uint256(0));
					besthashnonce=0;
					if(metahash==0 || td->m_metahashsize!=metahashsize)
					{
						metahashsize=td->m_metahashsize;
						if(metahash)
						{
							delete [] metahash;
						}
						metahash=new unsigned char[metahashsize];
					}

					for(int i=0; i<8; i++)
					{
						gpu.GetIn()->m_AH[i]=((unsigned int *)midbuffptr)[i];
					}
					gpu.GetIn()->m_merkle=((unsigned int *)blockbuffptr)[0];
					gpu.GetIn()->m_ntime=((unsigned int *)blockbuffptr)[1];
					gpu.GetIn()->m_nbits=((unsigned int *)blockbuffptr)[2];
				}
			}

			gpu.GetIn()->m_nonce=(*nonce);

			gpu.RunStep();

			for(int i=0; i<gpu.GetNumThreads()*gpu.GetNumBlocks(); i++)
			{
				memcpy(&hash,&(gpu.GetOut()[i].m_bestAH[0]),32);

				if(gpu.GetOut()[i].m_bestnonce!=0 && hash!=0 && hash<=currenttarget)
				{
					CRITICAL_BLOCK(td->m_cs);
					td->m_foundhashes.push_back(foundhash(currentblockid,gpu.GetOut()[i].m_bestnonce));
				}

				if(gpu.GetOut()[i].m_bestnonce!=0 && hash!=0 && hash<besthash && gpu.GetOut()[i].m_bestnonce<metahashstartnonce+metahashsize)
				{
					besthash=hash;
					besthashnonce=gpu.GetOut()[i].m_bestnonce;
				}

				for(int j=0; j<gpu.GetStepIterations(); j++)
				{
					(*nonce)++;
					metahash[metahashpos++]=gpu.GetMetaHash()[(i*gpu.GetStepIterations())+j];

					if(metahashpos>=metahashsize)
					{

						{
							CRITICAL_BLOCK(td->m_cs);
							td->m_hashresults.push_back(hashresult(currentblockid,besthash,besthashnonce,metahash,metahashstartnonce));

							if(td->m_metahashptrs.size()>0)
							{
								metahash=td->m_metahashptrs[td->m_metahashptrs.size()-1];
								td->m_metahashptrs.erase(td->m_metahashptrs.end()-1);
							}
							else
							{
								metahash=new unsigned char[metahashsize];
							}
						}

						metahashpos=0;
						metahashstartnonce=(*nonce);
						besthash=~(uint256(0));
						besthashnonce=0;

						{
							CRITICAL_BLOCK(td->m_cs);
							if(currentblockid!=td->m_nextblock.m_blockid)
							{
								currenttarget=td->m_nextblock.m_target;
								currentblockid=td->m_nextblock.m_blockid;
								::memcpy(midbuffptr,&td->m_nextblock.m_midstate[0],32);
								::memcpy(blockbuffptr,&td->m_nextblock.m_block[0],64);
								metahashpos=0;
								metahashstartnonce=0;
								(*nonce)=0;
								for(int i=0; i<8; i++)
								{
									gpu.GetIn()->m_AH[i]=((unsigned int *)midbuffptr)[i];
								}
								gpu.GetIn()->m_merkle=((unsigned int *)blockbuffptr)[0];
								gpu.GetIn()->m_ntime=((unsigned int *)blockbuffptr)[1];
								gpu.GetIn()->m_nbits=((unsigned int *)blockbuffptr)[2];
							}
						}

					}
				}	// j

			}

		}
		else
		{
			Sleep(100);
		}

	}

	{
		CRITICAL_BLOCK(td->m_cs);
		td->m_done=true;
	}

}
Exemplo n.º 11
0
CDB::CDB(const char* pszFile, const char* pszMode) : pdb(NULL)
{
    int ret;
    if (pszFile == NULL)
        return;

    fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
    bool fCreate = strchr(pszMode, 'c');
    unsigned int nFlags = DB_THREAD;
    if (fCreate)
        nFlags |= DB_CREATE;

    CRITICAL_BLOCK(cs_db)
    {
        if (!dbLock)
        {
            if (fShutdown)
                return;
            string strDataDir = GetDataDir();
            string strLogDir = strDataDir + "/database";
            filesystem::create_directory(strLogDir.c_str());
            string strErrorFile = strDataDir + "/db.log";
            printf("dbenv.open strLogDir=%s strErrorFile=%s\n", strLogDir.c_str(), strErrorFile.c_str());

            // if strErrorFile exists and is locked, exception: another Bitcoin must
            // be using this DataDir:
            if (boost::filesystem::exists(strErrorFile))
            {
                dbLock = new boost::interprocess::file_lock(strErrorFile.c_str());
                bool locked = dbLock->try_lock();
                if (!locked)
                    throw runtime_error(_("Cannot lock db.log, is bitcoin already running?\n"));
            }

            dbenv.set_lg_dir(strLogDir.c_str());
            dbenv.set_lg_max(10000000);
            dbenv.set_lk_max_locks(10000);
            dbenv.set_lk_max_objects(10000);
            dbenv.set_errfile(fopen(strErrorFile.c_str(), "a")); /// debug
            dbenv.set_flags(DB_AUTO_COMMIT, 1);
            ret = dbenv.open(strDataDir.c_str(),
                             DB_CREATE     |
                             DB_INIT_LOCK  |
                             DB_INIT_LOG   |
                             DB_INIT_MPOOL |
                             DB_INIT_TXN   |
                             DB_THREAD     |
                             DB_PRIVATE    |
                             DB_RECOVER,
                             S_IRUSR | S_IWUSR);
            if (ret > 0)
                throw runtime_error(strprintf("CDB() : error %d opening database environment\n", ret));

            if (!dbLock)
            {
                dbLock = new boost::interprocess::file_lock(strErrorFile.c_str());
                dbLock->lock();
            }
        }

        strFile = pszFile;
        ++mapFileUseCount[strFile];
        pdb = mapDb[strFile];
        if (pdb == NULL)
        {
            pdb = new Db(&dbenv, 0);

            ret = pdb->open(NULL,      // Txn pointer
                            pszFile,   // Filename
                            "main",    // Logical db name
                            DB_BTREE,  // Database type
                            nFlags,    // Flags
                            0);

            if (ret > 0)
            {
                delete pdb;
                pdb = NULL;
                CRITICAL_BLOCK(cs_db)
                    --mapFileUseCount[strFile];
                strFile = "";
                throw runtime_error(strprintf("CDB() : can't open database file %s, error %d\n", pszFile, ret));
            }

            if (fCreate && !Exists(string("version")))
            {
                bool fTmp = fReadOnly;
                fReadOnly = false;
                WriteVersion(VERSION);
                fReadOnly = fTmp;
            }

            mapDb[strFile] = pdb;
        }
    }
}
Exemplo n.º 12
0
CDB::CDB(const char* pszFile, const char* pszMode) : pdb(NULL)
{
    int ret;
    if (pszFile == NULL)
        return;

    fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
    bool fCreate = strchr(pszMode, 'c');
    unsigned int nFlags = DB_THREAD;
    if (fCreate)
        nFlags |= DB_CREATE;

    CRITICAL_BLOCK(cs_db)
    {
        if (!fDbEnvInit)
        {
            if (fShutdown)
                return;
            string strDataDir = GetDataDir();
            string strLogDir = strDataDir + "/database";
            filesystem::create_directory(strLogDir.c_str());
            string strErrorFile = strDataDir + "/db.log";
            printf("dbenv.open strLogDir=%s strErrorFile=%s\n", strLogDir.c_str(), strErrorFile.c_str());

            dbenv.set_lg_dir(strLogDir.c_str());
            dbenv.set_lg_max(10000000);
#if !(TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
            dbenv.set_lk_max_locks(10000);
            dbenv.set_lk_max_objects(10000);
#endif
            dbenv.set_errfile(fopen(strErrorFile.c_str(), "a")); /// debug
            dbenv.set_flags(DB_AUTO_COMMIT, 1);
            ret = dbenv.open(strDataDir.c_str(),
                             DB_CREATE     |
#if !(TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
                             DB_INIT_LOCK  |
#endif
                             DB_INIT_LOG   |
                             DB_INIT_MPOOL |
                             DB_INIT_TXN   |
                             DB_THREAD     |
                             DB_RECOVER,
                             S_IRUSR | S_IWUSR);
            if (ret > 0)
                throw runtime_error(strprintf("CDB() : error %d opening database environment", ret));
            fDbEnvInit = true;
        }

        strFile = pszFile;
        ++mapFileUseCount[strFile];
        pdb = mapDb[strFile];
        if (pdb == NULL)
        {
            pdb = new Db(&dbenv, 0);

            ret = pdb->open(NULL,      // Txn pointer
                            pszFile,   // Filename
                            "main",    // Logical db name
                            DB_BTREE,  // Database type
                            nFlags,    // Flags
                            0);

            if (ret > 0)
            {
                delete pdb;
                pdb = NULL;
                CRITICAL_BLOCK(cs_db)
                    --mapFileUseCount[strFile];
                strFile = "";
                throw runtime_error(strprintf("CDB() : can't open database file %s, error %d", pszFile, ret));
            }

            if (fCreate && !Exists(string("version")))
            {
                bool fTmp = fReadOnly;
                fReadOnly = false;
                WriteVersion(VERSION);
                fReadOnly = fTmp;
            }

            mapDb[strFile] = pdb;
        }
    }
}