6 btmTypes "github.com/bytom/protocol/bc/types"
7 "github.com/jinzhu/gorm"
8 log "github.com/sirupsen/logrus"
10 "github.com/vapor/errors"
11 "github.com/vapor/federation/config"
12 "github.com/vapor/federation/database/orm"
13 "github.com/vapor/federation/service"
14 "github.com/vapor/protocol/bc"
17 type mainchainKeeper struct {
24 func NewMainchainKeeper(db *gorm.DB, chainCfg *config.Chain) *mainchainKeeper {
25 return &mainchainKeeper{
28 node: service.NewNode(chainCfg.Upstream),
29 chainName: chainCfg.Name,
33 func (m *mainchainKeeper) Run() {
34 ticker := time.NewTicker(time.Duration(m.cfg.SyncSeconds) * time.Second)
35 for ; true; <-ticker.C {
37 isUpdate, err := m.syncBlock()
39 log.WithField("error", err).Errorln("blockKeeper fail on process block")
50 func (m *mainchainKeeper) syncBlock() (bool, error) {
51 chain := &orm.Chain{Name: m.chainName}
52 if err := m.db.Where(chain).First(chain).Error; err != nil {
53 return false, errors.Wrap(err, "query chain")
56 height, err := m.node.GetBlockCount()
61 if height <= chain.BlockHeight+m.cfg.Confirmations {
65 nextBlockStr, txStatus, err := m.node.GetBlockByHeight(chain.BlockHeight + 1)
70 nextBlock := &btmTypes.Block{}
71 nextBlock.UnmarshalText([]byte(nextBlockStr))
72 if nextBlock.PreviousBlockHash.String() == chain.BlockHash {
73 return true, m.attachBlock(chain, nextBlock, txStatus)
75 log.WithFields(log.Fields{
76 "remote PreviousBlockHash": nextBlock.PreviousBlockHash.String(),
77 "db block_hash": chain.BlockHash,
78 }).Fatalf("PreviousBlockHash mismatch")
83 func (m *mainchainKeeper) attachBlock(chain *orm.Chain, block *btmTypes.Block, txStatus *bc.TransactionStatus) error {
84 blockHash := block.Hash()
85 log.WithFields(log.Fields{"block_height": block.Height, "block_hash": blockHash.String()}).Info("start to attachBlock")
87 if err := m.processBlock(block); err != nil {
92 return m.db.Commit().Error
95 func (m *mainchainKeeper) processBlock(block *btmTypes.Block) error {
96 if err := m.processIssuing(block.Transactions); err != nil {
103 func (m *mainchainKeeper) processIssuing(txs []*btmTypes.Tx) error {
105 // if err := m.processIssuing(block.Transactions); err != nil {