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