config and env updates

This commit is contained in:
i-norden 2022-06-14 23:10:43 -05:00 committed by nabarun
parent 5b86cadeff
commit 0041f5823b
2 changed files with 31 additions and 4 deletions

View File

@ -19,6 +19,8 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/ethereum/go-ethereum/common"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/ethereum/go-ethereum/statediff/indexer/database/sql/postgres" "github.com/ethereum/go-ethereum/statediff/indexer/database/sql/postgres"
@ -38,9 +40,10 @@ const (
// Config contains params for both databases the service uses // Config contains params for both databases the service uses
type Config struct { type Config struct {
Eth *EthConfig Eth *EthConfig
DB *DBConfig DB *DBConfig
File *FileConfig File *FileConfig
Service *ServiceConfig
} }
// EthConfig is config parameters for the chain. // EthConfig is config parameters for the chain.
@ -60,11 +63,17 @@ type FileConfig struct {
OutputDir string OutputDir string
} }
type ServiceConfig struct {
AllowedPaths [][]byte
AllowedAccounts []common.Address
}
func NewConfig(mode SnapshotMode) (*Config, error) { func NewConfig(mode SnapshotMode) (*Config, error) {
ret := &Config{ ret := &Config{
&EthConfig{}, &EthConfig{},
&DBConfig{}, &DBConfig{},
&FileConfig{}, &FileConfig{},
&ServiceConfig{},
} }
return ret, ret.Init(mode) return ret, ret.Init(mode)
} }
@ -110,7 +119,7 @@ func (c *Config) Init(mode SnapshotMode) error {
default: default:
return fmt.Errorf("no output mode specified") return fmt.Errorf("no output mode specified")
} }
return nil return c.Service.Init()
} }
func (c *DBConfig) Init() { func (c *DBConfig) Init() {
@ -148,3 +157,18 @@ func (c *FileConfig) Init() error {
} }
return nil return nil
} }
func (c *ServiceConfig) Init() error {
viper.BindEnv(SNAPSHOT_ACCOUNTS_TOML, SNAPSHOT_ACCOUNTS)
allowedAccounts := viper.GetStringSlice(SNAPSHOT_ACCOUNTS)
accountsLen := len(allowedAccounts)
if accountsLen != 0 {
c.AllowedAccounts = make([]common.Address, accountsLen)
for i, allowedAccount := range allowedAccounts {
c.AllowedAccounts[i] = common.HexToAddress(allowedAccount)
}
} else {
logrus.Infof("no snapshot addresses specified, will perform snapshot of entire trie(s)")
}
return nil
}

View File

@ -23,6 +23,7 @@ const (
SNAPSHOT_MODE = "SNAPSHOT_MODE" SNAPSHOT_MODE = "SNAPSHOT_MODE"
SNAPSHOT_START_HEIGHT = "SNAPSHOT_START_HEIGHT" SNAPSHOT_START_HEIGHT = "SNAPSHOT_START_HEIGHT"
SNAPSHOT_END_HEIGHT = "SNAPSHOT_END_HEIGHT" SNAPSHOT_END_HEIGHT = "SNAPSHOT_END_HEIGHT"
SNAPSHOT_ACCOUNTS = "SNAPSHOT_ACCOUNTS"
LOGRUS_LEVEL = "LOGRUS_LEVEL" LOGRUS_LEVEL = "LOGRUS_LEVEL"
LOGRUS_FILE = "LOGRUS_FILE" LOGRUS_FILE = "LOGRUS_FILE"
@ -62,6 +63,7 @@ const (
SNAPSHOT_MODE_TOML = "snapshot.mode" SNAPSHOT_MODE_TOML = "snapshot.mode"
SNAPSHOT_START_HEIGHT_TOML = "snapshot.startHeight" SNAPSHOT_START_HEIGHT_TOML = "snapshot.startHeight"
SNAPSHOT_END_HEIGHT_TOML = "snapshot.endHeight" SNAPSHOT_END_HEIGHT_TOML = "snapshot.endHeight"
SNAPSHOT_ACCOUNTS_TOML = "snapshot.accounts"
LOGRUS_LEVEL_TOML = "log.level" LOGRUS_LEVEL_TOML = "log.level"
LOGRUS_FILE_TOML = "log.file" LOGRUS_FILE_TOML = "log.file"
@ -101,6 +103,7 @@ const (
SNAPSHOT_MODE_CLI = "snapshot-mode" SNAPSHOT_MODE_CLI = "snapshot-mode"
SNAPSHOT_START_HEIGHT_CLI = "start-height" SNAPSHOT_START_HEIGHT_CLI = "start-height"
SNAPSHOT_END_HEIGHT_CLI = "end-height" SNAPSHOT_END_HEIGHT_CLI = "end-height"
SNAPSHOT_ACCOUNTS_CLI = "snapshot-accounts"
LOGRUS_LEVEL_CLI = "log-level" LOGRUS_LEVEL_CLI = "log-level"
LOGRUS_FILE_CLI = "log-file" LOGRUS_FILE_CLI = "log-file"