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.

136 lines
3.5 KiB

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "sort"
  6. "time"
  7. "github.com/tendermint/tendermint/libs/log"
  8. e2e "github.com/tendermint/tendermint/test/e2e/pkg"
  9. )
  10. func Start(ctx context.Context, logger log.Logger, testnet *e2e.Testnet) error {
  11. if len(testnet.Nodes) == 0 {
  12. return fmt.Errorf("no nodes in testnet")
  13. }
  14. // Nodes are already sorted by name. Sort them by name then startAt,
  15. // which gives the overall order startAt, mode, name.
  16. nodeQueue := testnet.Nodes
  17. sort.SliceStable(nodeQueue, func(i, j int) bool {
  18. a, b := nodeQueue[i], nodeQueue[j]
  19. switch {
  20. case a.Mode == b.Mode:
  21. return false
  22. case a.Mode == e2e.ModeSeed:
  23. return true
  24. case a.Mode == e2e.ModeValidator && b.Mode == e2e.ModeFull:
  25. return true
  26. }
  27. return false
  28. })
  29. sort.SliceStable(nodeQueue, func(i, j int) bool {
  30. return nodeQueue[i].StartAt < nodeQueue[j].StartAt
  31. })
  32. if nodeQueue[0].StartAt > 0 {
  33. return fmt.Errorf("no initial nodes in testnet")
  34. }
  35. // Start initial nodes (StartAt: 0)
  36. logger.Info("Starting initial network nodes...")
  37. for len(nodeQueue) > 0 && nodeQueue[0].StartAt == 0 {
  38. node := nodeQueue[0]
  39. nodeQueue = nodeQueue[1:]
  40. if err := execCompose(testnet.Dir, "up", "-d", node.Name); err != nil {
  41. return err
  42. }
  43. if err := func() error {
  44. ctx, cancel := context.WithTimeout(ctx, time.Minute)
  45. defer cancel()
  46. _, err := waitForNode(ctx, logger, node, 0)
  47. return err
  48. }(); err != nil {
  49. return err
  50. }
  51. node.HasStarted = true
  52. logger.Info(fmt.Sprintf("Node %v up on http://127.0.0.1:%v", node.Name, node.ProxyPort))
  53. }
  54. networkHeight := testnet.InitialHeight
  55. // Wait for initial height
  56. logger.Info("Waiting for initial height",
  57. "height", networkHeight,
  58. "nodes", len(testnet.Nodes)-len(nodeQueue),
  59. "pending", len(nodeQueue))
  60. block, blockID, err := waitForHeight(ctx, testnet, networkHeight)
  61. if err != nil {
  62. return err
  63. }
  64. for _, node := range nodeQueue {
  65. if node.StartAt > networkHeight {
  66. // if we're starting a node that's ahead of
  67. // the last known height of the network, then
  68. // we should make sure that the rest of the
  69. // network has reached at least the height
  70. // that this node will start at before we
  71. // start the node.
  72. logger.Info("Waiting for network to advance to height",
  73. "node", node.Name,
  74. "last_height", networkHeight,
  75. "waiting_for", node.StartAt,
  76. "size", len(testnet.Nodes)-len(nodeQueue),
  77. "pending", len(nodeQueue))
  78. networkHeight = node.StartAt
  79. block, blockID, err = waitForHeight(ctx, testnet, networkHeight)
  80. if err != nil {
  81. return err
  82. }
  83. }
  84. // Update any state sync nodes with a trusted height and hash
  85. if node.StateSync != e2e.StateSyncDisabled || node.Mode == e2e.ModeLight {
  86. err = UpdateConfigStateSync(node, block.Height, blockID.Hash.Bytes())
  87. if err != nil {
  88. return err
  89. }
  90. }
  91. if err := execCompose(testnet.Dir, "up", "-d", node.Name); err != nil {
  92. return err
  93. }
  94. wctx, wcancel := context.WithTimeout(ctx, 8*time.Minute)
  95. status, err := waitForNode(wctx, logger, node, node.StartAt)
  96. if err != nil {
  97. wcancel()
  98. return err
  99. }
  100. wcancel()
  101. node.HasStarted = true
  102. var lastNodeHeight int64
  103. // If the node is a light client, we fetch its current height
  104. if node.Mode == e2e.ModeLight {
  105. lastNodeHeight = status.LightClientInfo.LastTrustedHeight
  106. } else {
  107. lastNodeHeight = status.SyncInfo.LatestBlockHeight
  108. }
  109. logger.Info(fmt.Sprintf("Node %v up on http://127.0.0.1:%v at height %v",
  110. node.Name, node.ProxyPort, lastNodeHeight))
  111. }
  112. return nil
  113. }