Skip to content

Commit

Permalink
Add read-only database flag (--db-read-only) (#2266)
Browse files Browse the repository at this point in the history
  • Loading branch information
danlaine committed Nov 7, 2023
1 parent 52f93c8 commit 683fcfa
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 1 deletion.
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

0 comments on commit 683fcfa

Please sign in to comment.