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

Refactor vm.Config out of vm.Controller #1146

Merged
merged 5 commits into from
Jul 17, 2024
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
124 changes: 7 additions & 117 deletions examples/morpheusvm/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,137 +5,27 @@ package config

import (
"encoding/json"
"fmt"
"strings"
"time"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/profiler"

"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/examples/morpheusvm/consts"
"github.com/ava-labs/hypersdk/examples/morpheusvm/version"
"github.com/ava-labs/hypersdk/trace"
"github.com/ava-labs/hypersdk/vm"
)

const (
defaultContinuousProfilerFrequency = 1 * time.Minute
defaultContinuousProfilerMaxFiles = 10
defaultStoreTransactions = true
)

type Config struct {
vm.Config

// Concurrency
AuthVerificationCores int `json:"authVerificationCores"`
RootGenerationCores int `json:"rootGenerationCores"`
TransactionExecutionCores int `json:"transactionExecutionCores"`
StateFetchConcurrency int `json:"stateFetchConcurrency"`

// Tracing
TraceEnabled bool `json:"traceEnabled"`
TraceSampleRate float64 `json:"traceSampleRate"`

// Profiling
ContinuousProfilerDir string `json:"continuousProfilerDir"` // "*" is replaced with rand int

// Streaming settings
StreamingBacklogSize int `json:"streamingBacklogSize"`

// Mempool
MempoolSize int `json:"mempoolSize"`
MempoolSponsorSize int `json:"mempoolSponsorSize"`
MempoolExemptSponsors []string `json:"mempoolExemptSponsors"`

// Misc
VerifyAuth bool `json:"verifyAuth"`
StoreTransactions bool `json:"storeTransactions"`
TestMode bool `json:"testMode"` // makes gossip/building manual
LogLevel logging.Level `json:"logLevel"`

// State Sync
StateSyncServerDelay time.Duration `json:"stateSyncServerDelay"` // for testing

loaded bool
nodeID ids.NodeID
parsedExemptSponsors []codec.Address
}

