github.com/adoriasoft/tendermint@v0.34.0-dev1.0.20200722151356-96d84601a75a/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  	var votingPower int64
    52  	if val := validatorAtHeight(latestUncommittedHeight()); val != nil {
    53  		votingPower = val.VotingPower
    54  	}
    55  
    56  	result := &ctypes.ResultStatus{
    57  		NodeInfo: env.P2PTransport.NodeInfo().(p2p.DefaultNodeInfo),
    58  		SyncInfo: ctypes.SyncInfo{
    59  			LatestBlockHash:     latestBlockHash,
    60  			LatestAppHash:       latestAppHash,
    61  			LatestBlockHeight:   latestHeight,
    62  			LatestBlockTime:     time.Unix(0, latestBlockTimeNano),
    63  			EarliestBlockHash:   earliestBlockHash,
    64  			EarliestAppHash:     earliestAppHash,
    65  			EarliestBlockHeight: earliestBlockHeight,
    66  			EarliestBlockTime:   time.Unix(0, earliestBlockTimeNano),
    67  			CatchingUp:          env.ConsensusReactor.WaitSync(),
    68  		},
    69  		ValidatorInfo: ctypes.ValidatorInfo{
    70  			Address:     env.PubKey.Address(),
    71  			PubKey:      env.PubKey,
    72  			VotingPower: votingPower,
    73  		},
    74  	}
    75  
    76  	return result, nil
    77  }
    78  
    79  func validatorAtHeight(h int64) *types.Validator {
    80  	vals, err := sm.LoadValidators(env.StateDB, h)
    81  	if err != nil {
    82  		return nil
    83  	}
    84  	privValAddress := env.PubKey.Address()
    85  	_, val := vals.GetByAddress(privValAddress)
    86  	return val
    87  }