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.

213 lines
7.6 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. // UseLegacyP2P uses the legacy p2p layer for all nodes in a test.
  52. UseLegacyP2P bool `toml:"use_legacy_p2p"`
  53. // QueueType describes the type of queue that the system uses internally
  54. QueueType string `toml:"queue_type"`
  55. // Number of bytes per tx. Default is 1kb (1024)
  56. TxSize int64
  57. }
  58. // ManifestNode represents a node in a testnet manifest.
  59. type ManifestNode struct {
  60. // Mode specifies the type of node: "validator", "full", "light" or "seed".
  61. // Defaults to "validator". Full nodes do not get a signing key (a dummy key
  62. // is generated), and seed nodes run in seed mode with the PEX reactor enabled.
  63. Mode string `toml:"mode"`
  64. // Seeds is the list of node names to use as P2P seed nodes. Defaults to none.
  65. Seeds []string `toml:"seeds"`
  66. // PersistentPeers is a list of node names to maintain persistent P2P
  67. // connections to. If neither seeds nor persistent peers are specified,
  68. // this defaults to all other nodes in the network. For light clients,
  69. // this relates to the providers the light client is connected to.
  70. PersistentPeers []string `toml:"persistent_peers"`
  71. // Database specifies the database backend: "goleveldb", "cleveldb",
  72. // "rocksdb", "boltdb", or "badgerdb". Defaults to goleveldb.
  73. Database string `toml:"database"`
  74. // ABCIProtocol specifies the protocol used to communicate with the ABCI
  75. // application: "unix", "tcp", "grpc", or "builtin". Defaults to unix.
  76. // builtin will build a complete Tendermint node into the application and
  77. // launch it instead of launching a separate Tendermint process.
  78. ABCIProtocol string `toml:"abci_protocol"`
  79. // PrivvalProtocol specifies the protocol used to sign consensus messages:
  80. // "file", "unix", "tcp", or "grpc". Defaults to "file". For tcp and unix, the ABCI
  81. // application will launch a remote signer client in a separate goroutine.
  82. // For grpc the ABCI application will launch a remote signer server.
  83. // Only nodes with mode=validator will actually make use of this.
  84. PrivvalProtocol string `toml:"privval_protocol"`
  85. // StartAt specifies the block height at which the node will be started. The
  86. // runner will wait for the network to reach at least this block height.
  87. StartAt int64 `toml:"start_at"`
  88. // BlockSync specifies the block sync mode: "" (disable), "v0" or "v2".
  89. // Defaults to disabled.
  90. BlockSync string `toml:"block_sync"`
  91. // Mempool specifies which version of mempool to use. Either "v0" or "v1"
  92. Mempool string `toml:"mempool_version"`
  93. // StateSync enables state sync. The runner automatically configures trusted
  94. // block hashes and RPC servers. At least one node in the network must have
  95. // SnapshotInterval set to non-zero, and the state syncing node must have
  96. // StartAt set to an appropriate height where a snapshot is available.
  97. // StateSync can either be "p2p" or "rpc" or an empty string to disable
  98. StateSync string `toml:"state_sync"`
  99. // PersistInterval specifies the height interval at which the application
  100. // will persist state to disk. Defaults to 1 (every height), setting this to
  101. // 0 disables state persistence.
  102. PersistInterval *uint64 `toml:"persist_interval"`
  103. // SnapshotInterval specifies the height interval at which the application
  104. // will take state sync snapshots. Defaults to 0 (disabled).
  105. SnapshotInterval uint64 `toml:"snapshot_interval"`
  106. // RetainBlocks specifies the number of recent blocks to retain. Defaults to
  107. // 0, which retains all blocks. Must be greater that PersistInterval,
  108. // SnapshotInterval and EvidenceAgeHeight.
  109. RetainBlocks uint64 `toml:"retain_blocks"`
  110. // Perturb lists perturbations to apply to the node after it has been
  111. // started and synced with the network:
  112. //
  113. // disconnect: temporarily disconnects the node from the network
  114. // kill: kills the node with SIGKILL then restarts it
  115. // pause: temporarily pauses (freezes) the node
  116. // restart: restarts the node, shutting it down with SIGTERM
  117. Perturb []string `toml:"perturb"`
  118. // Log level sets the log level of the specific node i.e. "info".
  119. // This is helpful when debugging a specific problem. This overrides the network
  120. // level.
  121. LogLevel string `toml:"log_level"`
  122. // UseLegacyP2P enables use of the legacy p2p layer for this node.
  123. UseLegacyP2P bool `toml:"use_legacy_p2p"`
  124. }
  125. // Save saves the testnet manifest to a file.
  126. func (m Manifest) Save(file string) error {
  127. f, err := os.Create(file)
  128. if err != nil {
  129. return fmt.Errorf("failed to create manifest file %q: %w", file, err)
  130. }
  131. return toml.NewEncoder(f).Encode(m)
  132. }
  133. // LoadManifest loads a testnet manifest from a file.
  134. func LoadManifest(file string) (Manifest, error) {
  135. manifest := Manifest{}
  136. _, err := toml.DecodeFile(file, &manifest)
  137. if err != nil {
  138. return manifest, fmt.Errorf("failed to load testnet manifest %q: %w", file, err)
  139. }
  140. return manifest, nil
  141. }
  142. // SortManifests orders (in-place) a list of manifests such that the
  143. // manifests will be ordered (vaguely) from least complex to most
  144. // complex.
  145. func SortManifests(manifests []Manifest) {
  146. sort.SliceStable(manifests, func(i, j int) bool {
  147. left, right := manifests[i], manifests[j]
  148. if len(left.Nodes) < len(right.Nodes) {
  149. return true
  150. }
  151. if left.InitialHeight < right.InitialHeight {
  152. return true
  153. }
  154. if left.TxSize < right.TxSize {
  155. return true
  156. }
  157. if left.Evidence < right.Evidence {
  158. return true
  159. }
  160. var (
  161. leftPerturb int
  162. rightPerturb int
  163. )
  164. for _, n := range left.Nodes {
  165. leftPerturb += len(n.Perturb)
  166. }
  167. for _, n := range right.Nodes {
  168. rightPerturb += len(n.Perturb)
  169. }
  170. return leftPerturb < rightPerturb
  171. })
  172. }