/
status.go
101 lines (87 loc) · 3.06 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package core
import (
"time"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
"github.com/tendermint/tendermint/p2p"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types"
)
// Status returns Tendermint status including node info, pubkey, latest block
// hash, app hash, block height and time.
// More: https://docs.tendermint.com/master/rpc/#/Info/status
func Status(ctx *rpctypes.Context) (*ctypes.ResultStatus, error) {
var (
earliestBlockHash tmbytes.HexBytes
earliestAppHash tmbytes.HexBytes
earliestBlockTimeNano int64
earliestBlockHeight = env.BlockStore.Base()
)
if earliestBlockMeta := env.BlockStore.LoadBlockMeta(earliestBlockHeight); earliestBlockMeta != nil {
earliestAppHash = earliestBlockMeta.Header.AppHash
earliestBlockHash = earliestBlockMeta.BlockID.Hash
earliestBlockTimeNano = earliestBlockMeta.Header.Time.UnixNano()
}
var (
latestBlockHash tmbytes.HexBytes
latestAppHash tmbytes.HexBytes
latestBlockTimeNano int64
latestHeight = env.BlockStore.Height()
)
if latestHeight != 0 {
latestBlockMeta := env.BlockStore.LoadBlockMeta(latestHeight)
if 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.
vals := validatorsAtHeight(latestUncommittedHeight())
var vInfo []ctypes.ValidatorInfo
for _, val := range vals {
vInfo = append(vInfo, ctypes.ValidatorInfo{
Address: val.Address,
PubKey: val.PubKey,
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.FastSync(),
},
ValidatorInfo: vInfo,
}
return result, nil
}
func validatorsAtHeight(h int64) (v []*types.Validator) {
v = make([]*types.Validator, 0)
vals, err := sm.LoadValidators(env.StateDB, h)
if err != nil {
return nil
}
for _, pubKey := range env.PubKey {
privValAddress := pubKey.Address()
_, val := vals.GetByAddress(privValAddress)
if val == nil {
continue
}
v = append(v, val)
}
return
}
// ConsensusReactorStatus returns Tendermint consensus reactor status - whether it's catching up in fast sync mode
func ConsensusReactorStatus(ctx *rpctypes.Context) (*ctypes.ResultConsensusReactorStatus, error) {
return &ctypes.ResultConsensusReactorStatus{IsCatchingUp: env.ConsensusReactor.FastSync()}, nil
}