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.

504 lines
14 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 nodeManifest.Mode != "" {
  140. node.Mode = Mode(nodeManifest.Mode)
  141. }
  142. if nodeManifest.Database != "" {
  143. node.Database = nodeManifest.Database
  144. }
  145. if nodeManifest.ABCIProtocol != "" {
  146. node.ABCIProtocol = Protocol(nodeManifest.ABCIProtocol)
  147. }
  148. if nodeManifest.PrivvalProtocol != "" {
  149. node.PrivvalProtocol = Protocol(nodeManifest.PrivvalProtocol)
  150. }
  151. if nodeManifest.PersistInterval != nil {
  152. node.PersistInterval = *nodeManifest.PersistInterval
  153. }
  154. for _, p := range nodeManifest.Perturb {
  155. node.Perturbations = append(node.Perturbations, Perturbation(p))
  156. }
  157. for heightString, misbehavior := range nodeManifest.Misbehaviors {
  158. height, err := strconv.ParseInt(heightString, 10, 64)
  159. if err != nil {
  160. return nil, fmt.Errorf("unable to parse height %s to int64: %w", heightString, err)
  161. }
  162. node.Misbehaviors[height] = misbehavior
  163. }
  164. testnet.Nodes = append(testnet.Nodes, node)
  165. }
  166. // We do a second pass to set up seeds and persistent peers, which allows graph cycles.
  167. for _, node := range testnet.Nodes {
  168. nodeManifest, ok := manifest.Nodes[node.Name]
  169. if !ok {
  170. return nil, fmt.Errorf("failed to look up manifest for node %q", node.Name)
  171. }
  172. for _, seedName := range nodeManifest.Seeds {
  173. seed := testnet.LookupNode(seedName)
  174. if seed == nil {
  175. return nil, fmt.Errorf("unknown seed %q for node %q", seedName, node.Name)
  176. }
  177. node.Seeds = append(node.Seeds, seed)
  178. }
  179. for _, peerName := range nodeManifest.PersistentPeers {
  180. peer := testnet.LookupNode(peerName)
  181. if peer == nil {
  182. return nil, fmt.Errorf("unknown persistent peer %q for node %q", peerName, node.Name)
  183. }
  184. node.PersistentPeers = append(node.PersistentPeers, peer)
  185. }
  186. // If there are no seeds or persistent peers specified, default to persistent
  187. // connections to all other nodes.
  188. if len(node.PersistentPeers) == 0 && len(node.Seeds) == 0 {
  189. for _, peer := range testnet.Nodes {
  190. if peer.Name == node.Name {
  191. continue
  192. }
  193. node.PersistentPeers = append(node.PersistentPeers, peer)
  194. }
  195. }
  196. }
  197. // Set up genesis validators. If not specified explicitly, use all validator nodes.
  198. if manifest.Validators != nil {
  199. for validatorName, power := range *manifest.Validators {
  200. validator := testnet.LookupNode(validatorName)
  201. if validator == nil {
  202. return nil, fmt.Errorf("unknown validator %q", validatorName)
  203. }
  204. testnet.Validators[validator] = power
  205. }
  206. } else {
  207. for _, node := range testnet.Nodes {
  208. if node.Mode == ModeValidator {
  209. testnet.Validators[node] = 100
  210. }
  211. }
  212. }
  213. // Set up validator updates.
  214. for heightStr, validators := range manifest.ValidatorUpdates {
  215. height, err := strconv.Atoi(heightStr)
  216. if err != nil {
  217. return nil, fmt.Errorf("invalid validator update height %q: %w", height, err)
  218. }
  219. valUpdate := map[*Node]int64{}
  220. for name, power := range validators {
  221. node := testnet.LookupNode(name)
  222. if node == nil {
  223. return nil, fmt.Errorf("unknown validator %q for update at height %v", name, height)
  224. }
  225. valUpdate[node] = power
  226. }
  227. testnet.ValidatorUpdates[int64(height)] = valUpdate
  228. }
  229. return testnet, testnet.Validate()
  230. }
  231. // Validate validates a testnet.
  232. func (t Testnet) Validate() error {
  233. if t.Name == "" {
  234. return errors.New("network has no name")
  235. }
  236. if t.IP == nil {
  237. return errors.New("network has no IP")
  238. }
  239. if len(t.Nodes) == 0 {
  240. return errors.New("network has no nodes")
  241. }
  242. for _, node := range t.Nodes {
  243. if err := node.Validate(t); err != nil {
  244. return fmt.Errorf("invalid node %q: %w", node.Name, err)
  245. }
  246. }
  247. return nil
  248. }
  249. // Validate validates a node.
  250. func (n Node) Validate(testnet Testnet) error {
  251. if n.Name == "" {
  252. return errors.New("node has no name")
  253. }
  254. if n.IP == nil {
  255. return errors.New("node has no IP address")
  256. }
  257. if !testnet.IP.Contains(n.IP) {
  258. return fmt.Errorf("node IP %v is not in testnet network %v", n.IP, testnet.IP)
  259. }
  260. if n.ProxyPort > 0 {
  261. if n.ProxyPort <= 1024 {
  262. return fmt.Errorf("local port %v must be >1024", n.ProxyPort)
  263. }
  264. for _, peer := range testnet.Nodes {
  265. if peer.Name != n.Name && peer.ProxyPort == n.ProxyPort {
  266. return fmt.Errorf("peer %q also has local port %v", peer.Name, n.ProxyPort)
  267. }
  268. }
  269. }
  270. switch n.FastSync {
  271. case "", "v0", "v1", "v2":
  272. default:
  273. return fmt.Errorf("invalid fast sync setting %q", n.FastSync)
  274. }
  275. switch n.Database {
  276. case "goleveldb", "cleveldb", "boltdb", "rocksdb", "badgerdb":
  277. default:
  278. return fmt.Errorf("invalid database setting %q", n.Database)
  279. }
  280. switch n.ABCIProtocol {
  281. case ProtocolBuiltin, ProtocolUNIX, ProtocolTCP, ProtocolGRPC:
  282. default:
  283. return fmt.Errorf("invalid ABCI protocol setting %q", n.ABCIProtocol)
  284. }
  285. switch n.PrivvalProtocol {
  286. case ProtocolFile, ProtocolUNIX, ProtocolTCP:
  287. default:
  288. return fmt.Errorf("invalid privval protocol setting %q", n.PrivvalProtocol)
  289. }
  290. if n.StartAt > 0 && n.StartAt < n.Testnet.InitialHeight {
  291. return fmt.Errorf("cannot start at height %v lower than initial height %v",
  292. n.StartAt, n.Testnet.InitialHeight)
  293. }
  294. if n.StateSync && n.StartAt == 0 {
  295. return errors.New("state synced nodes cannot start at the initial height")
  296. }
  297. if n.PersistInterval == 0 && n.RetainBlocks > 0 {
  298. return errors.New("persist_interval=0 requires retain_blocks=0")
  299. }
  300. if n.PersistInterval > 1 && n.RetainBlocks > 0 && n.RetainBlocks < n.PersistInterval {
  301. return errors.New("persist_interval must be less than or equal to retain_blocks")
  302. }
  303. if n.SnapshotInterval > 0 && n.RetainBlocks > 0 && n.RetainBlocks < n.SnapshotInterval {
  304. return errors.New("snapshot_interval must be less than er equal to retain_blocks")
  305. }
  306. for _, perturbation := range n.Perturbations {
  307. switch perturbation {
  308. case PerturbationDisconnect, PerturbationKill, PerturbationPause, PerturbationRestart:
  309. default:
  310. return fmt.Errorf("invalid perturbation %q", perturbation)
  311. }
  312. }
  313. if (n.PrivvalProtocol != "file" || n.Mode != "validator") && len(n.Misbehaviors) != 0 {
  314. return errors.New("must be using \"file\" privval protocol to implement misbehaviors")
  315. }
  316. for height, misbehavior := range n.Misbehaviors {
  317. if height < n.StartAt {
  318. return fmt.Errorf("misbehavior height %d is before start height %d", height, n.StartAt)
  319. }
  320. exists := false
  321. for possibleBehaviors := range mcs.MisbehaviorList {
  322. if possibleBehaviors == misbehavior {
  323. exists = true
  324. }
  325. }
  326. if !exists {
  327. return fmt.Errorf("misbehavior %s does not exist", misbehavior)
  328. }
  329. }
  330. return nil
  331. }
  332. // LookupNode looks up a node by name. For now, simply do a linear search.
  333. func (t Testnet) LookupNode(name string) *Node {
  334. for _, node := range t.Nodes {
  335. if node.Name == name {
  336. return node
  337. }
  338. }
  339. return nil
  340. }
  341. // ArchiveNodes returns a list of archive nodes that start at the initial height
  342. // and contain the entire blockchain history. They are used e.g. as light client
  343. // RPC servers.
  344. func (t Testnet) ArchiveNodes() []*Node {
  345. nodes := []*Node{}
  346. for _, node := range t.Nodes {
  347. if node.Mode != ModeSeed && node.StartAt == 0 && node.RetainBlocks == 0 {
  348. nodes = append(nodes, node)
  349. }
  350. }
  351. return nodes
  352. }
  353. // RandomNode returns a random non-seed node.
  354. func (t Testnet) RandomNode() *Node {
  355. for {
  356. node := t.Nodes[rand.Intn(len(t.Nodes))]
  357. if node.Mode != ModeSeed {
  358. return node
  359. }
  360. }
  361. }
  362. // IPv6 returns true if the testnet is an IPv6 network.
  363. func (t Testnet) IPv6() bool {
  364. return t.IP.IP.To4() == nil
  365. }
  366. // Address returns a P2P endpoint address for the node.
  367. func (n Node) AddressP2P(withID bool) string {
  368. ip := n.IP.String()
  369. if n.IP.To4() == nil {
  370. // IPv6 addresses must be wrapped in [] to avoid conflict with : port separator
  371. ip = fmt.Sprintf("[%v]", ip)
  372. }
  373. addr := fmt.Sprintf("%v:26656", ip)
  374. if withID {
  375. addr = fmt.Sprintf("%x@%v", n.Key.PubKey().Address().Bytes(), addr)
  376. }
  377. return addr
  378. }
  379. // Address returns an RPC endpoint address for the node.
  380. func (n Node) AddressRPC() string {
  381. ip := n.IP.String()
  382. if n.IP.To4() == nil {
  383. // IPv6 addresses must be wrapped in [] to avoid conflict with : port separator
  384. ip = fmt.Sprintf("[%v]", ip)
  385. }
  386. return fmt.Sprintf("%v:26657", ip)
  387. }
  388. // Client returns an RPC client for a node.
  389. func (n Node) Client() (*rpchttp.HTTP, error) {
  390. return rpchttp.New(fmt.Sprintf("http://127.0.0.1:%v", n.ProxyPort), "/websocket")
  391. }
  392. // keyGenerator generates pseudorandom Ed25519 keys based on a seed.
  393. type keyGenerator struct {
  394. random *rand.Rand
  395. }
  396. func newKeyGenerator(seed int64) *keyGenerator {
  397. return &keyGenerator{
  398. random: rand.New(rand.NewSource(seed)),
  399. }
  400. }
  401. func (g *keyGenerator) Generate() crypto.PrivKey {
  402. seed := make([]byte, ed25519.SeedSize)
  403. _, err := io.ReadFull(g.random, seed)
  404. if err != nil {
  405. panic(err) // this shouldn't happen
  406. }
  407. return ed25519.GenPrivKeyFromSecret(seed)
  408. }
  409. // portGenerator generates local Docker proxy ports for each node.
  410. type portGenerator struct {
  411. nextPort uint32
  412. }
  413. func newPortGenerator(firstPort uint32) *portGenerator {
  414. return &portGenerator{nextPort: firstPort}
  415. }
  416. func (g *portGenerator) Next() uint32 {
  417. port := g.nextPort
  418. g.nextPort++
  419. if g.nextPort == 0 {
  420. panic("port overflow")
  421. }
  422. return port
  423. }
  424. // ipGenerator generates sequential IP addresses for each node, using a random
  425. // network address.
  426. type ipGenerator struct {
  427. network *net.IPNet
  428. nextIP net.IP
  429. }
  430. func newIPGenerator(network *net.IPNet) *ipGenerator {
  431. nextIP := make([]byte, len(network.IP))
  432. copy(nextIP, network.IP)
  433. gen := &ipGenerator{network: network, nextIP: nextIP}
  434. // Skip network and gateway addresses
  435. gen.Next()
  436. gen.Next()
  437. return gen
  438. }
  439. func (g *ipGenerator) Network() *net.IPNet {
  440. n := &net.IPNet{
  441. IP: make([]byte, len(g.network.IP)),
  442. Mask: make([]byte, len(g.network.Mask)),
  443. }
  444. copy(n.IP, g.network.IP)
  445. copy(n.Mask, g.network.Mask)
  446. return n
  447. }
  448. func (g *ipGenerator) Next() net.IP {
  449. ip := make([]byte, len(g.nextIP))
  450. copy(ip, g.nextIP)
  451. for i := len(g.nextIP) - 1; i >= 0; i-- {
  452. g.nextIP[i]++
  453. if g.nextIP[i] != 0 {
  454. break
  455. }
  456. }
  457. return ip
  458. }