X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=wallet%2Futxo.go;h=3c172ff978f3b60223827404e27dcc42473e87e5;hb=refs%2Fheads%2Fwallet-store-interface-dev;hp=1e60a34e1eba1d6beace0761a66ae24aad64d132;hpb=54373c1a3efe0e373ec1605840a4363e4b246c46;p=bytom%2Fvapor.git diff --git a/wallet/utxo.go b/wallet/utxo.go index 1e60a34e..3c172ff9 100644 --- a/wallet/utxo.go +++ b/wallet/utxo.go @@ -9,7 +9,6 @@ import ( "github.com/vapor/consensus" "github.com/vapor/consensus/segwit" "github.com/vapor/crypto/sha3pool" - dbm "github.com/vapor/database/leveldb" "github.com/vapor/errors" "github.com/vapor/protocol/bc" "github.com/vapor/protocol/bc/types" @@ -27,28 +26,22 @@ func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSma accountUtxos = w.AccountMgr.ListUnconfirmedUtxo(accountID, isSmartContract) } - accountUtxoIter := w.DB.IteratorPrefix([]byte(prefix + id)) - defer accountUtxoIter.Release() - - for accountUtxoIter.Next() { - accountUtxo := &account.UTXO{} - if err := json.Unmarshal(accountUtxoIter.Value(), accountUtxo); err != nil { - log.WithFields(log.Fields{"module": logModule, "err": err}).Warn("GetAccountUtxos fail on unmarshal utxo") - continue - } - + confirmedUtxos := w.store.GetAccountUtxos(prefix + id) + accountUtxos = append(accountUtxos, confirmedUtxos...) + newAccountUtxos := []*account.UTXO{} + for _, accountUtxo := range accountUtxos { if vote && accountUtxo.Vote == nil { continue } if accountID == accountUtxo.AccountID || accountID == "" { - accountUtxos = append(accountUtxos, accountUtxo) + newAccountUtxos = append(newAccountUtxos, accountUtxo) } } - return accountUtxos + return newAccountUtxos } -func (w *Wallet) attachUtxos(batch dbm.Batch, b *types.Block, txStatus *bc.TransactionStatus) { +func (w *Wallet) attachUtxos(b *types.Block, txStatus *bc.TransactionStatus) { for txIndex, tx := range b.Transactions { statusFail, err := txStatus.GetStatus(txIndex) if err != nil { @@ -60,22 +53,22 @@ func (w *Wallet) attachUtxos(batch dbm.Batch, b *types.Block, txStatus *bc.Trans inputUtxos := txInToUtxos(tx, statusFail) for _, inputUtxo := range inputUtxos { if segwit.IsP2WScript(inputUtxo.ControlProgram) { - batch.Delete(account.StandardUTXOKey(inputUtxo.OutputID)) + w.store.DeleteStardardUTXOByOutputID(inputUtxo.OutputID) } else { - batch.Delete(account.ContractUTXOKey(inputUtxo.OutputID)) + w.store.DeleteContractUTXOByOutputID(inputUtxo.OutputID) } } //hand update the transaction output utxos outputUtxos := txOutToUtxos(tx, statusFail, b.Height) utxos := w.filterAccountUtxo(outputUtxos) - if err := batchSaveUtxos(utxos, batch); err != nil { + if err := w.saveUtxos(utxos); err != nil { log.WithFields(log.Fields{"module": logModule, "err": err}).Error("attachUtxos fail on batchSaveUtxos") } } } -func (w *Wallet) detachUtxos(batch dbm.Batch, b *types.Block, txStatus *bc.TransactionStatus) { +func (w *Wallet) detachUtxos(b *types.Block, txStatus *bc.TransactionStatus) { for txIndex := len(b.Transactions) - 1; txIndex >= 0; txIndex-- { tx := b.Transactions[txIndex] for j := range tx.Outputs { @@ -90,9 +83,9 @@ func (w *Wallet) detachUtxos(batch dbm.Batch, b *types.Block, txStatus *bc.Trans } if segwit.IsP2WScript(code) { - batch.Delete(account.StandardUTXOKey(*tx.ResultIds[j])) + w.store.DeleteStardardUTXOByOutputID(*tx.ResultIds[j]) } else { - batch.Delete(account.ContractUTXOKey(*tx.ResultIds[j])) + w.store.DeleteContractUTXOByOutputID(*tx.ResultIds[j]) } } @@ -104,7 +97,7 @@ func (w *Wallet) detachUtxos(batch dbm.Batch, b *types.Block, txStatus *bc.Trans inputUtxos := txInToUtxos(tx, statusFail) utxos := w.filterAccountUtxo(inputUtxos) - if err := batchSaveUtxos(utxos, batch); err != nil { + if err := w.saveUtxos(utxos); err != nil { log.WithFields(log.Fields{"module": logModule, "err": err}).Error("detachUtxos fail on batchSaveUtxos") return } @@ -129,7 +122,7 @@ func (w *Wallet) filterAccountUtxo(utxos []*account.UTXO) []*account.UTXO { var hash [32]byte sha3pool.Sum256(hash[:], []byte(s)) - data := w.DB.Get(account.ContractKey(hash)) + data := w.store.GetRawProgramByAccountHash(hash) if data == nil { continue } @@ -151,7 +144,7 @@ func (w *Wallet) filterAccountUtxo(utxos []*account.UTXO) []*account.UTXO { return result } -func batchSaveUtxos(utxos []*account.UTXO, batch dbm.Batch) error { +func (w *Wallet) saveUtxos(utxos []*account.UTXO) error { for _, utxo := range utxos { data, err := json.Marshal(utxo) if err != nil { @@ -159,9 +152,9 @@ func batchSaveUtxos(utxos []*account.UTXO, batch dbm.Batch) error { } if segwit.IsP2WScript(utxo.ControlProgram) { - batch.Set(account.StandardUTXOKey(utxo.OutputID), data) + w.store.SetStandardUTXO(utxo.OutputID, data) } else { - batch.Set(account.ContractUTXOKey(utxo.OutputID), data) + w.store.SetContractUTXO(utxo.OutputID, data) } } return nil