github.com/fiagdao/tendermint@v0.32.11-0.20220824195748-2087fcc480c1/rpc/core/status.go (about)

     1  package core
     2  
     3  import (
     4  	"time"
     5  
     6  	tmbytes "github.com/tendermint/tendermint/libs/bytes"
     7  	"github.com/tendermint/tendermint/p2p"
     8  	ctypes "github.com/tendermint/tendermint/rpc/core/types"
     9  	rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
    10  	sm "github.com/tendermint/tendermint/state"
    11  	"github.com/tendermint/tendermint/types"
    12  )
    13  
    14  // Status returns Tendermint status including node info, pubkey, latest block
    15  // hash, app hash, block height and time.
    16  // More: https://docs.tendermint.com/master/rpc/#/Info/status
    17  func Status(ctx *rpctypes.Context) (*ctypes.ResultStatus, error) {
    18  	var (
    19  		earliestBlockHash     tmbytes.HexBytes
    20  		earliestAppHash       tmbytes.HexBytes
    21  		earliestBlockTimeNano int64
    22  
    23  		earliestBlockHeight = env.BlockStore.Base()
    24  	)
    25  
    26  	if earliestBlockMeta := env.BlockStore.LoadBlockMeta(earliestBlockHeight); earliestBlockMeta != nil {
    27  		earliestAppHash = earliestBlockMeta.Header.AppHash
    28  		earliestBlockHash = earliestBlockMeta.BlockID.Hash
    29  		earliestBlockTimeNano = earliestBlockMeta.Header.Time.UnixNano()
    30  	}
    31  
    32  	var (
    33  		latestBlockHash     tmbytes.HexBytes
    34  		latestAppHash       tmbytes.HexBytes
    35  		latestBlockTimeNano int64
    36  
    37  		latestHeight = env.BlockStore.Height()
    38  	)
    39  
    40  	if latestHeight != 0 {
    41  		latestBlockMeta := env.BlockStore.LoadBlockMeta(latestHeight)
    42  		if latestBlockMeta != nil {
    43  			latestBlockHash = latestBlockMeta.BlockID.Hash
    44  			latestAppHash = latestBlockMeta.Header.AppHash
    45  			latestBlockTimeNano = latestBlockMeta.Header.Time.UnixNano()
    46  		}
    47  	}
    48  
    49  	// Return the very last voting power, not the voting power of this validator
    50  	// during the last block.
    51  
    52  	vals := validatorsAtHeight(latestUncommittedHeight())
    53  	var vInfo []ctypes.ValidatorInfo
    54  	for _, val := range vals {
    55  		vInfo = append(vInfo, ctypes.ValidatorInfo{
    56  			Address:     val.Address,
    57  			PubKey:      val.PubKey,
    58  			VotingPower: val.VotingPower,
    59  		})
    60  	}
    61  
    62  	result := &ctypes.ResultStatus{
    63  		NodeInfo: env.P2PTransport.NodeInfo().(p2p.DefaultNodeInfo),
    64  		SyncInfo: ctypes.SyncInfo{
    65  			LatestBlockHash:     latestBlockHash,
    66  			LatestAppHash:       latestAppHash,
    67  			LatestBlockHeight:   latestHeight,
    68  			LatestBlockTime:     time.Unix(0, latestBlockTimeNano),
    69  			EarliestBlockHash:   earliestBlockHash,
    70  			EarliestAppHash:     earliestAppHash,
    71  			EarliestBlockHeight: earliestBlockHeight,
    72  			EarliestBlockTime:   time.Unix(0, earliestBlockTimeNano),
    73  			CatchingUp:          env.ConsensusReactor.FastSync(),
    74  		},
    75  		ValidatorInfo: vInfo,
    76  	}
    77  
    78  	return result, nil
    79  }
    80  
    81  func validatorsAtHeight(h int64) (v []*types.Validator) {
    82  	v = make([]*types.Validator, 0)
    83  	vals, err := sm.LoadValidators(env.StateDB, h)
    84  	if err != nil {
    85  		return nil
    86  	}
    87  	for _, pubKey := range env.PubKey {
    88  		privValAddress := pubKey.Address()
    89  		_, val := vals.GetByAddress(privValAddress)
    90  		if val == nil {
    91  			continue
    92  		}
    93  		v = append(v, val)
    94  	}
    95  	return
    96  }
    97  
    98  // ConsensusReactorStatus returns Tendermint consensus reactor status - whether it's catching up in fast sync mode
    99  func ConsensusReactorStatus(ctx *rpctypes.Context) (*ctypes.ResultConsensusReactorStatus, error) {
   100  	return &ctypes.ResultConsensusReactorStatus{IsCatchingUp: env.ConsensusReactor.FastSync()}, nil
   101  }