Skip to content

Commit

Permalink
Refactor config interface into value struct (#1111)
Browse files Browse the repository at this point in the history
* remove Config interface

Signed-off-by: Joshua Kim <20001595+joshua-kim@users.noreply.github.com>

* nit

Signed-off-by: Joshua Kim <20001595+joshua-kim@users.noreply.github.com>

* nit

Signed-off-by: Joshua Kim <20001595+joshua-kim@users.noreply.github.com>

* nit

Signed-off-by: Joshua Kim <20001595+joshua-kim@users.noreply.github.com>

* nit

Signed-off-by: Joshua Kim <20001595+joshua-kim@users.noreply.github.com>

* nit

Signed-off-by: Joshua Kim <20001595+joshua-kim@users.noreply.github.com>

* nit

Signed-off-by: Joshua Kim <20001595+joshua-kim@users.noreply.github.com>

* nit

Signed-off-by: Joshua Kim <20001595+joshua-kim@users.noreply.github.com>

---------

Signed-off-by: Joshua Kim <20001595+joshua-kim@users.noreply.github.com>
  • Loading branch information
joshua-kim authored Jul 15, 2024
1 parent 72386fb commit 8f7baa7
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 158 deletions.
51 changes: 0 additions & 51 deletions config/config.go

This file was deleted.

26 changes: 12 additions & 14 deletions examples/morpheusvm/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,20 @@ import (
"github.com/ava-labs/avalanchego/utils/profiler"

"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/config"
"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"
)

var _ vm.Config = (*Config)(nil)

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

type Config struct {
*config.Config
vm.Config

// Concurrency
AuthVerificationCores int `json:"authVerificationCores"`
Expand Down Expand Up @@ -91,16 +88,17 @@ func New(nodeID ids.NodeID, b []byte) (*Config, error) {
}

func (c *Config) setDefault() {
c.LogLevel = c.Config.GetLogLevel()
c.AuthVerificationCores = c.Config.GetAuthVerificationCores()
c.RootGenerationCores = c.Config.GetRootGenerationCores()
c.TransactionExecutionCores = c.Config.GetTransactionExecutionCores()
c.StateFetchConcurrency = c.Config.GetStateFetchConcurrency()
c.MempoolSize = c.Config.GetMempoolSize()
c.MempoolSponsorSize = c.Config.GetMempoolSponsorSize()
c.StateSyncServerDelay = c.Config.GetStateSyncServerDelay()
c.StreamingBacklogSize = c.Config.GetStreamingBacklogSize()
c.VerifyAuth = c.Config.GetVerifyAuth()
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
}

Expand Down
14 changes: 7 additions & 7 deletions examples/morpheusvm/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,20 @@ func (c *Controller) Initialize(
var err error
c.metrics, err = newMetrics(gatherer)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, err
return vm.Config{}, nil, nil, nil, nil, nil, nil, nil, err
}

// Load config and genesis
c.config, err = config.New(c.snowCtx.NodeID, configBytes)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, err
return vm.Config{}, 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.genesis, err = genesis.New(genesisBytes, upgradeBytes)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf(
return vm.Config{}, 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 nil, nil, nil, nil, nil, nil, nil, nil, err
return vm.Config{}, 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 nil, nil, nil, nil, nil, nil, nil, nil, err
return vm.Config{}, 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 nil, nil, nil, nil, nil, nil, nil, nil, err
return vm.Config{}, nil, nil, nil, nil, nil, nil, nil, err
}
}
return c.config, c.genesis, build, gossip, apis, consts.ActionRegistry, consts.AuthRegistry, auth.Engines(), nil
return c.config.Config, c.genesis, build, gossip, apis, consts.ActionRegistry, consts.AuthRegistry, auth.Engines(), nil
}

