You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.8 KiB

types: add default values for the synchrony parameters (#7788) ## Summary This pull request adds a default set of values to the new Synchrony parameters. These values were chosen by after observation of three live networks: Emoney, Osmosis, and the Cosmos Hub. For the default Precision value, `505ms` was selected. The reasoning for this is summarized in https://github.com/tendermint/tendermint/issues/7724 For each observed chain, an experimental Message Delay was collected over a 24 hour period and an average over this period was calculated using this data. Values over 10s were considered outliers and treated separately for the average since the majority of observations were far below 10s. The message delay was calculated both for the quorum and the 'full' prevote. Description of the technique for collecting the experimental values can found in #7202. This value is calculated only using timestamps given by processes on the network, so large variation in values is almost certainly due to clock skew among the validator set. `12s` is proposed for the default MessageDelay value. This value would easily accomodates all non-outlier values, allowing even E-money's 4.25s value to be valid. This would also allow some validators with skewed clocks to still participate without allowing for huge variation in the timestamps produced by the network. Additionally, for the currently listed use-cases of PBTS, such as unbonding period, and light client trust period, the current bounds for these are in weeks. Adding a few seconds of tolerance by default is therefore unlikely to have serious side-effects. ## Data ### Cosmos Hub Observation Period: 2022-02-03 20:22-2022-02-04 20:22 Avg Full Prevote Message Delay: 1.27s Outliers: 11s,13s,50s,106s,144s Total Outlier Heights: 86 Avg Quorum Prevote Message Delay: .77s Outliers: 10s,14s,107s,144s Total Outlier Heights: 617 Total heights: 11528 ### Osmosis Observation Period: 2022-01-29 20:26-2022-01-28 20:26 Avg Quorum Prevote Message Delay: .46s Outliers: 21s,50s Total Outlier Heights: 26 NOTE: During the observation period, a 'full' prevote was not observed. Total heights: 13983 ### E-Money Observation Period: 2022-02-07 04:29-2022-02-08 04:29 Avg Full Prevote Message Delay: 4.25s Outliers: 12s,15s,39s Total Outlier Heights: 128 Avg Quorum Prevote Message Delay: .20s Outliers: 28s Total Outlier Heights: 15 Total heights: 3791
2 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "os"
  8. "time"
  9. "github.com/tendermint/tendermint/crypto"
  10. "github.com/tendermint/tendermint/internal/jsontypes"
  11. tmbytes "github.com/tendermint/tendermint/libs/bytes"
  12. tmtime "github.com/tendermint/tendermint/libs/time"
  13. )
  14. const (
  15. // MaxChainIDLen is a maximum length of the chain ID.
  16. MaxChainIDLen = 50
  17. )
  18. //------------------------------------------------------------
  19. // core types for a genesis definition
  20. // NOTE: any changes to the genesis definition should
  21. // be reflected in the documentation:
  22. // docs/tendermint-core/using-tendermint.md
  23. // GenesisValidator is an initial validator.
  24. type GenesisValidator struct {
  25. Address Address
  26. PubKey crypto.PubKey
  27. Power int64
  28. Name string
  29. }
  30. type genesisValidatorJSON struct {
  31. Address Address `json:"address"`
  32. PubKey json.RawMessage `json:"pub_key"`
  33. Power int64 `json:"power,string"`
  34. Name string `json:"name"`
  35. }
  36. func (g GenesisValidator) MarshalJSON() ([]byte, error) {
  37. pk, err := jsontypes.Marshal(g.PubKey)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return json.Marshal(genesisValidatorJSON{
  42. Address: g.Address, PubKey: pk, Power: g.Power, Name: g.Name,
  43. })
  44. }
  45. func (g *GenesisValidator) UnmarshalJSON(data []byte) error {
  46. var gv genesisValidatorJSON
  47. if err := json.Unmarshal(data, &gv); err != nil {
  48. return err
  49. }
  50. if err := jsontypes.Unmarshal(gv.PubKey, &g.PubKey); err != nil {
  51. return err
  52. }
  53. g.Address = gv.Address
  54. g.Power = gv.Power
  55. g.Name = gv.Name
  56. return nil
  57. }
  58. // GenesisDoc defines the initial conditions for a tendermint blockchain, in particular its validator set.
  59. type GenesisDoc struct {
  60. GenesisTime time.Time `json:"genesis_time"`
  61. ChainID string `json:"chain_id"`
  62. InitialHeight int64 `json:"initial_height,string"`
  63. ConsensusParams *ConsensusParams `json:"consensus_params,omitempty"`
  64. Validators []GenesisValidator `json:"validators,omitempty"`
  65. AppHash tmbytes.HexBytes `json:"app_hash"`
  66. AppState json.RawMessage `json:"app_state,omitempty"`
  67. }
  68. // SaveAs is a utility method for saving GenensisDoc as a JSON file.
  69. func (genDoc *GenesisDoc) SaveAs(file string) error {
  70. genDocBytes, err := json.MarshalIndent(genDoc, "", " ")
  71. if err != nil {
  72. return err
  73. }
  74. return os.WriteFile(file, genDocBytes, 0644) // nolint:gosec
  75. }
  76. // ValidatorHash returns the hash of the validator set contained in the GenesisDoc
  77. func (genDoc *GenesisDoc) ValidatorHash() []byte {
  78. vals := make([]*Validator, len(genDoc.Validators))
  79. for i, v := range genDoc.Validators {
  80. vals[i] = NewValidator(v.PubKey, v.Power)
  81. }
  82. vset := NewValidatorSet(vals)
  83. return vset.Hash()
  84. }
  85. // ValidateAndComplete checks that all necessary fields are present
  86. // and fills in defaults for optional fields left empty
  87. func (genDoc *GenesisDoc) ValidateAndComplete() error {
  88. if genDoc.ChainID == "" {
  89. return errors.New("genesis doc must include non-empty chain_id")
  90. }
  91. if len(genDoc.ChainID) > MaxChainIDLen {
  92. return fmt.Errorf("chain_id in genesis doc is too long (max: %d)", MaxChainIDLen)
  93. }
  94. if genDoc.InitialHeight < 0 {
  95. return fmt.Errorf("initial_height cannot be negative (got %v)", genDoc.InitialHeight)
  96. }
  97. if genDoc.InitialHeight == 0 {
  98. genDoc.InitialHeight = 1
  99. }
  100. if genDoc.ConsensusParams == nil {
  101. genDoc.ConsensusParams = DefaultConsensusParams()
  102. }
  103. genDoc.ConsensusParams.Complete()
  104. if err := genDoc.ConsensusParams.ValidateConsensusParams(); err != nil {
  105. return err
  106. }
  107. for i, v := range genDoc.Validators {
  108. if v.Power == 0 {
  109. return fmt.Errorf("the genesis file cannot contain validators with no voting power: %v", v)
  110. }
  111. if len(v.Address) > 0 && !bytes.Equal(v.PubKey.Address(), v.Address) {
  112. return fmt.Errorf("incorrect address for validator %v in the genesis file, should be %v", v, v.PubKey.Address())
  113. }
  114. if len(v.Address) == 0 {
  115. genDoc.Validators[i].Address = v.PubKey.Address()
  116. }
  117. }
  118. if genDoc.GenesisTime.IsZero() {
  119. genDoc.GenesisTime = tmtime.Now()
  120. }
  121. return nil
  122. }
  123. //------------------------------------------------------------
  124. // Make genesis state from file
  125. // GenesisDocFromJSON unmarshalls JSON data into a GenesisDoc.
  126. func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) {
  127. genDoc := GenesisDoc{}
  128. err := json.Unmarshal(jsonBlob, &genDoc)
  129. if err != nil {
  130. return nil, err
  131. }
  132. if err := genDoc.ValidateAndComplete(); err != nil {
  133. return nil, err
  134. }
  135. return &genDoc, err
  136. }
  137. // GenesisDocFromFile reads JSON data from a file and unmarshalls it into a GenesisDoc.
  138. func GenesisDocFromFile(genDocFile string) (*GenesisDoc, error) {
  139. jsonBlob, err := os.ReadFile(genDocFile)
  140. if err != nil {
  141. return nil, fmt.Errorf("couldn't read GenesisDoc file: %w", err)
  142. }
  143. genDoc, err := GenesisDocFromJSON(jsonBlob)
  144. if err != nil {
  145. return nil, fmt.Errorf("error reading GenesisDoc at %s: %w", genDocFile, err)
  146. }
  147. return genDoc, nil
  148. }