OSDN Git Service

Change saveSubBlock func
authorYahtoo Ma <yahtoo.ma@gmail.com>
Wed, 29 Aug 2018 02:24:45 +0000 (10:24 +0800)
committerYahtoo Ma <yahtoo.ma@gmail.com>
Wed, 29 Aug 2018 02:24:45 +0000 (10:24 +0800)
protocol/block.go
protocol/orphan_manage.go

index 260f7a9..41f67b9 100644 (file)
@@ -105,8 +105,7 @@ func (c *Chain) saveBlock(block *types.Block, txStatus *bc.TransactionStatus) er
        return nil
 }
 
-//后一个节点的txstatus信息在哪里?
-func (c *Chain) saveSubBlock(block *types.Block, txStatus *bc.TransactionStatus) *types.Block {
+func (c *Chain) saveSubBlock(block *types.Block) *types.Block {
        blockHash := block.Hash()
        prevOrphans, ok := c.orphanManage.GetPrevOrphans(&blockHash)
        if !ok {
@@ -115,7 +114,7 @@ func (c *Chain) saveSubBlock(block *types.Block, txStatus *bc.TransactionStatus)
 
        bestBlock := block
        for _, prevOrphan := range prevOrphans {
-               orphanBlock, ok := c.orphanManage.Get(prevOrphan)
+               orphanBlock, txStatus, ok := c.orphanManage.Get(prevOrphan)
                if !ok {
                        log.WithFields(log.Fields{"hash": prevOrphan.String()}).Warning("saveSubBlock fail to get block from orphanManage")
                        continue
@@ -125,7 +124,7 @@ func (c *Chain) saveSubBlock(block *types.Block, txStatus *bc.TransactionStatus)
                        continue
                }
 
-               if subBestBlock := c.saveSubBlock(orphanBlock, txStatus); subBestBlock.Height > bestBlock.Height {
+               if subBestBlock := c.saveSubBlock(orphanBlock); subBestBlock.Height > bestBlock.Height {
                        bestBlock = subBestBlock
                }
        }
@@ -167,7 +166,7 @@ func (c *Chain) processBlock(block *types.Block, txStatus *bc.TransactionStatus)
        }
 
        if parent := c.index.GetNode(&block.PreviousBlockHash); parent == nil {
-               c.orphanManage.Add(block)
+               c.orphanManage.Add(block, txStatus)
                return true, nil
        }
 
@@ -175,7 +174,7 @@ func (c *Chain) processBlock(block *types.Block, txStatus *bc.TransactionStatus)
                return false, err
        }
 
-       bestBlock := c.saveSubBlock(block, txStatus)
+       bestBlock := c.saveSubBlock(block)
        bestBlockHash := bestBlock.Hash()
        bestNode := c.index.GetNode(&bestBlockHash)
 
index 04fa7c1..ad4fdbf 100644 (file)
@@ -17,6 +17,7 @@ var (
 
 type orphanBlock struct {
        *types.Block
+       *bc.TransactionStatus
        expiration time.Time
 }
 
@@ -47,7 +48,7 @@ func (o *OrphanManage) BlockExist(hash *bc.Hash) bool {
 }
 
 // Add will add the block to OrphanManage
-func (o *OrphanManage) Add(block *types.Block) {
+func (o *OrphanManage) Add(block *types.Block, txStatus *bc.TransactionStatus) {
        blockHash := block.Hash()
        o.mtx.Lock()
        defer o.mtx.Unlock()
@@ -56,7 +57,7 @@ func (o *OrphanManage) Add(block *types.Block) {
                return
        }
 
-       o.orphan[blockHash] = &orphanBlock{block, time.Now().Add(orphanBlockTTL)}
+       o.orphan[blockHash] = &orphanBlock{block, txStatus, time.Now().Add(orphanBlockTTL)}
        o.prevOrphans[block.PreviousBlockHash] = append(o.prevOrphans[block.PreviousBlockHash], &blockHash)
 
        log.WithFields(log.Fields{"hash": blockHash.String(), "height": block.Height}).Info("add block to orphan")
@@ -70,11 +71,11 @@ func (o *OrphanManage) Delete(hash *bc.Hash) {
 }
 
 // Get return the orphan block by hash
-func (o *OrphanManage) Get(hash *bc.Hash) (*types.Block, bool) {
+func (o *OrphanManage) Get(hash *bc.Hash) (*types.Block, *bc.TransactionStatus, bool) {
        o.mtx.RLock()
        block, ok := o.orphan[*hash]
        o.mtx.RUnlock()
-       return block.Block, ok
+       return block.Block, block.TransactionStatus, ok
 }
 
 // GetPrevOrphans return the list of child orphans