+ blockHeader, err := c.store.GetBlockHeader(&hash)
+ if err != nil {
+ return false
+ }
+
+ blockHash, err := c.store.GetMainChainHash(blockHeader.Height)
+ if err != nil {
+ log.WithFields(log.Fields{"module": logModule, "height": blockHeader.Height}).Debug("not contain block hash in main chain for specified height")
+ return false
+ }
+ return *blockHash == hash
+}
+
+// SubProtocols return list of layer 2 consensus protocol
+func (c *Chain) SubProtocols() []SubProtocol {
+ return c.subProtocols
+}
+
+// trace back to the tail of the chain from the given block header
+func (c *Chain) traceLongestChainTail(blockHeader *types.BlockHeader) (*types.BlockHeader, error) {
+ longestTail, workQueue := blockHeader, []*types.BlockHeader{blockHeader}
+
+ for ; len(workQueue) > 0; workQueue = workQueue[1:] {
+ currentHeader := workQueue[0]
+ currentHash := currentHeader.Hash()
+ hashes, err := c.store.GetBlockHashesByHeight(currentHeader.Height + 1)
+ if err != nil {
+ return nil, err
+ }
+
+ for _, h := range hashes {
+ if header, err := c.store.GetBlockHeader(h); err != nil {
+ return nil, err
+ } else if header.PreviousBlockHash == currentHash {
+ if longestTail.Height < header.Height {
+ longestTail = header
+ }
+ workQueue = append(workQueue, header)
+ }
+ }
+ }
+ return longestTail, nil
+}
+
+func (c *Chain) hasSeenTx(tx *types.Tx) bool {
+ return c.knownTxs.Has(tx.ID.String())
+}
+
+func (c *Chain) markTransactions(txs ...*types.Tx) {
+ for _, tx := range txs {
+ c.knownTxs.Add(tx.ID.String())
+ }
+}
+
+func (c *Chain) syncProtocolStatus(subProtocol SubProtocol) error {
+ if c.bestBlockHeader.Height < subProtocol.StartHeight() {
+ return nil
+ }
+
+ protocolHeight, protocolHash, err := subProtocol.ChainStatus()
+ if err == ErrNotInitSubProtocolChainStatus {
+ startHash, err := c.store.GetMainChainHash(subProtocol.StartHeight())
+ if err != nil {
+ return errors.Wrap(err, subProtocol.Name(), "can't get block hash by height")
+ }
+
+ if err := subProtocol.InitChainStatus(startHash); err != nil {
+ return errors.Wrap(err, subProtocol.Name(), "fail init chain status")
+ }
+
+ protocolHeight, protocolHash = subProtocol.StartHeight(), startHash
+ } else if err != nil {
+ return errors.Wrap(err, subProtocol.Name(), "can't get chain status")
+ }
+
+ if *protocolHash == c.bestBlockHeader.Hash() {
+ return nil
+ }
+
+ for !c.InMainChain(*protocolHash) {
+ block, err := c.GetBlockByHash(protocolHash)
+ if err != nil {
+ return errors.Wrap(err, subProtocol.Name(), "can't get block by hash in chain")
+ }
+
+ if err := subProtocol.DetachBlock(block); err != nil {
+ return errors.Wrap(err, subProtocol.Name(), "sub protocol detach block err")
+ }
+
+ protocolHeight, protocolHash = block.Height-1, &block.PreviousBlockHash
+ }
+
+ for height := protocolHeight + 1; height <= c.bestBlockHeader.Height; height++ {
+ block, err := c.GetBlockByHeight(height)
+ if err != nil {
+ return errors.Wrap(err, subProtocol.Name(), "can't get block by height in chain")
+ }
+
+ if err := subProtocol.ApplyBlock(block); err != nil {
+ return errors.Wrap(err, subProtocol.Name(), "sub protocol apply block err")
+ }
+
+ blockHash := block.Hash()
+ protocolHeight, protocolHash = block.Height, &blockHash
+ }
+
+ return nil