OSDN Git Service

rawBlockStr
authorHAOYUatHZ <haoyu@protonmail.com>
Fri, 7 Jun 2019 07:24:52 +0000 (15:24 +0800)
committerHAOYUatHZ <haoyu@protonmail.com>
Fri, 7 Jun 2019 07:24:52 +0000 (15:24 +0800)
docs/federation/README.md
federation/config/config.go
federation/service/node.go
federation/synchron/block_keeper.go
main.go [new file with mode: 0644]

index 5624cf7..5dfaf21 100644 (file)
@@ -46,11 +46,13 @@ A `fed_cfg.json` would look like this:
         }
     ],
     "mainchain" : {
+        "is_mainchain" : true,
         "name" : "bytom",
         "upstream" : "http://127.0.0.1:9888",
         "sync_seconds" : 150
     },
     "sidechain" : {
+        "is_mainchain" : false,
         "name" : "vapor",
         "upstream" : "http://127.0.0.1:9888",
         "sync_seconds" : 5
index 4b0739d..fa2508e 100644 (file)
@@ -66,6 +66,7 @@ type Warder struct {
 }
 
 type Chain struct {
+       IsMainchain bool   `json:"is_mainchain"`
        Name        string `json:"name"`
        Upstream    string `json:"upstream"`
        SyncSeconds uint64 `json:"sync_seconds"`
index bf40d55..cd423d8 100644 (file)
@@ -18,11 +18,11 @@ func NewNode(ip string) *Node {
        return &Node{ip: ip}
 }
 
-func (n *Node) GetBlockByHash(hash string) (interface{}, interface{}, error) {
+func (n *Node) GetBlockByHash(hash string) (string, interface{}, error) {
        return n.getRawBlock(&getRawBlockReq{BlockHash: hash})
 }
 
-func (n *Node) GetBlockByHeight(height uint64) (interface{}, interface{}, error) {
+func (n *Node) GetBlockByHeight(height uint64) (string, interface{}, error) {
        return n.getRawBlock(&getRawBlockReq{BlockHeight: height})
 }
 
@@ -42,15 +42,15 @@ type getRawBlockReq struct {
 }
 
 type getRawBlockResp struct {
-       RawBlock          interface{} `json:"raw_block"`
+       RawBlock          string      `json:"raw_block"`
        TransactionStatus interface{} `json:"transaction_status"`
 }
 
-func (n *Node) getRawBlock(req *getRawBlockReq) (interface{}, interface{}, error) {
+func (n *Node) getRawBlock(req *getRawBlockReq) (string, interface{}, error) {
        url := "/get-raw-block"
        payload, err := json.Marshal(req)
        if err != nil {
-               return nil, nil, errors.Wrap(err, "json marshal")
+               return "", nil, errors.Wrap(err, "json marshal")
        }
 
        res := &getRawBlockResp{}
index 7239b93..8500950 100644 (file)
@@ -3,7 +3,7 @@ package synchron
 import (
        "time"
 
-       btmBc "github.com/bytom/protocol/bc"
+       // btmBc "github.com/bytom/protocol/bc"
        btmTypes "github.com/bytom/protocol/bc/types"
        "github.com/jinzhu/gorm"
        log "github.com/sirupsen/logrus"
@@ -12,6 +12,8 @@ import (
        "github.com/vapor/federation/config"
        "github.com/vapor/federation/database/orm"
        "github.com/vapor/federation/service"
+       // vaporBc "github.com/vapor/protocol/bc"
+       vaporTypes "github.com/vapor/protocol/bc/types"
 )
 
 type blockKeeper struct {
@@ -62,15 +64,27 @@ func (b *blockKeeper) syncBlock() (bool, error) {
                return false, nil
        }
 
-       nextBlock, txStatus, err := b.node.GetBlockByHeight(chain.BlockHeight + 1)
+       nextBlockStr, txStatus, err := b.node.GetBlockByHeight(chain.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() == chain.BlockHash {
-               return true, b.AttachBlock(chain, nextBlock, txStatus)
+       switch {
+       case b.cfg.IsMainchain:
+               nextBlock := &btmTypes.Block{}
+               nextBlock.UnmarshalText([]byte(nextBlockStr))
+               if nextBlock.PreviousBlockHash.String() == chain.BlockHash {
+                       return true, b.AttachBlock(chain, nextBlock, txStatus)
+               }
+
+       default:
+               nextBlock := &vaporTypes.Block{}
+               nextBlock.UnmarshalText([]byte(nextBlockStr))
+               if nextBlock.PreviousBlockHash.String() == chain.BlockHash {
+                       return true, b.AttachBlock(chain, nextBlock, txStatus)
+               }
        }
 
        log.WithField("block height", chain.BlockHeight).Debug("the prev hash of remote is not equals the hash of current best block, must rollback")
@@ -82,11 +96,22 @@ func (b *blockKeeper) syncBlock() (bool, error) {
        return true, b.DetachBlock(chain, currentBlock, txStatus)
 }
 
-func (b *blockKeeper) AttachBlock(chain *orm.Chain, block *btmTypes.Block, txStatus *btmBc.TransactionStatus) error {
-       // blockHash := block.Hash()
-       // log.WithFields(log.Fields{"block_height": block.Height, "block_hash": blockHash.String()}).Info("start to attachBlock")
+func (b *blockKeeper) AttachBlock(chain *orm.Chain, block interface{}, txStatus interface{}) error {
+       var blockHeight uint64
+       var blockHashStr string
+       switch {
+       case b.cfg.IsMainchain:
+               blockHeight = block.(*btmTypes.Block).Height
+               blockHash := block.(*btmTypes.Block).Hash()
+               blockHashStr = blockHash.String()
+       default:
+               blockHeight = block.(*vaporTypes.Block).Height
+               blockHash := block.(*vaporTypes.Block).Hash()
+               blockHashStr = blockHash.String()
+       }
+       log.WithFields(log.Fields{"block_height": blockHeight, "block_hash": blockHashStr}).Info("start to attachBlock")
 
-       // tx := b.db.Begin()
+       tx := b.db.Begin()
        // bp := &attachBlockProcessor{
        //      db:       tx,
        //      block:    block,
@@ -98,15 +123,25 @@ func (b *blockKeeper) AttachBlock(chain *orm.Chain, block *btmTypes.Block, txSta
        //      return err
        // }
 
-       // return tx.Commit().Error
-       return nil
+       return tx.Commit().Error
 }
 
-func (b *blockKeeper) DetachBlock(chain *orm.Chain, block *btmTypes.Block, txStatus *btmBc.TransactionStatus) error {
-       // blockHash := block.Hash()
-       // log.WithFields(log.Fields{"block_height": block.Height, "block_hash": blockHash.String()}).Info("start to detachBlock")
+func (b *blockKeeper) DetachBlock(chain *orm.Chain, block interface{}, txStatus interface{}) error {
+       var blockHeight uint64
+       var blockHashStr string
+       switch {
+       case b.cfg.IsMainchain:
+               blockHeight = block.(*btmTypes.Block).Height
+               blockHash := block.(*btmTypes.Block).Hash()
+               blockHashStr = blockHash.String()
+       default:
+               blockHeight = block.(*vaporTypes.Block).Height
+               blockHash := block.(*vaporTypes.Block).Hash()
+               blockHashStr = blockHash.String()
+       }
+       log.WithFields(log.Fields{"block_height": blockHeight, "block_hash": blockHashStr}).Info("start to detachBlock")
 
-       // tx := b.db.Begin()
+       tx := b.db.Begin()
        // bp := &detachBlockProcessor{
        //      db:       tx,
        //      block:    block,
@@ -118,6 +153,5 @@ func (b *blockKeeper) DetachBlock(chain *orm.Chain, block *btmTypes.Block, txSta
        //      return err
        // }
 
-       // return tx.Commit().Error
-       return nil
+       return tx.Commit().Error
 }
diff --git a/main.go b/main.go
new file mode 100644 (file)
index 0000000..e3af94a
--- /dev/null
+++ b/main.go
@@ -0,0 +1,30 @@
+package main
+
+import (
+       // "encoding/json"
+       "fmt"
+       "reflect"
+
+       btmTypes "github.com/bytom/protocol/bc/types"
+
+       "github.com/vapor/federation/service"
+       vaporTypes "github.com/vapor/protocol/bc/types"
+)
+
+func main() {
+
+       node := service.NewNode("http://127.0.0.1:9888")
+       a, b, c := node.GetBlockByHeight(1)
+       fmt.Println(reflect.TypeOf(a))
+       fmt.Println(reflect.TypeOf(b))
+       fmt.Println(reflect.TypeOf(c))
+
+       block := &btmTypes.Block{}
+       block.UnmarshalText([]byte(a))
+       fmt.Println(block)
+
+       block2 := &vaporTypes.Block{}
+       block2.UnmarshalText([]byte(a))
+       fmt.Println(block2)
+
+}