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 read-only database flag (--db-read-only) #2266

Merged
merged 5 commits into from
Nov 7, 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
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,8 @@ func getDatabaseConfig(v *viper.Viper, networkID uint32) (node.DatabaseConfig, e
}

return node.DatabaseConfig{
Name: v.GetString(DBTypeKey),
Name: v.GetString(DBTypeKey),
ReadOnly: v.GetBool(DBReadOnlyKey),
Path: filepath.Join(
GetExpandedArg(v, DBPathKey),
constants.NetworkName(networkID),
Expand Down
1 change: 1 addition & 0 deletions config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func addNodeFlags(fs *pflag.FlagSet) {

// Database
fs.String(DBTypeKey, leveldb.Name, fmt.Sprintf("Database type to use. Must be one of {%s, %s, %s}", leveldb.Name, memdb.Name, pebble.Name))
fs.Bool(DBReadOnlyKey, false, "If true, database writes are to memory and never persisted. May still initialize database directory/files on disk if they don't exist")
fs.String(DBPathKey, defaultDBDir, "Path to database directory")
fs.String(DBConfigFileKey, "", fmt.Sprintf("Path to database config file. Ignored if %s is specified", DBConfigContentKey))
fs.String(DBConfigContentKey, "", "Specifies base64 encoded database config content")
Expand Down
1 change: 1 addition & 0 deletions config/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
StakeMintingPeriodKey = "stake-minting-period"
StakeSupplyCapKey = "stake-supply-cap"
DBTypeKey = "db-type"
DBReadOnlyKey = "db-read-only"
DBPathKey = "db-dir"
DBConfigFileKey = "db-config-file"
DBConfigContentKey = "db-config-file-content"
Expand Down
3 changes: 3 additions & 0 deletions node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ type BootstrapConfig struct {
}

type DatabaseConfig struct {
// If true, all writes are to memory and are discarded at node shutdown.
ReadOnly bool `json:"readOnly"`

// Path to database
Path string `json:"path"`

Expand Down
5 changes: 5 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/ava-labs/avalanchego/database/meterdb"
"github.com/ava-labs/avalanchego/database/pebble"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/database/versiondb"
"github.com/ava-labs/avalanchego/genesis"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/indexer"
Expand Down Expand Up @@ -531,6 +532,10 @@ func (n *Node) initDatabase() error {
)
}

if n.Config.ReadOnly && n.Config.DatabaseConfig.Name != memdb.Name {
n.DB = versiondb.New(n.DB)
}

var err error
n.DB, err = meterdb.New("db", n.MetricsRegisterer, n.DB)
if err != nil {
Expand Down
Loading