Skip to content

Commit

Permalink
Update NewOption to unmarshal config bytes into option supplied confi…
Browse files Browse the repository at this point in the history
…g type (#1484)

* Add NewOptionWithConfig that auto-unmarshals config bytes into supplied config type

* update tx indexer to honor enabled config value

* fix lint

* Rename NewOptionWithConfig to single exported NewOption function signature
  • Loading branch information
aaronbuchwald authored Sep 4, 2024
1 parent f1bc00e commit 50f6fd7
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 30 deletions.
17 changes: 15 additions & 2 deletions api/indexer/tx_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,24 @@ var (
_ event.Subscription[*chain.StatefulBlock] = (*txDBIndexer)(nil)
)

type Config struct {
Enabled bool `json:"enabled"`
}

func NewDefaultConfig() Config {
return Config{
Enabled: true,
}
}

func With() vm.Option {
return vm.NewOption(Namespace, OptionFunc)
return vm.NewOption(Namespace, NewDefaultConfig(), OptionFunc)
}

func OptionFunc(v *vm.VM, _ []byte) error {
func OptionFunc(v *vm.VM, config Config) error {
if !config.Enabled {
return nil
}
dbPath := filepath.Join(v.DataDir, "indexer", "db")
db, _, err := pebble.New(dbPath, pebble.NewDefaultConfig())
if err != nil {
Expand Down
15 changes: 14 additions & 1 deletion api/jsonrpc/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,21 @@ import "github.com/ava-labs/hypersdk/vm"

const Namespace = "core"

type Config struct {
Enabled bool `json:"enabled"`
}

func NewDefaultConfig() Config {
return Config{
Enabled: true,
}
}

func With() vm.Option {
return vm.NewOption(Namespace, func(v *vm.VM, _ []byte) error {
return vm.NewOption(Namespace, NewDefaultConfig(), func(v *vm.VM, config Config) error {
if !config.Enabled {
return nil
}
vm.WithVMAPIs(JSONRPCServerFactory{})(v)
return nil
})
Expand Down
13 changes: 3 additions & 10 deletions api/ws/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package ws

import (
"context"
"encoding/json"
"errors"
"sync"

Expand Down Expand Up @@ -48,21 +47,15 @@ func NewDefaultConfig() Config {
}

func With() vm.Option {
return vm.NewOption(Namespace, OptionFunc)
return vm.NewOption(Namespace, NewDefaultConfig(), OptionFunc)
}

func OptionFunc(v *vm.VM, configBytes []byte) error {
actionRegistry, authRegistry := v.Registry()
config := NewDefaultConfig()
if len(configBytes) > 0 {
if err := json.Unmarshal(configBytes, &config); err != nil {
return err
}
}
func OptionFunc(v *vm.VM, config Config) error {
if !config.Enabled {
return nil
}

actionRegistry, authRegistry := v.Registry()
server, handler := NewWebSocketServer(
v,
v.Logger(),
Expand Down
15 changes: 14 additions & 1 deletion examples/morpheusvm/vm/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,21 @@ import "github.com/ava-labs/hypersdk/vm"

const Namespace = "controller"

type Config struct {
Enabled bool `json:"enabled"`
}

func NewDefaultConfig() Config {
return Config{
Enabled: true,
}
}

func With() vm.Option {
return vm.NewOption(Namespace, func(v *vm.VM, _ []byte) error {
return vm.NewOption(Namespace, NewDefaultConfig(), func(v *vm.VM, config Config) error {
if !config.Enabled {
return nil
}
vm.WithVMAPIs(jsonRPCServerFactory{})(v)
return nil
})
Expand Down
11 changes: 2 additions & 9 deletions extension/externalsubscriber/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package externalsubscriber

import (
"context"
"encoding/json"

"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/event"
Expand All @@ -26,16 +25,10 @@ func NewDefaultConfig() Config {
}

func With() vm.Option {
return vm.NewOption(Namespace, OptionFunc)
return vm.NewOption(Namespace, NewDefaultConfig(), OptionFunc)
}

func OptionFunc(v *vm.VM, configBytes []byte) error {
config := NewDefaultConfig()
if len(configBytes) > 0 {
if err := json.Unmarshal(configBytes, &config); err != nil {
return err
}
}
func OptionFunc(v *vm.VM, config Config) error {
if !config.Enabled {
return nil
}
Expand Down
36 changes: 30 additions & 6 deletions vm/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package vm

import (
"encoding/json"
"fmt"

"github.com/ava-labs/hypersdk/api"
"github.com/ava-labs/hypersdk/builder"
"github.com/ava-labs/hypersdk/chain"
Expand All @@ -13,18 +16,38 @@ import (

type RegisterFunc func(*VM)

type OptionFunc func(*VM, []byte) error
type optionFunc func(vm *VM, configBytes []byte) error

type OptionFunc[T any] func(vm *VM, config T) error

type Option struct {
Namespace string
OptionFunc OptionFunc
optionFunc optionFunc
}

func NewOption(namespace string, optionFunc OptionFunc) Option {
func newOptionWithBytes(namespace string, optionFunc optionFunc) Option {
return Option{
Namespace: namespace,
OptionFunc: optionFunc,
optionFunc: optionFunc,
}
}

// NewOption returns an option with:
// 1) A namespace to define the key in the VM's JSON config that should be supplied to this option
// 2) A default config value the VM will directly unmarshal into
// 3) An option function that takes the VM and resulting config value as arguments
func NewOption[T any](namespace string, defaultConfig T, optionFunc OptionFunc[T]) Option {
config := defaultConfig
configOptionFunc := func(vm *VM, configBytes []byte) error {
if len(configBytes) > 0 {
if err := json.Unmarshal(configBytes, &config); err != nil {
return fmt.Errorf("failed to unmarshal %q config %q: %w", namespace, string(configBytes), err)
}
}

return optionFunc(vm, config)
}
return newOptionWithBytes(namespace, configOptionFunc)
}

func WithBuilder() RegisterFunc {
Expand All @@ -40,9 +63,10 @@ func WithGossiper() RegisterFunc {
}

func WithManual() Option {
return NewOption(
return NewOption[struct{}](
"manual",
func(vm *VM, _ []byte) error {
struct{}{},
func(vm *VM, _ struct{}) error {
WithBuilder()(vm)
WithGossiper()(vm)
return nil
Expand Down
2 changes: 1 addition & 1 deletion vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (vm *VM) Initialize(

for _, Option := range vm.options {
config := namespacedConfig[Option.Namespace]
if err := Option.OptionFunc(vm, config); err != nil {
if err := Option.optionFunc(vm, config); err != nil {
return err
}
}
Expand Down

0 comments on commit 50f6fd7

Please sign in to comment.