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.

535 lines
15 KiB

  1. //nolint: gosec
  2. package e2e
  3. import (
  4. "errors"
  5. "fmt"
  6. "io"
  7. "math/rand"
  8. "net"
  9. "path/filepath"
  10. "sort"
  11. "strconv"
  12. "strings"
  13. "github.com/tendermint/tendermint/crypto"
  14. "github.com/tendermint/tendermint/crypto/ed25519"
  15. rpchttp "github.com/tendermint/tendermint/rpc/client/http"
  16. mcs "github.com/tendermint/tendermint/test/maverick/consensus"
  17. )
  18. const (
  19. randomSeed int64 = 2308084734268
  20. proxyPortFirst uint32 = 5701
  21. networkIPv4 = "10.186.73.0/24"
  22. networkIPv6 = "fd80:b10c::/48"
  23. )
  24. type Mode string
  25. type Protocol string
  26. type Perturbation string
  27. const (
  28. ModeValidator Mode = "validator"
  29. ModeFull Mode = "full"
  30. ModeSeed Mode = "seed"
  31. ProtocolBuiltin Protocol = "builtin"
  32. ProtocolFile Protocol = "file"
  33. ProtocolGRPC Protocol = "grpc"
  34. ProtocolTCP Protocol = "tcp"
  35. ProtocolUNIX Protocol = "unix"
  36. PerturbationDisconnect Perturbation = "disconnect"
  37. PerturbationKill Perturbation = "kill"
  38. PerturbationPause Perturbation = "pause"
  39. PerturbationRestart Perturbation = "restart"
  40. )
  41. // Testnet represents a single testnet.
  42. type Testnet struct {
  43. Name string
  44. File string
  45. Dir string
  46. IP *net.IPNet
  47. InitialHeight int64
  48. InitialState map[string]string
  49. Validators map[*Node]int64
  50. ValidatorUpdates map[int64]map[*Node]int64
  51. Nodes []*Node
  52. }
  53. // Node represents a Tendermint node in a testnet.
  54. type Node struct {
  55. Name string
  56. Testnet *Testnet
  57. Mode Mode
  58. Key crypto.PrivKey
  59. IP net.IP
  60. ProxyPort uint32
  61. StartAt int64
  62. FastSync string
  63. StateSync bool
  64. Database string
  65. ABCIProtocol Protocol
  66. PrivvalProtocol Protocol
  67. PersistInterval uint64
  68. SnapshotInterval uint64
  69. RetainBlocks uint64
  70. Seeds []*Node
  71. PersistentPeers []*Node
  72. Perturbations []Perturbation
  73. Misbehaviors map[int64]string
  74. }
  75. // LoadTestnet loads a testnet from a manifest file, using the filename to
  76. // determine the testnet name and directory (from the basename of the file).
  77. // The testnet generation must be deterministic, since it is generated
  78. // separately by the runner and the test cases. For this reason, testnets use a
  79. // random seed to generate e.g. keys.
  80. func LoadTestnet(file string) (*Testnet, error) {
  81. manifest, err := LoadManifest(file)
  82. if err != nil {
  83. return nil, err
  84. }
  85. dir := strings.TrimSuffix(file, filepath.Ext(file))
  86. // Set up resource generators. These must be deterministic.
  87. netAddress := networkIPv4
  88. if manifest.IPv6 {
  89. netAddress = networkIPv6
  90. }
  91. _, ipNet, err := net.ParseCIDR(netAddress)
  92. if err != nil {
  93. return nil, fmt.Errorf("invalid IP network address %q: %w", netAddress, err)
  94. }
  95. ipGen := newIPGenerator(ipNet)
  96. keyGen := newKeyGenerator(randomSeed)
  97. proxyPortGen := newPortGenerator(proxyPortFirst)
  98. testnet := &Testnet{
  99. Name: filepath.Base(dir),
  100. File: file,
  101. Dir: dir,
  102. IP: ipGen.Network(),
  103. InitialHeight: 1,
  104. InitialState: manifest.InitialState,
  105. Validators: map[*Node]int64{},
  106. ValidatorUpdates: map[int64]map[*Node]int64{},
  107. Nodes: []*Node{},
  108. }
  109. if manifest.InitialHeight > 0 {
  110. testnet.InitialHeight = manifest.InitialHeight
  111. }
  112. // Set up nodes, in alphabetical order (IPs and ports get same order).
  113. nodeNames := []string{}
  114. for name := range manifest.Nodes {
  115. nodeNames = append(nodeNames, name)
  116. }
  117. sort.Strings(nodeNames)
  118. for _, name := range nodeNames {
  119. nodeManifest := manifest.Nodes[name]
  120. node := &Node{
  121. Name: name,
  122. Testnet: testnet,
  123. Key: keyGen.Generate(),
  124. IP: ipGen.Next(),
  125. ProxyPort: proxyPortGen.Next(),
  126. Mode: ModeValidator,
  127. Database: "goleveldb",
  128. ABCIProtocol: ProtocolUNIX,
  129. PrivvalProtocol: ProtocolFile,
  130. StartAt: nodeManifest.StartAt,
  131. FastSync: nodeManifest.FastSync,
  132. StateSync: nodeManifest.StateSync,
  133. PersistInterval: 1,
  134. SnapshotInterval: nodeManifest.SnapshotInterval,
  135. RetainBlocks: nodeManifest.RetainBlocks,
  136. Perturbations: []Perturbation{},
  137. Misbehaviors: make(map[int64]string),
  138. }
  139. if node.StartAt == testnet.InitialHeight {
  140. node.StartAt = 0 // normalize to 0 for initial nodes, since code expects this
  141. }
  142. if nodeManifest.Mode != "" {
  143. node.Mode = Mode(nodeManifest.Mode)
  144. }
  145. if nodeManifest.Database != "" {
  146. node.Database = nodeManifest.Database
  147. }
  148. if nodeManifest.ABCIProtocol != "" {
  149. node.ABCIProtocol = Protocol(nodeManifest.ABCIProtocol)
  150. }
  151. if nodeManifest.PrivvalProtocol != "" {
  152. node.PrivvalProtocol = Protocol(nodeManifest.PrivvalProtocol)
  153. }
  154. if nodeManifest.PersistInterval != nil {
  155. node.PersistInterval = *nodeManifest.PersistInterval
  156. }
  157. for _, p := range nodeManifest.Perturb {
  158. node.Perturbations = append(node.Perturbations, Perturbation(p))
  159. }
  160. for heightString, misbehavior := range nodeManifest.Misbehaviors {
  161. height, err := strconv.ParseInt(heightString, 10, 64)
  162. if err != nil {
  163. return nil, fmt.Errorf("unable to parse height %s to int64: %w", heightString, err)
  164. }
  165. node.Misbehaviors[height] = misbehavior
  166. }
  167. testnet.Nodes = append(testnet.Nodes, node)
  168. }
  169. // We do a second pass to set up seeds and persistent peers, which allows graph cycles.
  170. for _, node := range testnet.Nodes {
  171. nodeManifest, ok := manifest.Nodes[node.Name]
  172. if !ok {
  173. return nil, fmt.Errorf("failed to look up manifest for node %q", node.Name)
  174. }
  175. for _, seedName := range nodeManifest.Seeds {
  176. seed := testnet.LookupNode(seedName)
  177. if seed == nil {
  178. return nil, fmt.Errorf("unknown seed %q for node %q", seedName, node.Name)
  179. }
  180. node.Seeds = append(node.Seeds, seed)
  181. }
  182. for _, peerName := range nodeManifest.PersistentPeers {
  183. peer := testnet.LookupNode(peerName)
  184. if peer == nil {
  185. return nil, fmt.Errorf("unknown persistent peer %q for node %q", peerName, node.Name)
  186. }
  187. node.PersistentPeers = append(node.PersistentPeers, peer)
  188. }
  189. // If there are no seeds or persistent peers specified, default to persistent
  190. // connections to all other nodes.
  191. if len(node.PersistentPeers) == 0 && len(node.Seeds) == 0 {
  192. for _, peer := range testnet.Nodes {
  193. if peer.Name == node.Name {
  194. continue
  195. }
  196. node.PersistentPeers = append(node.PersistentPeers, peer)
  197. }
  198. }
  199. }
  200. // Set up genesis validators. If not specified explicitly, use all validator nodes.
  201. if manifest.Validators != nil {
  202. for validatorName, power := range *manifest.Validators {
  203. validator := testnet.LookupNode(validatorName)
  204. if validator == nil {
  205. return nil, fmt.Errorf("unknown validator %q", validatorName)
  206. }
  207. testnet.Validators[validator] = power
  208. }
  209. } else {
  210. for _, node := range testnet.Nodes {
  211. if node.Mode == ModeValidator {
  212. testnet.Validators[node] = 100
  213. }
  214. }
  215. }
  216. // Set up validator updates.
  217. for heightStr, validators := range manifest.ValidatorUpdates {
  218. height, err := strconv.Atoi(heightStr)
  219. if err != nil {
  220. return nil, fmt.Errorf("invalid validator update height %q: %w", height, err)
  221. }
  222. valUpdate := map[*Node]int64{}
  223. for name, power := range validators {
  224. node := testnet.LookupNode(name)
  225. if node == nil {
  226. return nil, fmt.Errorf("unknown validator %q for update at height %v", name, height)
  227. }
  228. valUpdate[node] = power
  229. }
  230. testnet.ValidatorUpdates[int64(height)] = valUpdate
  231. }
  232. return testnet, testnet.Validate()
  233. }
  234. // Validate validates a testnet.
  235. func (t Testnet) Validate() error {
  236. if t.Name == "" {
  237. return errors.New("network has no name")
  238. }
  239. if t.IP == nil {
  240. return errors.New("network has no IP")
  241. }
  242. if len(t.Nodes) == 0 {
  243. return errors.New("network has no nodes")
  244. }
  245. for _, node := range t.Nodes {
  246. if err := node.Validate(t); err != nil {
  247. return fmt.Errorf("invalid node %q: %w", node.Name, err)
  248. }
  249. }
  250. return nil
  251. }
  252. // Validate validates a node.
  253. func (n Node) Validate(testnet Testnet) error {
  254. if n.Name == "" {
  255. return errors.New("node has no name")
  256. }
  257. if n.IP == nil {
  258. return errors.New("node has no IP address")
  259. }
  260. if !testnet.IP.Contains(n.IP) {
  261. return fmt.Errorf("node IP %v is not in testnet network %v", n.IP, testnet.IP)
  262. }
  263. if n.ProxyPort > 0 {
  264. if n.ProxyPort <= 1024 {
  265. return fmt.Errorf("local port %v must be >1024", n.ProxyPort)
  266. }
  267. for _, peer := range testnet.Nodes {
  268. if peer.Name != n.Name && peer.ProxyPort == n.ProxyPort {
  269. return fmt.Errorf("peer %q also has local port %v", peer.Name, n.ProxyPort)
  270. }
  271. }
  272. }
  273. switch n.FastSync {
  274. case "", "v0", "v1", "v2":
  275. default:
  276. return fmt.Errorf("invalid fast sync setting %q", n.FastSync)
  277. }
  278. switch n.Database {
  279. case "goleveldb", "cleveldb", "boltdb", "rocksdb", "badgerdb":
  280. default:
  281. return fmt.Errorf("invalid database setting %q", n.Database)
  282. }
  283. switch n.ABCIProtocol {
  284. case ProtocolBuiltin, ProtocolUNIX, ProtocolTCP, ProtocolGRPC:
  285. default:
  286. return fmt.Errorf("invalid ABCI protocol setting %q", n.ABCIProtocol)
  287. }
  288. switch n.PrivvalProtocol {
  289. case ProtocolFile, ProtocolUNIX, ProtocolTCP:
  290. default:
  291. return fmt.Errorf("invalid privval protocol setting %q", n.PrivvalProtocol)
  292. }
  293. if n.StartAt > 0 && n.StartAt < n.Testnet.InitialHeight {
  294. return fmt.Errorf("cannot start at height %v lower than initial height %v",
  295. n.StartAt, n.Testnet.InitialHeight)
  296. }
  297. if n.StateSync && n.StartAt == 0 {
  298. return errors.New("state synced nodes cannot start at the initial height")
  299. }
  300. if n.PersistInterval == 0 && n.RetainBlocks > 0 {
  301. return errors.New("persist_interval=0 requires retain_blocks=0")
  302. }
  303. if n.PersistInterval > 1 && n.RetainBlocks > 0 && n.RetainBlocks < n.PersistInterval {
  304. return errors.New("persist_interval must be less than or equal to retain_blocks")
  305. }
  306. if n.SnapshotInterval > 0 && n.RetainBlocks > 0 && n.RetainBlocks < n.SnapshotInterval {
  307. return errors.New("snapshot_interval must be less than er equal to retain_blocks")
  308. }
  309. for _, perturbation := range n.Perturbations {
  310. switch perturbation {
  311. case PerturbationDisconnect, PerturbationKill, PerturbationPause, PerturbationRestart:
  312. default:
  313. return fmt.Errorf("invalid perturbation %q", perturbation)
  314. }
  315. }
  316. if (n.PrivvalProtocol != "file" || n.Mode != "validator") && len(n.Misbehaviors) != 0 {
  317. return errors.New("must be using \"file\" privval protocol to implement misbehaviors")
  318. }
  319. for height, misbehavior := range n.Misbehaviors {
  320. if height < n.StartAt {
  321. return fmt.Errorf("misbehavior height %d is below node start height %d",
  322. height, n.StartAt)
  323. }
  324. if height < testnet.InitialHeight {
  325. return fmt.Errorf("misbehavior height %d is below network initial height %d",
  326. height, testnet.InitialHeight)
  327. }
  328. exists := false
  329. for possibleBehaviors := range mcs.MisbehaviorList {
  330. if possibleBehaviors == misbehavior {
  331. exists = true
  332. }
  333. }
  334. if !exists {
  335. return fmt.Errorf("misbehavior %s does not exist", misbehavior)
  336. }
  337. }
  338. return nil
  339. }
  340. // LookupNode looks up a node by name. For now, simply do a linear search.
  341. func (t Testnet) LookupNode(name string) *Node {
  342. for _, node := range t.Nodes {
  343. if node.Name == name {
  344. return node
  345. }
  346. }
  347. return nil
  348. }
  349. // ArchiveNodes returns a list of archive nodes that start at the initial height
  350. // and contain the entire blockchain history. They are used e.g. as light client
  351. // RPC servers.
  352. func (t Testnet) ArchiveNodes() []*Node {
  353. nodes := []*Node{}
  354. for _, node := range t.Nodes {
  355. if node.Mode != ModeSeed && node.StartAt == 0 && node.RetainBlocks == 0 {
  356. nodes = append(nodes, node)
  357. }
  358. }
  359. return nodes
  360. }
  361. // RandomNode returns a random non-seed node.
  362. func (t Testnet) RandomNode() *Node {
  363. for {
  364. node := t.Nodes[rand.Intn(len(t.Nodes))]
  365. if node.Mode != ModeSeed {
  366. return node
  367. }
  368. }
  369. }
  370. // IPv6 returns true if the testnet is an IPv6 network.
  371. func (t Testnet) IPv6() bool {
  372. return t.IP.IP.To4() == nil
  373. }
  374. // HasPerturbations returns whether the network has any perturbations.
  375. func (t Testnet) HasPerturbations() bool {
  376. for _, node := range t.Nodes {
  377. if len(node.Perturbations) > 0 {
  378. return true
  379. }
  380. }
  381. return false
  382. }
  383. // LastMisbehaviorHeight returns the height of the last misbehavior.
  384. func (t Testnet) LastMisbehaviorHeight() int64 {
  385. lastHeight := int64(0)
  386. for _, node := range t.Nodes {
  387. for height := range node.Misbehaviors {
  388. if height > lastHeight {
  389. lastHeight = height
  390. }
  391. }
  392. }
  393. return lastHeight
  394. }
  395. // Address returns a P2P endpoint address for the node.
  396. func (n Node) AddressP2P(withID bool) string {
  397. ip := n.IP.String()
  398. if n.IP.To4() == nil {
  399. // IPv6 addresses must be wrapped in [] to avoid conflict with : port separator
  400. ip = fmt.Sprintf("[%v]", ip)
  401. }
  402. addr := fmt.Sprintf("%v:26656", ip)
  403. if withID {
  404. addr = fmt.Sprintf("%x@%v", n.Key.PubKey().Address().Bytes(), addr)
  405. }
  406. return addr
  407. }
  408. // Address returns an RPC endpoint address for the node.
  409. func (n Node) AddressRPC() string {
  410. ip := n.IP.String()
  411. if n.IP.To4() == nil {
  412. // IPv6 addresses must be wrapped in [] to avoid conflict with : port separator
  413. ip = fmt.Sprintf("[%v]", ip)
  414. }
  415. return fmt.Sprintf("%v:26657", ip)
  416. }
  417. // Client returns an RPC client for a node.
  418. func (n Node) Client() (*rpchttp.HTTP, error) {
  419. return rpchttp.New(fmt.Sprintf("http://127.0.0.1:%v", n.ProxyPort), "/websocket")
  420. }
  421. // keyGenerator generates pseudorandom Ed25519 keys based on a seed.
  422. type keyGenerator struct {
  423. random *rand.Rand
  424. }
  425. func newKeyGenerator(seed int64) *keyGenerator {
  426. return &keyGenerator{
  427. random: rand.New(rand.NewSource(seed)),
  428. }
  429. }
  430. func (g *keyGenerator) Generate() crypto.PrivKey {
  431. seed := make([]byte, ed25519.SeedSize)
  432. _, err := io.ReadFull(g.random, seed)
  433. if err != nil {
  434. panic(err) // this shouldn't happen
  435. }
  436. return ed25519.GenPrivKeyFromSecret(seed)
  437. }
  438. // portGenerator generates local Docker proxy ports for each node.
  439. type portGenerator struct {
  440. nextPort uint32
  441. }
  442. func newPortGenerator(firstPort uint32) *portGenerator {
  443. return &portGenerator{nextPort: firstPort}
  444. }
  445. func (g *portGenerator) Next() uint32 {
  446. port := g.nextPort
  447. g.nextPort++
  448. if g.nextPort == 0 {
  449. panic("port overflow")
  450. }
  451. return port
  452. }
  453. // ipGenerator generates sequential IP addresses for each node, using a random
  454. // network address.
  455. type ipGenerator struct {
  456. network *net.IPNet
  457. nextIP net.IP
  458. }
  459. func newIPGenerator(network *net.IPNet) *ipGenerator {
  460. nextIP := make([]byte, len(network.IP))
  461. copy(nextIP, network.IP)
  462. gen := &ipGenerator{network: network, nextIP: nextIP}
  463. // Skip network and gateway addresses
  464. gen.Next()
  465. gen.Next()
  466. return gen
  467. }
  468. func (g *ipGenerator) Network() *net.IPNet {
  469. n := &net.IPNet{
  470. IP: make([]byte, len(g.network.IP)),
  471. Mask: make([]byte, len(g.network.Mask)),
  472. }
  473. copy(n.IP, g.network.IP)
  474. copy(n.Mask, g.network.Mask)
  475. return n
  476. }
  477. func (g *ipGenerator) Next() net.IP {
  478. ip := make([]byte, len(g.nextIP))
  479. copy(ip, g.nextIP)
  480. for i := len(g.nextIP) - 1; i >= 0; i-- {
  481. g.nextIP[i]++
  482. if g.nextIP[i] != 0 {
  483. break
  484. }
  485. }
  486. return ip
  487. }