github.com/badrootd/celestia-core@v0.0.0-20240305091328-aa4207a4b25d/rpc/core/status.go (about)

     1  package core
     2  
     3  import (
     4  	"time"
     5  
     6  	cmtbytes "github.com/badrootd/celestia-core/libs/bytes"
     7  	"github.com/badrootd/celestia-core/p2p"
     8  	ctypes "github.com/badrootd/celestia-core/rpc/core/types"
     9  	rpctypes "github.com/badrootd/celestia-core/rpc/jsonrpc/types"
    10  	"github.com/badrootd/celestia-core/types"
    11  )
    12  
    13  // Status returns CometBFT status including node info, pubkey, latest block
    14  // hash, app hash, block height and time.
    15  // More: https://docs.cometbft.com/v0.34/rpc/#/Info/status
    16  func Status(ctx *rpctypes.Context) (*ctypes.ResultStatus, error) {
    17  	var (
    18  		earliestBlockHeight   int64
    19  		earliestBlockHash     cmtbytes.HexBytes
    20  		earliestAppHash       cmtbytes.HexBytes
    21  		earliestBlockTimeNano int64
    22  	)
    23  
    24  	env := GetEnvironment()
    25  	if earliestBlockMeta := env.BlockStore.LoadBaseMeta(); earliestBlockMeta != nil {
    26  		earliestBlockHeight = earliestBlockMeta.Header.Height
    27  		earliestAppHash = earliestBlockMeta.Header.AppHash
    28  		earliestBlockHash = earliestBlockMeta.BlockID.Hash
    29  		earliestBlockTimeNano = earliestBlockMeta.Header.Time.UnixNano()
    30  	}
    31  
    32  	var (
    33  		latestBlockHash     cmtbytes.HexBytes
    34  		latestAppHash       cmtbytes.HexBytes
    35  		latestBlockTimeNano int64
    36  
    37  		latestHeight = env.BlockStore.Height()
    38  	)
    39  
    40  	if latestHeight != 0 {
    41  		if latestBlockMeta := env.BlockStore.LoadBlockMeta(latestHeight); latestBlockMeta != nil {
    42  			latestBlockHash = latestBlockMeta.BlockID.Hash
    43  			latestAppHash = latestBlockMeta.Header.AppHash
    44  			latestBlockTimeNano = latestBlockMeta.Header.Time.UnixNano()
    45  		}
    46  	}
    47  
    48  	// Return the very last voting power, not the voting power of this validator
    49  	// during the last block.
    50  	var votingPower int64
    51  	if val := validatorAtHeight(latestUncommittedHeight()); val != nil {
    52  		votingPower = val.VotingPower
    53  	}
    54  
    55  	result := &ctypes.ResultStatus{
    56  		NodeInfo: env.P2PTransport.NodeInfo().(p2p.DefaultNodeInfo),
    57  		SyncInfo: ctypes.SyncInfo{
    58  			LatestBlockHash:     latestBlockHash,
    59  			LatestAppHash:       latestAppHash,
    60  			LatestBlockHeight:   latestHeight,
    61  			LatestBlockTime:     time.Unix(0, latestBlockTimeNano),
    62  			EarliestBlockHash:   earliestBlockHash,
    63  			EarliestAppHash:     earliestAppHash,
    64  			EarliestBlockHeight: earliestBlockHeight,
    65  			EarliestBlockTime:   time.Unix(0, earliestBlockTimeNano),
    66  			CatchingUp:          env.ConsensusReactor.WaitSync(),
    67  		},
    68  		ValidatorInfo: ctypes.ValidatorInfo{
    69  			Address:     env.PubKey.Address(),
    70  			PubKey:      env.PubKey,
    71  			VotingPower: votingPower,
    72  		},
    73  	}
    74  
    75  	return result, nil
    76  }
    77  
    78  func validatorAtHeight(h int64) *types.Validator {
    79  	env := GetEnvironment()
    80  	vals, err := env.StateStore.LoadValidators(h)
    81  	if err != nil {
    82  		return nil
    83  	}
    84  	privValAddress := env.PubKey.Address()
    85  	_, val := vals.GetByAddress(privValAddress)
    86  	return val
    87  }