/
sources.go
123 lines (104 loc) · 4.75 KB
/
sources.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
package types
import (
"fmt"
"os"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/forbole/juno/v3/node/remote"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/forbole/juno/v3/node/local"
nodeconfig "github.com/forbole/juno/v3/node/config"
banksource "github.com/forbole/bdjuno/v3/modules/bank/source"
localbanksource "github.com/forbole/bdjuno/v3/modules/bank/source/local"
remotebanksource "github.com/forbole/bdjuno/v3/modules/bank/source/remote"
distrsource "github.com/forbole/bdjuno/v3/modules/distribution/source"
localdistrsource "github.com/forbole/bdjuno/v3/modules/distribution/source/local"
remotedistrsource "github.com/forbole/bdjuno/v3/modules/distribution/source/remote"
govsource "github.com/forbole/bdjuno/v3/modules/gov/source"
localgovsource "github.com/forbole/bdjuno/v3/modules/gov/source/local"
remotegovsource "github.com/forbole/bdjuno/v3/modules/gov/source/remote"
mintsource "github.com/forbole/bdjuno/v3/modules/mint/source"
localmintsource "github.com/forbole/bdjuno/v3/modules/mint/source/local"
remotemintsource "github.com/forbole/bdjuno/v3/modules/mint/source/remote"
slashingsource "github.com/forbole/bdjuno/v3/modules/slashing/source"
localslashingsource "github.com/forbole/bdjuno/v3/modules/slashing/source/local"
remoteslashingsource "github.com/forbole/bdjuno/v3/modules/slashing/source/remote"
stakingsource "github.com/forbole/bdjuno/v3/modules/staking/source"
localstakingsource "github.com/forbole/bdjuno/v3/modules/staking/source/local"
remotestakingsource "github.com/forbole/bdjuno/v3/modules/staking/source/remote"
)
type Sources struct {
BankSource banksource.Source
DistrSource distrsource.Source
GovSource govsource.Source
MintSource mintsource.Source
SlashingSource slashingsource.Source
StakingSource stakingsource.Source
}
func BuildSources(nodeCfg nodeconfig.Config, encodingConfig *params.EncodingConfig) (*Sources, error) {
switch cfg := nodeCfg.Details.(type) {
case *remote.Details:
return buildRemoteSources(cfg)
case *local.Details:
return buildLocalSources(cfg, encodingConfig)
default:
return nil, fmt.Errorf("invalid configuration type: %T", cfg)
}
}
func buildLocalSources(cfg *local.Details, encodingConfig *params.EncodingConfig) (*Sources, error) {
source, err := local.NewSource(cfg.Home, encodingConfig)
if err != nil {
return nil, err
}
app := simapp.NewSimApp(
log.NewTMLogger(log.NewSyncWriter(os.Stdout)), source.StoreDB, nil, true, map[int64]bool{},
cfg.Home, 0, simapp.MakeTestEncodingConfig(), simapp.EmptyAppOptions{},
)
sources := &Sources{
BankSource: localbanksource.NewSource(source, banktypes.QueryServer(app.BankKeeper)),
DistrSource: localdistrsource.NewSource(source, distrtypes.QueryServer(app.DistrKeeper)),
GovSource: localgovsource.NewSource(source, govtypes.QueryServer(app.GovKeeper)),
MintSource: localmintsource.NewSource(source, minttypes.QueryServer(app.MintKeeper)),
SlashingSource: localslashingsource.NewSource(source, slashingtypes.QueryServer(app.SlashingKeeper)),
StakingSource: localstakingsource.NewSource(source, stakingkeeper.Querier{Keeper: app.StakingKeeper}),
}
// Mount and initialize the stores
err = source.MountKVStores(app, "keys")
if err != nil {
return nil, err
}
err = source.MountTransientStores(app, "tkeys")
if err != nil {
return nil, err
}
err = source.MountMemoryStores(app, "memKeys")
if err != nil {
return nil, err
}
err = source.InitStores()
if err != nil {
return nil, err
}
return sources, nil
}
func buildRemoteSources(cfg *remote.Details) (*Sources, error) {
source, err := remote.NewSource(cfg.GRPC)
if err != nil {
return nil, fmt.Errorf("error while creating remote source: %s", err)
}
return &Sources{
BankSource: remotebanksource.NewSource(source, banktypes.NewQueryClient(source.GrpcConn)),
DistrSource: remotedistrsource.NewSource(source, distrtypes.NewQueryClient(source.GrpcConn)),
GovSource: remotegovsource.NewSource(source, govtypes.NewQueryClient(source.GrpcConn)),
MintSource: remotemintsource.NewSource(source, minttypes.NewQueryClient(source.GrpcConn)),
SlashingSource: remoteslashingsource.NewSource(source, slashingtypes.NewQueryClient(source.GrpcConn)),
StakingSource: remotestakingsource.NewSource(source, stakingtypes.NewQueryClient(source.GrpcConn)),
}, nil
}