OSDN Git Service

add GetTransactions GetAllUnconfirmedTxs
authorChengcheng Zhang <943420582@qq.com>
Fri, 21 Jun 2019 08:54:16 +0000 (16:54 +0800)
committerChengcheng Zhang <943420582@qq.com>
Fri, 21 Jun 2019 08:54:16 +0000 (16:54 +0800)
wallet/store.go
wallet/unconfirmed.go

index 48dbb87..92225c8 100644 (file)
@@ -29,6 +29,8 @@ type DB interface {
        GetTxIndexByTxID(string) []byte
        GetTxByTxIndex([]byte) []byte
        GetGlobalTxByTxID(string) []byte
+       GetTransactions() ([]*query.AnnotatedTx, error)
+       GetAllUnconfirmedTxs() ([]*query.AnnotatedTx, error)
        GetUnconfirmedTxByTxID(string) []byte
        SetUnconfirmedTx(string, []byte)
        DeleteStardardUTXOByOutputID(bc.Hash)
@@ -134,48 +136,38 @@ func (store *LevelDBStore) GetGlobalTxByTxID(txID string) []byte {
        return store.DB.Get(calcGlobalTxIndexKey(txID))
 }
 
-// // GetTransactions get all walletDB transactions, and filter transactions by accountID optional
-// func (store *LevelDBStore) GetTransactionsByAccountID(accountID string) ([]*query.AnnotatedTx, error) {
-//     annotatedTxs := []*query.AnnotatedTx{}
-
-//     txIter := store.DB.IteratorPrefix([]byte(TxPrefix))
-//     defer txIter.Release()
-//     for txIter.Next() {
-//             annotatedTx := &query.AnnotatedTx{}
-//             if err := json.Unmarshal(txIter.Value(), &annotatedTx); err != nil {
-//                     return nil, err
-//             }
-
-//             if accountID == "" || findTransactionsByAccount(annotatedTx, accountID) {
-//                     annotateTxsAsset(w, []*query.AnnotatedTx{annotatedTx})
-//                     annotatedTxs = append([]*query.AnnotatedTx{annotatedTx}, annotatedTxs...)
-//             }
-//     }
-
-//     return annotatedTxs, nil
-// }
-
-// // GetUnconfirmedTxs get account unconfirmed transactions, filter transactions by accountID when accountID is not empty
-// func (w *Wallet) GetUnconfirmedTxs(accountID string) ([]*query.AnnotatedTx, error) {
-//     annotatedTxs := []*query.AnnotatedTx{}
-//     txIter := w.DB.IteratorPrefix([]byte(UnconfirmedTxPrefix))
-//     defer txIter.Release()
-
-//     for txIter.Next() {
-//             annotatedTx := &query.AnnotatedTx{}
-//             if err := json.Unmarshal(txIter.Value(), &annotatedTx); err != nil {
-//                     return nil, err
-//             }
-
-//             if accountID == "" || findTransactionsByAccount(annotatedTx, accountID) {
-//                     annotateTxsAsset(w, []*query.AnnotatedTx{annotatedTx})
-//                     annotatedTxs = append([]*query.AnnotatedTx{annotatedTx}, annotatedTxs...)
-//             }
-//     }
-
-//     sort.Sort(SortByTimestamp(annotatedTxs))
-//     return annotatedTxs, nil
-// }
+// GetTransactions get all walletDB transactions
+func (store *LevelDBStore) GetTransactions() ([]*query.AnnotatedTx, error) {
+       annotatedTxs := []*query.AnnotatedTx{}
+
+       txIter := store.DB.IteratorPrefix([]byte(TxPrefix))
+       defer txIter.Release()
+       for txIter.Next() {
+               annotatedTx := &query.AnnotatedTx{}
+               if err := json.Unmarshal(txIter.Value(), &annotatedTx); err != nil {
+                       return nil, err
+               }
+               annotatedTxs = append(annotatedTxs, annotatedTx)
+       }
+
+       return annotatedTxs, nil
+}
+
+// GetAllUnconfirmedTxs get all unconfirmed txs
+func (store *LevelDBStore) GetAllUnconfirmedTxs() ([]*query.AnnotatedTx, error) {
+       annotatedTxs := []*query.AnnotatedTx{}
+       txIter := store.DB.IteratorPrefix([]byte(UnconfirmedTxPrefix))
+       defer txIter.Release()
+
+       for txIter.Next() {
+               annotatedTx := &query.AnnotatedTx{}
+               if err := json.Unmarshal(txIter.Value(), &annotatedTx); err != nil {
+                       return nil, err
+               }
+               annotatedTxs = append(annotatedTxs, annotatedTx)
+       }
+       return annotatedTxs, nil
+}
 
 // GetUnconfirmedTxByTxID get unconfirmed tx by txID
 func (store *LevelDBStore) GetUnconfirmedTxByTxID(txID string) []byte {
index 4eec6a2..dc09c34 100644 (file)
@@ -50,6 +50,7 @@ func (w *Wallet) GetUnconfirmedTxs(accountID string) ([]*query.AnnotatedTx, erro
        txIter := w.DB.IteratorPrefix([]byte(UnconfirmedTxPrefix))
        defer txIter.Release()
 
+       // replace with GetAllUnconfirmedTxs
        for txIter.Next() {
                annotatedTx := &query.AnnotatedTx{}
                if err := json.Unmarshal(txIter.Value(), &annotatedTx); err != nil {