OSDN Git Service

add InitStore
authorChengcheng Zhang <943420582@qq.com>
Mon, 8 Jul 2019 08:46:55 +0000 (16:46 +0800)
committerChengcheng Zhang <943420582@qq.com>
Mon, 8 Jul 2019 08:46:55 +0000 (16:46 +0800)
database/wallet_store.go
wallet/indexer.go
wallet/store.go
wallet/utxo.go
wallet/wallet.go
wallet/wallet_test.go

index d021aed..17893e7 100644 (file)
@@ -108,14 +108,11 @@ func NewWalletStore(db dbm.DB) *WalletStore {
        }
 }
 
-// InitBatch initial batch
-func (store *WalletStore) InitBatch() error {
-       if store.batch != nil {
-               return errors.New("WalletStore initail fail, store batch is not nil.")
-       }
-
-       store.batch = store.db.NewBatch()
-       return nil
+// InitStore initial new wallet store
+func (store *WalletStore) InitStore() wallet.WalletStore {
+       newStore := NewWalletStore(store.db)
+       newStore.batch = newStore.db.NewBatch()
+       return newStore
 }
 
 // CommitBatch commit batch
index fdb85e0..16b538d 100644 (file)
@@ -29,26 +29,24 @@ func parseGlobalTxIdx(globalTxIdx []byte) (*bc.Hash, uint64) {
 // when query ,query local first and if have no then query external
 // details see getAliasDefinition
 func saveExternalAssetDefinition(b *types.Block, store WalletStore) error {
-       if err := store.InitBatch(); err != nil {
-               return err
-       }
+       newStore := store.InitStore()
 
        for _, tx := range b.Transactions {
                for _, orig := range tx.Inputs {
                        if cci, ok := orig.TypedInput.(*types.CrossChainInput); ok {
                                assetID := cci.AssetId
-                               assetExist, err := store.GetAsset(assetID)
+                               assetExist, err := newStore.GetAsset(assetID)
                                if err != nil {
                                        return err
                                }
 
                                if assetExist == nil {
-                                       store.SetAssetDefinition(assetID, cci.AssetDefinition)
+                                       newStore.SetAssetDefinition(assetID, cci.AssetDefinition)
                                }
                        }
                }
        }
-       if err := store.CommitBatch(); err != nil {
+       if err := newStore.CommitBatch(); err != nil {
                return err
        }
 
@@ -75,13 +73,13 @@ type TxSummary struct {
 }
 
 // indexTransactions saves all annotated transactions to the database.
-func (w *Wallet) indexTransactions(b *types.Block, txStatus *bc.TransactionStatus, annotatedTxs []*query.AnnotatedTx) error {
+func (w *Wallet) indexTransactions(b *types.Block, txStatus *bc.TransactionStatus, annotatedTxs []*query.AnnotatedTx, store WalletStore) error {
        for _, tx := range annotatedTxs {
                if err := w.store.SetTransaction(b.Height, tx); err != nil {
                        return err
                }
 
-               w.store.DeleteUnconfirmedTransaction(tx.ID.String())
+               store.DeleteUnconfirmedTransaction(tx.ID.String())
        }
 
        if !w.TxIndexFlag {
@@ -90,7 +88,7 @@ func (w *Wallet) indexTransactions(b *types.Block, txStatus *bc.TransactionStatu
 
        for position, globalTx := range b.Transactions {
                blockHash := b.BlockHeader.Hash()
-               w.store.SetGlobalTransactionIndex(globalTx.ID.String(), &blockHash, uint64(position))
+               store.SetGlobalTransactionIndex(globalTx.ID.String(), &blockHash, uint64(position))
        }
 
        return nil
index 7adff87..51b02c2 100644 (file)
@@ -9,7 +9,7 @@ import (
 
 // WalletStore interface contains wallet storage functions.
 type WalletStore interface {
-       InitBatch() error
+       InitStore() WalletStore
        CommitBatch() error
        DeleteContractUTXO(bc.Hash)
        DeleteRecoveryStatus()
index b1b20c9..f90e000 100644 (file)
@@ -36,7 +36,7 @@ func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSma
        return newAccountUtxos
 }
 
-func (w *Wallet) attachUtxos(b *types.Block, txStatus *bc.TransactionStatus) {
+func (w *Wallet) attachUtxos(b *types.Block, txStatus *bc.TransactionStatus, store WalletStore) {
        for txIndex, tx := range b.Transactions {
                statusFail, err := txStatus.GetStatus(txIndex)
                if err != nil {
@@ -50,20 +50,20 @@ func (w *Wallet) attachUtxos(b *types.Block, txStatus *bc.TransactionStatus) {
                        if segwit.IsP2WScript(inputUtxo.ControlProgram) {
                                w.AccountMgr.DeleteStandardUTXO(inputUtxo.OutputID)
                        } else {
-                               w.store.DeleteContractUTXO(inputUtxo.OutputID)
+                               store.DeleteContractUTXO(inputUtxo.OutputID)
                        }
                }
 
                //hand update the transaction output utxos
                outputUtxos := txOutToUtxos(tx, statusFail, b.Height)
                utxos := w.filterAccountUtxo(outputUtxos)
-               if err := w.saveUtxos(utxos); err != nil {
+               if err := w.saveUtxos(utxos, store); err != nil {
                        log.WithFields(log.Fields{"module": logModule, "err": err}).Error("attachUtxos fail on saveUtxos")
                }
        }
 }
 
-func (w *Wallet) detachUtxos(b *types.Block, txStatus *bc.TransactionStatus) {
+func (w *Wallet) detachUtxos(b *types.Block, txStatus *bc.TransactionStatus, store WalletStore) {
        for txIndex := len(b.Transactions) - 1; txIndex >= 0; txIndex-- {
                tx := b.Transactions[txIndex]
                for j := range tx.Outputs {
@@ -83,7 +83,7 @@ func (w *Wallet) detachUtxos(b *types.Block, txStatus *bc.TransactionStatus) {
                        if segwit.IsP2WScript(code) {
                                w.AccountMgr.DeleteStandardUTXO(*tx.ResultIds[j])
                        } else {
-                               w.store.DeleteContractUTXO(*tx.ResultIds[j])
+                               store.DeleteContractUTXO(*tx.ResultIds[j])
                        }
                }
 
@@ -95,7 +95,7 @@ func (w *Wallet) detachUtxos(b *types.Block, txStatus *bc.TransactionStatus) {
 
                inputUtxos := txInToUtxos(tx, statusFail)
                utxos := w.filterAccountUtxo(inputUtxos)
-               if err := w.saveUtxos(utxos); err != nil {
+               if err := w.saveUtxos(utxos, store); err != nil {
                        log.WithFields(log.Fields{"module": logModule, "err": err}).Error("detachUtxos fail on batchSaveUtxos")
                        return
                }
@@ -140,14 +140,14 @@ func (w *Wallet) filterAccountUtxo(utxos []*account.UTXO) []*account.UTXO {
        return result
 }
 
-func (w *Wallet) saveUtxos(utxos []*account.UTXO) error {
+func (w *Wallet) saveUtxos(utxos []*account.UTXO, store WalletStore) error {
        for _, utxo := range utxos {
                if segwit.IsP2WScript(utxo.ControlProgram) {
                        if err := w.AccountMgr.SetStandardUTXO(utxo.OutputID, utxo); err != nil {
                                return err
                        }
                } else {
-                       if err := w.store.SetContractUTXO(utxo.OutputID, utxo); err != nil {
+                       if err := store.SetContractUTXO(utxo.OutputID, utxo); err != nil {
                                return err
                        }
                }
index 72f7d8c..523f9aa 100644 (file)
@@ -157,8 +157,8 @@ func (w *Wallet) LoadWalletInfo() error {
        return w.AttachBlock(block)
 }
 
-func (w *Wallet) commitWalletInfo() error {
-       if err := w.store.SetWalletInfo(&w.Status); err != nil {
+func (w *Wallet) commitWalletInfo(store WalletStore) error {
+       if err := store.SetWalletInfo(&w.Status); err != nil {
                log.WithFields(log.Fields{"module": logModule, "err": err}).Error("save wallet info")
                return err
        }
@@ -193,15 +193,13 @@ func (w *Wallet) AttachBlock(block *types.Block) error {
 
        w.annotateTxsAccount(annotatedTxs)
 
-       if err := w.store.InitBatch(); err != nil {
-               return err
-       }
+       newStore := w.store.InitStore()
 
-       if err := w.indexTransactions(block, txStatus, annotatedTxs); err != nil {
+       if err := w.indexTransactions(block, txStatus, annotatedTxs, newStore); err != nil {
                return err
        }
 
-       w.attachUtxos(block, txStatus)
+       w.attachUtxos(block, txStatus, newStore)
        w.Status.WorkHeight = block.Height
        w.Status.WorkHash = block.Hash()
        if w.Status.WorkHeight >= w.Status.BestHeight {
@@ -209,11 +207,11 @@ func (w *Wallet) AttachBlock(block *types.Block) error {
                w.Status.BestHash = w.Status.WorkHash
        }
 
-       if err := w.commitWalletInfo(); err != nil {
+       if err := w.commitWalletInfo(newStore); err != nil {
                return err
        }
 
-       if err := w.store.CommitBatch(); err != nil {
+       if err := newStore.CommitBatch(); err != nil {
                return err
        }
 
@@ -231,12 +229,10 @@ func (w *Wallet) DetachBlock(block *types.Block) error {
                return err
        }
 
-       if err := w.store.InitBatch(); err != nil {
-               return err
-       }
+       newStore := w.store.InitStore()
 
-       w.detachUtxos(block, txStatus)
-       w.store.DeleteTransactions(w.Status.BestHeight)
+       w.detachUtxos(block, txStatus, newStore)
+       newStore.DeleteTransactions(w.Status.BestHeight)
 
        w.Status.BestHeight = block.Height - 1
        w.Status.BestHash = block.PreviousBlockHash
@@ -245,11 +241,11 @@ func (w *Wallet) DetachBlock(block *types.Block) error {
                w.Status.WorkHeight = w.Status.BestHeight
                w.Status.WorkHash = w.Status.BestHash
        }
-       if err := w.commitWalletInfo(); err != nil {
+       if err := w.commitWalletInfo(newStore); err != nil {
                return err
        }
 
-       if err := w.store.CommitBatch(); err != nil {
+       if err := newStore.CommitBatch(); err != nil {
                return err
        }
 
index 790104d..ef4e908 100644 (file)
@@ -306,13 +306,11 @@ func NewMockWalletStore(db dbm.DB) *MockWalletStore {
        }
 }
 
-// InitBatch initial batch
-func (store *MockWalletStore) InitBatch() error {
-       if store.batch != nil {
-               return errors.New("MockWalletStore initail fail, store batch is not nil.")
-       }
-       store.batch = store.db.NewBatch()
-       return nil
+// InitStore initial new wallet store
+func (store *MockWalletStore) InitStore() WalletStore {
+       newStore := NewMockWalletStore(store.db)
+       newStore.batch = newStore.db.NewBatch()
+       return newStore
 }
 
 // CommitBatch commit batch