X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=account%2Futxo_keeper.go;h=57d5d681b52b7cf27c2b35235953752c661db6e2;hb=a0796ff72d52a3c5c1ad7d1dd02690978cb8ce3d;hp=ed4d685cc3e7ea9dd792bcfcfc648ed5ab440230;hpb=bbc2994305810e711972618633d6cacd0cb77f76;p=bytom%2Fvapor.git diff --git a/account/utxo_keeper.go b/account/utxo_keeper.go index ed4d685c..57d5d681 100644 --- a/account/utxo_keeper.go +++ b/account/utxo_keeper.go @@ -3,15 +3,11 @@ package account import ( "bytes" "container/list" - "encoding/json" "sort" "sync" "sync/atomic" "time" - log "github.com/sirupsen/logrus" - - dbm "github.com/vapor/database/leveldb" "github.com/vapor/errors" "github.com/vapor/protocol/bc" ) @@ -56,7 +52,7 @@ type utxoKeeper struct { // `sync/atomic` expects the first word in an allocated struct to be 64-bit // aligned on both ARM and x86-32. See https://goo.gl/zW7dgq for more details. nextIndex uint64 - db dbm.DB + store AccountStorer mtx sync.RWMutex currentHeight func() uint64 @@ -65,9 +61,9 @@ type utxoKeeper struct { reservations map[uint64]*reservation } -func newUtxoKeeper(f func() uint64, walletdb dbm.DB) *utxoKeeper { +func newUtxoKeeper(f func() uint64, store AccountStorer) *utxoKeeper { uk := &utxoKeeper{ - db: walletdb, + store: store, currentHeight: f, unconfirmed: make(map[bc.Hash]*UTXO), reserved: make(map[bc.Hash]uint64), @@ -223,16 +219,11 @@ func (uk *utxoKeeper) findUtxos(accountID string, assetID *bc.AssetID, useUnconf } } - utxoIter := uk.db.IteratorPrefix([]byte(UTXOPreFix)) - defer utxoIter.Release() - for utxoIter.Next() { - u := &UTXO{} - if err := json.Unmarshal(utxoIter.Value(), u); err != nil { - log.WithFields(log.Fields{"module": logModule, "err": err}).Error("utxoKeeper findUtxos fail on unmarshal utxo") - continue - } - appendUtxo(u) + UTXOs := uk.store.GetUTXOs() + for _, UTXO := range UTXOs { + appendUtxo(UTXO) } + if !useUnconfirmed { return utxos, immatureAmount } @@ -247,15 +238,7 @@ func (uk *utxoKeeper) findUtxo(outHash bc.Hash, useUnconfirmed bool) (*UTXO, err if u, ok := uk.unconfirmed[outHash]; useUnconfirmed && ok { return u, nil } - - u := &UTXO{} - if data := uk.db.Get(StandardUTXOKey(outHash)); data != nil { - return u, json.Unmarshal(data, u) - } - if data := uk.db.Get(ContractUTXOKey(outHash)); data != nil { - return u, json.Unmarshal(data, u) - } - return nil, ErrMatchUTXO + return uk.store.GetUTXO(outHash) } func (uk *utxoKeeper) optUTXOs(utxos []*UTXO, amount uint64) ([]*UTXO, uint64, uint64) {