func New(nodeID ids.NodeID, b []byte) (*Config, error) {
c := &Config{nodeID: nodeID}
c.setDefault()
if len(b) > 0 {
if err := json.Unmarshal(b, c); err != nil {
return nil, fmt.Errorf("failed to unmarshal config %s: %w", string(b), err)
}
c.loaded = true
func New(b []byte) (*Config, error) {
c := &Config{
StoreTransactions: true,
LogLevel: logging.Info,
}

// Parse any exempt sponsors (usually used when a single account is
// broadcasting many txs at once)
c.parsedExemptSponsors = make([]codec.Address, len(c.MempoolExemptSponsors))
for i, sponsor := range c.MempoolExemptSponsors {
p, err := codec.ParseAddressBech32(consts.HRP, sponsor)
if err != nil {
if len(b) > 0 {
if err := json.Unmarshal(b, c); err != nil {
return nil, err
}
c.parsedExemptSponsors[i] = p
}
return c, nil
}

func (c *Config) setDefault() {
c.Config = vm.NewConfig()
c.LogLevel = logging.Info
c.AuthVerificationCores = c.Config.AuthVerificationCores
c.RootGenerationCores = c.Config.RootGenerationCores
c.TransactionExecutionCores = c.Config.TransactionExecutionCores
c.StateFetchConcurrency = c.Config.StateFetchConcurrency
c.MempoolSize = c.Config.MempoolSize
c.MempoolSponsorSize = c.Config.MempoolSponsorSize
c.StateSyncServerDelay = c.Config.StateSyncServerDelay
c.StreamingBacklogSize = c.Config.StreamingBacklogSize
c.VerifyAuth = c.Config.VerifyAuth
c.StoreTransactions = defaultStoreTransactions
}

func (c *Config) GetLogLevel() logging.Level { return c.LogLevel }
func (c *Config) GetTestMode() bool { return c.TestMode }
func (c *Config) GetAuthVerificationCores() int { return c.AuthVerificationCores }
func (c *Config) GetRootGenerationCores() int { return c.RootGenerationCores }
func (c *Config) GetTransactionExecutionCores() int { return c.TransactionExecutionCores }
func (c *Config) GetStateFetchConcurrency() int { return c.StateFetchConcurrency }
func (c *Config) GetMempoolSize() int { return c.MempoolSize }
func (c *Config) GetMempoolSponsorSize() int { return c.MempoolSponsorSize }
func (c *Config) GetMempoolExemptSponsors() []codec.Address { return c.parsedExemptSponsors }
func (c *Config) GetTraceConfig() *trace.Config {
return &trace.Config{
Enabled: c.TraceEnabled,
TraceSampleRate: c.TraceSampleRate,
AppName: consts.Name,
Agent: c.nodeID.String(),
Version: version.Version.String(),
}
}
func (c *Config) GetStateSyncServerDelay() time.Duration { return c.StateSyncServerDelay }
func (c *Config) GetStreamingBacklogSize() int { return c.StreamingBacklogSize }
func (c *Config) GetContinuousProfilerConfig() *profiler.Config {
if len(c.ContinuousProfilerDir) == 0 {
return &profiler.Config{Enabled: false}
}
// Replace all instances of "*" with nodeID. This is useful when
// running multiple instances of morpheusvm on the same machine.
c.ContinuousProfilerDir = strings.ReplaceAll(c.ContinuousProfilerDir, "*", c.nodeID.String())
return &profiler.Config{
Enabled: true,
Dir: c.ContinuousProfilerDir,
Freq: defaultContinuousProfilerFrequency,
MaxNumFiles: defaultContinuousProfilerMaxFiles,
}
return c, nil
}
func (c *Config) GetVerifyAuth() bool { return c.VerifyAuth }
func (c *Config) GetStoreTransactions() bool { return c.StoreTransactions }
func (c *Config) Loaded() bool { return c.loaded }
24 changes: 12 additions & 12 deletions examples/morpheusvm/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func (c *Controller) Initialize(
upgradeBytes []byte, // subnets to allow for AWM
configBytes []byte,
) (
vm.Config,
vm.Genesis,
builder.Builder,
gossiper.Gossiper,
Expand All @@ -76,20 +75,21 @@ func (c *Controller) Initialize(
var err error
c.metrics, err = newMetrics(gatherer)
if err != nil {
return vm.Config{}, nil, nil, nil, nil, nil, nil, nil, err
return nil, nil, nil, nil, nil, nil, nil, err
}

// Load config and genesis
c.config, err = config.New(c.snowCtx.NodeID, configBytes)
c.config, err = config.New(configBytes)
if err != nil {
return vm.Config{}, nil, nil, nil, nil, nil, nil, nil, err
return nil, nil, nil, nil, nil, nil, nil, err
}
c.snowCtx.Log.SetLevel(c.config.GetLogLevel())
snowCtx.Log.Info("initialized config", zap.Bool("loaded", c.config.Loaded()), zap.Any("contents", c.config))

c.snowCtx.Log.SetLevel(c.config.LogLevel)
snowCtx.Log.Info("initialized config", zap.Any("contents", c.config))

c.genesis, err = genesis.New(genesisBytes, upgradeBytes)
if err != nil {
return vm.Config{}, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf(
return nil, nil, nil, nil, nil, nil, nil, fmt.Errorf(
"unable to read genesis: %w",
err,
)
Expand All @@ -98,7 +98,7 @@ func (c *Controller) Initialize(

c.db, err = hstorage.New(pebble.NewDefaultConfig(), snowCtx.ChainDataDir, "db", gatherer)
if err != nil {
return vm.Config{}, nil, nil, nil, nil, nil, nil, nil, err
return nil, nil, nil, nil, nil, nil, nil, err
}

// Create handlers
Expand All @@ -111,7 +111,7 @@ func (c *Controller) Initialize(
rpc.NewJSONRPCServer(c),
)
if err != nil {
return vm.Config{}, nil, nil, nil, nil, nil, nil, nil, err
return nil, nil, nil, nil, nil, nil, nil, err
}
apis[rpc.JSONRPCEndpoint] = jsonRPCHandler

Expand All @@ -129,10 +129,10 @@ func (c *Controller) Initialize(
gcfg := gossiper.DefaultProposerConfig()
gossip, err = gossiper.NewProposer(inner, gcfg)
if err != nil {
return vm.Config{}, nil, nil, nil, nil, nil, nil, nil, err
return nil, nil, nil, nil, nil, nil, nil, err
}
}
return c.config.Config, c.genesis, build, gossip, apis, consts.ActionRegistry, consts.AuthRegistry, auth.Engines(), nil
return c.genesis, build, gossip, apis, consts.ActionRegistry, consts.AuthRegistry, auth.Engines(), nil
}

func (c *Controller) Rules(t int64) chain.Rules {
Expand All @@ -151,7 +151,7 @@ func (c *Controller) Accepted(ctx context.Context, blk *chain.StatelessBlock) er
results := blk.Results()
for i, tx := range blk.Txs {
result := results[i]
if c.config.GetStoreTransactions() {
if c.config.StoreTransactions {
err := storage.StoreTransaction(
ctx,
batch,
Expand Down
8 changes: 7 additions & 1 deletion examples/morpheusvm/tests/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,13 @@ var _ = ginkgo.BeforeSuite(func() {
genesisBytes,
nil,
[]byte(
`{"parallelism":3, "testMode":true, "logLevel":"debug"}`,
`{
"config": {
"parallelism":3,
joshua-kim marked this conversation as resolved.
Show resolved Hide resolved
"testMode":true,
"logLevel":"debug"
}
}`,
),
toEngine,
nil,
Expand Down
Loading
Loading