OSDN Git Service

fix api
[bytom/bytom.git] / wallet / indexer.go
index 04d9643..9cf3392 100644 (file)
@@ -26,7 +26,7 @@ const (
        GlobalTxIndexPrefix = "GTID:"
 )
 
-var errAccntTxIDNotFound = errors.New("account TXID not found")
+var ErrAccntTxIDNotFound = errors.New("account TXID not found")
 
 func formatKey(blockHeight uint64, position uint32) string {
        return fmt.Sprintf("%016x%08x", blockHeight, position)
@@ -158,7 +158,7 @@ func (w *Wallet) getAccountTxByTxID(txID string) (*query.AnnotatedTx, error) {
        annotatedTx := &query.AnnotatedTx{}
        formatKey := w.DB.Get(calcTxIndexKey(txID))
        if formatKey == nil {
-               return nil, errAccntTxIDNotFound
+               return nil, ErrAccntTxIDNotFound
        }
 
        txInfo := w.DB.Get(calcAnnotatedKey(string(formatKey)))
@@ -174,7 +174,7 @@ func (w *Wallet) getGlobalTxByIndex(index string) (*query.AnnotatedTx, error) {
        annotatedTx := &query.AnnotatedTx{}
        formatKey := w.DB.Get(calcGlobalTxIndexKey(index))
        if formatKey == nil {
-               return nil, errAccntTxIDNotFound
+               return nil, ErrAccntTxIDNotFound
        }
 
        txInfo := w.DB.Get(calcAnnotatedKey(string(formatKey)))
@@ -238,24 +238,50 @@ func findTransactionsByAccount(annotatedTx *query.AnnotatedTx, accountID string)
        return false
 }
 
-// GetTransactions get all walletDB transactions, and filter transactions by accountID optional
-func (w *Wallet) GetTransactions(accountID string) ([]*query.AnnotatedTx, error) {
+// GetTransactions get all walletDB transactions or unconfirmed transactions, and filter transactions by accountID and StartTxID optional
+func (w *Wallet) GetTransactions(accountID string, StartTxID string, count uint, unconfirmed bool) ([]*query.AnnotatedTx, error) {
        annotatedTxs := []*query.AnnotatedTx{}
+       var startKey []byte
+       preFix := TxPrefix
 
-       txIter := w.DB.IteratorPrefix([]byte(TxPrefix))
-       defer txIter.Release()
-       for txIter.Next() {
+       if StartTxID != "" {
+               if unconfirmed {
+                       startKey = calcUnconfirmedTxKey(StartTxID)
+               } else {
+                       formatKey := w.DB.Get(calcTxIndexKey(StartTxID))
+                       if formatKey == nil {
+                               return nil, ErrAccntTxIDNotFound
+                       }
+                       startKey = calcAnnotatedKey(string(formatKey))
+               }
+       }
+
+       if unconfirmed {
+               preFix = UnconfirmedTxPrefix
+       }
+
+       itr := w.DB.IteratorPrefixWithStart([]byte(preFix), startKey, true)
+       defer itr.Release()
+
+       for txNum := count; itr.Next() && txNum > 0; {
                annotatedTx := &query.AnnotatedTx{}
-               if err := json.Unmarshal(txIter.Value(), &annotatedTx); err != nil {
+               if err := json.Unmarshal(itr.Value(), &annotatedTx); err != nil {
                        return nil, err
                }
 
                if accountID == "" || findTransactionsByAccount(annotatedTx, accountID) {
                        annotateTxsAsset(w, []*query.AnnotatedTx{annotatedTx})
                        annotatedTxs = append([]*query.AnnotatedTx{annotatedTx}, annotatedTxs...)
+                       txNum--
                }
        }
 
+       if unconfirmed {
+               sort.Sort(SortByTimestamp(annotatedTxs))
+       } else {
+               sort.Sort(SortByHeight(annotatedTxs))
+       }
+
        return annotatedTxs, nil
 }