/
source.go
177 lines (144 loc) · 5.26 KB
/
source.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package local
import (
"fmt"
"os"
"path"
"reflect"
"unsafe"
db "github.com/cometbft/cometbft-db"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
cfg "github.com/cometbft/cometbft/config"
"github.com/cometbft/cometbft/libs/log"
tmnode "github.com/cometbft/cometbft/node"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmstore "github.com/cometbft/cometbft/store"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/viper"
"github.com/forbole/juno/v5/node"
"github.com/forbole/juno/v5/types/params"
)
var (
_ node.Source = &Source{}
)
// Source represents the Source interface implementation that reads the data from a local node
type Source struct {
Initialized bool
StoreDB db.DB
Codec codec.Codec
LegacyAmino *codec.LegacyAmino
BlockStore *tmstore.BlockStore
Logger log.Logger
Cms sdk.CommitMultiStore
}
// NewSource returns a new Source instance
func NewSource(home string, encodingConfig params.EncodingConfig) (*Source, error) {
levelDB, err := db.NewGoLevelDB("application", path.Join(home, "data"))
if err != nil {
return nil, err
}
tmCfg, err := parseConfig(home)
if err != nil {
return nil, err
}
blockStoreDB, err := tmnode.DefaultDBProvider(&tmnode.DBContext{ID: "blockstore", Config: tmCfg})
if err != nil {
return nil, err
}
return &Source{
StoreDB: levelDB,
Codec: encodingConfig.Codec,
LegacyAmino: encodingConfig.Amino,
BlockStore: tmstore.NewBlockStore(blockStoreDB),
Logger: log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "explorer"),
Cms: store.NewCommitMultiStore(levelDB),
}, nil
}
func parseConfig(home string) (*cfg.Config, error) {
viper.SetConfigFile(path.Join(home, "config", "config.toml"))
conf := cfg.DefaultConfig()
err := viper.Unmarshal(conf)
if err != nil {
return nil, err
}
conf.SetRoot(conf.RootDir)
err = conf.ValidateBasic()
if err != nil {
return nil, fmt.Errorf("error in config file: %v", err)
}
return conf, nil
}
// Type implements keeper.Source
func (k Source) Type() string {
return node.LocalKeeper
}
func getFieldUsingReflection(app interface{}, fieldName string) interface{} {
fv := reflect.ValueOf(app).Elem().FieldByName(fieldName)
return reflect.NewAt(fv.Type(), unsafe.Pointer(fv.UnsafeAddr())).Elem().Interface()
}
// MountKVStores allows to register the KV stores using the same KVStoreKey instances
// that are used inside the given app. To do so, this method uses the reflection to access
// the field with the specified name inside the given app. Such field must be of type
// map[string]*sdk.KVStoreKey and is commonly named something similar to "keys"
func (k Source) MountKVStores(app interface{}, fieldName string) error {
keys, ok := getFieldUsingReflection(app, fieldName).(map[string]*storetypes.KVStoreKey)
if !ok {
return fmt.Errorf("error while getting keys")
}
for _, key := range keys {
k.Cms.MountStoreWithDB(key, storetypes.StoreTypeIAVL, nil)
}
return nil
}
// MountTransientStores allows to register the Transient stores using the same TransientStoreKey instances
// that are used inside the given app. To do so, this method uses the reflection to access
// the field with the specified name inside the given app. Such field must be of type
// map[string]*sdk.TransientStoreKey and is commonly named something similar to "tkeys"
func (k Source) MountTransientStores(app interface{}, fieldName string) error {
tkeys, ok := getFieldUsingReflection(app, fieldName).(map[string]*storetypes.TransientStoreKey)
if !ok {
return fmt.Errorf("error while getting transient keys")
}
for _, key := range tkeys {
k.Cms.MountStoreWithDB(key, storetypes.StoreTypeTransient, nil)
}
return nil
}
// MountMemoryStores allows to register the Memory stores using the same MemoryStoreKey instances
// that are used inside the given app. To do so, this method uses the reflection to access
// the field with the specified name inside the given app. Such field must be of type
// map[string]*sdk.MemoryStoreKey and is commonly named something similar to "memkeys"
func (k Source) MountMemoryStores(app interface{}, fieldName string) error {
memKeys, ok := getFieldUsingReflection(app, fieldName).(map[string]*storetypes.MemoryStoreKey)
if !ok {
return fmt.Errorf("error while getting memory keys")
}
for _, key := range memKeys {
k.Cms.MountStoreWithDB(key, storetypes.StoreTypeMemory, nil)
}
return nil
}
// InitStores initializes the stores by mounting the various keys that have been specified.
// This method MUST be called before using any method that relies on the local storage somehow.
func (k Source) InitStores() error {
return k.Cms.LoadLatestVersion()
}
// LoadHeight loads the given height from the store.
// It returns a new Context that can be used to query the data, or an error if something wrong happens.
func (k Source) LoadHeight(height int64) (sdk.Context, error) {
var err error
var cms sdk.CacheMultiStore
if height > 0 {
cms, err = k.Cms.CacheMultiStoreWithVersion(height)
if err != nil {
return sdk.Context{}, err
}
} else {
cms, err = k.Cms.CacheMultiStoreWithVersion(k.BlockStore.Height())
if err != nil {
return sdk.Context{}, err
}
}
return sdk.NewContext(cms, tmproto.Header{}, false, k.Logger), nil
}