Skip to content
This repository
Browse code

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

  • Loading branch information...
commit 9dd96f321a91ae1ae7ca676c60391274422ce3b6 1 parent 2a7758e
khalahan authored March 11, 2012

Showing 1 changed file with 36 additions and 21 deletions. Show diff stats Hide diff stats

  1. 57  src/namecoin.cpp
57  src/namecoin.cpp
@@ -195,6 +195,11 @@ int GetTxPosHeight(const CDiskTxPos& txPos)
195 195
         return 0;
196 196
     return pindex->nHeight;
197 197
 }
  198
+int GetTxPosHeight2(const CDiskTxPos& txPos, int nHeight)
  199
+{
  200
+	nHeight = GetTxPosHeight(txPos);
  201
+	return nHeight;
  202
+}
198 203
 
199 204
 
200 205
 int GetNameHeight(CTxDB& txdb, vector<unsigned char> vchName) {
@@ -473,12 +478,8 @@ bool GetTxOfName(CNameDB& dbName, vector<unsigned char> vchName, CTransaction& t
473 478
     return true;
474 479
 }
475 480
 
476  
-bool GetNameAddress(const CDiskTxPos& txPos, std::string& strAddress)
  481
+bool GetNameAddress(const CTransaction& tx, std::string& strAddress)
477 482
 {
478  
-    CTransaction tx;
479  
-    if (!tx.ReadFromDisk(txPos))
480  
-        return error("GetNameAddress() : could not read tx from disk");
481  
-
482 483
     int op;
483 484
     int nOut;
484 485
     vector<vector<unsigned char> > vvch;
@@ -489,6 +490,15 @@ bool GetNameAddress(const CDiskTxPos& txPos, std::string& strAddress)
489 490
     return true;
490 491
 }
491 492
 
  493
+bool GetNameAddress(const CDiskTxPos& txPos, std::string& strAddress)
  494
+{
  495
+    CTransaction tx;
  496
+    if (!tx.ReadFromDisk(txPos))
  497
+        return error("GetNameAddress() : could not read tx from disk");
  498
+
  499
+	return GetNameAddress(tx, strAddress);
  500
+}
  501
+
492 502
 Value name_list(const Array& params, bool fHelp)
493 503
 {
494 504
     if (fHelp || params.size() > 1)
@@ -526,25 +536,27 @@ Value name_list(const Array& params, bool fHelp)
526 536
                 continue;
527 537
 
528 538
             vchLastName = pairScan.first;
529  
-            string name = stringFromVch(pairScan.first);
530  
-            CDiskTxPos txPos = pairScan.second;
531 539
             vector<unsigned char> vchValue;
532  
-            int nHeight;
533  
-            uint256 hash;
  540
+			CTransaction tx;
  541
+            CDiskTxPos txPos = pairScan.second;
534 542
             if (!txPos.IsNull() &&
535  
-                    GetValueOfTxPos(txPos, vchValue, hash, nHeight) &&
536  
-                    pwalletMain->mapWallet.count(hash)
  543
+			        tx.ReadFromDisk(txPos) &&
  544
+					GetValueOfNameTx(tx, vchValue) &&
  545
+                    pwalletMain->mapWallet.count(tx.GetHash())
537 546
                     )
538 547
             {
539 548
                 string value = stringFromVch(vchValue);
540 549
                 Object oName;
  550
+                string name = stringFromVch(pairScan.first);
541 551
                 oName.push_back(Pair("name", name));
542 552
                 oName.push_back(Pair("value", value));
543  
-                if (!hooks->IsMine(pwalletMain->mapWallet[hash]))
  553
+                if (!hooks->IsMine(pwalletMain->mapWallet[tx.GetHash()]))
544 554
                     oName.push_back(Pair("transferred", 1));
545 555
                 string strAddress = "";
546  
-                GetNameAddress(txPos, strAddress);
  556
+                GetNameAddress(tx, strAddress);
547 557
                 oName.push_back(Pair("address", strAddress));
  558
+                int nHeight;
  559
+			    nHeight = GetTxPosHeight(txPos);
548 560
                 oName.push_back(Pair("expires_in", nHeight + GetDisplayExpirationDepth(nHeight) - pindexBest->nHeight));
549 561
                 if(nHeight + GetDisplayExpirationDepth(nHeight) - pindexBest->nHeight <= 0)
550 562
                 {
@@ -558,6 +570,7 @@ Value name_list(const Array& params, bool fHelp)
558 570
         if (vchName == vchLastName)
559 571
             break;
560 572
         vchName = vchLastName;
  573
+		break;
561 574
     }
562 575
 
563 576
     return oRes;
@@ -711,24 +724,26 @@ Value name_scan(const Array& params, bool fHelp)
711 724
     {
712 725
         Object oName;
713 726
         string name = stringFromVch(pairScan.first);
714  
-        CDiskTxPos txPos = pairScan.second;
715 727
         oName.push_back(Pair("name", name));
716 728
         vector<unsigned char> vchValue;
  729
+		CTransaction tx;
717 730
         int nHeight;
718  
-        uint256 hash;
719  
-        if (txPos.IsNull() ||
720  
-            !GetValueOfTxPos(txPos, vchValue, hash, nHeight) ||
721  
-            (GetValueOfTxPos(txPos, vchValue, hash, nHeight) && nHeight + GetDisplayExpirationDepth(nHeight) - pindexBest->nHeight <= 0))
  731
+        CDiskTxPos txPos = pairScan.second;
  732
+		nHeight = GetTxPosHeight(txPos);
  733
+        if (txPos.IsNull()
  734
+			|| (nHeight + GetDisplayExpirationDepth(nHeight) - pindexBest->nHeight <= 0)
  735
+			|| !tx.ReadFromDisk(txPos)
  736
+			|| !GetValueOfNameTx(tx, vchValue))
722 737
         {
723 738
             oName.push_back(Pair("expired", 1));
724 739
         }
725 740
         else
726 741
         {
727 742
             string value = stringFromVch(vchValue);
728  
-            oName.push_back(Pair("value", value));
729  
-            oName.push_back(Pair("txid", hash.GetHex()));
730 743
             string strAddress = "";
731  
-            GetNameAddress(txPos, strAddress);
  744
+            GetNameAddress(tx, strAddress);
  745
+            oName.push_back(Pair("value", value));
  746
+            oName.push_back(Pair("txid", tx.GetHash().GetHex()));
732 747
             oName.push_back(Pair("address", strAddress));
733 748
             oName.push_back(Pair("expires_in", nHeight + GetDisplayExpirationDepth(nHeight) - pindexBest->nHeight));
734 749
         }

0 notes on commit 9dd96f3

Please sign in to comment.
Something went wrong with that request. Please try again.