OSDN Git Service

fix list-transactions unconfirmed transactions sorting by time (#1037)
authoroysheng <33340252+oysheng@users.noreply.github.com>
Sat, 9 Jun 2018 07:26:07 +0000 (15:26 +0800)
committerPaladz <yzhu101@uottawa.ca>
Sat, 9 Jun 2018 07:26:07 +0000 (15:26 +0800)
* list-transactions result order by time desc

* optimise list-transactions unconfirmed transaction order

* optimise

wallet/indexer.go
wallet/unconfirmed.go

index 44f729a..3f8b0ac 100644 (file)
@@ -491,8 +491,8 @@ func (w *Wallet) GetTransactions(accountID string) ([]*query.AnnotatedTx, error)
                }
 
                if accountID == "" || findTransactionsByAccount(annotatedTx, accountID) {
-                       annotateTxsAsset(w, annotatedTxs)
-                       annotatedTxs = append(annotatedTxs, annotatedTx)
+                       annotateTxsAsset(w, []*query.AnnotatedTx{annotatedTx})
+                       annotatedTxs = append([]*query.AnnotatedTx{annotatedTx}, annotatedTxs...)
                }
        }
 
index 04f0e3c..d78a50c 100644 (file)
@@ -3,27 +3,30 @@ package wallet
 import (
        "encoding/json"
        "fmt"
+       "time"
 
        "github.com/bytom/blockchain/query"
        "github.com/bytom/protocol/bc/types"
+       "sort"
 )
 
 const (
-       //unconfirmedTxPrefix is txpool unconfirmed transactions prefix
-       unconfirmedTxPrefix = "UTXS:"
+       //UnconfirmedTxPrefix is txpool unconfirmed transactions prefix
+       UnconfirmedTxPrefix = "UTXS:"
 )
 
 func calcUnconfirmedTxKey(formatKey string) []byte {
-       return []byte(unconfirmedTxPrefix + formatKey)
+       return []byte(UnconfirmedTxPrefix + formatKey)
 }
 
 // SaveUnconfirmedTx save unconfirmed annotated transaction to the database
 func (w *Wallet) SaveUnconfirmedTx(tx *types.Tx) error {
        annotatedTx := &query.AnnotatedTx{
-               ID:      tx.ID,
-               Inputs:  make([]*query.AnnotatedInput, 0, len(tx.Inputs)),
-               Outputs: make([]*query.AnnotatedOutput, 0, len(tx.Outputs)),
-               Size:    tx.SerializedSize,
+               ID:        tx.ID,
+               Timestamp: uint64(time.Now().Unix()),
+               Inputs:    make([]*query.AnnotatedInput, 0, len(tx.Inputs)),
+               Outputs:   make([]*query.AnnotatedOutput, 0, len(tx.Outputs)),
+               Size:      tx.SerializedSize,
        }
 
        for i := range tx.Inputs {
@@ -63,11 +66,18 @@ func (w *Wallet) GetUnconfirmedTxByTxID(txID string) (*query.AnnotatedTx, error)
        return annotatedTx, nil
 }
 
+// SortByTimestamp implements sort.Interface for AnnotatedTx slices
+type SortByTimestamp []*query.AnnotatedTx
+
+func (a SortByTimestamp) Len() int           { return len(a) }
+func (a SortByTimestamp) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
+func (a SortByTimestamp) Less(i, j int) bool { return a[i].Timestamp > a[j].Timestamp }
+
 // 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))
+       txIter := w.DB.IteratorPrefix([]byte(UnconfirmedTxPrefix))
        defer txIter.Release()
        for txIter.Next() {
                annotatedTx := &query.AnnotatedTx{}
@@ -76,10 +86,12 @@ func (w *Wallet) GetUnconfirmedTxs(accountID string) ([]*query.AnnotatedTx, erro
                }
 
                if accountID == "" || findTransactionsByAccount(annotatedTx, accountID) {
-                       annotateTxsAsset(w, annotatedTxs)
-                       annotatedTxs = append(annotatedTxs, annotatedTx)
+                       annotateTxsAsset(w, []*query.AnnotatedTx{annotatedTx})
+                       annotatedTxs = append([]*query.AnnotatedTx{annotatedTx}, annotatedTxs...)
                }
        }
 
+       // sort SortByTimestamp by timestamp
+       sort.Sort(SortByTimestamp(annotatedTxs))
        return annotatedTxs, nil
 }