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

Replace ChainSwitchingThreshold option in protocol with ProtocolParameter #613

Merged
merged 1 commit into from
Dec 6, 2023
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
5 changes: 3 additions & 2 deletions pkg/protocol/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func newChains(protocol *Protocol) *Chains {

shutdown := lo.Batch(
c.initLogger(protocol.NewChildLogger("Chains")),
c.initChainSwitching(protocol.Options.ChainSwitchingThreshold),
c.initChainSwitching(),

protocol.Constructed.WithNonEmptyValue(func(_ bool) (shutdown func()) {
return c.deriveLatestSeenSlot(protocol)
Expand Down Expand Up @@ -92,7 +92,7 @@ func (c *Chains) initLogger(logger log.Logger, shutdownLogger func()) (shutdown
}

// initChainSwitching initializes the chain switching logic.
func (c *Chains) initChainSwitching(chainSwitchingThreshold iotago.SlotIndex) (shutdown func()) {
func (c *Chains) initChainSwitching() (shutdown func()) {
mainChain := c.newChain()
mainChain.StartEngine.Set(true)

Expand All @@ -102,6 +102,7 @@ func (c *Chains) initChainSwitching(chainSwitchingThreshold iotago.SlotIndex) (s
forkingPointBelowChainSwitchingThreshold := func(chain *Chain) func(_ *Commitment, latestCommitment *Commitment) bool {
return func(_ *Commitment, latestCommitment *Commitment) bool {
forkingPoint := chain.ForkingPoint.Get()
chainSwitchingThreshold := iotago.SlotIndex(c.protocol.APIForSlot(latestCommitment.Slot()).ProtocolParameters().ChainSwitchingThreshold())

return forkingPoint != nil && latestCommitment != nil && (latestCommitment.ID().Slot()-forkingPoint.ID().Slot()) > chainSwitchingThreshold
}
Expand Down
14 changes: 1 addition & 13 deletions pkg/protocol/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ type Options struct {
// SnapshotPath is the path to the snapshot file that should be used to initialize the protocol.
SnapshotPath string

// ChainSwitchingThreshold is the threshold that defines how far away a heavier chain needs to be from its forking
// point to be considered for switching.
ChainSwitchingThreshold iotago.SlotIndex

// EngineOptions contains the options for the Engines.
EngineOptions []options.Option[engine.Engine]

Expand Down Expand Up @@ -120,8 +116,7 @@ type Options struct {
// NewDefaultOptions creates new default options instance for the Protocol.
func NewDefaultOptions() *Options {
return &Options{
BaseDirectory: "",
ChainSwitchingThreshold: 3,
BaseDirectory: "",

PreSolidFilterProvider: presolidblockfilter.NewProvider(),
PostSolidFilterProvider: postsolidblockfilter.NewProvider(),
Expand Down Expand Up @@ -157,13 +152,6 @@ func WithSnapshotPath(snapshot string) options.Option[Protocol] {
}
}

// WithChainSwitchingThreshold is an option for the Protocol that allows to set the chain switching threshold.
func WithChainSwitchingThreshold(threshold iotago.SlotIndex) options.Option[Protocol] {
return func(p *Protocol) {
p.Options.ChainSwitchingThreshold = threshold
}
}

// WithPreSolidFilterProvider is an option for the Protocol that allows to set the PreSolidFilterProvider.
func WithPreSolidFilterProvider(optsFilterProvider module.Provider[*engine.Engine, presolidfilter.PreSolidFilter]) options.Option[Protocol] {
return func(p *Protocol) {
Expand Down
Loading