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.

210 lines
7.5 KiB

  1. package e2e
  2. import (
  3. "fmt"
  4. "os"
  5. "sort"
  6. "github.com/BurntSushi/toml"
  7. )
  8. // Manifest represents a TOML testnet manifest.
  9. type Manifest struct {
  10. // IPv6 uses IPv6 networking instead of IPv4. Defaults to IPv4.
  11. IPv6 bool `toml:"ipv6"`
  12. // InitialHeight specifies the initial block height, set in genesis. Defaults to 1.
  13. InitialHeight int64 `toml:"initial_height"`
  14. // InitialState is an initial set of key/value pairs for the application,
  15. // set in genesis. Defaults to nothing.
  16. InitialState map[string]string `toml:"initial_state"`
  17. // Validators is the initial validator set in genesis, given as node names
  18. // and power:
  19. //
  20. // validators = { validator01 = 10; validator02 = 20; validator03 = 30 }
  21. //
  22. // Defaults to all nodes that have mode=validator at power 100. Explicitly
  23. // specifying an empty set will start with no validators in genesis, and
  24. // the application must return the validator set in InitChain via the
  25. // setting validator_update.0 (see below).
  26. Validators *map[string]int64 `toml:"validators"`
  27. // ValidatorUpdates is a map of heights to validator names and their power,
  28. // and will be returned by the ABCI application. For example, the following
  29. // changes the power of validator01 and validator02 at height 1000:
  30. //
  31. // [validator_update.1000]
  32. // validator01 = 20
  33. // validator02 = 10
  34. //
  35. // Specifying height 0 returns the validator update during InitChain. The
  36. // application returns the validator updates as-is, i.e. removing a
  37. // validator must be done by returning it with power 0, and any validators
  38. // not specified are not changed.
  39. ValidatorUpdates map[string]map[string]int64 `toml:"validator_update"`
  40. // Nodes specifies the network nodes. At least one node must be given.
  41. Nodes map[string]*ManifestNode `toml:"node"`
  42. // KeyType sets the curve that will be used by validators.
  43. // Options are ed25519 & secp256k1
  44. KeyType string `toml:"key_type"`
  45. // Evidence indicates the amount of evidence that will be injected into the
  46. // testnet via the RPC endpoint of a random node. Default is 0
  47. Evidence int `toml:"evidence"`
  48. // LogLevel sets the log level of the entire testnet. This can be overridden
  49. // by individual nodes.
  50. LogLevel string `toml:"log_level"`
  51. // QueueType describes the type of queue that the system uses internally
  52. QueueType string `toml:"queue_type"`
  53. // Number of bytes per tx. Default is 1kb (1024)
  54. TxSize int64
  55. }
  56. // ManifestNode represents a node in a testnet manifest.
  57. type ManifestNode struct {
  58. // Mode specifies the type of node: "validator", "full", "light" or "seed".
  59. // Defaults to "validator". Full nodes do not get a signing key (a dummy key
  60. // is generated), and seed nodes run in seed mode with the PEX reactor enabled.
  61. Mode string `toml:"mode"`
  62. // Seeds is the list of node names to use as P2P seed nodes. Defaults to none.
  63. Seeds []string `toml:"seeds"`
  64. // PersistentPeers is a list of node names to maintain persistent P2P
  65. // connections to. If neither seeds nor persistent peers are specified,
  66. // this defaults to all other nodes in the network. For light clients,
  67. // this relates to the providers the light client is connected to.
  68. PersistentPeers []string `toml:"persistent_peers"`
  69. // Database specifies the database backend: "goleveldb", "cleveldb",
  70. // "rocksdb", "boltdb", or "badgerdb". Defaults to goleveldb.
  71. Database string `toml:"database"`
  72. // ABCIProtocol specifies the protocol used to communicate with the ABCI
  73. // application: "unix", "tcp", "grpc", or "builtin". Defaults to unix.
  74. // builtin will build a complete Tendermint node into the application and
  75. // launch it instead of launching a separate Tendermint process.
  76. ABCIProtocol string `toml:"abci_protocol"`
  77. // PrivvalProtocol specifies the protocol used to sign consensus messages:
  78. // "file", "unix", "tcp", or "grpc". Defaults to "file". For tcp and unix, the ABCI
  79. // application will launch a remote signer client in a separate goroutine.
  80. // For grpc the ABCI application will launch a remote signer server.
  81. // Only nodes with mode=validator will actually make use of this.
  82. PrivvalProtocol string `toml:"privval_protocol"`
  83. // StartAt specifies the block height at which the node will be started. The
  84. // runner will wait for the network to reach at least this block height.
  85. StartAt int64 `toml:"start_at"`
  86. // BlockSync specifies the block sync mode: "" (disable), "v0" or "v2".
  87. // Defaults to disabled.
  88. BlockSync string `toml:"block_sync"`
  89. // Mempool specifies which version of mempool to use. Either "v0" or "v1"
  90. Mempool string `toml:"mempool_version"`
  91. // StateSync enables state sync. The runner automatically configures trusted
  92. // block hashes and RPC servers. At least one node in the network must have
  93. // SnapshotInterval set to non-zero, and the state syncing node must have
  94. // StartAt set to an appropriate height where a snapshot is available.
  95. // StateSync can either be "p2p" or "rpc" or an empty string to disable
  96. StateSync string `toml:"state_sync"`
  97. // PersistInterval specifies the height interval at which the application
  98. // will persist state to disk. Defaults to 1 (every height), setting this to
  99. // 0 disables state persistence.
  100. PersistInterval *uint64 `toml:"persist_interval"`
  101. // SnapshotInterval specifies the height interval at which the application
  102. // will take state sync snapshots. Defaults to 0 (disabled).
  103. SnapshotInterval uint64 `toml:"snapshot_interval"`
  104. // RetainBlocks specifies the number of recent blocks to retain. Defaults to
  105. // 0, which retains all blocks. Must be greater that PersistInterval,
  106. // SnapshotInterval and EvidenceAgeHeight.
  107. RetainBlocks uint64 `toml:"retain_blocks"`
  108. // Perturb lists perturbations to apply to the node after it has been
  109. // started and synced with the network:
  110. //
  111. // disconnect: temporarily disconnects the node from the network
  112. // kill: kills the node with SIGKILL then restarts it
  113. // pause: temporarily pauses (freezes) the node
  114. // restart: restarts the node, shutting it down with SIGTERM
  115. Perturb []string `toml:"perturb"`
  116. // Log level sets the log level of the specific node i.e. "info".
  117. // This is helpful when debugging a specific problem. This overrides the network
  118. // level.
  119. LogLevel string `toml:"log_level"`
  120. // UseLegacyP2P enables use of the legacy p2p layer for this node.
  121. UseLegacyP2P bool `toml:"use_legacy_p2p"`
  122. }
  123. // Save saves the testnet manifest to a file.
  124. func (m Manifest) Save(file string) error {
  125. f, err := os.Create(file)
  126. if err != nil {
  127. return fmt.Errorf("failed to create manifest file %q: %w", file, err)
  128. }
  129. return toml.NewEncoder(f).Encode(m)
  130. }
  131. // LoadManifest loads a testnet manifest from a file.
  132. func LoadManifest(file string) (Manifest, error) {
  133. manifest := Manifest{}
  134. _, err := toml.DecodeFile(file, &manifest)
  135. if err != nil {
  136. return manifest, fmt.Errorf("failed to load testnet manifest %q: %w", file, err)
  137. }
  138. return manifest, nil
  139. }
  140. // SortManifests orders (in-place) a list of manifests such that the
  141. // manifests will be ordered (vaguely) from least complex to most
  142. // complex.
  143. func SortManifests(manifests []Manifest) {
  144. sort.SliceStable(manifests, func(i, j int) bool {
  145. left, right := manifests[i], manifests[j]
  146. if len(left.Nodes) < len(right.Nodes) {
  147. return true
  148. }
  149. if left.InitialHeight < right.InitialHeight {
  150. return true
  151. }
  152. if left.TxSize < right.TxSize {
  153. return true
  154. }
  155. if left.Evidence < right.Evidence {
  156. return true
  157. }
  158. var (
  159. leftPerturb int
  160. rightPerturb int
  161. )
  162. for _, n := range left.Nodes {
  163. leftPerturb += len(n.Perturb)
  164. }
  165. for _, n := range right.Nodes {
  166. rightPerturb += len(n.Perturb)
  167. }
  168. return leftPerturb < rightPerturb
  169. })
  170. }