Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add storage helper #274

Merged
merged 2 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 3 additions & 25 deletions examples/morpheusvm/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import (
"github.com/ava-labs/hypersdk/builder"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/gossiper"
"github.com/ava-labs/hypersdk/pebble"
hrpc "github.com/ava-labs/hypersdk/rpc"
"github.com/ava-labs/hypersdk/utils"
hstorage "github.com/ava-labs/hypersdk/storage"
"github.com/ava-labs/hypersdk/vm"
"go.uber.org/zap"

Expand Down Expand Up @@ -96,32 +95,11 @@ func (c *Controller) Initialize(
snowCtx.Log.Info("loaded genesis", zap.Any("genesis", c.genesis))

// Create DBs
blockPath, err := utils.InitSubDirectory(snowCtx.ChainDataDir, "block")
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}
// TODO: tune Pebble config based on each sub-db focus
cfg := pebble.NewDefaultConfig()
blockDB, err := pebble.New(blockPath, cfg)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}
statePath, err := utils.InitSubDirectory(snowCtx.ChainDataDir, "state")
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}
stateDB, err := pebble.New(statePath, cfg)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}
metaPath, err := utils.InitSubDirectory(snowCtx.ChainDataDir, "metadata")
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}
c.metaDB, err = pebble.New(metaPath, cfg)
blockDB, stateDB, metaDB, err := hstorage.New(snowCtx.ChainDataDir)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}
c.metaDB = metaDB

// Create handlers
//
Expand Down
28 changes: 3 additions & 25 deletions examples/tokenvm/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import (
"github.com/ava-labs/hypersdk/builder"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/gossiper"
"github.com/ava-labs/hypersdk/pebble"
hrpc "github.com/ava-labs/hypersdk/rpc"
"github.com/ava-labs/hypersdk/utils"
hstorage "github.com/ava-labs/hypersdk/storage"
"github.com/ava-labs/hypersdk/vm"
"go.uber.org/zap"

Expand Down Expand Up @@ -100,32 +99,11 @@ func (c *Controller) Initialize(
snowCtx.Log.Info("loaded genesis", zap.Any("genesis", c.genesis))

// Create DBs
blockPath, err := utils.InitSubDirectory(snowCtx.ChainDataDir, "block")
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}
// TODO: tune Pebble config based on each sub-db focus
cfg := pebble.NewDefaultConfig()
blockDB, err := pebble.New(blockPath, cfg)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}
statePath, err := utils.InitSubDirectory(snowCtx.ChainDataDir, "state")
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}
stateDB, err := pebble.New(statePath, cfg)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}
metaPath, err := utils.InitSubDirectory(snowCtx.ChainDataDir, "metadata")
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}
c.metaDB, err = pebble.New(metaPath, cfg)
blockDB, stateDB, metaDB, err := hstorage.New(snowCtx.ChainDataDir)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}
c.metaDB = metaDB

// Create handlers
//
Expand Down
10 changes: 10 additions & 0 deletions storage/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package storage

const (
blockDir = "block"
stateDir = "state"
metadataDir = "metadata"
)
42 changes: 42 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package storage

import (
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/hypersdk/pebble"
"github.com/ava-labs/hypersdk/utils"
)

// TODO: add option to use a single DB with prefixes to allow for atomic writes
// TODO: add metrics to pebble
func New(chainDataDir string) (database.Database, database.Database, database.Database, error) {
// TODO: tune Pebble config based on each sub-db focus
cfg := pebble.NewDefaultConfig()
blockPath, err := utils.InitSubDirectory(chainDataDir, blockDir)
if err != nil {
return nil, nil, nil, err
}
blockDB, err := pebble.New(blockPath, cfg)
if err != nil {
return nil, nil, nil, err
}
statePath, err := utils.InitSubDirectory(chainDataDir, stateDir)
if err != nil {
return nil, nil, nil, err
}
stateDB, err := pebble.New(statePath, cfg)
if err != nil {
return nil, nil, nil, err
}
metaPath, err := utils.InitSubDirectory(chainDataDir, metadataDir)
if err != nil {
return nil, nil, nil, err
}
metaDB, err := pebble.New(metaPath, cfg)
if err != nil {
return nil, nil, nil, err
}
return blockDB, stateDB, metaDB, nil
}