OSDN Git Service

update GetControlProgram
[bytom/vapor.git] / wallet / indexer.go
index 39f09fe..139f8c4 100644 (file)
@@ -13,7 +13,6 @@ import (
        "github.com/vapor/blockchain/query"
        "github.com/vapor/consensus"
        "github.com/vapor/crypto/sha3pool"
-       "github.com/vapor/database"
        chainjson "github.com/vapor/encoding/json"
        "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
@@ -30,17 +29,25 @@ func parseGlobalTxIdx(globalTxIdx []byte) (*bc.Hash, uint64) {
 // saveExternalAssetDefinition save external and local assets definition,
 // when query ,query local first and if have no then query external
 // details see getAliasDefinition
-func saveExternalAssetDefinition(b *types.Block, store database.WalletStorer) {
+func saveExternalAssetDefinition(b *types.Block, store WalletStorer) error {
+       store.InitBatch()
+       defer store.CommitBatch()
+
        for _, tx := range b.Transactions {
                for _, orig := range tx.Inputs {
                        if cci, ok := orig.TypedInput.(*types.CrossChainInput); ok {
                                assetID := cci.AssetId
-                               if assetExist := store.GetAssetDefinition(assetID); assetExist == nil {
+                               assetExist, err := store.GetAssetDefinition(assetID)
+                               if err != nil {
+                                       return err
+                               }
+                               if assetExist == nil {
                                        store.SetAssetDefinition(assetID, cci.AssetDefinition)
                                }
                        }
                }
        }
+       return nil
 }
 
 // Summary is the struct of transaction's input and output summary
@@ -63,11 +70,7 @@ type TxSummary struct {
 }
 
 // indexTransactions saves all annotated transactions to the database.
-func (w *Wallet) indexTransactions(b *types.Block, txStatus *bc.TransactionStatus) error {
-       annotatedTxs := w.filterAccountTxs(b, txStatus)
-       saveExternalAssetDefinition(b, w.store)
-       annotateTxsAccount(annotatedTxs, w.store)
-
+func (w *Wallet) indexTransactions(b *types.Block, txStatus *bc.TransactionStatus, annotatedTxs []*query.AnnotatedTx) error {
        for _, tx := range annotatedTxs {
                rawTx, err := json.Marshal(tx)
                if err != nil {
@@ -102,7 +105,12 @@ transactionLoop:
                        var hash [32]byte
                        sha3pool.Sum256(hash[:], v.ControlProgram())
 
-                       if bytes := w.store.GetRawProgram(hash); bytes != nil {
+                       cp, err := w.store.GetControlProgram(hash)
+                       if err != nil {
+                               log.WithFields(log.Fields{"module": logModule, "err": err, "hash": string(hash[:])}).Error("filterAccountTxs fail.")
+                               continue
+                       }
+                       if cp != nil {
                                annotatedTxs = append(annotatedTxs, w.buildAnnotatedTransaction(tx, b, statusFail, pos))
                                continue transactionLoop
                        }