-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
154 lines (134 loc) · 5.55 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (C) 2024, AllianceBlock. All rights reserved.
// See the file LICENSE for licensing terms.
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/config"
"github.com/ava-labs/hypersdk/gossiper"
"github.com/ava-labs/hypersdk/trace"
"github.com/ava-labs/hypersdk/vm"
nconsts "github.com/nuklai/nuklaivm/consts"
"github.com/nuklai/nuklaivm/version"
)
var _ vm.Config = (*Config)(nil)
const (
defaultContinuousProfilerFrequency = 1 * time.Minute
defaultContinuousProfilerMaxFiles = 10
defaultStoreTransactions = true
)
type Config struct {
*config.Config
// Concurrency
AuthVerificationCores int `json:"authVerificationCores"`
RootGenerationCores int `json:"rootGenerationCores"`
TransactionExecutionCores int `json:"transactionExecutionCores"`
// Gossip
GossipMaxSize int `json:"gossipMaxSize"`
GossipProposerDiff int `json:"gossipProposerDiff"`
GossipProposerDepth int `json:"gossipProposerDepth"`
NoGossipBuilderDiff int `json:"noGossipBuilderDiff"`
VerifyTimeout int64 `json:"verifyTimeout"`
// 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
}
// 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(nconsts.HRP, sponsor)
if err != nil {
return nil, err
}
c.parsedExemptSponsors[i] = p
}
return c, nil
}
func (c *Config) setDefault() {
c.LogLevel = c.Config.GetLogLevel()
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.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.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) 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: nconsts.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 nuklaivm 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,
}
}
func (c *Config) GetVerifyAuth() bool { return c.VerifyAuth }
func (c *Config) GetStoreTransactions() bool { return c.StoreTransactions }
func (c *Config) Loaded() bool { return c.loaded }