Permalink
Browse files

name_scan and name_list faster (35% and 55% faster)

  • Loading branch information...
1 parent 2a7758e commit 9dd96f321a91ae1ae7ca676c60391274422ce3b6 @khalahan khalahan committed Mar 11, 2012
Showing with 36 additions and 21 deletions.
  1. +36 −21 src/namecoin.cpp
View
@@ -195,6 +195,11 @@ int GetTxPosHeight(const CDiskTxPos& txPos)
return 0;
return pindex->nHeight;
}
+int GetTxPosHeight2(const CDiskTxPos& txPos, int nHeight)
+{
+ nHeight = GetTxPosHeight(txPos);
+ return nHeight;
+}
int GetNameHeight(CTxDB& txdb, vector<unsigned char> vchName) {
@@ -473,12 +478,8 @@ bool GetTxOfName(CNameDB& dbName, vector<unsigned char> vchName, CTransaction& t
return true;
}
-bool GetNameAddress(const CDiskTxPos& txPos, std::string& strAddress)
+bool GetNameAddress(const CTransaction& tx, std::string& strAddress)
{
- CTransaction tx;
- if (!tx.ReadFromDisk(txPos))
- return error("GetNameAddress() : could not read tx from disk");
-
int op;
int nOut;
vector<vector<unsigned char> > vvch;
@@ -489,6 +490,15 @@ bool GetNameAddress(const CDiskTxPos& txPos, std::string& strAddress)
return true;
}
+bool GetNameAddress(const CDiskTxPos& txPos, std::string& strAddress)
+{
+ CTransaction tx;
+ if (!tx.ReadFromDisk(txPos))
+ return error("GetNameAddress() : could not read tx from disk");
+
+ return GetNameAddress(tx, strAddress);
+}
+
Value name_list(const Array& params, bool fHelp)
{
if (fHelp || params.size() > 1)
@@ -526,25 +536,27 @@ Value name_list(const Array& params, bool fHelp)
continue;
vchLastName = pairScan.first;
- string name = stringFromVch(pairScan.first);
- CDiskTxPos txPos = pairScan.second;
vector<unsigned char> vchValue;
- int nHeight;
- uint256 hash;
+ CTransaction tx;
+ CDiskTxPos txPos = pairScan.second;
if (!txPos.IsNull() &&
- GetValueOfTxPos(txPos, vchValue, hash, nHeight) &&
- pwalletMain->mapWallet.count(hash)
+ tx.ReadFromDisk(txPos) &&
+ GetValueOfNameTx(tx, vchValue) &&
+ pwalletMain->mapWallet.count(tx.GetHash())
)
{
string value = stringFromVch(vchValue);
Object oName;
+ string name = stringFromVch(pairScan.first);
oName.push_back(Pair("name", name));
oName.push_back(Pair("value", value));
- if (!hooks->IsMine(pwalletMain->mapWallet[hash]))
+ if (!hooks->IsMine(pwalletMain->mapWallet[tx.GetHash()]))
oName.push_back(Pair("transferred", 1));
string strAddress = "";
- GetNameAddress(txPos, strAddress);
+ GetNameAddress(tx, strAddress);
oName.push_back(Pair("address", strAddress));
+ int nHeight;
+ nHeight = GetTxPosHeight(txPos);
oName.push_back(Pair("expires_in", nHeight + GetDisplayExpirationDepth(nHeight) - pindexBest->nHeight));
if(nHeight + GetDisplayExpirationDepth(nHeight) - pindexBest->nHeight <= 0)
{
@@ -558,6 +570,7 @@ Value name_list(const Array& params, bool fHelp)
if (vchName == vchLastName)
break;
vchName = vchLastName;
+ break;
}
return oRes;
@@ -711,24 +724,26 @@ Value name_scan(const Array& params, bool fHelp)
{
Object oName;
string name = stringFromVch(pairScan.first);
- CDiskTxPos txPos = pairScan.second;
oName.push_back(Pair("name", name));
vector<unsigned char> vchValue;
+ CTransaction tx;
int nHeight;
- uint256 hash;
- if (txPos.IsNull() ||
- !GetValueOfTxPos(txPos, vchValue, hash, nHeight) ||
- (GetValueOfTxPos(txPos, vchValue, hash, nHeight) && nHeight + GetDisplayExpirationDepth(nHeight) - pindexBest->nHeight <= 0))
+ CDiskTxPos txPos = pairScan.second;
+ nHeight = GetTxPosHeight(txPos);
+ if (txPos.IsNull()
+ || (nHeight + GetDisplayExpirationDepth(nHeight) - pindexBest->nHeight <= 0)
+ || !tx.ReadFromDisk(txPos)
+ || !GetValueOfNameTx(tx, vchValue))
{
oName.push_back(Pair("expired", 1));
}
else
{
string value = stringFromVch(vchValue);
- oName.push_back(Pair("value", value));
- oName.push_back(Pair("txid", hash.GetHex()));
string strAddress = "";
- GetNameAddress(txPos, strAddress);
+ GetNameAddress(tx, strAddress);
+ oName.push_back(Pair("value", value));
+ oName.push_back(Pair("txid", tx.GetHash().GetHex()));
oName.push_back(Pair("address", strAddress));
oName.push_back(Pair("expires_in", nHeight + GetDisplayExpirationDepth(nHeight) - pindexBest->nHeight));
}

0 comments on commit 9dd96f3

Please sign in to comment.