-
Notifications
You must be signed in to change notification settings - Fork 964
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(share)!: add functional options to share pkg (#1798)
## Overview This PR introduces functional options to the share package and deprecates usage of default values directly in code, in favor of using parameterized values. ## Breaking This PR breaks the configuration. The on-disk configuration becomes (_note that `Share.Availability` is only available for light nodes_): ``` [Share] [Share.Availability] SampleAmount = 16 [Share.Discovery] PeersLimit = 5 AdvertiseInterval = "8h0m0s" ``` ## Checklist - [x] New and updated code has appropriate documentation - [x] New and updated code has new and/or updated testing - [x] Required CI checks are passing - [x] Visual proof for any user facing features like CLI or documentation updates - [x] Linked issues closed with keywords
- Loading branch information
Showing
16 changed files
with
235 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,69 @@ | ||
package share | ||
|
||
import ( | ||
disc "github.com/celestiaorg/celestia-node/share/availability/discovery" | ||
"fmt" | ||
|
||
"github.com/celestiaorg/celestia-node/nodebuilder/node" | ||
"github.com/celestiaorg/celestia-node/share/availability/discovery" | ||
"github.com/celestiaorg/celestia-node/share/availability/light" | ||
"github.com/celestiaorg/celestia-node/share/p2p/peers" | ||
"github.com/celestiaorg/celestia-node/share/p2p/shrexeds" | ||
"github.com/celestiaorg/celestia-node/share/p2p/shrexnd" | ||
) | ||
|
||
// TODO: some params are pointers and other are not, Let's fix this. | ||
type Config struct { | ||
// UseShareExchange is a flag toggling the usage of shrex protocols for blocksync. | ||
UseShareExchange bool | ||
// ShrExEDSParams sets shrexeds client and server configuration parameters | ||
ShrExEDSParams *shrexeds.Parameters | ||
// ShrExNDParams sets shrexnd client and server configuration parameters | ||
ShrExNDParams *shrexnd.Parameters | ||
// PeerManagerParams sets peer-manager configuration parameters | ||
PeerManagerParams peers.Parameters | ||
// Discovery sets peer discovery configuration parameters. | ||
Discovery disc.Parameters | ||
|
||
LightAvailability light.Parameters `toml:",omitempty"` | ||
Discovery discovery.Parameters | ||
} | ||
|
||
func DefaultConfig() Config { | ||
return Config{ | ||
UseShareExchange: true, | ||
func DefaultConfig(tp node.Type) Config { | ||
cfg := Config{ | ||
Discovery: discovery.DefaultParameters(), | ||
ShrExEDSParams: shrexeds.DefaultParameters(), | ||
ShrExNDParams: shrexnd.DefaultParameters(), | ||
UseShareExchange: true, | ||
PeerManagerParams: peers.DefaultParameters(), | ||
Discovery: disc.DefaultParameters(), | ||
} | ||
|
||
if tp == node.Light { | ||
cfg.LightAvailability = light.DefaultParameters() | ||
} | ||
|
||
return cfg | ||
} | ||
|
||
// Validate performs basic validation of the config. | ||
func (cfg *Config) Validate() error { | ||
func (cfg *Config) Validate(tp node.Type) error { | ||
if tp == node.Light { | ||
if err := cfg.LightAvailability.Validate(); err != nil { | ||
return fmt.Errorf("nodebuilder/share: %w", err) | ||
} | ||
} | ||
|
||
if err := cfg.Discovery.Validate(); err != nil { | ||
return fmt.Errorf("nodebuilder/share: %w", err) | ||
} | ||
|
||
if err := cfg.ShrExNDParams.Validate(); err != nil { | ||
return err | ||
return fmt.Errorf("nodebuilder/share: %w", err) | ||
} | ||
|
||
if err := cfg.ShrExEDSParams.Validate(); err != nil { | ||
return err | ||
return fmt.Errorf("nodebuilder/share: %w", err) | ||
} | ||
return cfg.PeerManagerParams.Validate() | ||
|
||
if err := cfg.PeerManagerParams.Validate(); err != nil { | ||
return fmt.Errorf("nodebuilder/share: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package discovery | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// Parameters is the set of Parameters that must be configured for the Discovery module | ||
type Parameters struct { | ||
// PeersLimit defines the soft limit of FNs to connect to via discovery. | ||
// Set 0 to disable. | ||
PeersLimit uint | ||
// AdvertiseInterval is a interval between advertising sessions. | ||
// Set -1 to disable. | ||
// NOTE: only full and bridge can advertise themselves. | ||
AdvertiseInterval time.Duration | ||
} | ||
|
||
// Option is a function that configures Discovery Parameters | ||
type Option func(*Parameters) | ||
|
||
// DefaultParameters returns the default Parameters' configuration values | ||
// for the Discovery module | ||
func DefaultParameters() Parameters { | ||
return Parameters{ | ||
PeersLimit: 5, | ||
// based on https://github.com/libp2p/go-libp2p-kad-dht/pull/793 | ||
AdvertiseInterval: time.Hour * 22, | ||
} | ||
} | ||
|
||
// Validate validates the values in Parameters | ||
func (p *Parameters) Validate() error { | ||
if p.AdvertiseInterval <= 0 { | ||
return fmt.Errorf( | ||
"discovery: invalid option: value AdvertiseInterval %s, %s", | ||
"is 0 or negative.", | ||
"value must be positive", | ||
) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// WithPeersLimit is a functional option that Discovery | ||
// uses to set the PeersLimit configuration param | ||
func WithPeersLimit(peersLimit uint) Option { | ||
return func(p *Parameters) { | ||
p.PeersLimit = peersLimit | ||
} | ||
} | ||
|
||
// WithAdvertiseInterval is a functional option that Discovery | ||
// uses to set the AdvertiseInterval configuration param | ||
func WithAdvertiseInterval(advInterval time.Duration) Option { | ||
return func(p *Parameters) { | ||
p.AdvertiseInterval = advInterval | ||
} | ||
} |
Oops, something went wrong.