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.

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