Skip to content

Commit

Permalink
Add config validation
Browse files Browse the repository at this point in the history
  • Loading branch information
anodar committed Apr 22, 2024
1 parent 005f441 commit a0268fe
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
6 changes: 6 additions & 0 deletions staker/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ func (c *BlockValidatorConfig) Validate() error {
if err := c.ExecutionServerConfig.Validate(); err != nil {
return fmt.Errorf("validating execution server config: %w", err)
}
if err := c.RedisValidationClientConfig.Validate(); err != nil {
return fmt.Errorf("validating redis validation client configuration: %w", err)
}
return nil
}

Expand All @@ -146,6 +149,8 @@ type BlockValidatorConfigFetcher func() *BlockValidatorConfig
func BlockValidatorConfigAddOptions(prefix string, f *pflag.FlagSet) {
f.Bool(prefix+".enable", DefaultBlockValidatorConfig.Enable, "enable block-by-block validation")
rpcclient.RPCClientAddOptions(prefix+".validation-server", f, &DefaultBlockValidatorConfig.ValidationServer)
rpcclient.RPCClientAddOptions(prefix+".execution-server-config", f, &DefaultBlockValidatorConfig.ExecutionServerConfig)
validatorclient.RedisValidationClientConfigAddOptions(prefix+"redis-validation-client-config", f)
f.String(prefix+".validation-server-configs-list", DefaultBlockValidatorConfig.ValidationServerConfigsList, "array of validation rpc configs given as a json string. time duration should be supplied in number indicating nanoseconds")
f.Duration(prefix+".validation-poll", DefaultBlockValidatorConfig.ValidationPoll, "poll time to check validations")
f.Uint64(prefix+".forward-blocks", DefaultBlockValidatorConfig.ForwardBlocks, "prepare entries for up to that many blocks ahead of validation (small footprint)")
Expand All @@ -165,6 +170,7 @@ var DefaultBlockValidatorConfig = BlockValidatorConfig{
Enable: false,
ValidationServerConfigsList: "default",
ValidationServer: rpcclient.DefaultClientConfig,
ExecutionServerConfig: rpcclient.DefaultClientConfig,
ValidationPoll: time.Second,
ForwardBlocks: 1024,
PrerecordedBlocks: uint64(2 * runtime.NumCPU()),
Expand Down
13 changes: 6 additions & 7 deletions staker/stateless_block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ func NewStatelessBlockValidator(
validationSpawners = append(validationSpawners, validatorclient.NewValidationClient(valConfFetcher, stack))
}

validator := &StatelessBlockValidator{
valConfFetcher := func() *rpcclient.ClientConfig {
return &config().ExecutionServerConfig
}
return &StatelessBlockValidator{
config: config(),
recorder: recorder,
validationSpawners: validationSpawners,
Expand All @@ -218,12 +221,8 @@ func NewStatelessBlockValidator(
db: arbdb,
daService: das,
blobReader: blobReader,
}
valConfFetcher := func() *rpcclient.ClientConfig {
return &config().ExecutionServerConfig
}
validator.execSpawner = validatorclient.NewExecutionClient(valConfFetcher, stack)
return validator, nil
execSpawner: validatorclient.NewExecutionClient(valConfFetcher, stack),
}, nil
}

func (v *StatelessBlockValidator) GetModuleRootsToValidate() []common.Hash {
Expand Down
29 changes: 26 additions & 3 deletions validator/client/redisproducer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync/atomic"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/offchainlabs/nitro/pubsub"
"github.com/offchainlabs/nitro/util/containers"
"github.com/offchainlabs/nitro/util/redisutil"
Expand All @@ -23,12 +24,35 @@ type RedisValidationClientConfig struct {
ProducerConfig pubsub.ProducerConfig `koanf:"producer-config"`
// Supported wasm module roots, when the list is empty this is disabled.
ModuleRoots []string `koanf:"module-roots"`
moduleRoots []common.Hash
}

func (c RedisValidationClientConfig) Enabled() bool {
return c.RedisURL != ""
}

func (c *RedisValidationClientConfig) Validate() error {
m := make(map[string]bool)
// Add all moduleRoot hashes in case Validate is called twice so that we
// don't add duplicate moduleRoots again.
for _, mr := range c.moduleRoots {
m[mr.Hex()] = true
}
for _, mr := range c.ModuleRoots {
if _, exists := m[mr]; exists {
log.Warn("Duplicate module root", "hash", mr)
continue
}
h := common.HexToHash(mr)
if h == (common.Hash{}) {
return fmt.Errorf("invalid module root hash: %q", mr)
}
m[mr] = true
c.moduleRoots = append(c.moduleRoots, h)
}
return nil
}

var DefaultRedisValidationClientConfig = RedisValidationClientConfig{
Name: "redis validation client",
Room: 2,
Expand Down Expand Up @@ -72,11 +96,10 @@ func NewRedisValidationClient(cfg *RedisValidationClientConfig) (*RedisValidatio
if err != nil {
return nil, err
}
if len(cfg.ModuleRoots) == 0 {
if len(cfg.moduleRoots) == 0 {
return nil, fmt.Errorf("moduleRoots must be specified to enable redis streams")
}
for _, hash := range cfg.ModuleRoots {
mr := common.HexToHash(hash)
for _, mr := range cfg.moduleRoots {
p, err := pubsub.NewProducer[*validator.ValidationInput, validator.GoGlobalState](
redisClient, server_api.RedisStreamForRoot(mr), &cfg.ProducerConfig)
if err != nil {
Expand Down

0 comments on commit a0268fe

Please sign in to comment.