Skip to content

Commit

Permalink
create db for kvindexer in AppCreator (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vritra4 authored Jul 17, 2024
1 parent 2718e7a commit e4a132c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 15 deletions.
7 changes: 4 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ type MinitiaApp struct {
func NewMinitiaApp(
logger log.Logger,
db dbm.DB,
kvindexerDB dbm.DB,
traceStore io.Writer,
loadLatest bool,
wasmOpts []wasmkeeper.Option,
Expand Down Expand Up @@ -802,7 +803,7 @@ func NewMinitiaApp(
marketmap.NewAppModule(appCodec, app.MarketMapKeeper),
)

if err := app.setupIndexer(appOpts, homePath, ac, vc, appCodec); err != nil {
if err := app.setupIndexer(kvindexerDB, appOpts, ac, vc, appCodec); err != nil {
panic(err)
}

Expand Down Expand Up @@ -1314,7 +1315,7 @@ func (app *MinitiaApp) GetScopedIBCKeeper() capabilitykeeper.ScopedKeeper {
func (app *MinitiaApp) TxConfig() client.TxConfig {
return app.txConfig
}
func (app *MinitiaApp) setupIndexer(appOpts servertypes.AppOptions, homePath string, ac, vc address.Codec, appCodec codec.Codec) error {
func (app *MinitiaApp) setupIndexer(db dbm.DB, appOpts servertypes.AppOptions, ac, vc address.Codec, appCodec codec.Codec) error {
// initialize the indexer fake-keeper
indexerConfig, err := indexerconfig.NewConfig(appOpts)
if err != nil {
Expand All @@ -1323,7 +1324,7 @@ func (app *MinitiaApp) setupIndexer(appOpts servertypes.AppOptions, homePath str
app.indexerKeeper = indexerkeeper.NewKeeper(
appCodec,
"wasm",
homePath,
db,
indexerConfig,
ac,
vc,
Expand Down
4 changes: 2 additions & 2 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestInitGenesisOnMigration(t *testing.T) {
db := dbm.NewMemDB()
logger := log.NewLogger(os.Stdout)
app := NewMinitiaApp(
logger, db, nil, true, []wasmkeeper.Option{}, EmptyAppOptions{homeDir: t.TempDir()})
logger, db, getOrCreateMemDB(nil), nil, true, []wasmkeeper.Option{}, EmptyAppOptions{homeDir: t.TempDir()})
ctx := app.NewContextLegacy(true, cmtproto.Header{Height: app.LastBlockHeight()})

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

require.NotEmpty(t, app.GetKey(banktypes.StoreKey))
require.NotEmpty(t, app.GetMemKey(capabilitytypes.MemStoreKey))
Expand Down
6 changes: 3 additions & 3 deletions app/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

// MakeEncodingConfig creates an EncodingConfig for testing
func MakeEncodingConfig() params.EncodingConfig {
tempApp := NewMinitiaApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, []wasmkeeper.Option{}, NewEmptyAppOptions())
tempApp := NewMinitiaApp(log.NewNopLogger(), dbm.NewMemDB(), dbm.NewMemDB(), nil, true, []wasmkeeper.Option{}, NewEmptyAppOptions())
encodingConfig := params.EncodingConfig{
InterfaceRegistry: tempApp.InterfaceRegistry(),
Codec: tempApp.AppCodec(),
Expand All @@ -35,7 +35,7 @@ func MakeEncodingConfig() params.EncodingConfig {
}

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

func BasicManager() module.BasicManager {
tempApp := NewMinitiaApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, []wasmkeeper.Option{}, NewEmptyAppOptions())
tempApp := NewMinitiaApp(log.NewNopLogger(), dbm.NewMemDB(), dbm.NewMemDB(), nil, true, []wasmkeeper.Option{}, NewEmptyAppOptions())
return tempApp.BasicModuleManager
}

Expand Down
1 change: 1 addition & 0 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func setup(homeDir string, db *dbm.DB, withGenesis bool) (*MinitiaApp, GenesisSt
app := NewMinitiaApp(
log.NewNopLogger(),
getOrCreateMemDB(db),
getOrCreateMemDB(nil),
nil,
true,
[]wasmkeeper.Option{},
Expand Down
37 changes: 33 additions & 4 deletions cmd/minitiad/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"path"
"path/filepath"

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

opchildcli "github.com/initia-labs/OPinit/x/opchild/client/cli"
kvindexerconfig "github.com/initia-labs/kvindexer/config"
kvindexerstore "github.com/initia-labs/kvindexer/store"
kvindexerkeeper "github.com/initia-labs/kvindexer/x/kvindexer/keeper"
)

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

dbDir, kvdbConfig := getDBConfig(appOpts)
kvindexerDB, err := kvindexerstore.OpenDB(dbDir, kvindexerkeeper.StoreName, kvdbConfig.BackendConfig)
if err != nil {
panic(err)
}

app := minitiaapp.NewMinitiaApp(
logger, db, traceStore, true,
logger, db, kvindexerDB, traceStore, true,
wasmOpts,
appOpts,
baseappOptions...,
)

a.app = app

return app
}
}
Expand All @@ -302,13 +311,13 @@ func (a appCreator) appExport(

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

if err := initiaApp.LoadHeight(height); err != nil {
return servertypes.ExportedApp{}, err
}
} else {
initiaApp = minitiaapp.NewMinitiaApp(logger, db, traceStore, true, []wasmkeeper.Option{}, appOpts)
initiaApp = minitiaapp.NewMinitiaApp(logger, db, dbm.NewMemDB(), traceStore, true, []wasmkeeper.Option{}, appOpts)
}

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

return clientCtx, nil
}

// getDBConfig returns the database configuration for the EVM indexer
func getDBConfig(appOpts servertypes.AppOptions) (string, *kvindexerconfig.IndexerConfig) {
rootDir := cast.ToString(appOpts.Get("home"))
dbDir := cast.ToString(appOpts.Get("db_dir"))
dbBackend, err := kvindexerconfig.NewConfig(appOpts)
if err != nil {
panic(err)
}

return rootify(dbDir, rootDir), dbBackend
}

// helper function to make config creation independent of root dir
func rootify(path, root string) string {
if filepath.IsAbs(path) {
return path
}
return filepath.Join(root, path)
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ require (
github.com/hashicorp/go-metrics v0.5.3
github.com/initia-labs/OPinit v0.3.2
github.com/initia-labs/initia v0.3.4
github.com/initia-labs/kvindexer v0.1.3
github.com/initia-labs/kvindexer v0.1.5
github.com/initia-labs/kvindexer/submodules/block v0.1.0
github.com/initia-labs/kvindexer/submodules/tx v0.1.0
github.com/initia-labs/kvindexer/submodules/wasm-nft v0.1.3
Expand Down Expand Up @@ -70,6 +70,7 @@ require (
github.com/99designs/keyring v1.2.2 // indirect
github.com/DataDog/datadog-go v3.2.0+incompatible // indirect
github.com/DataDog/zstd v1.5.5 // indirect
github.com/allegro/bigcache/v3 v3.1.0 // indirect
github.com/avast/retry-go/v4 v4.5.1 // indirect
github.com/aws/aws-sdk-go v1.44.312 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/allegro/bigcache/v3 v3.1.0 h1:H2Vp8VOvxcrB91o86fUSVJFqeuz8kpyyB02eH3bSzwk=
github.com/allegro/bigcache/v3 v3.1.0/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
Expand Down Expand Up @@ -806,8 +808,8 @@ github.com/initia-labs/ibc-go/v8 v8.0.0-20240419124350-4275a05abe2c h1:FDwh5zZbm
github.com/initia-labs/ibc-go/v8 v8.0.0-20240419124350-4275a05abe2c/go.mod h1:wj3qx75iC/XNnsMqbPDCIGs0G6Y3E/lo3bdqCyoCy+8=
github.com/initia-labs/initia v0.3.4 h1:i2fwTx9WDwGp2nyX64r6jtdtgfJmRqKAXGdxbHBRg9s=
github.com/initia-labs/initia v0.3.4/go.mod h1:nwtnVe3obacErGb7w6tq8Ych3U0d2f59rsgpVUeMnmM=
github.com/initia-labs/kvindexer v0.1.3 h1:TLkgJjp5TiPnH+OzYfk7ZKQTKqGOfSte59Y3gasob+o=
github.com/initia-labs/kvindexer v0.1.3/go.mod h1:rvAmgCAmEs4KM8sRRPcyTqNNwi8s2JiHybiFkYfp4KE=
github.com/initia-labs/kvindexer v0.1.5 h1:YLR4d237dNkGR8pe2zRGxx3CFB2CU6hhUmeJsshUw/0=
github.com/initia-labs/kvindexer v0.1.5/go.mod h1:OV85HaQ9KVrg+zGPUlxT9RF9nAaM3Yq4/3MoHqGqhWk=
github.com/initia-labs/kvindexer/submodules/block v0.1.0 h1:y+EXnksd/I2F96mzIoQA64nZUZON2P+99YrSzeLCLoY=
github.com/initia-labs/kvindexer/submodules/block v0.1.0/go.mod h1:4c+c59wVAnjuaJv/pcDYaUkeVmOqVV+orqEjya/RIjo=
github.com/initia-labs/kvindexer/submodules/tx v0.1.0 h1:6kbf6wmzXPN0XCQLasiFgq1AlZHkt5K3/ZG+IWw1nNs=
Expand Down

0 comments on commit e4a132c

Please sign in to comment.