OSDN Git Service

pass ci
[bytom/vapor.git] / wallet / utxo.go
index 20775fd..3c172ff 100644 (file)
@@ -9,14 +9,13 @@ 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"
 )
 
 // GetAccountUtxos return all account unspent outputs
-func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSmartContract bool) []*account.UTXO {
+func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSmartContract bool, vote bool) []*account.UTXO {
        prefix := account.UTXOPreFix
        if isSmartContract {
                prefix = account.SUTXOPrefix
@@ -27,24 +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")
+       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 {
@@ -56,38 +53,39 @@ 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
-               validHeight := uint64(0)
-               if txIndex == 0 {
-                       validHeight = b.Height + consensus.CoinbasePendingBlockNumber
-               }
-               outputUtxos := txOutToUtxos(tx, statusFail, validHeight)
+               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 {
-                       resOut, err := tx.IntraChainOutput(*tx.ResultIds[j])
-                       if err != nil {
+                       code := []byte{}
+                       switch resOut := tx.Entries[*tx.ResultIds[j]].(type) {
+                       case *bc.IntraChainOutput:
+                               code = resOut.ControlProgram.Code
+                       case *bc.VoteOutput:
+                               code = resOut.ControlProgram.Code
+                       default:
                                continue
                        }
 
-                       if segwit.IsP2WScript(resOut.ControlProgram.Code) {
-                               batch.Delete(account.StandardUTXOKey(*tx.ResultIds[j]))
+                       if segwit.IsP2WScript(code) {
+                               w.store.DeleteStardardUTXOByOutputID(*tx.ResultIds[j])
                        } else {
-                               batch.Delete(account.ContractUTXOKey(*tx.ResultIds[j]))
+                               w.store.DeleteContractUTXOByOutputID(*tx.ResultIds[j])
                        }
                }
 
@@ -99,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
                }
@@ -124,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
                }
@@ -146,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 {
@@ -154,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
@@ -165,38 +163,41 @@ func batchSaveUtxos(utxos []*account.UTXO, batch dbm.Batch) error {
 func txInToUtxos(tx *types.Tx, statusFail bool) []*account.UTXO {
        utxos := []*account.UTXO{}
        for _, inpID := range tx.Tx.InputIDs {
-               sp, err := tx.Spend(inpID)
-               if err != nil {
-                       continue
-               }
 
-               entryOutput, err := tx.Entry(*sp.SpentOutputId)
+               e, err := tx.Entry(inpID)
                if err != nil {
-                       log.WithFields(log.Fields{"module": logModule, "err": err}).Error("txInToUtxos fail on get entryOutput")
                        continue
                }
-
                utxo := &account.UTXO{}
-               switch resOut := entryOutput.(type) {
-               case *bc.IntraChainOutput:
+               switch inp := e.(type) {
+               case *bc.Spend:
+                       resOut, err := tx.IntraChainOutput(*inp.SpentOutputId)
+                       if err != nil {
+                               log.WithFields(log.Fields{"module": logModule, "err": err}).Error("txInToUtxos fail on get resOut for spedn")
+                               continue
+                       }
                        if statusFail && *resOut.Source.Value.AssetId != *consensus.BTMAssetID {
                                continue
                        }
                        utxo = &account.UTXO{
-                               OutputID:       *sp.SpentOutputId,
+                               OutputID:       *inp.SpentOutputId,
                                AssetID:        *resOut.Source.Value.AssetId,
                                Amount:         resOut.Source.Value.Amount,
                                ControlProgram: resOut.ControlProgram.Code,
                                SourceID:       *resOut.Source.Ref,
                                SourcePos:      resOut.Source.Position,
                        }
-
-               case *bc.VoteOutput:
+               case *bc.VetoInput:
+                       resOut, err := tx.VoteOutput(*inp.SpentOutputId)
+                       if err != nil {
+                               log.WithFields(log.Fields{"module": logModule, "err": err}).Error("txInToUtxos fail on get resOut for vetoInput")
+                               continue
+                       }
                        if statusFail && *resOut.Source.Value.AssetId != *consensus.BTMAssetID {
                                continue
                        }
                        utxo = &account.UTXO{
-                               OutputID:       *sp.SpentOutputId,
+                               OutputID:       *inp.SpentOutputId,
                                AssetID:        *resOut.Source.Value.AssetId,
                                Amount:         resOut.Source.Value.Amount,
                                ControlProgram: resOut.ControlProgram.Code,
@@ -204,18 +205,20 @@ func txInToUtxos(tx *types.Tx, statusFail bool) []*account.UTXO {
                                SourcePos:      resOut.Source.Position,
                                Vote:           resOut.Vote,
                        }
-
                default:
-                       log.WithFields(log.Fields{"module": logModule}).Error("txInToUtxos fail on get resOut")
                        continue
                }
-
                utxos = append(utxos, utxo)
        }
        return utxos
 }
 
-func txOutToUtxos(tx *types.Tx, statusFail bool, vaildHeight uint64) []*account.UTXO {
+func txOutToUtxos(tx *types.Tx, statusFail bool, blockHeight uint64) []*account.UTXO {
+       validHeight := uint64(0)
+       if tx.Inputs[0].InputType() == types.CoinbaseInputType {
+               validHeight = blockHeight + consensus.CoinbasePendingBlockNumber
+       }
+
        utxos := []*account.UTXO{}
        for i, out := range tx.Outputs {
                entryOutput, err := tx.Entry(*tx.ResultIds[i])
@@ -237,13 +240,19 @@ func txOutToUtxos(tx *types.Tx, statusFail bool, vaildHeight uint64) []*account.
                                ControlProgram: out.ControlProgram(),
                                SourceID:       *bcOut.Source.Ref,
                                SourcePos:      bcOut.Source.Position,
-                               ValidHeight:    vaildHeight,
+                               ValidHeight:    validHeight,
                        }
 
                case *bc.VoteOutput:
                        if statusFail && *out.AssetAmount().AssetId != *consensus.BTMAssetID {
                                continue
                        }
+
+                       voteValidHeight := blockHeight + consensus.VotePendingBlockNumber
+                       if validHeight < voteValidHeight {
+                               validHeight = voteValidHeight
+                       }
+
                        utxo = &account.UTXO{
                                OutputID:       *tx.OutputID(i),
                                AssetID:        *out.AssetAmount().AssetId,
@@ -251,12 +260,12 @@ func txOutToUtxos(tx *types.Tx, statusFail bool, vaildHeight uint64) []*account.
                                ControlProgram: out.ControlProgram(),
                                SourceID:       *bcOut.Source.Ref,
                                SourcePos:      bcOut.Source.Position,
-                               ValidHeight:    vaildHeight,
+                               ValidHeight:    validHeight,
                                Vote:           bcOut.Vote,
                        }
 
                default:
-                       log.WithFields(log.Fields{"module": logModule}).Error("txOutToUtxos fail on get bcOut")
+                       log.WithFields(log.Fields{"module": logModule}).Warn("txOutToUtxos fail on get bcOut")
                        continue
                }