-
Notifications
You must be signed in to change notification settings - Fork 323
/
inspect_tendermint.go
87 lines (72 loc) · 2.11 KB
/
inspect_tendermint.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
package cmd
import (
"errors"
"fmt"
"path/filepath"
dbm "github.com/cometbft/cometbft-db"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/os"
"github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/store"
)
func loadStateAndBlockStore(config *cfg.Config) (*store.BlockStore, state.Store, error) {
dbType := dbm.BackendType(config.DBBackend)
if !os.FileExists(filepath.Join(config.DBDir(), "blockstore.db")) {
return nil, nil, fmt.Errorf("no blockstore found in %v", config.DBDir())
}
// Get BlockStore
blockStoreDB, err := dbm.NewDB("blockstore", dbType, config.DBDir())
if err != nil {
return nil, nil, err
}
blockStore := store.NewBlockStore(blockStoreDB)
if !os.FileExists(filepath.Join(config.DBDir(), "state.db")) {
return nil, nil, fmt.Errorf("no statestore found in %v", config.DBDir())
}
// Get StateStore
stateDB, err := dbm.NewDB("state", dbType, config.DBDir())
if err != nil {
return nil, nil, err
}
stateStore := state.NewStore(stateDB, state.StoreOptions{
DiscardABCIResponses: config.Storage.DiscardABCIResponses,
})
return blockStore, stateStore, nil
}
func getTendermintState(config *cfg.Config) error {
// use the parsed config to load the block and state store
blockStore, stateStore, err := loadStateAndBlockStore(config)
if err != nil {
return err
}
defer func() {
_ = blockStore.Close()
_ = stateStore.Close()
}()
// Read the data from the KVStore
fmt.Println("LOADING STATE")
state, err := stateStore.Load()
if err != nil {
return err
}
if state.IsEmpty() {
return errors.New("no state found")
}
fmt.Printf("%+v\n", state)
bh := blockStore.Height()
latestBlock := blockStore.LoadBlock(bh)
if latestBlock == nil {
return errors.New("no block found for latest height")
}
fmt.Printf("%+v\n", latestBlock)
if bh != state.LastBlockHeight {
// printing block for state height
fmt.Println("LOADING BLOCK FOR STATE HEIGHT")
block := blockStore.LoadBlock(state.LastBlockHeight)
if block == nil {
return errors.New("no block found for state height")
}
fmt.Printf("%+v\n", block)
}
return nil
}