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.

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