github.com/iotexproject/iotex-core@v1.14.1-rc1/consensus/consensusfsm/consensus_ttl.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package consensusfsm
     7  
     8  import (
     9  	"time"
    10  
    11  	"github.com/iotexproject/iotex-core/blockchain/genesis"
    12  )
    13  
    14  // DefaultDardanellesUpgradeConfig is the default config for dardanelles upgrade
    15  var DefaultDardanellesUpgradeConfig = DardanellesUpgrade{
    16  	UnmatchedEventTTL:            2 * time.Second,
    17  	UnmatchedEventInterval:       100 * time.Millisecond,
    18  	AcceptBlockTTL:               2 * time.Second,
    19  	AcceptProposalEndorsementTTL: time.Second,
    20  	AcceptLockEndorsementTTL:     time.Second,
    21  	CommitTTL:                    time.Second,
    22  	BlockInterval:                5 * time.Second,
    23  	Delay:                        2 * time.Second,
    24  }
    25  
    26  type (
    27  	// DardanellesUpgrade is the config for dardanelles upgrade
    28  	DardanellesUpgrade struct {
    29  		UnmatchedEventTTL            time.Duration `yaml:"unmatchedEventTTL"`
    30  		UnmatchedEventInterval       time.Duration `yaml:"unmatchedEventInterval"`
    31  		AcceptBlockTTL               time.Duration `yaml:"acceptBlockTTL"`
    32  		AcceptProposalEndorsementTTL time.Duration `yaml:"acceptProposalEndorsementTTL"`
    33  		AcceptLockEndorsementTTL     time.Duration `yaml:"acceptLockEndorsementTTL"`
    34  		CommitTTL                    time.Duration `yaml:"commitTTL"`
    35  		BlockInterval                time.Duration `yaml:"blockInterval"`
    36  		Delay                        time.Duration `yaml:"delay"`
    37  	}
    38  
    39  	// ConsensusTiming defines a set of time durations used in fsm and event queue size
    40  	ConsensusTiming struct {
    41  		EventChanSize                uint          `yaml:"eventChanSize"`
    42  		UnmatchedEventTTL            time.Duration `yaml:"unmatchedEventTTL"`
    43  		UnmatchedEventInterval       time.Duration `yaml:"unmatchedEventInterval"`
    44  		AcceptBlockTTL               time.Duration `yaml:"acceptBlockTTL"`
    45  		AcceptProposalEndorsementTTL time.Duration `yaml:"acceptProposalEndorsementTTL"`
    46  		AcceptLockEndorsementTTL     time.Duration `yaml:"acceptLockEndorsementTTL"`
    47  		CommitTTL                    time.Duration `yaml:"commitTTL"`
    48  	}
    49  
    50  	// ConsensusConfig defines a set of time durations used in fsm
    51  	ConsensusConfig interface {
    52  		EventChanSize() uint
    53  		UnmatchedEventTTL(uint64) time.Duration
    54  		UnmatchedEventInterval(uint64) time.Duration
    55  		AcceptBlockTTL(uint64) time.Duration
    56  		AcceptProposalEndorsementTTL(uint64) time.Duration
    57  		AcceptLockEndorsementTTL(uint64) time.Duration
    58  		CommitTTL(uint64) time.Duration
    59  		BlockInterval(uint64) time.Duration
    60  		Delay(uint64) time.Duration
    61  	}
    62  
    63  	// config implements ConsensusConfig
    64  	consensusCfg struct {
    65  		cfg               ConsensusTiming
    66  		dardanelles       DardanellesUpgrade
    67  		dardanellesHeight uint64
    68  		blockInterval     time.Duration
    69  		delay             time.Duration
    70  	}
    71  )
    72  
    73  // NewConsensusConfig creates a ConsensusConfig out of config.
    74  func NewConsensusConfig(timing ConsensusTiming, dardanelles DardanellesUpgrade, g genesis.Genesis, delay time.Duration) ConsensusConfig {
    75  	return &consensusCfg{
    76  		cfg:               timing,
    77  		dardanelles:       dardanelles,
    78  		dardanellesHeight: g.DardanellesBlockHeight,
    79  		blockInterval:     g.Blockchain.BlockInterval,
    80  		delay:             delay,
    81  	}
    82  }
    83  
    84  func (c *consensusCfg) isDardanelles(height uint64) bool {
    85  	return height >= c.dardanellesHeight
    86  }
    87  
    88  func (c *consensusCfg) EventChanSize() uint {
    89  	return c.cfg.EventChanSize
    90  }
    91  
    92  func (c *consensusCfg) UnmatchedEventTTL(height uint64) time.Duration {
    93  	if c.isDardanelles(height) {
    94  		return c.dardanelles.UnmatchedEventTTL
    95  	}
    96  	return c.cfg.UnmatchedEventTTL
    97  }
    98  
    99  func (c *consensusCfg) UnmatchedEventInterval(height uint64) time.Duration {
   100  	if c.isDardanelles(height) {
   101  		return c.dardanelles.UnmatchedEventInterval
   102  	}
   103  	return c.cfg.UnmatchedEventInterval
   104  }
   105  
   106  func (c *consensusCfg) AcceptBlockTTL(height uint64) time.Duration {
   107  	if c.isDardanelles(height) {
   108  		return c.dardanelles.AcceptBlockTTL
   109  	}
   110  	return c.cfg.AcceptBlockTTL
   111  }
   112  
   113  func (c *consensusCfg) AcceptProposalEndorsementTTL(height uint64) time.Duration {
   114  	if c.isDardanelles(height) {
   115  		return c.dardanelles.AcceptProposalEndorsementTTL
   116  	}
   117  	return c.cfg.AcceptProposalEndorsementTTL
   118  }
   119  
   120  func (c *consensusCfg) AcceptLockEndorsementTTL(height uint64) time.Duration {
   121  	if c.isDardanelles(height) {
   122  		return c.dardanelles.AcceptLockEndorsementTTL
   123  	}
   124  	return c.cfg.AcceptLockEndorsementTTL
   125  }
   126  
   127  func (c *consensusCfg) CommitTTL(height uint64) time.Duration {
   128  	if c.isDardanelles(height) {
   129  		return c.dardanelles.CommitTTL
   130  	}
   131  	return c.cfg.CommitTTL
   132  }
   133  
   134  func (c *consensusCfg) BlockInterval(height uint64) time.Duration {
   135  	if c.isDardanelles(height) {
   136  		return c.dardanelles.BlockInterval
   137  	}
   138  	return c.blockInterval
   139  }
   140  
   141  func (c *consensusCfg) Delay(height uint64) time.Duration {
   142  	if c.isDardanelles(height) {
   143  		return c.dardanelles.Delay
   144  	}
   145  	return c.delay
   146  }