OSDN Git Service

add AttachBlock & DetachBlock
authorHAOYUatHZ <haoyu@protonmail.com>
Thu, 6 Jun 2019 08:05:12 +0000 (16:05 +0800)
committerHAOYUatHZ <haoyu@protonmail.com>
Thu, 6 Jun 2019 08:05:12 +0000 (16:05 +0800)
docs/federation/README.md
federation/config/config.go
federation/synchron/block_keeper.go

index ee86f3b..552aedd 100644 (file)
@@ -46,18 +46,14 @@ A `fed_cfg.json` would look like this:
         }
     ],
     "mainchain" : {
-        "name" : "bytom",
-        "upstream" : {
-            "rpc" : "http://127.0.0.1:9888",
-            "web_socket" : "127.0.0.1:9888"
-        }
+        "name" : "vapor",
+        "upstream" : "http://127.0.0.1:9888",
+        "sync_seconds" : 150
     },
     "sidechain" : {
         "name" : "vapor",
-        "upstream" : {
-            "rpc" : "http://127.0.0.1:9889",
-            "web_socket" : "127.0.0.1:9889"            
-        }
+        "upstream" : "http://127.0.0.1:9888",
+        "sync_seconds" : 5
     }
 }
 ```
index 747eec4..4b0739d 100644 (file)
@@ -66,11 +66,7 @@ type Warder struct {
 }
 
 type Chain struct {
-       Name     string   `json:"name"`
-       Upstream Upstream `json:"upstream"`
-}
-
-type Upstream struct {
-       RPC       string `json:"rpc"`
-       WebSocket string `json:"web_socket"`
+       Name        string `json:"name"`
+       Upstream    string `json:"upstream"`
+       SyncSeconds uint64 `json:"sync_seconds"`
 }
index 15cb6bb..9b5f15a 100644 (file)
@@ -1,13 +1,13 @@
 package synchron
 
 import (
-       // "time"
+       "time"
 
        // "github.com/bytom/errors"
        // "github.com/bytom/protocol/bc"
        // "github.com/bytom/protocol/bc/types"
        "github.com/jinzhu/gorm"
-       // log "github.com/sirupsen/logrus"
+       log "github.com/sirupsen/logrus"
 
        "github.com/vapor/federation/config"
        // "github.com/blockcenter/database"
@@ -27,10 +27,100 @@ func NewBlockKeeper(db *gorm.DB, chainCfg *config.Chain) *blockKeeper {
        return &blockKeeper{
                cfg:  chainCfg,
                db:   db,
-               node: service.NewNode(chainCfg.Upstream.RPC),
+               node: service.NewNode(chainCfg.Upstream),
        }
 }
 
 func (b *blockKeeper) Run() {
+       ticker := time.NewTicker(time.Duration(b.cfg.SyncSeconds) * time.Second)
+       for ; true; <-ticker.C {
+               for {
+                       isUpdate, err := b.syncBlock()
+                       if err != nil {
+                               log.WithField("error", err).Errorln("blockKeeper fail on process block")
+                               break
+                       }
 
+                       if !isUpdate {
+                               break
+                       }
+               }
+       }
+}
+
+func (b *blockKeeper) syncBlock() (bool, error) {
+       /*
+               coin := &orm.Coin{Name: b.coinName}
+               if err := b.db.Where(coin).First(coin).Error; err != nil {
+                       return false, errors.Wrap(err, "query coin")
+               }
+
+               height, err := b.node.GetBlockCount()
+               if err != nil {
+                       return false, err
+               }
+
+               if height == coin.BlockHeight {
+                       return false, nil
+               }
+
+               nextBlock, txStatus, err := b.node.GetBlockByHeight(coin.BlockHeight + 1)
+               if err != nil {
+                       return false, err
+               }
+
+               // Normal case, the previous hash of next block equals to the hash of current block,
+               // just sync to database directly.
+               if nextBlock.PreviousBlockHash.String() == coin.BlockHash {
+                       return true, b.AttachBlock(coin, nextBlock, txStatus)
+               }
+
+               log.WithField("block height", coin.BlockHeight).Debug("the prev hash of remote is not equals the hash of current best block, must rollback")
+               currentBlock, txStatus, err := b.node.GetBlockByHash(coin.BlockHash)
+               if err != nil {
+                       return false, err
+               }
+       */
+       // return true, b.DetachBlock( /*coin, */ currentBlock, txStatus)
+       return true, nil
+}
+
+func (b *blockKeeper) AttachBlock(coin *orm.Coin, block *types.Block, txStatus *bc.TransactionStatus) error {
+       // blockHash := block.Hash()
+       // log.WithFields(log.Fields{"block_height": block.Height, "block_hash": blockHash.String()}).Info("start to attachBlock")
+
+       // tx := b.db.Begin()
+       // bp := &attachBlockProcessor{
+       //      db:       tx,
+       //      block:    block,
+       //      coin:     coin,
+       //      txStatus: txStatus,
+       // }
+       // if err := updateBlock(b.cfg, tx, bp); err != nil {
+       //      tx.Rollback()
+       //      return err
+       // }
+
+       // return tx.Commit().Error
+       return nil
+}
+
+func (b *blockKeeper) DetachBlock(coin *orm.Coin, block *types.Block, txStatus *bc.TransactionStatus) error {
+       // blockHash := block.Hash()
+       // log.WithFields(log.Fields{"block_height": block.Height, "block_hash": blockHash.String()}).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
+       // }
+
+       // return tx.Commit().Error
+       return nil
 }