Exemple #1
0
bool SysTestBase::IsTxInTipBlock(const uint256& txHash) {
		CBlockIndex* pindex = chainActive.Tip();
		CBlock block;
		if (!ReadBlockFromDisk(block, pindex))
			return false;

		block.BuildMerkleTree();
		std::tuple<bool, int> ret = block.GetTxIndex(txHash);
		if (!std::get<0>(ret)) {
			return false;
		}

		return true;
	}
	bool GetTxIndexInBlock(const uint256& txHash, int& nIndex) {
		CBlockIndex* pindex = chainActive.Tip();
		CBlock block;
		if (!ReadBlockFromDisk(block, pindex))
			return false;

		block.BuildMerkleTree();
		std::tuple<bool,int> ret = block.GetTxIndex(txHash);
		if (!std::get<0>(ret)) {
			return false;
		}

		nIndex = std::get<1>(ret);
		return true;
	}
Value getscriptid(const Array& params, bool fHelp)
{
	if (fHelp || params.size() != 1) {
		throw runtime_error("getscriptid \n"
							"\nreturn an object containing regid and script\n"
							"\nArguments:\n"
							"1. txhash   (string, required) the transaction hash.\n"
							"\nResult:\n"
							"\nExamples:\n"
							+ HelpExampleCli("getscriptid", "5zQPcC1YpFMtwxiH787pSXanUECoGsxUq3KZieJxVG")
							+ HelpExampleRpc("getscriptid","5zQPcC1YpFMtwxiH787pSXanUECoGsxUq3KZieJxVG"));
	}

	uint256 txhash(uint256S(params[0].get_str()));

	int nIndex = 0;
	int BlockHeight =GetTxComfirmHigh(txhash, *pScriptDBTip) ;
	if(BlockHeight > chainActive.Height())
	{
		throw runtime_error("height larger than tip block \n");
	}
	else if(BlockHeight == -1){
		throw runtime_error("tx hash unconfirmed \n");
	}
	CBlockIndex* pindex = chainActive[BlockHeight];
	CBlock block;
	if (!ReadBlockFromDisk(block, pindex))
		return false;

	block.BuildMerkleTree();
	std::tuple<bool,int> ret = block.GetTxIndex(txhash);
	if (!std::get<0>(ret)) {
		 throw runtime_error("tx not exit in block");
	}

	nIndex = std::get<1>(ret);

	CRegID striptID(BlockHeight, nIndex);

	Object result;
	result.push_back(Pair("regid:", striptID.ToString()));
	result.push_back(Pair("script", HexStr(striptID.GetVec6())));
	return result;
}