func (c *Controller) Rules(t int64) chain.Rules {
Expand Down
26 changes: 12 additions & 14 deletions examples/tokenvm/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ import (
"github.com/ava-labs/avalanchego/utils/profiler"

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

var _ vm.Config = (*Config)(nil)

const (
defaultContinuousProfilerFrequency = 1 * time.Minute
defaultContinuousProfilerMaxFiles = 10
Expand All @@ -32,7 +29,7 @@ const (
)

type Config struct {
*config.Config
vm.Config

// Concurrency
AuthVerificationCores int `json:"authVerificationCores"`
Expand Down Expand Up @@ -106,22 +103,23 @@ func New(nodeID ids.NodeID, b []byte) (*Config, error) {
}

func (c *Config) setDefault() {
c.LogLevel = c.Config.GetLogLevel()
c.Config = vm.NewConfig()
c.LogLevel = logging.Info
gcfg := gossiper.DefaultProposerConfig()
c.GossipMaxSize = gcfg.GossipMaxSize
c.GossipProposerDiff = gcfg.GossipProposerDiff
c.GossipProposerDepth = gcfg.GossipProposerDepth
c.NoGossipBuilderDiff = gcfg.NoGossipBuilderDiff
c.VerifyTimeout = gcfg.VerifyTimeout
c.AuthVerificationCores = c.Config.GetAuthVerificationCores()
c.RootGenerationCores = c.Config.GetRootGenerationCores()
c.TransactionExecutionCores = c.Config.GetTransactionExecutionCores()
c.StateFetchConcurrency = c.Config.GetStateFetchConcurrency()
c.MempoolSize = c.Config.GetMempoolSize()
c.MempoolSponsorSize = c.Config.GetMempoolSponsorSize()
c.StateSyncServerDelay = c.Config.GetStateSyncServerDelay()
c.StreamingBacklogSize = c.Config.GetStreamingBacklogSize()
c.VerifyAuth = c.Config.GetVerifyAuth()
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
c.MaxOrdersPerPair = defaultMaxOrdersPerPair
}
Expand Down
14 changes: 7 additions & 7 deletions examples/tokenvm/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,20 @@ func (c *Controller) Initialize(
var err error
c.metrics, err = newMetrics(gatherer)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, err
return vm.Config{}, nil, nil, nil, nil, nil, nil, nil, err
}

// Load config and genesis
c.config, err = config.New(c.snowCtx.NodeID, configBytes)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, err
return vm.Config{}, 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.genesis, err = genesis.New(genesisBytes, upgradeBytes)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf(
return vm.Config{}, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf(
"unable to read genesis: %w",
err,
)
Expand All @@ -102,7 +102,7 @@ func (c *Controller) Initialize(
// Create DBs
c.db, err = hstorage.New(pebble.NewDefaultConfig(), snowCtx.ChainDataDir, "db", gatherer)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, err
return vm.Config{}, nil, nil, nil, nil, nil, nil, nil, err
}

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

Expand All @@ -138,13 +138,13 @@ func (c *Controller) Initialize(
gcfg.VerifyTimeout = c.config.VerifyTimeout
gossip, err = gossiper.NewProposer(inner, gcfg)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, err
return vm.Config{}, nil, nil, nil, nil, nil, nil, nil, err
}
}

// Initialize order book used to track all open orders
c.orderBook = orderbook.New(c, c.config.TrackedPairs, c.config.MaxOrdersPerPair)
return c.config, c.genesis, build, gossip, apis, consts.ActionRegistry, consts.AuthRegistry, auth.Engines(), nil
return c.config.Config, c.genesis, build, gossip, apis, consts.ActionRegistry, consts.AuthRegistry, auth.Engines(), nil
}

func (c *Controller) Rules(t int64) chain.Rules {
Expand Down
89 changes: 61 additions & 28 deletions vm/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils/profiler"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/x/merkledb"

"github.com/ava-labs/hypersdk/builder"
Expand All @@ -25,34 +26,66 @@ import (

type Handlers map[string]http.Handler

type Config interface {
GetTraceConfig() *trace.Config
GetMempoolSize() int
GetAuthVerificationCores() int
GetVerifyAuth() bool
GetRootGenerationCores() int
GetTransactionExecutionCores() int
GetStateFetchConcurrency() int
GetMempoolSponsorSize() int
GetMempoolExemptSponsors() []codec.Address
GetStreamingBacklogSize() int
GetStateHistoryLength() int // how many roots back of data to keep to serve state queries
GetIntermediateNodeCacheSize() int // how many bytes to keep in intermediate cache
GetStateIntermediateWriteBufferSize() int // how many bytes to keep unwritten in intermediate cache
GetStateIntermediateWriteBatchSize() int // how many bytes to write from intermediate cache at once
GetValueNodeCacheSize() int // how many bytes to keep in value cache
GetAcceptorSize() int // how far back we can fall in processing accepted blocks
GetStateSyncParallelism() int
GetStateSyncMinBlocks() uint64
GetStateSyncServerDelay() time.Duration
GetParsedBlockCacheSize() int
GetAcceptedBlockWindow() int
GetAcceptedBlockWindowCache() int
GetContinuousProfilerConfig() *profiler.Config
GetTargetBuildDuration() time.Duration
GetProcessingBuildSkip() int
GetTargetGossipDuration() time.Duration
GetBlockCompactionFrequency() int
type Config struct {
TraceConfig trace.Config
MempoolSize int
AuthVerificationCores int
VerifyAuth bool
RootGenerationCores int
TransactionExecutionCores int
StateFetchConcurrency int
MempoolSponsorSize int
MempoolExemptSponsors []codec.Address
StreamingBacklogSize int
StateHistoryLength int // how many roots back of data to keep to serve state queries
IntermediateNodeCacheSize int // how many bytes to keep in intermediate cache
StateIntermediateWriteBufferSize int // how many bytes to keep unwritten in intermediate cache
StateIntermediateWriteBatchSize int // how many bytes to write from intermediate cache at once
ValueNodeCacheSize int // how many bytes to keep in value cache
AcceptorSize int // how far back we can fall in processing accepted blocks
StateSyncParallelism int
StateSyncMinBlocks uint64
StateSyncServerDelay time.Duration
ParsedBlockCacheSize int
AcceptedBlockWindow int
AcceptedBlockWindowCache int
ContinuousProfilerConfig profiler.Config
TargetBuildDuration time.Duration
ProcessingBuildSkip int
TargetGossipDuration time.Duration
BlockCompactionFrequency int
}

func NewConfig() Config {
return Config{
TraceConfig: trace.Config{Enabled: false},
MempoolSize: 2_048,
AuthVerificationCores: 1,
VerifyAuth: true,
RootGenerationCores: 1,
TransactionExecutionCores: 1,
StateFetchConcurrency: 1,
MempoolSponsorSize: 32,
MempoolExemptSponsors: nil,
StreamingBacklogSize: 1_024,
StateHistoryLength: 256,
IntermediateNodeCacheSize: 4 * units.GiB,
StateIntermediateWriteBufferSize: 32 * units.MiB,
StateIntermediateWriteBatchSize: 4 * units.MiB,
ValueNodeCacheSize: 2 * units.GiB,
AcceptorSize: 64,
StateSyncParallelism: 4,
StateSyncMinBlocks: 768, // set to max int for archive nodes to ensure no skips
StateSyncServerDelay: 0, // used for testing
ParsedBlockCacheSize: 128,
AcceptedBlockWindow: 50_000, // ~3.5hr with 250ms block time (100GB at 2MB)
AcceptedBlockWindowCache: 128, // 256MB at 2MB blocks
ContinuousProfilerConfig: profiler.Config{Enabled: false},
TargetBuildDuration: 100 * time.Millisecond,
ProcessingBuildSkip: 16,
TargetGossipDuration: 20 * time.Millisecond,
BlockCompactionFrequency: 32, // 64 MB of deletion if 2 MB blocks
}
}

type Genesis interface {
Expand Down
2 changes: 1 addition & 1 deletion vm/network_state_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s *StateSyncHandler) AppRequest(
deadline time.Time,
request []byte,
) error {
if delay := s.vm.config.GetStateSyncServerDelay(); delay > 0 {
if delay := s.vm.config.StateSyncServerDelay; delay > 0 {
time.Sleep(delay)
}
return s.vm.stateSyncNetworkServer.AppRequest(ctx, nodeID, requestID, deadline, request)
Expand Down
Loading

0 comments on commit 8f7baa7

Please sign in to comment.