// "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
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
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
}
// "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"
// "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)
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()
+}
// "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
+}