OSDN Git Service

processChainInfo
authorHAOYUatHZ <haoyu@protonmail.com>
Fri, 7 Jun 2019 08:25:43 +0000 (16:25 +0800)
committerHAOYUatHZ <haoyu@protonmail.com>
Fri, 7 Jun 2019 08:25:43 +0000 (16:25 +0800)
federation/synchron/attach_block_processor.go
federation/synchron/block_keeper.go
federation/synchron/block_processor.go
federation/synchron/detach_block_processor.go

index ad26fa9..7f39646 100644 (file)
@@ -11,25 +11,57 @@ import (
        // "github.com/bytom/consensus"
        // "github.com/bytom/errors"
        // TODO:
-       btmBc "github.com/bytom/protocol/bc"
+       // btmBc "github.com/bytom/protocol/bc"
        btmTypes "github.com/bytom/protocol/bc/types"
        "github.com/jinzhu/gorm"
-       // TODO:
-       // "github.com/blockcenter/coin/btm"
-       // "github.com/blockcenter/database/orm"
+
+       "github.com/vapor/federation/config"
+       "github.com/vapor/federation/database/orm"
+       vaporTypes "github.com/vapor/protocol/bc/types"
 )
 
 type attachBlockProcessor struct {
-       db *gorm.DB
-       // coin     *orm.Coin
-       block    *btmTypes.Block
-       txStatus *btmBc.TransactionStatus
+       cfg   *config.Chain
+       db    *gorm.DB
+       chain *orm.Chain
+       block interface{}
+       // txStatus *btmBc.TransactionStatus
 }
 
 func (p *attachBlockProcessor) processIssuing(db *gorm.DB, txs []*btmTypes.Tx) error {
        return addIssueAssets(db, txs)
 }
 
