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.

265 lines
8.3 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "sort"
  6. "strconv"
  7. "strings"
  8. e2e "github.com/tendermint/tendermint/test/e2e/pkg"
  9. )
  10. var (
  11. // testnetCombinations defines global testnet options, where we generate a
  12. // separate testnet for each combination (Cartesian product) of options.
  13. testnetCombinations = map[string][]interface{}{
  14. "topology": {"single", "quad", "large"},
  15. "ipv6": {false, true},
  16. "initialHeight": {0, 1000},
  17. "initialState": {
  18. map[string]string{},
  19. map[string]string{"initial01": "a", "initial02": "b", "initial03": "c"},
  20. },
  21. "validators": {"genesis", "initchain"},
  22. }
  23. // The following specify randomly chosen values for testnet nodes.
  24. nodeDatabases = uniformChoice{"goleveldb", "cleveldb", "rocksdb", "boltdb", "badgerdb"}
  25. nodeABCIProtocols = uniformChoice{"unix", "tcp", "grpc", "builtin"}
  26. nodePrivvalProtocols = uniformChoice{"file", "unix", "tcp"}
  27. // FIXME v1 disabled due to https://github.com/tendermint/tendermint/issues/5444
  28. // FIXME v2 disabled due to:
  29. // https://github.com/tendermint/tendermint/issues/5513
  30. // https://github.com/tendermint/tendermint/issues/5541
  31. nodeFastSyncs = uniformChoice{"", "v0"} // "v1", "v2"
  32. nodeStateSyncs = uniformChoice{false, true}
  33. nodePersistIntervals = uniformChoice{0, 1, 5}
  34. nodeSnapshotIntervals = uniformChoice{0, 3}
  35. nodeRetainBlocks = uniformChoice{0, 1, 5}
  36. nodePerturbations = probSetChoice{
  37. "disconnect": 0.1,
  38. "pause": 0.1,
  39. "kill": 0.1,
  40. "restart": 0.1,
  41. }
  42. nodeMisbehaviors = weightedChoice{
  43. // FIXME evidence disabled due to node panicing when not
  44. // having sufficient block history to process evidence.
  45. // https://github.com/tendermint/tendermint/issues/5617
  46. // misbehaviorOption{"double-prevote"}: 1,
  47. misbehaviorOption{}: 9,
  48. }
  49. )
  50. // Generate generates random testnets using the given RNG.
  51. func Generate(r *rand.Rand) ([]e2e.Manifest, error) {
  52. manifests := []e2e.Manifest{}
  53. for _, opt := range combinations(testnetCombinations) {
  54. manifest, err := generateTestnet(r, opt)
  55. if err != nil {
  56. return nil, err
  57. }
  58. manifests = append(manifests, manifest)
  59. }
  60. return manifests, nil
  61. }
  62. // generateTestnet generates a single testnet with the given options.
  63. func generateTestnet(r *rand.Rand, opt map[string]interface{}) (e2e.Manifest, error) {
  64. manifest := e2e.Manifest{
  65. IPv6: opt["ipv6"].(bool),
  66. InitialHeight: int64(opt["initialHeight"].(int)),
  67. InitialState: opt["initialState"].(map[string]string),
  68. Validators: &map[string]int64{},
  69. ValidatorUpdates: map[string]map[string]int64{},
  70. Nodes: map[string]*e2e.ManifestNode{},
  71. }
  72. var numSeeds, numValidators, numFulls int
  73. switch opt["topology"].(string) {
  74. case "single":
  75. numValidators = 1
  76. case "quad":
  77. numValidators = 4
  78. case "large":
  79. // FIXME Networks are kept small since large ones use too much CPU.
  80. numSeeds = r.Intn(4)
  81. numValidators = 4 + r.Intn(7)
  82. numFulls = r.Intn(5)
  83. default:
  84. return manifest, fmt.Errorf("unknown topology %q", opt["topology"])
  85. }
  86. // First we generate seed nodes, starting at the initial height.
  87. for i := 1; i <= numSeeds; i++ {
  88. manifest.Nodes[fmt.Sprintf("seed%02d", i)] = generateNode(
  89. r, e2e.ModeSeed, 0, manifest.InitialHeight, false)
  90. }
  91. // Next, we generate validators. We make sure a BFT quorum of validators start
  92. // at the initial height, and that we have two archive nodes. We also set up
  93. // the initial validator set, and validator set updates for delayed nodes.
  94. nextStartAt := manifest.InitialHeight + 5
  95. quorum := numValidators*2/3 + 1
  96. for i := 1; i <= numValidators; i++ {
  97. startAt := int64(0)
  98. if i > quorum {
  99. startAt = nextStartAt
  100. nextStartAt += 5
  101. }
  102. name := fmt.Sprintf("validator%02d", i)
  103. manifest.Nodes[name] = generateNode(
  104. r, e2e.ModeValidator, startAt, manifest.InitialHeight, i <= 2)
  105. if startAt == 0 {
  106. (*manifest.Validators)[name] = int64(30 + r.Intn(71))
  107. } else {
  108. manifest.ValidatorUpdates[fmt.Sprint(startAt+5)] = map[string]int64{
  109. name: int64(30 + r.Intn(71)),
  110. }
  111. }
  112. }
  113. // Move validators to InitChain if specified.
  114. switch opt["validators"].(string) {
  115. case "genesis":
  116. case "initchain":
  117. manifest.ValidatorUpdates["0"] = *manifest.Validators
  118. manifest.Validators = &map[string]int64{}
  119. default:
  120. return manifest, fmt.Errorf("invalid validators option %q", opt["validators"])
  121. }
  122. // Finally, we generate random full nodes.
  123. for i := 1; i <= numFulls; i++ {
  124. startAt := int64(0)
  125. if r.Float64() >= 0.5 {
  126. startAt = nextStartAt
  127. nextStartAt += 5
  128. }
  129. manifest.Nodes[fmt.Sprintf("full%02d", i)] = generateNode(
  130. r, e2e.ModeFull, startAt, manifest.InitialHeight, false)
  131. }
  132. // We now set up peer discovery for nodes. Seed nodes are fully meshed with
  133. // each other, while non-seed nodes either use a set of random seeds or a
  134. // set of random peers that start before themselves.
  135. var seedNames, peerNames []string
  136. for name, node := range manifest.Nodes {
  137. if node.Mode == string(e2e.ModeSeed) {
  138. seedNames = append(seedNames, name)
  139. } else {
  140. peerNames = append(peerNames, name)
  141. }
  142. }
  143. for _, name := range seedNames {
  144. for _, otherName := range seedNames {
  145. if name != otherName {
  146. manifest.Nodes[name].Seeds = append(manifest.Nodes[name].Seeds, otherName)
  147. }
  148. }
  149. }
  150. sort.Slice(peerNames, func(i, j int) bool {
  151. iName, jName := peerNames[i], peerNames[j]
  152. switch {
  153. case manifest.Nodes[iName].StartAt < manifest.Nodes[jName].StartAt:
  154. return true
  155. case manifest.Nodes[iName].StartAt > manifest.Nodes[jName].StartAt:
  156. return false
  157. default:
  158. return strings.Compare(iName, jName) == -1
  159. }
  160. })
  161. for i, name := range peerNames {
  162. if len(seedNames) > 0 && (i == 0 || r.Float64() >= 0.5) {
  163. manifest.Nodes[name].Seeds = uniformSetChoice(seedNames).Choose(r)
  164. } else if i > 0 {
  165. manifest.Nodes[name].PersistentPeers = uniformSetChoice(peerNames[:i]).Choose(r)
  166. }
  167. }
  168. return manifest, nil
  169. }
  170. // generateNode randomly generates a node, with some constraints to avoid
  171. // generating invalid configurations. We do not set Seeds or PersistentPeers
  172. // here, since we need to know the overall network topology and startup
  173. // sequencing.
  174. func generateNode(
  175. r *rand.Rand, mode e2e.Mode, startAt int64, initialHeight int64, forceArchive bool,
  176. ) *e2e.ManifestNode {
  177. node := e2e.ManifestNode{
  178. Mode: string(mode),
  179. StartAt: startAt,
  180. Database: nodeDatabases.Choose(r).(string),
  181. ABCIProtocol: nodeABCIProtocols.Choose(r).(string),
  182. PrivvalProtocol: nodePrivvalProtocols.Choose(r).(string),
  183. FastSync: nodeFastSyncs.Choose(r).(string),
  184. StateSync: nodeStateSyncs.Choose(r).(bool) && startAt > 0,
  185. PersistInterval: ptrUint64(uint64(nodePersistIntervals.Choose(r).(int))),
  186. SnapshotInterval: uint64(nodeSnapshotIntervals.Choose(r).(int)),
  187. RetainBlocks: uint64(nodeRetainBlocks.Choose(r).(int)),
  188. Perturb: nodePerturbations.Choose(r),
  189. }
  190. // If this node is forced to be an archive node, retain all blocks and
  191. // enable state sync snapshotting.
  192. if forceArchive {
  193. node.RetainBlocks = 0
  194. node.SnapshotInterval = 3
  195. }
  196. if node.Mode == "validator" {
  197. misbehaveAt := startAt + 5 + int64(r.Intn(10))
  198. if startAt == 0 {
  199. misbehaveAt += initialHeight - 1
  200. }
  201. node.Misbehaviors = nodeMisbehaviors.Choose(r).(misbehaviorOption).atHeight(misbehaveAt)
  202. if len(node.Misbehaviors) != 0 {
  203. node.PrivvalProtocol = "file"
  204. }
  205. }
  206. // If a node which does not persist state also does not retain blocks, randomly
  207. // choose to either persist state or retain all blocks.
  208. if node.PersistInterval != nil && *node.PersistInterval == 0 && node.RetainBlocks > 0 {
  209. if r.Float64() > 0.5 {
  210. node.RetainBlocks = 0
  211. } else {
  212. node.PersistInterval = ptrUint64(node.RetainBlocks)
  213. }
  214. }
  215. // If either PersistInterval or SnapshotInterval are greater than RetainBlocks,
  216. // expand the block retention time.
  217. if node.RetainBlocks > 0 {
  218. if node.PersistInterval != nil && node.RetainBlocks < *node.PersistInterval {
  219. node.RetainBlocks = *node.PersistInterval
  220. }
  221. if node.RetainBlocks < node.SnapshotInterval {
  222. node.RetainBlocks = node.SnapshotInterval
  223. }
  224. }
  225. return &node
  226. }
  227. func ptrUint64(i uint64) *uint64 {
  228. return &i
  229. }
  230. type misbehaviorOption struct {
  231. misbehavior string
  232. }
  233. func (m misbehaviorOption) atHeight(height int64) map[string]string {
  234. misbehaviorMap := make(map[string]string)
  235. if m.misbehavior == "" {
  236. return misbehaviorMap
  237. }
  238. misbehaviorMap[strconv.Itoa(int(height))] = m.misbehavior
  239. return misbehaviorMap
  240. }