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.

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