OSDN Git Service

update GetUTXOs wallet-store-interface-marshal-test
authorChengcheng Zhang <943420582@qq.com>
Thu, 27 Jun 2019 17:55:43 +0000 (01:55 +0800)
committerChengcheng Zhang <943420582@qq.com>
Thu, 27 Jun 2019 17:55:43 +0000 (01:55 +0800)
account/store.go
account/utxo_keeper.go
database/account_store.go
test/mock/UTXO.go

index bdae964..0fb3dcc 100644 (file)
@@ -33,7 +33,7 @@ type AccountStorer interface {
        SetControlProgram(common.Hash, *CtrlProgram) error
        SetContractIndex(string, uint64)
        SetBip44ContractIndex(string, bool, uint64)
-       GetUTXOs() [][]byte
+       GetUTXOs() []*UTXO
        GetStandardUTXO(bc.Hash) []byte
        GetContractUTXO(bc.Hash) []byte
        SetStandardUTXO(bc.Hash, []byte)
index fc27b7e..41e645a 100644 (file)
@@ -9,8 +9,6 @@ import (
        "sync/atomic"
        "time"
 
-       log "github.com/sirupsen/logrus"
-
        "github.com/vapor/errors"
        "github.com/vapor/protocol/bc"
 )
@@ -222,14 +220,9 @@ func (uk *utxoKeeper) findUtxos(accountID string, assetID *bc.AssetID, useUnconf
                }
        }
 
-       rawUTXOs := uk.store.GetUTXOs()
-       for _, rawUTXO := range rawUTXOs {
-               utxo := new(UTXO)
-               if err := json.Unmarshal(rawUTXO, utxo); err != nil {
-                       log.WithFields(log.Fields{"module": logModule, "err": err}).Error("utxoKeeper findUtxos fail on unmarshal utxo")
-                       continue
-               }
-               appendUtxo(utxo)
+       UTXOs := uk.store.GetUTXOs()
+       for _, UTXO := range UTXOs {
+               appendUtxo(UTXO)
        }
 
        if !useUnconfirmed {
index ae88f23..22cc0b6 100644 (file)
@@ -4,6 +4,7 @@ import (
        "encoding/json"
        "strings"
 
+       log "github.com/sirupsen/logrus"
        acc "github.com/vapor/account"
        "github.com/vapor/common"
        "github.com/vapor/crypto/ed25519/chainkd"
@@ -328,13 +329,18 @@ func (store *AccountStore) SetBip44ContractIndex(accountID string, change bool,
 }
 
 // GetUTXOs get utxos by accountID
-func (store *AccountStore) GetUTXOs() [][]byte {
+func (store *AccountStore) GetUTXOs() []*acc.UTXO {
        utxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
        defer utxoIter.Release()
 
-       utxos := make([][]byte, 0)
+       utxos := []*acc.UTXO{}
        for utxoIter.Next() {
-               utxos = append(utxos, utxoIter.Value())
+               utxo := new(acc.UTXO)
+               if err := json.Unmarshal(utxoIter.Value(), utxo); err != nil {
+                       log.WithFields(log.Fields{"module": logModule, "err": err}).Error("utxoKeeper findUtxos fail on unmarshal utxo")
+                       continue
+               }
+               utxos = append(utxos, utxo)
        }
        return utxos
 }
index dffd1d7..4ec94ed 100644 (file)
@@ -9,8 +9,6 @@ import (
        "sync/atomic"
        "time"
 
-       log "github.com/sirupsen/logrus"
-
        acc "github.com/vapor/account"
        "github.com/vapor/protocol/bc"
 )
@@ -88,7 +86,7 @@ func (uk *UTXOKeeper) Reserve(accountID string, assetID *bc.AssetID, amount uint
        uk.mtx.Lock()
        defer uk.mtx.Unlock()
 
-       utxos, immatureAmount := uk.findUtxos(accountID, assetID, useUnconfirmed, vote)
+       utxos, immatureAmount := uk.FindUtxos(accountID, assetID, useUnconfirmed, vote)
        optUtxos, optAmount, reservedAmount := uk.optUTXOs(utxos, amount)
        if optAmount+reservedAmount+immatureAmount < amount {
                return nil, acc.ErrInsufficient
@@ -161,7 +159,7 @@ func (uk *UTXOKeeper) FindUtxo(outHash bc.Hash, useUnconfirmed bool) (*acc.UTXO,
        return nil, acc.ErrMatchUTXO
 }
 
-func (uk *UTXOKeeper) findUtxos(accountID string, assetID *bc.AssetID, useUnconfirmed bool, vote []byte) ([]*acc.UTXO, uint64) {
+func (uk *UTXOKeeper) FindUtxos(accountID string, assetID *bc.AssetID, useUnconfirmed bool, vote []byte) ([]*acc.UTXO, uint64) {
        immatureAmount := uint64(0)
        currentHeight := uk.CurrentHeight()
        utxos := []*acc.UTXO{}
@@ -176,14 +174,9 @@ func (uk *UTXOKeeper) findUtxos(accountID string, assetID *bc.AssetID, useUnconf
                }
        }
 
-       rawUTXOs := uk.Store.GetUTXOs()
-       for _, rawUTXO := range rawUTXOs {
-               utxo := new(acc.UTXO)
-               if err := json.Unmarshal(rawUTXO, utxo); err != nil {
-                       log.WithFields(log.Fields{"module": logModule, "err": err}).Error("utxoKeeper findUtxos fail on unmarshal utxo")
-                       continue
-               }
-               appendUtxo(utxo)
+       UTXOs := uk.Store.GetUTXOs()
+       for _, UTXO := range UTXOs {
+               appendUtxo(UTXO)
        }
 
        if !useUnconfirmed {
@@ -248,38 +241,3 @@ func (uk *UTXOKeeper) optUTXOs(utxos []*acc.UTXO, amount uint64) ([]*acc.UTXO, u
        }
        return optUtxos, optAmount, reservedAmount
 }
-
-func (uk *UTXOKeeper) FindUtxos(accountID string, assetID *bc.AssetID, useUnconfirmed bool, vote []byte) ([]*acc.UTXO, uint64) {
-       immatureAmount := uint64(0)
-       currentHeight := uk.CurrentHeight()
-       utxos := []*acc.UTXO{}
-       appendUtxo := func(u *acc.UTXO) {
-               if u.AccountID != accountID || u.AssetID != *assetID || !bytes.Equal(u.Vote, vote) {
-                       return
-               }
-               if u.ValidHeight > currentHeight {
-                       immatureAmount += u.Amount
-               } else {
-                       utxos = append(utxos, u)
-               }
-       }
-
-       rawUTXOs := uk.Store.GetUTXOs()
-       for _, rawUTXO := range rawUTXOs {
-               utxo := new(acc.UTXO)
-               if err := json.Unmarshal(rawUTXO, utxo); err != nil {
-                       log.WithFields(log.Fields{"module": logModule, "err": err}).Error("utxoKeeper findUtxos fail on unmarshal utxo")
-                       continue
-               }
-               appendUtxo(utxo)
-       }
-
-       if !useUnconfirmed {
-               return utxos, immatureAmount
-       }
-
-       for _, u := range uk.Unconfirmed {
-               appendUtxo(u)
-       }
-       return utxos, immatureAmount
-}