Skip to content
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
8 changes: 8 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Config struct {
topologyConfig *topology.TopologyConfig
tracing bool
tracingStdout bool
devMode bool
}

// configPopulateNetworkMagic uses the named network (if specified) to determine the network magic value (if not specified)
Expand Down Expand Up @@ -260,3 +261,10 @@ func WithMempoolCapacity(capacity int64) ConfigOptionFunc {
c.mempoolCapacity = capacity
}
}

// WithDevMode enables development mode which prevents outbound connections.
func WithDevMode(devMode bool) ConfigOptionFunc {
return func(c *Config) {
c.devMode = devMode
}
}
4 changes: 4 additions & 0 deletions dingo.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ badgerCacheSize: 1073741824
# Transactions exceeding this limit will be rejected.
# Default: 1048576 (1 MB)
mempoolCapacity: 1048576

# Enable development mode which prevents outbound connections
# Default: false
devMode: false
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Config struct {
RelayPort uint ` yaml:"relayPort" envconfig:"port"`
UtxorpcPort uint `split_words:"true" yaml:"utxorpcPort"`
IntersectTip bool `split_words:"true" yaml:"intersectTip"`
DevMode bool `split_words:"true" yaml:"devMode"`
}

var globalConfig = &Config{
Expand All @@ -78,6 +79,7 @@ var globalConfig = &Config{
Topology: "",
TlsCertFilePath: "",
TlsKeyFilePath: "",
DevMode: false,
}

func LoadConfig(configFile string) (*Config, error) {
Expand Down Expand Up @@ -113,6 +115,7 @@ func LoadConfig(configFile string) (*Config, error) {
if err != nil {
return nil, fmt.Errorf("error processing environment: %+w", err)
}

_, err = LoadTopologyConfig()
if err != nil {
return nil, fmt.Errorf("error loading topology: %+w", err)
Expand Down
35 changes: 34 additions & 1 deletion internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"os"
"path/filepath"
"reflect"
"testing"
)
Expand All @@ -24,6 +25,7 @@ func resetGlobalConfig() {
Topology: "",
TlsCertFilePath: "",
TlsKeyFilePath: "",
DevMode: false,
}
}

Expand All @@ -48,7 +50,9 @@ tlsCertFilePath: "cert1.pem"
tlsKeyFilePath: "key1.pem"
`

tmpFile := "test-dingo.yaml"
tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "test-dingo.yaml")

err := os.WriteFile(tmpFile, []byte(yamlContent), 0644)
if err != nil {
t.Fatalf("failed to write config file: %v", err)
Expand All @@ -72,6 +76,7 @@ tlsKeyFilePath: "key1.pem"
Topology: "",
TlsCertFilePath: "cert1.pem",
TlsKeyFilePath: "key1.pem",
DevMode: false,
}

actual, err := LoadConfig(tmpFile)
Expand Down Expand Up @@ -114,6 +119,7 @@ func TestLoad_WithoutConfigFile_UsesDefaults(t *testing.T) {
Topology: "",
TlsCertFilePath: "",
TlsKeyFilePath: "",
DevMode: false,
}

if !reflect.DeepEqual(cfg, expected) {
Expand All @@ -124,3 +130,30 @@ func TestLoad_WithoutConfigFile_UsesDefaults(t *testing.T) {
)
}
}

func TestLoad_WithDevModeConfig(t *testing.T) {
resetGlobalConfig()

// Test with dev mode in config file
yamlContent := `
devMode: true
network: "preview"
`

tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "test-dev-mode.yaml")

err := os.WriteFile(tmpFile, []byte(yamlContent), 0644)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use a temp directory for this test file. This avoids the possibility of accidentally overwriting/removing a file that already exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made the changes and please review it.

if err != nil {
t.Fatalf("failed to write config file: %v", err)
}

cfg, err := LoadConfig(tmpFile)
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}

if !cfg.DevMode {
t.Errorf("expected DevMode to be true, got: %v", cfg.DevMode)
}
}
1 change: 1 addition & 0 deletions internal/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func Run(cfg *config.Config, logger *slog.Logger) error {
dingo.WithUtxorpcPort(cfg.UtxorpcPort),
dingo.WithUtxorpcTlsCertFilePath(cfg.TlsCertFilePath),
dingo.WithUtxorpcTlsKeyFilePath(cfg.TlsKeyFilePath),
dingo.WithDevMode(cfg.DevMode),
// Enable metrics with default prometheus registry
dingo.WithPrometheusRegistry(prometheus.DefaultRegisterer),
// TODO: make this configurable (#387)
Expand Down
7 changes: 4 additions & 3 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,10 @@ func (n *Node) Run() error {
// Configure peer governor
n.peerGov = peergov.NewPeerGovernor(
peergov.PeerGovernorConfig{
Logger: n.config.logger,
EventBus: n.eventBus,
ConnManager: n.connManager,
Logger: n.config.logger,
EventBus: n.eventBus,
ConnManager: n.connManager,
DisableOutbound: n.config.devMode,
},
)
n.eventBus.SubscribeFunc(
Expand Down
17 changes: 14 additions & 3 deletions peergov/peergov.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ type PeerGovernor struct {
}

type PeerGovernorConfig struct {
Logger *slog.Logger
EventBus *event.EventBus
ConnManager *connmanager.ConnectionManager
Logger *slog.Logger
EventBus *event.EventBus
ConnManager *connmanager.ConnectionManager
DisableOutbound bool
}

func NewPeerGovernor(cfg PeerGovernorConfig) *PeerGovernor {
Expand Down Expand Up @@ -182,10 +183,20 @@ func (p *PeerGovernor) peerIndexByConnId(connId ouroboros.ConnectionId) int {
}

func (p *PeerGovernor) startOutboundConnections() {
// Skip outbound connections if disabled
if p.config.DisableOutbound {
p.config.Logger.Info(
"outbound connections disabled, skipping outbound connections",
"role", "client",
)
return
}

p.config.Logger.Debug(
"starting connections",
"role", "client",
)

for _, tmpPeer := range p.peers {
go p.createOutboundConnection(tmpPeer)
}
Expand Down
Loading