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.

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