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.

341 lines
9.9 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "sort"
  6. "strings"
  7. e2e "github.com/tendermint/tendermint/test/e2e/pkg"
  8. "github.com/tendermint/tendermint/types"
  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. "queueType": {"priority"}, // "fifo"
  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 = weightedChoice{
  25. "goleveldb": 35,
  26. "badgerdb": 35,
  27. "boltdb": 15,
  28. "rocksdb": 10,
  29. "cleveldb": 5,
  30. }
  31. nodeABCIProtocols = weightedChoice{
  32. "builtin": 50,
  33. "tcp": 20,
  34. "grpc": 20,
  35. "unix": 10,
  36. }
  37. nodePrivvalProtocols = weightedChoice{
  38. "file": 50,
  39. "grpc": 20,
  40. "tcp": 20,
  41. "unix": 10,
  42. }
  43. nodeStateSyncs = weightedChoice{
  44. e2e.StateSyncDisabled: 10,
  45. e2e.StateSyncP2P: 45,
  46. e2e.StateSyncRPC: 45,
  47. }
  48. nodePersistIntervals = uniformChoice{0, 1, 5}
  49. nodeSnapshotIntervals = uniformChoice{0, 5}
  50. nodeRetainBlocks = uniformChoice{
  51. 0,
  52. 2 * int(e2e.EvidenceAgeHeight),
  53. 4 * int(e2e.EvidenceAgeHeight),
  54. }
  55. nodePerturbations = probSetChoice{
  56. "disconnect": 0.1,
  57. "pause": 0.1,
  58. "kill": 0.1,
  59. "restart": 0.1,
  60. }
  61. evidence = uniformChoice{0, 1, 10}
  62. txSize = uniformChoice{1024, 4096} // either 1kb or 4kb
  63. ipv6 = uniformChoice{false, true}
  64. keyType = uniformChoice{types.ABCIPubKeyTypeEd25519, types.ABCIPubKeyTypeSecp256k1}
  65. )
  66. // Generate generates random testnets using the given RNG.
  67. func Generate(r *rand.Rand, opts Options) ([]e2e.Manifest, error) {
  68. manifests := []e2e.Manifest{}
  69. for _, opt := range combinations(testnetCombinations) {
  70. manifest, err := generateTestnet(r, opt)
  71. if err != nil {
  72. return nil, err
  73. }
  74. if len(manifest.Nodes) < opts.MinNetworkSize {
  75. continue
  76. }
  77. if opts.MaxNetworkSize > 0 && len(manifest.Nodes) >= opts.MaxNetworkSize {
  78. continue
  79. }
  80. manifests = append(manifests, manifest)
  81. }
  82. return manifests, nil
  83. }
  84. type Options struct {
  85. MinNetworkSize int
  86. MaxNetworkSize int
  87. NumGroups int
  88. Directory string
  89. Reverse bool
  90. }
  91. // generateTestnet generates a single testnet with the given options.
  92. func generateTestnet(r *rand.Rand, opt map[string]interface{}) (e2e.Manifest, error) {
  93. manifest := e2e.Manifest{
  94. IPv6: ipv6.Choose(r).(bool),
  95. ABCIProtocol: nodeABCIProtocols.Choose(r),
  96. InitialHeight: int64(opt["initialHeight"].(int)),
  97. InitialState: opt["initialState"].(map[string]string),
  98. Validators: &map[string]int64{},
  99. ValidatorUpdates: map[string]map[string]int64{},
  100. Nodes: map[string]*e2e.ManifestNode{},
  101. KeyType: keyType.Choose(r).(string),
  102. Evidence: evidence.Choose(r).(int),
  103. QueueType: opt["queueType"].(string),
  104. TxSize: int64(txSize.Choose(r).(int)),
  105. }
  106. var numSeeds, numValidators, numFulls, numLightClients int
  107. switch opt["topology"].(string) {
  108. case "single":
  109. numValidators = 1
  110. case "quad":
  111. numValidators = 4
  112. case "large":
  113. // FIXME Networks are kept small since large ones use too much CPU.
  114. numSeeds = r.Intn(1)
  115. numLightClients = r.Intn(2)
  116. numValidators = 4 + r.Intn(4)
  117. numFulls = r.Intn(4)
  118. default:
  119. return manifest, fmt.Errorf("unknown topology %q", opt["topology"])
  120. }
  121. // First we generate seed nodes, starting at the initial height.
  122. for i := 1; i <= numSeeds; i++ {
  123. node := generateNode(r, manifest, e2e.ModeSeed, 0, false)
  124. manifest.Nodes[fmt.Sprintf("seed%02d", i)] = node
  125. }
  126. var numSyncingNodes = 0
  127. // Next, we generate validators. We make sure a BFT quorum of validators start
  128. // at the initial height, and that we have two archive nodes. We also set up
  129. // the initial validator set, and validator set updates for delayed nodes.
  130. nextStartAt := manifest.InitialHeight + 5
  131. quorum := numValidators*2/3 + 1
  132. for i := 1; i <= numValidators; i++ {
  133. startAt := int64(0)
  134. if i > quorum && numSyncingNodes < 2 && r.Float64() >= 0.25 {
  135. numSyncingNodes++
  136. startAt = nextStartAt
  137. nextStartAt += 5
  138. }
  139. name := fmt.Sprintf("validator%02d", i)
  140. node := generateNode(r, manifest, e2e.ModeValidator, startAt, i <= 2)
  141. manifest.Nodes[name] = node
  142. if startAt == 0 {
  143. (*manifest.Validators)[name] = int64(30 + r.Intn(71))
  144. } else {
  145. manifest.ValidatorUpdates[fmt.Sprint(startAt+5)] = map[string]int64{
  146. name: int64(30 + r.Intn(71)),
  147. }
  148. }
  149. }
  150. // Move validators to InitChain if specified.
  151. switch opt["validators"].(string) {
  152. case "genesis":
  153. case "initchain":
  154. manifest.ValidatorUpdates["0"] = *manifest.Validators
  155. manifest.Validators = &map[string]int64{}
  156. default:
  157. return manifest, fmt.Errorf("invalid validators option %q", opt["validators"])
  158. }
  159. // Finally, we generate random full nodes.
  160. for i := 1; i <= numFulls; i++ {
  161. startAt := int64(0)
  162. if numSyncingNodes < 2 && r.Float64() >= 0.5 {
  163. numSyncingNodes++
  164. startAt = nextStartAt
  165. nextStartAt += 5
  166. }
  167. node := generateNode(r, manifest, e2e.ModeFull, startAt, false)
  168. manifest.Nodes[fmt.Sprintf("full%02d", i)] = node
  169. }
  170. // We now set up peer discovery for nodes. Seed nodes are fully meshed with
  171. // each other, while non-seed nodes either use a set of random seeds or a
  172. // set of random peers that start before themselves.
  173. var seedNames, peerNames, lightProviders []string
  174. for name, node := range manifest.Nodes {
  175. if node.Mode == string(e2e.ModeSeed) {
  176. seedNames = append(seedNames, name)
  177. } else {
  178. // if the full node or validator is an ideal candidate, it is added as a light provider.
  179. // There are at least two archive nodes so there should be at least two ideal candidates
  180. if (node.StartAt == 0 || node.StartAt == manifest.InitialHeight) && node.RetainBlocks == 0 {
  181. lightProviders = append(lightProviders, name)
  182. }
  183. peerNames = append(peerNames, name)
  184. }
  185. }
  186. for _, name := range seedNames {
  187. for _, otherName := range seedNames {
  188. if name != otherName {
  189. manifest.Nodes[name].Seeds = append(manifest.Nodes[name].Seeds, otherName)
  190. }
  191. }
  192. }
  193. sort.Slice(peerNames, func(i, j int) bool {
  194. iName, jName := peerNames[i], peerNames[j]
  195. switch {
  196. case manifest.Nodes[iName].StartAt < manifest.Nodes[jName].StartAt:
  197. return true
  198. case manifest.Nodes[iName].StartAt > manifest.Nodes[jName].StartAt:
  199. return false
  200. default:
  201. return strings.Compare(iName, jName) == -1
  202. }
  203. })
  204. for i, name := range peerNames {
  205. // there are seeds, statesync is disabled, and it's
  206. // either the first peer by the sort order, and
  207. // (randomly half of the remaining peers use a seed
  208. // node; otherwise, choose some remaining set of the
  209. // peers.
  210. if len(seedNames) > 0 &&
  211. manifest.Nodes[name].StateSync == e2e.StateSyncDisabled &&
  212. (i == 0 || r.Float64() >= 0.5) {
  213. // choose one of the seeds
  214. manifest.Nodes[name].Seeds = uniformSetChoice(seedNames).Choose(r)
  215. } else if i > 1 && r.Float64() >= 0.5 {
  216. peers := uniformSetChoice(peerNames[:i])
  217. manifest.Nodes[name].PersistentPeers = peers.ChooseAtLeast(r, 2)
  218. }
  219. }
  220. // lastly, set up the light clients
  221. for i := 1; i <= numLightClients; i++ {
  222. startAt := manifest.InitialHeight + 5
  223. node := generateLightNode(r, startAt+(5*int64(i)), lightProviders)
  224. manifest.Nodes[fmt.Sprintf("light%02d", i)] = node
  225. }
  226. return manifest, nil
  227. }
  228. // generateNode randomly generates a node, with some constraints to avoid
  229. // generating invalid configurations. We do not set Seeds or PersistentPeers
  230. // here, since we need to know the overall network topology and startup
  231. // sequencing.
  232. func generateNode(
  233. r *rand.Rand,
  234. manifest e2e.Manifest,
  235. mode e2e.Mode,
  236. startAt int64,
  237. forceArchive bool,
  238. ) *e2e.ManifestNode {
  239. node := e2e.ManifestNode{
  240. Mode: string(mode),
  241. StartAt: startAt,
  242. Database: nodeDatabases.Choose(r),
  243. PrivvalProtocol: nodePrivvalProtocols.Choose(r),
  244. StateSync: e2e.StateSyncDisabled,
  245. PersistInterval: ptrUint64(uint64(nodePersistIntervals.Choose(r).(int))),
  246. SnapshotInterval: uint64(nodeSnapshotIntervals.Choose(r).(int)),
  247. RetainBlocks: uint64(nodeRetainBlocks.Choose(r).(int)),
  248. Perturb: nodePerturbations.Choose(r),
  249. }
  250. if node.PrivvalProtocol == "" {
  251. node.PrivvalProtocol = "file"
  252. }
  253. if startAt > 0 {
  254. node.StateSync = nodeStateSyncs.Choose(r)
  255. if manifest.InitialHeight-startAt <= 5 && node.StateSync == e2e.StateSyncDisabled {
  256. // avoid needing to blocsync more than five total blocks.
  257. node.StateSync = uniformSetChoice([]string{
  258. e2e.StateSyncP2P,
  259. e2e.StateSyncRPC,
  260. }).Choose(r)[0]
  261. }
  262. }
  263. // If this node is forced to be an archive node, retain all blocks and
  264. // enable state sync snapshotting.
  265. if forceArchive {
  266. node.RetainBlocks = 0
  267. node.SnapshotInterval = 3
  268. }
  269. // If a node which does not persist state also does not retain blocks, randomly
  270. // choose to either persist state or retain all blocks.
  271. if node.PersistInterval != nil && *node.PersistInterval == 0 && node.RetainBlocks > 0 {
  272. if r.Float64() > 0.5 {
  273. node.RetainBlocks = 0
  274. } else {
  275. node.PersistInterval = ptrUint64(node.RetainBlocks)
  276. }
  277. }
  278. // If either PersistInterval or SnapshotInterval are greater than RetainBlocks,
  279. // expand the block retention time.
  280. if node.RetainBlocks > 0 {
  281. if node.PersistInterval != nil && node.RetainBlocks < *node.PersistInterval {
  282. node.RetainBlocks = *node.PersistInterval
  283. }
  284. if node.RetainBlocks < node.SnapshotInterval {
  285. node.RetainBlocks = node.SnapshotInterval
  286. }
  287. }
  288. return &node
  289. }
  290. func generateLightNode(r *rand.Rand, startAt int64, providers []string) *e2e.ManifestNode {
  291. return &e2e.ManifestNode{
  292. Mode: string(e2e.ModeLight),
  293. StartAt: startAt,
  294. Database: nodeDatabases.Choose(r),
  295. PersistInterval: ptrUint64(0),
  296. PersistentPeers: providers,
  297. }
  298. }
  299. func ptrUint64(i uint64) *uint64 {
  300. return &i
  301. }