OSDN Git Service

fix mainchain keeper (#398)
[bytom/vapor.git] / toolbar / federation / synchron / mainchain_keeper.go
index 030aa37..8bb13d0 100644 (file)
@@ -17,11 +17,11 @@ import (
        "github.com/vapor/consensus"
        "github.com/vapor/errors"
        "github.com/vapor/protocol/bc"
-       "github.com/vapor/toolbar/common/service"
        "github.com/vapor/toolbar/federation/common"
        "github.com/vapor/toolbar/federation/config"
        "github.com/vapor/toolbar/federation/database"
        "github.com/vapor/toolbar/federation/database/orm"
+       "github.com/vapor/toolbar/federation/service"
 )
 
 type mainchainKeeper struct {
@@ -125,10 +125,8 @@ func (m *mainchainKeeper) isWithdrawalTx(tx *types.Tx) bool {
                }
        }
 
-       if sourceTxHash := locateSideChainTx(tx.Outputs[len(tx.Outputs)-1]); sourceTxHash == "" {
-               return false
-       }
-       return true
+       sourceTxHash := locateSideChainTx(tx.Outputs[len(tx.Outputs)-1])
+       return sourceTxHash != ""
 }
 
 func locateSideChainTx(output *types.TxOutput) string {
@@ -141,7 +139,15 @@ func locateSideChainTx(output *types.TxOutput) string {
                return ""
        }
 
-       return string(insts[1].Data)
+       if insts[0].Op != vm.OP_FAIL {
+               return ""
+       }
+
+       sourceTxHash := string(insts[1].Data)
+       if _, err = hex.DecodeString(sourceTxHash); err == nil && len(sourceTxHash) == 64 {
+               return sourceTxHash
+       }
+       return ""
 }
 
 func (m *mainchainKeeper) processBlock(db *gorm.DB, block *types.Block, txStatus *bc.TransactionStatus) error {
@@ -253,10 +259,11 @@ func (m *mainchainKeeper) processWithdrawalTx(db *gorm.DB, block *types.Block, t
        blockHash := block.Hash()
        tx := block.Transactions[txIndex]
 
-       stmt := db.Model(&orm.CrossTransaction{}).Where(&orm.CrossTransaction{
+       crossTx := &orm.CrossTransaction{
                SourceTxHash: locateSideChainTx(tx.Outputs[len(tx.Outputs)-1]),
                Status:       common.CrossTxPendingStatus,
-       }).UpdateColumn(&orm.CrossTransaction{
+       }
+       stmt := db.Model(&orm.CrossTransaction{}).Where(crossTx).UpdateColumn(&orm.CrossTransaction{
                DestBlockHeight:    sql.NullInt64{int64(block.Height), true},
                DestBlockTimestamp: sql.NullInt64{int64(block.Timestamp), true},
                DestBlockHash:      sql.NullString{blockHash.String(), true},
@@ -269,7 +276,7 @@ func (m *mainchainKeeper) processWithdrawalTx(db *gorm.DB, block *types.Block, t
        }
 
        if stmt.RowsAffected != 1 {
-               return ErrInconsistentDB
+               log.WithFields(log.Fields{"sourceTxHash": crossTx.SourceTxHash, "destTxHash": tx.ID.String()}).Error("fail to update withdrawal transaction")
        }
        return nil
 }