Example #1
0
bool CAccountViewTest::TestGetAccount(bool bCheckExist) {
	CSecureAccount account;

	//get account by keyID
	for (int i = 0; i < VECTOR_SIZE; i++) {
		if (bCheckExist) {
			if (!pAccountViewTip->GetAccount(vRandomKeyID.at(i), account)) {
				return false;
			}
		} else {
			if (pAccountViewTip->GetAccount(vRandomKeyID.at(i), account)) {
				return false;
			}
		}
	}

	//get account by accountID
	for (int i = 0; i < VECTOR_SIZE; i++) {
		if (bCheckExist) {
			if (!pAccountViewTip->GetAccount(vRandomRegID.at(i).vRegID, account)) {
				return false;
			}
		} else {
			if (pAccountViewTip->GetAccount(vRandomRegID.at(i).vRegID, account)) {
				return false;
			}
		}
	}

	return true;
}
Example #2
0
bool CAccountViewTest::SetKeyID() {
	for (int i = 0; i < VECTOR_SIZE; i++) {
		pAccountViewTip->SetKeyId(vRandomRegID.at(i).vRegID, vRandomKeyID.at(i));
	}

	return true;
}
Example #3
0
bool CAccountViewTest::EraseAccount() {
	for (int i = 0; i < VECTOR_SIZE; i++) {
		pAccountViewTip->EraseAccount(vRandomKeyID.at(i));
	}

	return true;
}
Example #4
0
bool CAccountViewTest::CheckKeyMap(bool bCheckExist) {
	CKeyID keyID;
	for (int i = 0; i < VECTOR_SIZE; i++) {
		if (bCheckExist) {
			if (!pAccountViewTip->GetKeyId(vRandomRegID.at(i).vRegID, keyID)) {
				return false;
			}
		} else {
			if (pAccountViewTip->GetKeyId(vRandomRegID.at(i).vRegID, keyID)) {
				return false;
			}
		}
	}

	return true;
}
Example #5
0
bool CAccountViewTest::HaveAccount() {
	for (int i = 0; i < VECTOR_SIZE; i++) {
		if (pAccountViewTip->HaveAccount(vRandomKeyID.at(i)))
			return false;
	}

	return true;
}
Example #6
0
bool CreatePosTx(const CBlockIndex *pPrevIndex, CBlock *pBlock, set<CKeyID>&setCreateKey, CAccountViewCache &view,
		CTransactionDBCache &txCache, CScriptDBViewCache &scriptCache) {
	set<CKeyID> setKeyID;
	setKeyID.clear();

	set<CAccount, CAccountComparator> setAcctInfo;

	{
		LOCK2(cs_main, pwalletMain->cs_wallet);

		if((unsigned int)(chainActive.Tip()->nHeight + 1) !=  pBlock->GetHeight())
			return false;
		pwalletMain->GetKeys(setKeyID, true);                         // first:get keyID from pwalletMain
		if (setKeyID.empty()) {
			return ERRORMSG("CreatePosTx setKeyID empty");
		}

		LogPrint("INFO","CreatePosTx block time:%d\n",  pBlock->GetTime());
		for(const auto &keyid:setKeyID) {                             //second:get account by keyID
			//find CAccount info by keyid
			if(setCreateKey.size()) {
				bool bfind = false;
				for(auto &item: setCreateKey){
					if(item == keyid){
						bfind = true;
						break;
					}
				}
				if (!bfind)
					continue;
			}
			CUserID userId = keyid;
			CAccount acctInfo;
			if (view.GetAccount(userId, acctInfo)) {     // check  acctInfo is or not allowed to mining ,
				//available
//				LogPrint("miner", "account info:regid=%s keyid=%s ncoinday=%lld isMiner=%d\n", acctInfo.regID.ToString(),
//						acctInfo.keyID.ToString(), acctInfo.GetAccountPos(pBlock->nHeight), acctInfo.IsMiner(pBlock->nHeight));
				if (acctInfo.IsRegister() && acctInfo.GetAccountPos(pBlock->GetHeight()) > 0 && acctInfo.IsMiner(pBlock->GetHeight())) {
					setAcctInfo.insert(std::move(acctInfo));
//					LogPrint("miner", "miner account info:%s\n", acctInfo.ToString());
				}
			}
		}
	}

	if (setAcctInfo.empty()) {
		setCreateKey.clear();
		LogPrint("INFO", "CreatePosTx setSecureAcc empty");
		return false;
	}

	uint64_t maxNonce = SysCfg().GetBlockMaxNonce(); //cacul times

	uint256 prevblockhash = pPrevIndex->GetBlockHash();
	const uint256 targetHash = CBigNum().SetCompact(pBlock->GetBits()).getuint256(); //target hash difficult
	set<CAccount, CAccountComparator>::iterator iterAcct = setAcctInfo.begin();
	for (;iterAcct!=setAcctInfo.end();++iterAcct) {                            //third: 根据不同的账户 ,去计算挖矿
		CAccount  &item = const_cast<CAccount&>(*iterAcct);
		uint64_t posacc = item.GetAccountPos(pBlock->GetHeight());
		if (0 == posacc) {  //have no pos
			LogPrint("ERROR", "CreatePosTx posacc zero\n");
			continue;
		}
		LogPrint("miner", "miner account:%s\n", item.ToString());
//		LogPrint("INFO", "target hash:%s\n", targetHash.ToString());
//		LogPrint("INFO", "posacc:%d\n", posacc);
		uint256 adjusthash = GetAdjustHash(targetHash, posacc, pBlock->GetHeight()-1); //adjust nbits
//		LogPrint("INFO", "adjusthash:%s\n", adjusthash.ToString());

		//need compute this block proofofwork
		struct PosTxInfo postxinfo;
		postxinfo.nVersion = pBlock->GetVersion();
		postxinfo.hashPrevBlock = prevblockhash;
		postxinfo.hashMerkleRoot = item.GetHash();
		postxinfo.nValues = item.llValues;
		postxinfo.nHeight = pBlock->GetHeight();
		postxinfo.nFuel = pBlock->GetFuel();
		postxinfo.nFuelRate = pBlock->GetFuelRate();
		postxinfo.nTime = pBlock->GetTime(); //max(pPrevIndex->GetMedianTimePast() + 1, GetAdjustedTime());
		unsigned int nNonce = 0;
		for (; nNonce < maxNonce; ++nNonce) {        //循环的 更改随机数,计算curhash看是否满足
			postxinfo.nNonce = nNonce;
			pBlock->SetNonce(nNonce);
			uint256 curhash = postxinfo.GetHash();

			if (UintToArith256(curhash) <= UintToArith256(adjusthash)) {
				CRegID regid;

				if (pAccountViewTip->GetRegId(item.keyID, regid)) {
					CRewardTransaction *prtx = (CRewardTransaction *) pBlock->vptx[0].get();
					prtx->account = regid;                                   //存矿工的 账户ID
					prtx->nHeight = pPrevIndex->nHeight+1;

					pBlock->SetHashMerkleRoot(pBlock->BuildMerkleTree());
					pBlock->SetHashPos(curhash);
					LogPrint("INFO", "find pos tx hash succeed: \n"
									  "   pos hash:%s \n"
									  "adjust hash:%s \r\n", curhash.GetHex(), adjusthash.GetHex());
					vector<unsigned char> vSign;
					if (pwalletMain->Sign(item.keyID, pBlock->SignatureHash(), vSign,
							item.MinerPKey.IsValid())) {
						LogPrint("INFO", "Create new block hash:%s\n", pBlock->GetHash().GetHex());
						LogPrint("miner", "Miner account info:%s\n", item.ToString());
						LogPrint("miner", "CreatePosTx block hash:%s, postxinfo:%s\n",pBlock->GetHash().GetHex(), postxinfo.ToString().c_str());
						pBlock->SetSignature(vSign);
						return true;
					} else {
						LogPrint("ERROR", "sign fail\r\n");
					}
				} else {
					LogPrint("ERROR", "GetKey fail or GetVec6 fail\r\n");
				}

			}
		}
	}

	return false;
}
Example #7
0
bool CVmRunEvn::OpeatorAccount(const vector<CVmOperate>& listoperate, CAccountViewCache& view, const int nCurHeight) {

	NewAccont.clear();
	for (auto& it : listoperate) {
//		CTransaction* tx = static_cast<CTransaction*>(listTx.get());
//		CFund fund;
//		memcpy(&fund.value,it.money,sizeof(it.money));
//		fund.nHeight = it.outheight;
		uint64_t value;
		memcpy(&value, it.money, sizeof(it.money));

		auto tem = std::make_shared<CAccount>();
//		vector_unsigned_char accountid = GetAccountID(it);
//		if (accountid.size() == 0) {
//			return false;
//		}
//		vector_unsigned_char accountid(it.accountid,it.accountid+sizeof(it.accountid));
		vector_unsigned_char accountid = GetAccountID(it);
		CRegID userregId;
		CKeyID userkeyid;

		if(accountid.size() == 6){
			userregId.SetRegID(accountid);
			if(!view.GetAccount(CUserID(userregId), *tem.get())){
				return false;                                           /// 账户不存在
			}
		}else{
			string sharkfundaddr(accountid.begin(), accountid.end());
			userkeyid = CKeyID(sharkfundaddr);
			 if(!view.GetAccount(CUserID(userkeyid), *tem.get()))
			 {
					tem->keyID = userkeyid;									  /// 未产生过交易记录的账户
					//return false;                                           /// 账户不存在
			 }
		}

		shared_ptr<CAccount> vmAccount = GetAccount(tem);
		if (vmAccount.get() == NULL) {
			RawAccont.push_back(tem);
			vmAccount = tem;
		}
		LogPrint("vm", "account id:%s\r\n", HexStr(accountid).c_str());
		LogPrint("vm", "befer account:%s\r\n", vmAccount.get()->ToString().c_str());
		bool ret = false;
//		vector<CScriptDBOperLog> vAuthorLog;
		//todolist
//		if(IsSignatureAccount(vmAccount.get()->regID) || vmAccount.get()->regID == boost::get<CRegID>(tx->appRegId))
		{
			ret = vmAccount.get()->OperateAccount((OperType)it.opeatortype, value, nCurHeight);
		}
//		else{
//			ret = vmAccount.get()->OperateAccount((OperType)it.opeatortype, fund, *m_ScriptDBTip, vAuthorLog,  height, &GetScriptRegID().GetVec6(), true);
//		}

//		LogPrint("vm", "after account:%s\r\n", vmAccount.get()->ToString().c_str());
		if (!ret) {
			return false;
		}
		NewAccont.push_back(vmAccount);
//		m_dblog->insert(m_dblog->end(), vAuthorLog.begin(), vAuthorLog.end());

	}
	return true;
}