OSDN Git Service

update
authorChengcheng Zhang <943420582@qq.com>
Mon, 24 Jun 2019 06:29:25 +0000 (14:29 +0800)
committerChengcheng Zhang <943420582@qq.com>
Mon, 24 Jun 2019 06:29:25 +0000 (14:29 +0800)
database/wallet_store.go
wallet/indexer.go
wallet/wallet.go

index 9d3bc41..058e423 100644 (file)
@@ -29,11 +29,13 @@ const (
 
 // WalletStorer interface contains wallet storage functions.
 type WalletStorer interface {
+       initBatch()
+       commitBatch()
        GetAssetDefinition(*bc.AssetID) []byte
        SetAssetDefinition(*bc.AssetID, []byte)
        GetRawProgram(common.Hash) []byte
        GetAccountByAccountID(string) []byte
-       DeleteTransaction(uint64)
+       DeleteTransactions(uint64)
        SetTransaction(uint64, uint32, string, []byte)
        DeleteUnconfirmedTransaction(string)
        SetGlobalTransactionIndex(string, *bc.Hash, uint64)
@@ -71,12 +73,12 @@ func NewWalletStore(db dbm.DB) *WalletStore {
        }
 }
 
+// initBatch initial batch
 func (store *WalletStore) initBatch() {
-       if store.batch == nil {
-               store.batch = store.DB.NewBatch()
-       }
+       store.batch = store.DB.NewBatch()
 }
 
+// commitBatch commit batch
 func (store *WalletStore) commitBatch() {
        if store.batch != nil {
                store.batch.Write()
@@ -143,7 +145,11 @@ func (store *WalletStore) GetAssetDefinition(assetID *bc.AssetID) []byte {
 
 // SetAssetDefinition set assetID and definition
 func (store *WalletStore) SetAssetDefinition(assetID *bc.AssetID, definition []byte) {
-       store.DB.Set(asset.ExtAssetKey(assetID), definition)
+       if store.batch == nil {
+               store.DB.Set(asset.ExtAssetKey(assetID), definition)
+       } else {
+               store.batch.Set(asset.ExtAssetKey(assetID), definition)
+       }
 }
 
 // GetRawProgram get raw program by hash
@@ -156,8 +162,8 @@ func (store *WalletStore) GetAccountByAccountID(accountID string) []byte {
        return store.DB.Get(Key(accountID))
 }
 
-// DeleteTransaction delete transactions when orphan block rollback
-func (store *WalletStore) DeleteTransaction(height uint64) {
+// DeleteTransactions delete transactions when orphan block rollback
+func (store *WalletStore) DeleteTransactions(height uint64) {
        tmpTx := query.AnnotatedTx{}
        batch := store.DB.NewBatch()
        txIter := store.DB.IteratorPrefix(calcDeleteKey(height))
@@ -165,7 +171,11 @@ func (store *WalletStore) DeleteTransaction(height uint64) {
 
        for txIter.Next() {
                if err := json.Unmarshal(txIter.Value(), &tmpTx); err == nil {
-                       batch.Delete(calcTxIndexKey(tmpTx.ID.String()))
+                       if store.batch == nil {
+                               batch.Delete(calcTxIndexKey(tmpTx.ID.String()))
+                       } else {
+                               store.batch.Delete(calcTxIndexKey(tmpTx.ID.String()))
+                       }
                }
                batch.Delete(txIter.Key())
        }
index 39f09fe..9729c17 100644 (file)
@@ -31,6 +31,7 @@ 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 database.WalletStorer) {
+
        for _, tx := range b.Transactions {
                for _, orig := range tx.Inputs {
                        if cci, ok := orig.TypedInput.(*types.CrossChainInput); ok {
index c073ef2..b7ee866 100644 (file)
@@ -213,7 +213,7 @@ func (w *Wallet) DetachBlock(block *types.Block) error {
        }
 
        w.detachUtxos(block, txStatus)
-       w.store.DeleteTransaction(w.status.BestHeight)
+       w.store.DeleteTransactions(w.status.BestHeight)
 
        w.status.BestHeight = block.Height - 1
        w.status.BestHash = block.PreviousBlockHash