Skip to content

Commit e4a132c

Browse files
authored
create db for kvindexer in AppCreator (#52)
1 parent 2718e7a commit e4a132c

File tree

7 files changed

+49
-15
lines changed

7 files changed

+49
-15
lines changed

app/app.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ type MinitiaApp struct {
278278
func NewMinitiaApp(
279279
logger log.Logger,
280280
db dbm.DB,
281+
kvindexerDB dbm.DB,
281282
traceStore io.Writer,
282283
loadLatest bool,
283284
wasmOpts []wasmkeeper.Option,
@@ -802,7 +803,7 @@ func NewMinitiaApp(
802803
marketmap.NewAppModule(appCodec, app.MarketMapKeeper),
803804
)
804805

805-
if err := app.setupIndexer(appOpts, homePath, ac, vc, appCodec); err != nil {
806+
if err := app.setupIndexer(kvindexerDB, appOpts, ac, vc, appCodec); err != nil {
806807
panic(err)
807808
}
808809

@@ -1314,7 +1315,7 @@ func (app *MinitiaApp) GetScopedIBCKeeper() capabilitykeeper.ScopedKeeper {
13141315
func (app *MinitiaApp) TxConfig() client.TxConfig {
13151316
return app.txConfig
13161317
}
1317-
func (app *MinitiaApp) setupIndexer(appOpts servertypes.AppOptions, homePath string, ac, vc address.Codec, appCodec codec.Codec) error {
1318+
func (app *MinitiaApp) setupIndexer(db dbm.DB, appOpts servertypes.AppOptions, ac, vc address.Codec, appCodec codec.Codec) error {
13181319
// initialize the indexer fake-keeper
13191320
indexerConfig, err := indexerconfig.NewConfig(appOpts)
13201321
if err != nil {
@@ -1323,7 +1324,7 @@ func (app *MinitiaApp) setupIndexer(appOpts servertypes.AppOptions, homePath str
13231324
app.indexerKeeper = indexerkeeper.NewKeeper(
13241325
appCodec,
13251326
"wasm",
1326-
homePath,
1327+
db,
13271328
indexerConfig,
13281329
ac,
13291330
vc,

app/app_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TestInitGenesisOnMigration(t *testing.T) {
6666
db := dbm.NewMemDB()
6767
logger := log.NewLogger(os.Stdout)
6868
app := NewMinitiaApp(
69-
logger, db, nil, true, []wasmkeeper.Option{}, EmptyAppOptions{homeDir: t.TempDir()})
69+
logger, db, getOrCreateMemDB(nil), nil, true, []wasmkeeper.Option{}, EmptyAppOptions{homeDir: t.TempDir()})
7070
ctx := app.NewContextLegacy(true, cmtproto.Header{Height: app.LastBlockHeight()})
7171

7272
// Create a mock module. This module will serve as the new module we're
@@ -122,7 +122,7 @@ func TestGetKey(t *testing.T) {
122122
db := dbm.NewMemDB()
123123
app := NewMinitiaApp(
124124
log.NewLogger(os.Stdout),
125-
db, nil, true, []wasmkeeper.Option{}, EmptyAppOptions{homeDir: t.TempDir()})
125+
db, dbm.NewMemDB(), nil, true, []wasmkeeper.Option{}, EmptyAppOptions{homeDir: t.TempDir()})
126126

127127
require.NotEmpty(t, app.GetKey(banktypes.StoreKey))
128128
require.NotEmpty(t, app.GetMemKey(capabilitytypes.MemStoreKey))

app/encoding.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
// MakeEncodingConfig creates an EncodingConfig for testing
2525
func MakeEncodingConfig() params.EncodingConfig {
26-
tempApp := NewMinitiaApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, []wasmkeeper.Option{}, NewEmptyAppOptions())
26+
tempApp := NewMinitiaApp(log.NewNopLogger(), dbm.NewMemDB(), dbm.NewMemDB(), nil, true, []wasmkeeper.Option{}, NewEmptyAppOptions())
2727
encodingConfig := params.EncodingConfig{
2828
InterfaceRegistry: tempApp.InterfaceRegistry(),
2929
Codec: tempApp.AppCodec(),
@@ -35,7 +35,7 @@ func MakeEncodingConfig() params.EncodingConfig {
3535
}
3636

3737
func AutoCliOpts() autocli.AppOptions {
38-
tempApp := NewMinitiaApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, []wasmkeeper.Option{}, NewEmptyAppOptions())
38+
tempApp := NewMinitiaApp(log.NewNopLogger(), dbm.NewMemDB(), dbm.NewMemDB(), nil, true, []wasmkeeper.Option{}, NewEmptyAppOptions())
3939
modules := make(map[string]appmodule.AppModule, 0)
4040
for _, m := range tempApp.ModuleManager.Modules {
4141
if moduleWithName, ok := m.(module.HasName); ok {
@@ -56,7 +56,7 @@ func AutoCliOpts() autocli.AppOptions {
5656
}
5757

5858
func BasicManager() module.BasicManager {
59-
tempApp := NewMinitiaApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, []wasmkeeper.Option{}, NewEmptyAppOptions())
59+
tempApp := NewMinitiaApp(log.NewNopLogger(), dbm.NewMemDB(), dbm.NewMemDB(), nil, true, []wasmkeeper.Option{}, NewEmptyAppOptions())
6060
return tempApp.BasicModuleManager
6161
}
6262

app/test_helpers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func setup(homeDir string, db *dbm.DB, withGenesis bool) (*MinitiaApp, GenesisSt
5858
app := NewMinitiaApp(
5959
log.NewNopLogger(),
6060
getOrCreateMemDB(db),
61+
getOrCreateMemDB(nil),
6162
nil,
6263
true,
6364
[]wasmkeeper.Option{},

cmd/minitiad/root.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"os"
88
"path"
9+
"path/filepath"
910

1011
tmcli "github.com/cometbft/cometbft/libs/cli"
1112
"golang.org/x/sync/errgroup"
@@ -47,6 +48,9 @@ import (
4748
minitiaapp "github.com/initia-labs/miniwasm/app"
4849

4950
opchildcli "github.com/initia-labs/OPinit/x/opchild/client/cli"
51+
kvindexerconfig "github.com/initia-labs/kvindexer/config"
52+
kvindexerstore "github.com/initia-labs/kvindexer/store"
53+
kvindexerkeeper "github.com/initia-labs/kvindexer/x/kvindexer/keeper"
5054
)
5155

5256
// NewRootCmd creates a new root command for initiad. It is called once in the
@@ -267,15 +271,20 @@ func (a *appCreator) AppCreator() servertypes.AppCreator {
267271
wasmOpts = append(wasmOpts, wasmkeeper.WithVMCacheMetrics(prometheus.DefaultRegisterer))
268272
}
269273

274+
dbDir, kvdbConfig := getDBConfig(appOpts)
275+
kvindexerDB, err := kvindexerstore.OpenDB(dbDir, kvindexerkeeper.StoreName, kvdbConfig.BackendConfig)
276+
if err != nil {
277+
panic(err)
278+
}
279+
270280
app := minitiaapp.NewMinitiaApp(
271-
logger, db, traceStore, true,
281+
logger, db, kvindexerDB, traceStore, true,
272282
wasmOpts,
273283
appOpts,
274284
baseappOptions...,
275285
)
276286

277287
a.app = app
278-
279288
return app
280289
}
281290
}
@@ -302,13 +311,13 @@ func (a appCreator) appExport(
302311

303312
var initiaApp *minitiaapp.MinitiaApp
304313
if height != -1 {
305-
initiaApp = minitiaapp.NewMinitiaApp(logger, db, traceStore, false, []wasmkeeper.Option{}, appOpts)
314+
initiaApp = minitiaapp.NewMinitiaApp(logger, db, dbm.NewMemDB(), traceStore, false, []wasmkeeper.Option{}, appOpts)
306315

307316
if err := initiaApp.LoadHeight(height); err != nil {
308317
return servertypes.ExportedApp{}, err
309318
}
310319
} else {
311-
initiaApp = minitiaapp.NewMinitiaApp(logger, db, traceStore, true, []wasmkeeper.Option{}, appOpts)
320+
initiaApp = minitiaapp.NewMinitiaApp(logger, db, dbm.NewMemDB(), traceStore, true, []wasmkeeper.Option{}, appOpts)
312321
}
313322

314323
return initiaApp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs, modulesToExport)
@@ -357,3 +366,23 @@ func readEnv(clientCtx client.Context) (client.Context, error) {
357366

358367
return clientCtx, nil
359368
}
369+
370+
// getDBConfig returns the database configuration for the EVM indexer
371+
func getDBConfig(appOpts servertypes.AppOptions) (string, *kvindexerconfig.IndexerConfig) {
372+
rootDir := cast.ToString(appOpts.Get("home"))
373+
dbDir := cast.ToString(appOpts.Get("db_dir"))
374+
dbBackend, err := kvindexerconfig.NewConfig(appOpts)
375+
if err != nil {
376+
panic(err)
377+
}
378+
379+
return rootify(dbDir, rootDir), dbBackend
380+
}
381+
382+
// helper function to make config creation independent of root dir
383+
func rootify(path, root string) string {
384+
if filepath.IsAbs(path) {
385+
return path
386+
}
387+
return filepath.Join(root, path)
388+
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ require (
3636
github.com/hashicorp/go-metrics v0.5.3
3737
github.com/initia-labs/OPinit v0.3.2
3838
github.com/initia-labs/initia v0.3.4
39-
github.com/initia-labs/kvindexer v0.1.3
39+
github.com/initia-labs/kvindexer v0.1.5
4040
github.com/initia-labs/kvindexer/submodules/block v0.1.0
4141
github.com/initia-labs/kvindexer/submodules/tx v0.1.0
4242
github.com/initia-labs/kvindexer/submodules/wasm-nft v0.1.3
@@ -70,6 +70,7 @@ require (
7070
github.com/99designs/keyring v1.2.2 // indirect
7171
github.com/DataDog/datadog-go v3.2.0+incompatible // indirect
7272
github.com/DataDog/zstd v1.5.5 // indirect
73+
github.com/allegro/bigcache/v3 v3.1.0 // indirect
7374
github.com/avast/retry-go/v4 v4.5.1 // indirect
7475
github.com/aws/aws-sdk-go v1.44.312 // indirect
7576
github.com/beorn7/perks v1.0.1 // indirect

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
261261
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
262262
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
263263
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
264+
github.com/allegro/bigcache/v3 v3.1.0 h1:H2Vp8VOvxcrB91o86fUSVJFqeuz8kpyyB02eH3bSzwk=
265+
github.com/allegro/bigcache/v3 v3.1.0/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I=
264266
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
265267
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
266268
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
@@ -806,8 +808,8 @@ github.com/initia-labs/ibc-go/v8 v8.0.0-20240419124350-4275a05abe2c h1:FDwh5zZbm
806808
github.com/initia-labs/ibc-go/v8 v8.0.0-20240419124350-4275a05abe2c/go.mod h1:wj3qx75iC/XNnsMqbPDCIGs0G6Y3E/lo3bdqCyoCy+8=
807809
github.com/initia-labs/initia v0.3.4 h1:i2fwTx9WDwGp2nyX64r6jtdtgfJmRqKAXGdxbHBRg9s=
808810
github.com/initia-labs/initia v0.3.4/go.mod h1:nwtnVe3obacErGb7w6tq8Ych3U0d2f59rsgpVUeMnmM=
809-
github.com/initia-labs/kvindexer v0.1.3 h1:TLkgJjp5TiPnH+OzYfk7ZKQTKqGOfSte59Y3gasob+o=
810-
github.com/initia-labs/kvindexer v0.1.3/go.mod h1:rvAmgCAmEs4KM8sRRPcyTqNNwi8s2JiHybiFkYfp4KE=
811+
github.com/initia-labs/kvindexer v0.1.5 h1:YLR4d237dNkGR8pe2zRGxx3CFB2CU6hhUmeJsshUw/0=
812+
github.com/initia-labs/kvindexer v0.1.5/go.mod h1:OV85HaQ9KVrg+zGPUlxT9RF9nAaM3Yq4/3MoHqGqhWk=
811813
github.com/initia-labs/kvindexer/submodules/block v0.1.0 h1:y+EXnksd/I2F96mzIoQA64nZUZON2P+99YrSzeLCLoY=
812814
github.com/initia-labs/kvindexer/submodules/block v0.1.0/go.mod h1:4c+c59wVAnjuaJv/pcDYaUkeVmOqVV+orqEjya/RIjo=
813815
github.com/initia-labs/kvindexer/submodules/tx v0.1.0 h1:6kbf6wmzXPN0XCQLasiFgq1AlZHkt5K3/ZG+IWw1nNs=

0 commit comments

Comments
 (0)