github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/internal/rpc/core/status.go (about)

     1  package core
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"time"
     8  
     9  	tmbytes "github.com/ari-anchor/sei-tendermint/libs/bytes"
    10  	"github.com/ari-anchor/sei-tendermint/rpc/coretypes"
    11  	"github.com/ari-anchor/sei-tendermint/types"
    12  )
    13  
    14  // Status returns Tendermint status including node info, pubkey, latest block
    15  // hash, app hash, block height, current max peer block height, and time.
    16  // More: https://docs.tendermint.com/master/rpc/#/Info/status
    17  func (env *Environment) Status(ctx context.Context) (*coretypes.ResultStatus, error) {
    18  	var (
    19  		earliestBlockHeight   int64
    20  		earliestBlockHash     tmbytes.HexBytes
    21  		earliestAppHash       tmbytes.HexBytes
    22  		earliestBlockTimeNano int64
    23  	)
    24  
    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     tmbytes.HexBytes
    34  		latestAppHash       tmbytes.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 := env.validatorAtHeight(env.latestUncommittedHeight()); val != nil {
    52  		votingPower = val.VotingPower
    53  	}
    54  	validatorInfo := coretypes.ValidatorInfo{}
    55  	if env.PubKey != nil {
    56  		validatorInfo = coretypes.ValidatorInfo{
    57  			Address:     env.PubKey.Address(),
    58  			PubKey:      env.PubKey,
    59  			VotingPower: votingPower,
    60  		}
    61  	}
    62  
    63  	var applicationInfo coretypes.ApplicationInfo
    64  	if abciInfo, err := env.ABCIInfo(ctx); err == nil {
    65  		applicationInfo.Version = fmt.Sprint(abciInfo.Response.AppVersion)
    66  	}
    67  
    68  	result := &coretypes.ResultStatus{
    69  		NodeInfo:        env.NodeInfo,
    70  		ApplicationInfo: applicationInfo,
    71  		SyncInfo: coretypes.SyncInfo{
    72  			LatestBlockHash:     latestBlockHash,
    73  			LatestAppHash:       latestAppHash,
    74  			LatestBlockHeight:   latestHeight,
    75  			LatestBlockTime:     time.Unix(0, latestBlockTimeNano),
    76  			EarliestBlockHash:   earliestBlockHash,
    77  			EarliestAppHash:     earliestAppHash,
    78  			EarliestBlockHeight: earliestBlockHeight,
    79  			EarliestBlockTime:   time.Unix(0, earliestBlockTimeNano),
    80  			// this should start as true, if consensus
    81  			// hasn't started yet, and then flip to false
    82  			// (or true,) depending on what's actually
    83  			// happening.
    84  			CatchingUp: true,
    85  		},
    86  		ValidatorInfo: validatorInfo,
    87  	}
    88  
    89  	if env.ConsensusReactor != nil {
    90  		result.SyncInfo.CatchingUp = env.ConsensusReactor.WaitSync()
    91  	}
    92  
    93  	if env.BlockSyncReactor != nil {
    94  		result.SyncInfo.MaxPeerBlockHeight = env.BlockSyncReactor.GetMaxPeerBlockHeight()
    95  		result.SyncInfo.TotalSyncedTime = env.BlockSyncReactor.GetTotalSyncedTime()
    96  		result.SyncInfo.RemainingTime = env.BlockSyncReactor.GetRemainingSyncTime()
    97  	}
    98  
    99  	if env.StateSyncMetricer != nil {
   100  		result.SyncInfo.TotalSnapshots = env.StateSyncMetricer.TotalSnapshots()
   101  		result.SyncInfo.ChunkProcessAvgTime = env.StateSyncMetricer.ChunkProcessAvgTime()
   102  		result.SyncInfo.SnapshotHeight = env.StateSyncMetricer.SnapshotHeight()
   103  		result.SyncInfo.SnapshotChunksCount = env.StateSyncMetricer.SnapshotChunksCount()
   104  		result.SyncInfo.SnapshotChunksTotal = env.StateSyncMetricer.SnapshotChunksTotal()
   105  		result.SyncInfo.BackFilledBlocks = env.StateSyncMetricer.BackFilledBlocks()
   106  		result.SyncInfo.BackFillBlocksTotal = env.StateSyncMetricer.BackFillBlocksTotal()
   107  	}
   108  
   109  	return result, nil
   110  }
   111  
   112  func (env *Environment) validatorAtHeight(h int64) *types.Validator {
   113  	valsWithH, err := env.StateStore.LoadValidators(h)
   114  	if err != nil {
   115  		return nil
   116  	}
   117  	if env.ConsensusState == nil {
   118  		return nil
   119  	}
   120  	if env.PubKey == nil {
   121  		return nil
   122  	}
   123  	privValAddress := env.PubKey.Address()
   124  
   125  	// If we're still at height h, search in the current validator set.
   126  	lastBlockHeight, vals := env.ConsensusState.GetValidators()
   127  	if lastBlockHeight == h {
   128  		for _, val := range vals {
   129  			if bytes.Equal(val.Address, privValAddress) {
   130  				return val
   131  			}
   132  		}
   133  	}
   134  
   135  	_, val := valsWithH.GetByAddress(privValAddress)
   136  	return val
   137  }