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.

330 lines
10 KiB

7 years ago
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
3 years ago
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
3 years ago
  1. package types
  2. import (
  3. "errors"
  4. "fmt"
  5. "time"
  6. "github.com/tendermint/tendermint/crypto/ed25519"
  7. "github.com/tendermint/tendermint/crypto/secp256k1"
  8. "github.com/tendermint/tendermint/crypto/sr25519"
  9. "github.com/tendermint/tendermint/crypto/tmhash"
  10. tmstrings "github.com/tendermint/tendermint/libs/strings"
  11. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  12. )
  13. const (
  14. // MaxBlockSizeBytes is the maximum permitted size of the blocks.
  15. MaxBlockSizeBytes = 104857600 // 100MB
  16. // BlockPartSizeBytes is the size of one block part.
  17. BlockPartSizeBytes uint32 = 65536 // 64kB
  18. // MaxBlockPartsCount is the maximum number of block parts.
  19. MaxBlockPartsCount = (MaxBlockSizeBytes / BlockPartSizeBytes) + 1
  20. ABCIPubKeyTypeEd25519 = ed25519.KeyType
  21. ABCIPubKeyTypeSecp256k1 = secp256k1.KeyType
  22. ABCIPubKeyTypeSr25519 = sr25519.KeyType
  23. )
  24. var ABCIPubKeyTypesToNames = map[string]string{
  25. ABCIPubKeyTypeEd25519: ed25519.PubKeyName,
  26. ABCIPubKeyTypeSecp256k1: secp256k1.PubKeyName,
  27. ABCIPubKeyTypeSr25519: sr25519.PubKeyName,
  28. }
  29. // ConsensusParams contains consensus critical parameters that determine the
  30. // validity of blocks.
  31. type ConsensusParams struct {
  32. Block BlockParams `json:"block"`
  33. Evidence EvidenceParams `json:"evidence"`
  34. Validator ValidatorParams `json:"validator"`
  35. Version VersionParams `json:"version"`
  36. Synchrony SynchronyParams `json:"synchrony"`
  37. }
  38. // HashedParams is a subset of ConsensusParams.
  39. // It is amino encoded and hashed into
  40. // the Header.ConsensusHash.
  41. type HashedParams struct {
  42. BlockMaxBytes int64
  43. BlockMaxGas int64
  44. }
  45. // BlockParams define limits on the block size and gas plus minimum time
  46. // between blocks.
  47. type BlockParams struct {
  48. MaxBytes int64 `json:"max_bytes,string"`
  49. MaxGas int64 `json:"max_gas,string"`
  50. }
  51. // EvidenceParams determine how we handle evidence of malfeasance.
  52. type EvidenceParams struct {
  53. MaxAgeNumBlocks int64 `json:"max_age_num_blocks,string"` // only accept new evidence more recent than this
  54. MaxAgeDuration time.Duration `json:"max_age_duration,string"`
  55. MaxBytes int64 `json:"max_bytes,string"`
  56. }
  57. // ValidatorParams restrict the public key types validators can use.
  58. // NOTE: uses ABCI pubkey naming, not Amino names.
  59. type ValidatorParams struct {
  60. PubKeyTypes []string `json:"pub_key_types"`
  61. }
  62. type VersionParams struct {
  63. AppVersion uint64 `json:"app_version,string"`
  64. }
  65. // SynchronyParams influence the validity of block timestamps.
  66. // For more information on the relationship of the synchrony parameters to
  67. // block validity, see the Proposer-Based Timestamps specification:
  68. // https://github.com/tendermint/tendermint/blob/master/spec/consensus/proposer-based-timestamp/README.md
  69. type SynchronyParams struct {
  70. Precision time.Duration `json:"precision,string"`
  71. MessageDelay time.Duration `json:"message_delay,string"`
  72. }
  73. // DefaultConsensusParams returns a default ConsensusParams.
  74. func DefaultConsensusParams() *ConsensusParams {
  75. return &ConsensusParams{
  76. Block: DefaultBlockParams(),
  77. Evidence: DefaultEvidenceParams(),
  78. Validator: DefaultValidatorParams(),
  79. Version: DefaultVersionParams(),
  80. Synchrony: DefaultSynchronyParams(),
  81. }
  82. }
  83. // DefaultBlockParams returns a default BlockParams.
  84. func DefaultBlockParams() BlockParams {
  85. return BlockParams{
  86. MaxBytes: 22020096, // 21MB
  87. MaxGas: -1,
  88. }
  89. }
  90. // DefaultEvidenceParams returns a default EvidenceParams.
  91. func DefaultEvidenceParams() EvidenceParams {
  92. return EvidenceParams{
  93. MaxAgeNumBlocks: 100000, // 27.8 hrs at 1block/s
  94. MaxAgeDuration: 48 * time.Hour,
  95. MaxBytes: 1048576, // 1MB
  96. }
  97. }
  98. // DefaultValidatorParams returns a default ValidatorParams, which allows
  99. // only ed25519 pubkeys.
  100. func DefaultValidatorParams() ValidatorParams {
  101. return ValidatorParams{
  102. PubKeyTypes: []string{ABCIPubKeyTypeEd25519},
  103. }
  104. }
  105. func DefaultVersionParams() VersionParams {
  106. return VersionParams{
  107. AppVersion: 0,
  108. }
  109. }
  110. func DefaultSynchronyParams() SynchronyParams {
  111. return SynchronyParams{
  112. // 505ms was selected as the default to enable chains that have validators in
  113. // mixed leap-second handling environments.
  114. // For more information, see: https://github.com/tendermint/tendermint/issues/7724
  115. Precision: 505 * time.Millisecond,
  116. MessageDelay: 12 * time.Second,
  117. }
  118. }
  119. func (val *ValidatorParams) IsValidPubkeyType(pubkeyType string) bool {
  120. for i := 0; i < len(val.PubKeyTypes); i++ {
  121. if val.PubKeyTypes[i] == pubkeyType {
  122. return true
  123. }
  124. }
  125. return false
  126. }
  127. func (params *ConsensusParams) Complete() {
  128. if params.Synchrony == (SynchronyParams{}) {
  129. params.Synchrony = DefaultSynchronyParams()
  130. }
  131. }
  132. // Validate validates the ConsensusParams to ensure all values are within their
  133. // allowed limits, and returns an error if they are not.
  134. func (params ConsensusParams) ValidateConsensusParams() error {
  135. if params.Block.MaxBytes <= 0 {
  136. return fmt.Errorf("block.MaxBytes must be greater than 0. Got %d",
  137. params.Block.MaxBytes)
  138. }
  139. if params.Block.MaxBytes > MaxBlockSizeBytes {
  140. return fmt.Errorf("block.MaxBytes is too big. %d > %d",
  141. params.Block.MaxBytes, MaxBlockSizeBytes)
  142. }
  143. if params.Block.MaxGas < -1 {
  144. return fmt.Errorf("block.MaxGas must be greater or equal to -1. Got %d",
  145. params.Block.MaxGas)
  146. }
  147. if params.Evidence.MaxAgeNumBlocks <= 0 {
  148. return fmt.Errorf("evidence.MaxAgeNumBlocks must be greater than 0. Got %d",
  149. params.Evidence.MaxAgeNumBlocks)
  150. }
  151. if params.Evidence.MaxAgeDuration <= 0 {
  152. return fmt.Errorf("evidence.MaxAgeDuration must be greater than 0 if provided, Got %v",
  153. params.Evidence.MaxAgeDuration)
  154. }
  155. if params.Evidence.MaxBytes > params.Block.MaxBytes {
  156. return fmt.Errorf("evidence.MaxBytesEvidence is greater than upper bound, %d > %d",
  157. params.Evidence.MaxBytes, params.Block.MaxBytes)
  158. }
  159. if params.Evidence.MaxBytes < 0 {
  160. return fmt.Errorf("evidence.MaxBytes must be non negative. Got: %d",
  161. params.Evidence.MaxBytes)
  162. }
  163. if params.Synchrony.MessageDelay <= 0 {
  164. return fmt.Errorf("synchrony.MessageDelay must be greater than 0. Got: %d",
  165. params.Synchrony.MessageDelay)
  166. }
  167. if params.Synchrony.Precision <= 0 {
  168. return fmt.Errorf("synchrony.Precision must be greater than 0. Got: %d",
  169. params.Synchrony.Precision)
  170. }
  171. if len(params.Validator.PubKeyTypes) == 0 {
  172. return errors.New("len(Validator.PubKeyTypes) must be greater than 0")
  173. }
  174. // Check if keyType is a known ABCIPubKeyType
  175. for i := 0; i < len(params.Validator.PubKeyTypes); i++ {
  176. keyType := params.Validator.PubKeyTypes[i]
  177. if _, ok := ABCIPubKeyTypesToNames[keyType]; !ok {
  178. return fmt.Errorf("params.Validator.PubKeyTypes[%d], %s, is an unknown pubkey type",
  179. i, keyType)
  180. }
  181. }
  182. return nil
  183. }
  184. // Hash returns a hash of a subset of the parameters to store in the block header.
  185. // Only the Block.MaxBytes and Block.MaxGas are included in the hash.
  186. // This allows the ConsensusParams to evolve more without breaking the block
  187. // protocol. No need for a Merkle tree here, just a small struct to hash.
  188. func (params ConsensusParams) HashConsensusParams() []byte {
  189. hasher := tmhash.New()
  190. hp := tmproto.HashedParams{
  191. BlockMaxBytes: params.Block.MaxBytes,
  192. BlockMaxGas: params.Block.MaxGas,
  193. }
  194. bz, err := hp.Marshal()
  195. if err != nil {
  196. panic(err)
  197. }
  198. _, err = hasher.Write(bz)
  199. if err != nil {
  200. panic(err)
  201. }
  202. return hasher.Sum(nil)
  203. }
  204. func (params *ConsensusParams) Equals(params2 *ConsensusParams) bool {
  205. return params.Block == params2.Block &&
  206. params.Evidence == params2.Evidence &&
  207. params.Version == params2.Version &&
  208. params.Synchrony == params2.Synchrony &&
  209. tmstrings.StringSliceEqual(params.Validator.PubKeyTypes, params2.Validator.PubKeyTypes)
  210. }
  211. // Update returns a copy of the params with updates from the non-zero fields of p2.
  212. // NOTE: note: must not modify the original
  213. func (params ConsensusParams) UpdateConsensusParams(params2 *tmproto.ConsensusParams) ConsensusParams {
  214. res := params // explicit copy
  215. if params2 == nil {
  216. return res
  217. }
  218. // we must defensively consider any structs may be nil
  219. if params2.Block != nil {
  220. res.Block.MaxBytes = params2.Block.MaxBytes
  221. res.Block.MaxGas = params2.Block.MaxGas
  222. }
  223. if params2.Evidence != nil {
  224. res.Evidence.MaxAgeNumBlocks = params2.Evidence.MaxAgeNumBlocks
  225. res.Evidence.MaxAgeDuration = params2.Evidence.MaxAgeDuration
  226. res.Evidence.MaxBytes = params2.Evidence.MaxBytes
  227. }
  228. if params2.Validator != nil {
  229. // Copy params2.Validator.PubkeyTypes, and set result's value to the copy.
  230. // This avoids having to initialize the slice to 0 values, and then write to it again.
  231. res.Validator.PubKeyTypes = append([]string{}, params2.Validator.PubKeyTypes...)
  232. }
  233. if params2.Version != nil {
  234. res.Version.AppVersion = params2.Version.AppVersion
  235. }
  236. if params2.Synchrony != nil {
  237. res.Synchrony.Precision = params2.Synchrony.Precision
  238. res.Synchrony.MessageDelay = params2.Synchrony.MessageDelay
  239. }
  240. return res
  241. }
  242. func (params *ConsensusParams) ToProto() tmproto.ConsensusParams {
  243. return tmproto.ConsensusParams{
  244. Block: &tmproto.BlockParams{
  245. MaxBytes: params.Block.MaxBytes,
  246. MaxGas: params.Block.MaxGas,
  247. },
  248. Evidence: &tmproto.EvidenceParams{
  249. MaxAgeNumBlocks: params.Evidence.MaxAgeNumBlocks,
  250. MaxAgeDuration: params.Evidence.MaxAgeDuration,
  251. MaxBytes: params.Evidence.MaxBytes,
  252. },
  253. Validator: &tmproto.ValidatorParams{
  254. PubKeyTypes: params.Validator.PubKeyTypes,
  255. },
  256. Version: &tmproto.VersionParams{
  257. AppVersion: params.Version.AppVersion,
  258. },
  259. Synchrony: &tmproto.SynchronyParams{
  260. MessageDelay: params.Synchrony.MessageDelay,
  261. Precision: params.Synchrony.Precision,
  262. },
  263. }
  264. }
  265. func ConsensusParamsFromProto(pbParams tmproto.ConsensusParams) ConsensusParams {
  266. return ConsensusParams{
  267. Block: BlockParams{
  268. MaxBytes: pbParams.Block.MaxBytes,
  269. MaxGas: pbParams.Block.MaxGas,
  270. },
  271. Evidence: EvidenceParams{
  272. MaxAgeNumBlocks: pbParams.Evidence.MaxAgeNumBlocks,
  273. MaxAgeDuration: pbParams.Evidence.MaxAgeDuration,
  274. MaxBytes: pbParams.Evidence.MaxBytes,
  275. },
  276. Validator: ValidatorParams{
  277. PubKeyTypes: pbParams.Validator.PubKeyTypes,
  278. },
  279. Version: VersionParams{
  280. AppVersion: pbParams.Version.AppVersion,
  281. },
  282. Synchrony: SynchronyParams{
  283. MessageDelay: pbParams.Synchrony.MessageDelay,
  284. Precision: pbParams.Synchrony.Precision,
  285. },
  286. }
  287. }