github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/api/chain_status.go (about)

     1  package api
     2  
     3  // ChainStatus indicate chain status
     4  type ChainStatus struct {
     5  	CurrentHeight   uint64 `json:"current_height"`
     6  	CurrentHash     string `json:"current_hash"`
     7  	FinalizedHeight uint64 `json:"finalized_height"`
     8  	FinalizedHash   string `json:"finalized_hash"`
     9  	JustifiedHeight uint64 `json:"justified_height"`
    10  	JustifiedHash   string `json:"justified_hash"`
    11  }
    12  
    13  // getChainStatus return chain  status
    14  func (a *API) getChainStatus() Response {
    15  	chainStatus, err := a.GetChainStatus()
    16  	if err != nil {
    17  		return NewErrorResponse(err)
    18  	}
    19  
    20  	return NewSuccessResponse(chainStatus)
    21  }
    22  
    23  // GetChainStatus return chain status
    24  func (a *API) GetChainStatus() (*ChainStatus, error) {
    25  	finalizedBlockHeader, err := a.chain.LastFinalizedHeader()
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  
    30  	justifiedBlockHeader, err := a.chain.LastJustifiedHeader()
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	finalizedHash := finalizedBlockHeader.Hash()
    36  	justifiedHash := justifiedBlockHeader.Hash()
    37  	return &ChainStatus{
    38  		CurrentHeight:   a.chain.BestBlockHeight(),
    39  		CurrentHash:     a.chain.BestBlockHash().String(),
    40  		FinalizedHeight: finalizedBlockHeader.Height,
    41  		FinalizedHash:   finalizedHash.String(),
    42  		JustifiedHeight: justifiedBlockHeader.Height,
    43  		JustifiedHash:   justifiedHash.String(),
    44  	}, nil
    45  }