+func (p *attachBlockProcessor) processChainInfo() error {
+       var blockHeight uint64
+       var blockHashStr, previousBlockHashStr string
+
+       switch {
+       case p.cfg.IsMainchain:
+               blockHeight = p.block.(*btmTypes.Block).Height
+               blockHash := p.block.(*btmTypes.Block).Hash()
+               blockHashStr = blockHash.String()
+               previousBlockHashStr = p.block.(*vaporTypes.Block).PreviousBlockHash.String()
+       default:
+               blockHeight = p.block.(*vaporTypes.Block).Height
+               blockHash := p.block.(*vaporTypes.Block).Hash()
+               blockHashStr = blockHash.String()
+               previousBlockHashStr = p.block.(*vaporTypes.Block).PreviousBlockHash.String()
+       }
+
+       p.chain.BlockHeight = blockHeight
+       p.chain.BlockHash = blockHashStr
+       db := p.db.Model(p.chain).Where("block_hash = ?", previousBlockHashStr).Updates(p.chain)
+       if err := db.Error; err != nil {
+               return err
+       }
+
+       if db.RowsAffected != 1 {
+               return ErrInconsistentDB
+       }
+       return nil
+}
+
 /*
 func (p *attachBlockProcessor) getBlock() *btmTypes.Block {
        return p.block
@@ -43,20 +75,6 @@ func (p *attachBlockProcessor) getCoin() *orm.Coin {
        return p.coin
 }
 
-func (p *attachBlockProcessor) processCoinInfo() error {
-       blockHash := p.block.Hash()
-       p.coin.BlockHeight = p.block.Height
-       p.coin.BlockHash = blockHash.String()
-       db := p.db.Model(p.coin).Where("block_hash = ?", p.block.PreviousBlockHash.String()).Updates(p.coin)
-       if err := db.Error; err != nil {
-               return err
-       }
-
-       if db.RowsAffected != 1 {
-               return ErrInconsistentDB
-       }
-       return nil
-}
 
 type addressTxSorter []*orm.AddressTransaction
 
index 8500950..c98fe16 100644 (file)
@@ -142,16 +142,17 @@ func (b *blockKeeper) DetachBlock(chain *orm.Chain, block interface{}, txStatus
        log.WithFields(log.Fields{"block_height": blockHeight, "block_hash": blockHashStr}).Info("start to detachBlock")
 
        tx := b.db.Begin()
-       // bp := &detachBlockProcessor{
-       //      db:       tx,
-       //      block:    block,
-       //      coin:     coin,
-       //      txStatus: txStatus,
-       // }
-       // if err := updateBlock(b.cfg, tx, bp); err != nil {
-       //      tx.Rollback()
-       //      return err
-       // }
+       bp := &detachBlockProcessor{
+               cfg:   b.cfg,
+               db:    tx,
+               chain: chain,
+               block: block,
+               // txStatus: txStatus,
+       }
+       if err := updateBlock(tx, bp); err != nil {
+               tx.Rollback()
+               return err
+       }
 
        return tx.Commit().Error
 }
index 993e414..b5f80c3 100644 (file)
@@ -9,7 +9,6 @@ import (
 
        // "github.com/bytom/consensus"
        // "github.com/bytom/consensus/segwit"
-       // "github.com/bytom/errors"
        // "github.com/bytom/protocol/bc"
        btmTypes "github.com/bytom/protocol/bc/types"
        // "github.com/bytom/protocol/vm/vmutil"
@@ -18,15 +17,18 @@ import (
        // "github.com/blockcenter/coin/btm"
        // "github.com/blockcenter/config"
        // "github.com/blockcenter/types"
+       "github.com/vapor/errors"
        "github.com/vapor/federation/database/orm"
 )
 
+var ErrInconsistentDB = errors.New("inconsistent db status")
+
 type blockProcessor interface {
        processIssuing(db *gorm.DB, txs []*btmTypes.Tx) error
+       processChainInfo() error
        // getBlock() *btmTypes.Block
        // getCoin() *orm.Coin
        // getTxStatus() *bc.TransactionStatus
-       // processCoinInfo() error
        // processAddressTransaction(mappings []*addressTxMapping) error
        // processSpendBalance(input *btmTypes.TxInput, deltaBalance *deltaBalance)
        // processReceiveBalance(output *btmTypes.TxOutput, deltaBalance *deltaBalance)
@@ -67,3 +69,29 @@ func addIssueAssets(db *gorm.DB, txs []*btmTypes.Tx) error {
 
        return nil
 }
+
+func updateBlock(db *gorm.DB, bp blockProcessor) error {
+       // txs := bp.getBlock().Transactions
+       // if err := bp.processIssuing(db, txs, bp.getCoin().ID); err != nil {
+       //      return err
+       // }
+
+       // addressTxMappings, err := GetAddressTxMappings(cfg, txs, bp.getTxStatus(), db)
+       // if err != nil {
+       //      return err
+       // }
+
+       // if err := bp.processAddressTransaction(addressTxMappings); err != nil {
+       //      return err
+       // }
+
+       // if err := updateBalanceAndUTXO(db, addressTxMappings, bp); err != nil {
+       //      return err
+       // }
+
+       // if err := updateDeletedTransaction(db); err != nil {
+       //      return err
+       // }
+
+       return bp.processChainInfo()
+}
index 6e76a84..35ca020 100644 (file)
@@ -5,16 +5,34 @@ import (
        // "github.com/bytom/protocol/bc"
        btmTypes "github.com/bytom/protocol/bc/types"
        "github.com/jinzhu/gorm"
-       // "github.com/blockcenter/database/orm"
+
+       "github.com/vapor/federation/config"
+       "github.com/vapor/federation/database/orm"
 )
 
 type detachBlockProcessor struct {
-       db *gorm.DB
-       // coin     *orm.Coin
-       // block    *btmTypes.Block
+       cfg   *config.Chain
+       db    *gorm.DB
+       chain *orm.Chain
+       block interface{}
        // txStatus *bc.TransactionStatus
 }
 
 func (p *detachBlockProcessor) processIssuing(db *gorm.DB, txs []*btmTypes.Tx) error {
        return nil
 }
+
+func (p *detachBlockProcessor) processChainInfo() error {
+       // p.coin.BlockHeight = p.block.Height - 1
+       // p.coin.BlockHash = p.block.PreviousBlockHash.String()
+       // oldBlockHash := p.block.Hash()
+       // db := p.db.Model(p.coin).Where("block_hash = ?", oldBlockHash.String()).Updates(p.coin)
+       // if err := db.Error; err != nil {
+       //      return err
+       // }
+
+       // if db.RowsAffected != 1 {
+       //      return ErrInconsistentDB
+       // }
+       return nil
+}