github.com/turingchain2020/turingchain@v1.1.21/blockchain/sequences.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package blockchain
     6  
     7  import (
     8  	"github.com/turingchain2020/turingchain/common"
     9  	"github.com/turingchain2020/turingchain/types"
    10  )
    11  
    12  //GetBlockSequences 通过记录的block序列号获取blockd序列存储的信息
    13  func (chain *BlockChain) GetBlockSequences(requestblock *types.ReqBlocks) (*types.BlockSequences, error) {
    14  	blockLastSeq, err := chain.blockStore.LoadBlockLastSequence()
    15  	if err != nil {
    16  		chainlog.Debug("GetBlockSequences LoadBlockLastSequence", "blockLastSeq", blockLastSeq, "err", err)
    17  	}
    18  	if requestblock.Start > blockLastSeq {
    19  		chainlog.Error("GetBlockSequences StartSeq err", "startSeq", requestblock.Start, "lastSeq", blockLastSeq)
    20  		return nil, types.ErrStartHeight
    21  	}
    22  	if requestblock.GetStart() > requestblock.GetEnd() {
    23  		chainlog.Error("GetBlockSequences input must Start <= End:", "startSeq", requestblock.Start, "endSeq", requestblock.End)
    24  		return nil, types.ErrEndLessThanStartHeight
    25  	}
    26  	if requestblock.End-requestblock.Start >= types.MaxBlockCountPerTime {
    27  		return nil, types.ErrMaxCountPerTime
    28  	}
    29  	end := requestblock.End
    30  	if requestblock.End > blockLastSeq {
    31  		end = blockLastSeq
    32  	}
    33  	start := requestblock.Start
    34  	count := end - start + 1
    35  
    36  	chainlog.Debug("GetBlockSequences", "Start", requestblock.Start, "End", requestblock.End, "lastSeq", blockLastSeq, "counts", count)
    37  
    38  	var blockSequences types.BlockSequences
    39  
    40  	for i := start; i <= end; i++ {
    41  		blockSequence, err := chain.blockStore.GetBlockSequence(i)
    42  		if err == nil && blockSequence != nil {
    43  			blockSequences.Items = append(blockSequences.Items, blockSequence)
    44  		} else {
    45  			blockSequences.Items = append(blockSequences.Items, nil)
    46  		}
    47  	}
    48  	return &blockSequences, nil
    49  }
    50  
    51  //ProcDelParaChainBlockMsg 处理共识过来的删除block的消息,目前只提供给平行链使用
    52  func (chain *BlockChain) ProcDelParaChainBlockMsg(broadcast bool, ParaChainblockdetail *types.ParaChainBlockDetail, pid string) (err error) {
    53  	cfg := chain.client.GetConfig()
    54  	if ParaChainblockdetail == nil || ParaChainblockdetail.GetBlockdetail() == nil || ParaChainblockdetail.GetBlockdetail().GetBlock() == nil {
    55  		chainlog.Error("ProcDelParaChainBlockMsg input block is null")
    56  		return types.ErrInvalidParam
    57  	}
    58  	blockdetail := ParaChainblockdetail.GetBlockdetail()
    59  	block := ParaChainblockdetail.GetBlockdetail().GetBlock()
    60  	sequence := ParaChainblockdetail.GetSequence()
    61  
    62  	_, ismain, isorphan, err := chain.ProcessBlock(broadcast, blockdetail, pid, false, sequence)
    63  	chainlog.Debug("ProcDelParaChainBlockMsg result:", "height", block.Height, "sequence", sequence, "ismain", ismain, "isorphan", isorphan, "hash", common.ToHex(block.Hash(cfg)), "err", err)
    64  
    65  	return err
    66  }
    67  
    68  //ProcAddParaChainBlockMsg 处理共识过来的add block的消息,目前只提供给平行链使用
    69  func (chain *BlockChain) ProcAddParaChainBlockMsg(broadcast bool, ParaChainblockdetail *types.ParaChainBlockDetail, pid string) (*types.BlockDetail, error) {
    70  	cfg := chain.client.GetConfig()
    71  	if ParaChainblockdetail == nil || ParaChainblockdetail.GetBlockdetail() == nil || ParaChainblockdetail.GetBlockdetail().GetBlock() == nil {
    72  		chainlog.Error("ProcAddParaChainBlockMsg input block is null")
    73  		return nil, types.ErrInvalidParam
    74  	}
    75  	blockdetail := ParaChainblockdetail.GetBlockdetail()
    76  	block := ParaChainblockdetail.GetBlockdetail().GetBlock()
    77  	sequence := ParaChainblockdetail.GetSequence()
    78  
    79  	fullBlockDetail, ismain, isorphan, err := chain.ProcessBlock(broadcast, blockdetail, pid, true, sequence)
    80  	chainlog.Debug("ProcAddParaChainBlockMsg result:", "height", block.Height, "sequence", sequence, "ismain", ismain, "isorphan", isorphan, "hash", common.ToHex(block.Hash(cfg)), "err", err)
    81  
    82  	return fullBlockDetail, err
    83  }
    84  
    85  //ProcGetSeqByHash 处理共识过来的通过blockhash获取seq的消息,只提供add block时的seq,用于平行链block回退
    86  func (chain *BlockChain) ProcGetSeqByHash(hash []byte) (int64, error) {
    87  	if len(hash) == 0 {
    88  		chainlog.Error("ProcGetSeqByHash input hash is null")
    89  		return -1, types.ErrInvalidParam
    90  	}
    91  	seq, err := chain.blockStore.GetSequenceByHash(hash)
    92  	chainlog.Debug("ProcGetSeqByHash", "blockhash", common.ToHex(hash), "seq", seq, "err", err)
    93  
    94  	return seq, err
    95  }
    96  
    97  //ProcGetMainSeqByHash 处理共识过来的通过blockhash获取seq的消息,只提供add block时的seq,用于平行链block回退
    98  func (chain *BlockChain) ProcGetMainSeqByHash(hash []byte) (int64, error) {
    99  	if len(hash) == 0 {
   100  		chainlog.Error("ProcGetMainSeqByHash input hash is null")
   101  		return -1, types.ErrInvalidParam
   102  	}
   103  	seq, err := chain.blockStore.GetMainSequenceByHash(hash)
   104  	chainlog.Debug("ProcGetMainSeqByHash", "blockhash", common.ToHex(hash), "seq", seq, "err", err)
   105  
   106  	return seq, err
   107  }