OSDN Git Service

update GetAccountUTXOs
authorChengcheng Zhang <943420582@qq.com>
Fri, 28 Jun 2019 11:10:00 +0000 (19:10 +0800)
committerChengcheng Zhang <943420582@qq.com>
Fri, 28 Jun 2019 11:10:00 +0000 (19:10 +0800)
database/wallet_store.go
wallet/store.go
wallet/utxo.go

index a65fd8b..f5795ad 100644 (file)
@@ -504,16 +504,19 @@ func (store *WalletStore) DeleteWalletUTXOs() {
 }
 
 // GetAccountUTXOs get all account unspent outputs
-func (store *WalletStore) GetAccountUTXOs(key string) [][]byte {
+func (store *WalletStore) GetAccountUTXOs(key string) ([]*acc.UTXO, error) {
        accountUtxoIter := store.walletDB.IteratorPrefix([]byte(key))
        defer accountUtxoIter.Release()
 
-       rawUTXOs := make([][]byte, 0)
+       confirmedUTXOs := []*acc.UTXO{}
        for accountUtxoIter.Next() {
-               utxo := accountUtxoIter.Value()
-               rawUTXOs = append(rawUTXOs, utxo)
+               utxo := new(acc.UTXO)
+               if err := json.Unmarshal(accountUtxoIter.Value(), utxo); err != nil {
+                       return nil, err
+               }
+               confirmedUTXOs = append(confirmedUTXOs, utxo)
        }
-       return rawUTXOs
+       return confirmedUTXOs, nil
 }
 
 // SetRecoveryStatus set recovery status
index da4b7da..e8da9a6 100644 (file)
@@ -35,7 +35,7 @@ type WalletStorer interface {
        SetWalletInfo([]byte)
        DeleteWalletTransactions()
        DeleteWalletUTXOs()
-       GetAccountUTXOs(key string) [][]byte
+       GetAccountUTXOs(string) ([]*acc.UTXO, error)
        SetRecoveryStatus([]byte, []byte)
        DeleteRecoveryStatus([]byte)
        GetRecoveryStatus([]byte) []byte
index f7685d7..6ad0380 100644 (file)
@@ -1,8 +1,6 @@
 package wallet
 
 import (
-       "encoding/json"
-
        log "github.com/sirupsen/logrus"
 
        "github.com/vapor/account"
@@ -26,15 +24,9 @@ func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSma
                accountUtxos = w.AccountMgr.ListUnconfirmedUtxo(accountID, isSmartContract)
        }
 
-       rawConfirmedUTXOs := w.store.GetAccountUTXOs(string(prefix) + id)
-       confirmedUTXOs := []*account.UTXO{}
-       for _, rawConfirmedUTXO := range rawConfirmedUTXOs {
-               confirmedUTXO := new(account.UTXO)
-               if err := json.Unmarshal(rawConfirmedUTXO, confirmedUTXO); err != nil {
-                       log.WithFields(log.Fields{"module": logModule, "err": err}).Warn("GetAccountUTXOs fail on unmarshal utxo")
-                       continue
-               }
-               confirmedUTXOs = append(confirmedUTXOs, confirmedUTXO)
+       confirmedUTXOs, err := w.store.GetAccountUTXOs(string(prefix) + id)
+       if err != nil {
+               log.WithFields(log.Fields{"module": logModule, "err": err}).Error("GetAccountUtxos fail.")
        }
        accountUtxos = append(accountUtxos, confirmedUTXOs...)
 
@@ -43,7 +35,6 @@ func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSma
                if vote && accountUtxo.Vote == nil {
                        continue
                }
-
                if accountID == accountUtxo.AccountID || accountID == "" {
                        newAccountUtxos = append(newAccountUtxos, accountUtxo)
                }