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